How I fully compromised the “most advanced code execution system in the world”

Daniel Cooper (Security Consultant · Tanto Security)

BSides Canberra VII (2024) · Day 1 · Main Track

Overview

In this compelling talk at BSides Canberra, Daniel Cooper, a Security Consultant at Tanto Security, unveils a series of critical vulnerabilities that allowed him to achieve full compromise of Judge0, an open-source remote code execution (RCE) system. Billed as "the most advanced code execution system in the world," Judge0 is widely adopted by competitive programming platforms, educational institutions, and various services requiring secure execution of user-submitted code across approximately 80 programming languages. Cooper's presentation meticulously details a complex attack chain, starting from seemingly minor bypasses to ultimately gaining root access on the host system, completely circumventing Judge0's sandboxing mechanisms.

Watch on YouTube

Visual summary for How I fully compromised the “most advanced code execution system in the world” by Daniel Cooper
Visual summary for How I fully compromised the “most advanced code execution system in the world” by Daniel Cooper

Key moments

  1. 0:45 Understanding Judge0: A Code Execution System
  2. 4:00 Docker 'privileged' flag: A critical security flaw
  3. 11:00 SSRF and database command injection exploit
  4. 17:30 Arbitrary file write via Ruby File.open symlink
  5. 29:45 Live demo: Compromising the host machine
  6. 33:30 Post-patch exploit: Overwriting the environment script
  7. 34:45 Leveraging chown vulnerability after privilege downgrade
  8. 40:00 Mitigation advice for secure code execution systems

How I fully compromised the “most advanced code execution system in the world”

Speakers: Daniel Cooper, Security Consultant, Tanto Security

Conference: BSides Canberra

YouTube: https://www.youtube.com/watch?v=aW-w0c3v7Mw

Overview

In this compelling talk at BSides Canberra, Daniel Cooper, a Security Consultant at Tanto Security, unveils a series of critical vulnerabilities that allowed him to achieve full compromise of Judge0, an open-source remote code execution (RCE) system. Billed as "the most advanced code execution system in the world," Judge0 is widely adopted by competitive programming platforms, educational institutions, and various services requiring secure execution of user-submitted code across approximately 80 programming languages. Cooper's presentation meticulously details a complex attack chain, starting from seemingly minor bypasses to ultimately gaining root access on the host system, completely circumventing Judge0's sandboxing mechanisms.

The talk highlights the inherent challenges of providing RCE as a service and the often-overlooked supply chain security risks associated with integrating third-party components. Cooper's journey, initially spurred by curiosity about a friend's competitive programming platform, evolved into a deep dive into Judge0's architecture, exposing fundamental flaws in its handling of file operations, process isolation, and configuration defaults. This research serves as a stark reminder that even systems designed with security in mind can harbor critical vulnerabilities when implementation details deviate from secure design principles.

Background

▶ Watch: Understanding Judge0: A Code Execution System (0:45)

The premise of Judge0 stems from a common development challenge: securely executing untrusted user code. In competitive programming, users submit programs that are compiled and run against test cases. These programs must be sandboxed to prevent malicious actions, such as accessing sensitive files, disrupting the host system, or interfering with other submissions. Judge0 emerged as a popular open-source solution, lauded for its flexibility and ability to handle a vast array of programming languages. Its design, even backed by a research paper claiming its security, positions it as a go-to choice for developers seeking to outsource this complex task.

Despite its reputation, the very nature of remote code execution as a service inherently introduces significant security risks. The core problem lies in the delicate balance between providing sufficient resources for code execution and robustly isolating it from the underlying infrastructure. Judge0 attempts to achieve this isolation primarily through the use of the isolate binary, a Linux tool leveraging namespaces to create a secure, isolated environment for each code submission. However, as Cooper discovered, the integration of isolate within a broader application stack, particularly when deployed in a Docker privileged container, introduced new attack surfaces that were not adequately secured, turning a robust sandboxing tool into a potential stepping stone for full system compromise. The speaker's initial motivation was a personal challenge: to audit his friend's competitive programming application, quickly identifying Judge0 as the most critical component in the supply chain to investigate.

Key Findings

▶ Watch: SSRF and database command injection exploit (11:00)

Daniel Cooper's investigation uncovered a series of critical vulnerabilities, culminating in multiple avenues for remote code execution (RCE) and sandbox escape. These findings were later assigned CVEs through GitHub's security advisory process.

  1. Privileged Docker Container: The initial and most glaring red flag was Judge0's deployment within a Docker container configured with the --privileged flag. While not a direct vulnerability in Judge0 itself, this configuration meant that any successful RCE inside the container could easily lead to root access on the host machine by mounting the host's disk.
  2. gsub Blacklist Bypass (Initial Attempt): Cooper identified a weak blacklist ($, &, ;) applied to command_line_arguments before being written to a bash script. He bypassed this using a newline character (\n), allowing arbitrary commands to be executed on a separate line. However, this initial bypass was limited because the script was immediately run inside the isolate sandbox, rendering it ineffective for true sandbox escape.
  3. PostgreSQL Command Injection via Data Type Confusion: This was the first successful sandbox escape primitive. Cooper discovered that by leveraging Server-Side Request Forgery (SSRF) capabilities (enabled by Judge0's --share-net and enable_network flags), he could connect to the internal PostgreSQL database. By changing the data type of an integer parameter within a submission record to a string, he could inject shell commands using $(command) directly into the isolate execution string. This injection occurred outside the isolate sandbox during job processing. This required a custom PostgreSQL client implementation within the sandbox, a race condition to ensure the malicious job executed after the database modification, and relied on the common default PostgreSQL password "your password here 1 2 3 4".
  4. Arbitrary File Write via Symlink Race Condition and File.open: This was the most critical and versatile vulnerability. Cooper found that Ruby's File.open function, used to write run.sh (the script executed inside the sandbox), follows symlinks. By creating a symlink named run.sh inside the shared directory that pointed to a critical system binary (e.g., /usr/local/bin/isolate), and then triggering the application to write to run.sh, he could overwrite /usr/local/bin/isolate with arbitrary content. The symlink creation was achieved by injecting a newline and a ln -s command into the compiler_arguments field during the compilation phase, which occurred inside the sandbox.
  5. Arbitrary File Ownership Change via Symlink and chown: After the initial fixes restricted the Ruby process to a low-privilege judge_zero user, direct overwriting of root-owned files like /usr/local/bin/isolate became impossible. However, Cooper revisited the chown command, noting that it does follow symlinks when the recursive flag is not used. By creating a symlink run.sh -> /usr/local/bin/isolate and then triggering the cleanup step which chowns run.sh, he could change the ownership of /usr/local/bin/isolate to the judge_zero user. Once owned by judge_zero, the isolate binary could then be overwritten, leading to RCE.
  6. "No Newline" Exploit Techniques: To demonstrate the exploit without relying on newline injection (which a developer might simply blacklist), Cooper developed two additional techniques:
  • Zip File Symlink Upload: Judge0 allowed uploading zip files. While unzip was sandboxed, it still processed symlinks within the archive. This allowed creating the run.sh symlink pointing to /usr/local/bin/isolate without using a newline injection in compiler_arguments.
  • SQLite -CMD Command Injection: For the command line argument part, he discovered that SQLite's -CMD flag allows executing commands before processing standard input. By setting the language to SQL and injecting -CMD '.shell <command>', he could execute arbitrary shell commands without using newlines, as this command ran outside the sandbox after isolate was already compromised.

Technical Deep Dive

▶ Watch: Live demo: Compromising the host machine (29:45)

Judge0's architecture comprises several interconnected services: a Ruby on Rails API server, a PostgreSQL database, a Redis queue, and a worker server. User code submission flows as follows: The Rails API receives the submission, inserts its metadata into the database, and adds a job to the Redis queue. The worker retrieves job info from the database, processes it, and updates results. The critical component for code execution is the isolate binary, a sandboxing tool that uses Linux namespaces to restrict processes and files.

The initial investigation quickly highlighted a severe misconfiguration: Judge0's Docker containers were run with the --privileged flag. This flag grants the container extensive capabilities, effectively nullifying many container isolation benefits. If RCE could be achieved within such a container, escaping to the host system would be trivial, typically by mounting the host's disk devices (e.g., /dev/sda1).

Cooper's first attempts focused on command injection into parameters passed to the isolate binary. He observed a Ruby gsub operation attempting to filter characters like $, &, and ; from command_line_arguments before they were written to a bash script (run.sh) and executed. He discovered that a newline character (\n) could bypass this blacklist, allowing a second, arbitrary command to be executed on a new line. However, this run.sh script was executed inside the isolate sandbox, limiting its impact. Other parameters were strictly validated, often requiring numeric input, making direct shell injection difficult.

The first significant breakthrough involved the PostgreSQL database. Judge0 supported a --share-net flag if enable_network was set to true, hinting at potential SSRF. Cooper realized he could leverage this to connect to the internal PostgreSQL database from within the sandboxed environment. By crafting a custom PostgreSQL client (since external libraries were unavailable), he could interact with the database. He discovered that Judge0's Ruby application would dynamically construct the isolate command string, including parameters retrieved from the database. If an integer parameter, like time_limit, was changed directly in the database to a string containing a shell command (e.g., $(ls -la /)), the Ruby application would concatenate this string into the isolate command. This effectively became a command injection outside the sandbox. This attack required a race condition: the attacker's job (which performs the SSRF and database modification) must execute before another job (the target) is processed, but after the target job's record has been created in the database. Cooper achieved this by submitting 20 "dummy" jobs to fill the queue, then his malicious job, and finally waited for a dummy job to pick up the injected command. The default PostgreSQL password, "your password here 1 2 3 4", made this attack highly reliable against unconfigured deployments. This vulnerability was assigned a CVE.

However, the most impactful vulnerability centered around symlink handling and arbitrary file writing. Cooper observed a File.open operation in the Ruby code that wrote the run.sh script. He realized that Ruby's File.open function, unlike rm -rf, follows symbolic links. The attack chain was as follows:

  1. Symlink Creation: During the compilation phase, which occurs inside the sandbox, Cooper injected a newline into the compiler_arguments to execute ln -s /usr/local/bin/isolate run.sh. This created a symlink run.sh in the shared box directory, pointing to the isolate binary on the host system.
  2. Arbitrary File Overwrite: After compilation, the sandbox was temporarily disabled. The Ruby process would then attempt to write the actual run.sh script (containing the user's program and arguments) to the run.sh path. Because run.sh was now a symlink to /usr/local/bin/isolate, the Ruby process would inadvertently overwrite the core isolate binary with the contents of the run.sh script.
  3. Code Execution: Any subsequent code submission would then attempt to execute /usr/local/bin/isolate, but instead, it would execute the attacker's injected script, leading to RCE outside the sandbox.

To make the injected payload more complex (e.g., a reverse shell), Cooper needed to bypass the remaining gsub blacklist for command_line_arguments. He achieved this by using python3 -c 'import os; os.system("hex_encoded_command")', where the actual payload was hex-encoded, effectively circumventing character restrictions.

After reporting, the developer implemented a fix by changing the Ruby process to run as a low-privilege judge_zero user instead of root. This prevented direct overwriting of /usr/local/bin/isolate as the judge_zero user lacked permissions. However, Cooper quickly identified a new attack vector. He revisited the chown command, which was used in a cleanup step and also follows symlinks.

  1. Symlink Creation: Similar to before, a symlink run.sh -> /usr/local/bin/isolate was created.
  2. Arbitrary File Ownership Change: When the cleanup step executed chown on run.sh, it would follow the symlink and change the ownership of /usr/local/bin/isolate from root to judge_zero.
  3. Overwrite and Execution: Since isolate was now owned by judge_zero, the low-privilege Ruby process could then overwrite it, leading to the same RCE. This second sandbox escape primitive was also assigned a CVE.

Finally, to create an exploit that didn't rely on newline injection (a common developer fix), Cooper devised an alternative:

  1. Zip File Symlink Creation: Judge0 allowed zip file uploads. While the unzipping occurred in the sandbox, symlinks within the zip file were still honored. An attacker could create a zip file containing a symlink run.sh -> /usr/local/bin/isolate.
  2. SQLite -CMD Injection: For the command-line argument injection, he discovered SQLite's -CMD flag, which executes a command before processing standard input. By specifying the language as SQL and using -CMD '.shell <command>', arbitrary shell commands could be executed. This technique, combined with the zip file symlink, provided a robust "no newline" exploit.

Demo / Proof of Concept

▶ Watch: Post-patch exploit: Overwriting the environment script (33:30)

Daniel Cooper provided a compelling live demonstration of the full exploit chain. He showed his local setup, running Judge0 version 1.13.0.

  1. He started a netcat listener on port 9001 on his host machine.
  2. He then executed his Python exploit script, which:
  • Creates a malicious zip file containing a symlink runsh -> /usr/local/bin/isolate.
  • Submits a code execution request to Judge0, setting the language to SQL and injecting the SQLite -CMD flag with a hex-encoded bash reverse shell payload.
  1. Almost instantly, a reverse shell connected back to his netcat listener, demonstrating root access within the Docker container.
  2. To prove host compromise, he proceeded to mount his host's Windows partition from within the Docker container, showcasing direct access to files on his underlying operating system. This vividly illustrated the severity of the privileged Docker flag combined with the sandbox escape, allowing a full bypass of Docker's intended isolation. The demo concluded with clear evidence of complete system compromise.

Defensive Implications

▶ Watch: Mitigation advice for secure code execution systems (40:00)

This research provides several crucial lessons for developers and defenders operating systems that execute untrusted code:

  1. Avoid Privileged Containers: Never run Docker containers with the --privileged flag unless absolutely necessary and with a full understanding of the security implications. This flag significantly broadens the attack surface and makes host system compromise trivial once container escape is achieved.
  2. Principle of Least Privilege: Implement strict user separation. Run application processes (like the Ruby on Rails server) with the lowest possible privileges. If a service doesn't need root, it shouldn't have root. This was a direct fix that mitigated the initial arbitrary file write.
  3. Careful Symlink Handling: Be extremely cautious with file operations (File.open, chown, rm, unzip, etc.) that interact with user-controlled paths or shared directories, especially when these operations occur outside a sandbox. Assume that an attacker can create symbolic links to arbitrary system files. Robustly check for and resolve symlinks or ensure that operations are performed in a strictly controlled, non-writable environment.
  4. Secure Configuration Defaults: Ensure that default configurations are secure by design. The widespread use of "your password here 1 2 3 4" for the PostgreSQL database highlights the danger of insecure defaults that users rarely change. Similarly, network access flags (enable_network, allow_network) should default to false.
  5. Strict Input Validation (Whitelisting): Rely on whitelisting for all user-controlled input, rather than blacklisting. Blacklists are notoriously difficult to maintain and are often bypassed (as demonstrated by the newline injection). Input should be validated for type, format, and content.
  6. Maintain Sandbox Integrity: The core issue in Judge0 was the "turning the sandbox on and off again" model. Critical file operations and command constructions occurred outside the isolate sandbox, creating windows of opportunity for attack. Ideally, once the sandbox is configured, the application should interact with it minimally, and all sensitive operations should occur within the sandbox's protective boundaries or in a completely isolated, read-only environment.
  7. Supply Chain Security: Integrating third-party components, even open-source ones like Judge0, requires thorough security vetting. The "most advanced" claim did not prevent fundamental flaws in its integration and configuration.
  8. Regular Security Audits: Continuous security auditing and penetration testing are essential, especially for systems handling untrusted code. This helps identify complex multi-step attack chains that might be missed by isolated component testing.
  9. Vulnerability Disclosure Program: Establish a clear and accessible vulnerability reporting process. Judge0's developer, Herman, was commendable in his response, but the initial lack of a formal process created friction. GitHub's security advisory feature can streamline this.

Key Takeaways

  • "Privileged" Docker containers are a critical security risk: Any RCE inside such a container can easily lead to host compromise.
  • Sandbox "on/off" models are dangerous: Operations performed outside the sandbox, even briefly, create windows for attackers to manipulate the environment (e.g., creating symlinks).
  • Symlink handling is a common vulnerability source: File operations like File.open and chown that follow symlinks can be abused for arbitrary file writes or ownership changes if not handled carefully.
  • Insecure defaults undermine security: Default passwords and permissive network configurations are frequently exploited if not explicitly changed by users.
  • Input validation should be whitelisting, not blacklisting: Blacklists are easily bypassed, as demonstrated by newline injection techniques.
  • Layered defenses and least privilege are paramount: Even after initial fixes, new attack vectors emerged due to insufficient privilege separation.

About the Speaker(s)

Daniel Cooper is a Security Consultant at Tanto Security. Known by his username "Sparrow," he is an active participant in Capture The Flag (CTF) competitions, playing for teams such as Blitzkrieg and Emu Exploit. His work in security consulting and competitive hacking provides him with a practical, offensive-minded approach to identifying and exploiting vulnerabilities, as demonstrated by his detailed research into Judge0.

Reviews

→ Top-rated talks at BSides Canberra VII (2024)

All talks from BSides Canberra VII (2024)