PHP process models (H1)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
For PHP served one request per process at a time: PHP-FPM behind nginx, Apache or Caddy; Apache with mod_php; the built-in server php -S; and CGI (php-cgi started per request). The kernel already links these exactly, so you don't need this hook to see which request started a shell, opened a file or connected to a database. What the hook adds is optional: explicit request boundaries, so that requests on one keep-alive connection, or one persistent FastCGI connection, are told apart.
It gets in config-only: one ini file mounted into PHP's conf.d and a read-only mount of vulnboxd's hook directory. The image and the source stay untouched. The same files are also the base for the long-running runtimes in H2 (Swoole, Workerman, ReactPHP, Amp, RoadRunner, FrankenPHP).
Covers
| Versions | |
|---|---|
| PHP | 7.4–8.5, NTS and ZTS, glibc and musl (Alpine), x86_64 and aarch64 (samples: 7.4, 8.3, 8.4) |
| PHP-FPM | behind nginx (sample), Apache mod_proxy_fcgi, Caddy, any FastCGI client |
Apache mod_php | prefork MPM, php:*-apache images (sample: php:8.4-apache) |
php -S | document-root mode (php -S addr -t dir; sample on PHP 7.4); not router-script mode, see Known gaps |
| CGI | php-cgi started per request by lighttpd (sample), Apache mod_cgi, busybox httpd, … |
| Images | official php:* (samples with and without ext-ffi), distribution packages (sample: Alpine php83), others |
Why the kernel already links these:
- PHP-FPM, mod_php: a worker process serves one request at a time and holds exactly one live client socket while it does: the FastCGI connection from the web server, or the HTTP connection under
mod_php. The sensor's "unambiguous ownership" rule links everything the worker does to that socket. - CGI: a new process per request. Process lineage links it.
php -S: one process, one request at a time, one connection per request (it closes the connection after every response).
What the kernel can't tell apart, and the hook can: two requests on one keep-alive connection (mod_php), or two requests over one persistent FastCGI connection (fastcgi_keep_conn on).
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): composer require vulnboxd/vbxtrace:1.0.0, then auto_prepend_file=vendor/vulnboxd/vbxtrace/php/prepend.php; the prebuilt extension is vbxtrace-php-ext-1.0.0.tar.gz on the release page; see Local development.
The hook is a PHP file run before every script through auto_prepend_file. It reaches the core library through one of two backends:
- PHP's FFI extension (
ext-ffi). It ships in Debian's and Alpine's PHP packages, but none of the officialphpDocker images include it. - The vbxtrace PHP extension, prebuilt by vulnboxd for every PHP minor version 7.4–8.5, NTS and ZTS, x86_64 and aarch64. It is a thin shim that exposes the core's functions to PHP. One file per variant loads under both glibc and musl.
That gives four variants of the ini file, all in /usr/local/lib/vulnboxd/trace/php/conf.d/. The hub picks one:
| Variant | File | When |
|---|---|---|
| extension | zz-vbxtrace-ext.ini | the default for official images and any PHP without ext-ffi |
| preload | zz-vbxtrace-preload.ini | ext-ffi present, OPcache enabled, FPM / mod_php / FrankenPHP |
| ffi | zz-vbxtrace-ffi.ini | ext-ffi present, but no OPcache or no preloading (CGI, PHP 7.4 images) |
| basic | zz-vbxtrace.ini | only the CLI SAPI matters (the H2 runtimes) and ext-ffi is present |
The extension variant (default)
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ - /usr/local/lib/vulnboxd/trace/php/conf.d/zz-vbxtrace-ext.ini:/usr/local/etc/php/conf.d/zz-vbxtrace.ini:ro
+ - /usr/local/lib/vulnboxd/trace/php/ext/vbxtrace-php8.3-nts-x86_64.ini:/usr/local/etc/php/conf.d/zz-vbxtrace-ext.ini:roThe first ini file installs the request hook (auto_prepend_file = /opt/vbxtrace/php/prepend.php). The second loads the extension that matches the service's PHP, named by a triple:
- the PHP minor version (
php8.3); ntsorzts;- the vulnbox's architecture (
x86_64oraarch64).
; php/ext/vbxtrace-php8.3-nts-x86_64.ini, written next to each built extension
extension = /opt/vbxtrace/php/ext/vbxtrace-php8.3-nts-x86_64.soThe hub derives the triple from the image tag (php:8.3-fpm-alpine is php8.3-nts, php:8.3-zts is zts, and dunglas/frankenphp is always zts) or from php -v in the running container, and fixes it in the proposal. It is in the file name, not taken from the environment, so a PHP process started through sudo, env -i or socat EXEC: loads the same extension, and no environment variable is needed.
A triple that does not match doesn't stop the service, and prints nothing. The extension compares its module API, debug and ZTS flags and build ID with the running PHP's. On a mismatch, it registers an empty module instead: no functions, no output, the hook stays off. That covers an image upgraded to another 8.x minor version after the proposal, and NTS against ZTS. One case is left: on musl (Alpine), a PHP 7.4 build loaded into PHP 8.x, or the other way round, fails while loading, with PHP's startup warning (Unable to load dynamic library …), because musl resolves every symbol at load time. In the CLI SAPI such a warning is printed to standard output when no php.ini sets display_errors, which is the case in the official images. For a CLI program whose stdout is the service's protocol (a PHP script behind socat EXEC: or xinetd), that line would end up in the protocol. So the hub proposes the extension variant only when it knows the exact triple. All 28 builds (7 PHP versions × NTS/ZTS × x86_64/aarch64) are tested in the official Alpine and Debian images, from Debian 11's glibc 2.31 to the current ones. The extension never fails PHP's startup: without the core library, or with VBXTRACE=0, every function is a no-op.
The preload variant
PHP's FFI is restricted by default (ffi.enable=preload). Plain request code can't use it, but code preloaded through opcache.preload can, and so can the CLI. The preload variant preloads the hook's own class, so the hook can use FFI and the service's code still can't:
auto_prepend_file = /opt/vbxtrace/php/prepend.php
opcache.preload = /opt/vbxtrace/php/preload.php
opcache.preload_user = www-data services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ - /usr/local/lib/vulnboxd/trace/php/conf.d/zz-vbxtrace-preload.ini:/usr/local/etc/php/conf.d/zz-vbxtrace.ini:roThe preload variant has three requirements, each checked by the hub:
- OPcache must be enabled. The official images enable it from PHP 8.1 on.
opcache.preload_usermust name an existing user when PHP starts as root. That iswww-datain the official Debian and Alpine images. PHP refuses to start if the user doesn't exist.- The
/opt/vbxtracemount must be present. A preload script that can't be opened is a fatal startup error. Both mounts are in the same change, so they come and go together.
The FFI variant
ffi.enable=1 lets the hook use FFI anywhere, without preloading. It also lets every other piece of PHP code in the service use FFI, including code an attacker manages to run, which can then bypass disable_functions and open_basedir. Use it only when the extension and the preload variant are not options: no matching extension build, no OPcache, or classic CGI, where every request starts a new process.
Adding ext-ffi to an image
The hub also offers a Dockerfile change for an official image:
FROM php:8.3-fpm-alpine
+RUN apk add --no-cache libffi && apk add --no-cache --virtual .vbx libffi-dev $PHPIZE_DEPS \
+ && docker-php-ext-install ffi && apk del .vbxOn Debian-based images: apt-get install -y libffi-dev && docker-php-ext-install ffi. It needs network access at build time, which A/D build environments often lack. The extension variant avoids it.
Optional: request index and local port from nginx
PHP sees the client's address and port (REMOTE_ADDR, REMOTE_PORT), but not which request on the connection this is. nginx knows both, and can pass them:
fastcgi_param VBXTRACE_CONNECTION_REQUESTS $connection_requests;
fastcgi_param VBXTRACE_LOCAL_PORT $server_port;Header names reach PHP as HTTP_*, so a client can't forge these two parameters. Without them the index is reported as unknown. SERVER_PORT is trusted as the local port only behind nginx and with php -S: Apache, lighttpd and Caddy derive it from the Host header or ServerName.
When the service already sets these ini values
The ini file is named zz-… so it loads last, and it replaces any earlier auto_prepend_file and opcache.preload. When the service already uses either, the hub adds the old value to the environment and the hook runs it right after itself:
environment:
VBXTRACE_CHAIN_PREPEND: "/var/www/bootstrap.php"
VBXTRACE_CHAIN_PRELOAD: "/var/www/config/preload.php"A php_value auto_prepend_file, or php_admin_value[auto_prepend_file] in an FPM pool, or a .user.ini setting overrides the ini file. The hook is then not loaded for those requests.
Distribution packages
On Debian or Ubuntu packages, mount the file into each SAPI's directory (/etc/php/8.2/fpm/conf.d/, /etc/php/8.2/apache2/conf.d/, /etc/php/8.2/cli/conf.d/). On Alpine packages, use /etc/php83/conf.d/. These packages ship ext-ffi (Alpine: php83-ffi), so the preload or FFI variant applies.
The hub proposes this package when it detects any of:
- a
php:*,bitnami/php-fpm,webdevops/php*,trafex/php-nginxordunglas/frankenphpbase image; composer.jsonor*.phpfiles in the service directory;php-fpm,apache2-foreground,php -Sorphp-cgiin the command.
The proposal templates are in hooks/php/h1/proposals/.
Proposal variables
PHP proposals depend on facts about the service that a static template can't know: which extension build matches its PHP, where its conf.d is, and whether it already sets auto_prepend_file. The PHP templates therefore extend the common proposal format (kind, [detect], [compose], [config], [source]):
| Key | Meaning |
|---|---|
[vars.<name>] | a value the hub works out before rendering. {name} in any string of the template is replaced by it |
detect | what to determine: php_ext_triple (php<major.minor>-<nts|zts>-<arch>, from the image tag or php -v, plus the vulnbox's uname -m), php_conf_d (the scanned ini directory), php_ini:<directive> (a value the service already sets, from its ini files and Dockerfile), php_worker_user, php_image_libc (alpine or debian) |
default | used when detection finds nothing. Without a default, the variable is required and the proposal isn't offered when it can't be determined |
must_equal | the proposal is offered only when the detected value equals this, e.g. opcache.preload_user must be www-data |
env = { X = { value = "…", omit_empty = true } } | the variable is left out of the compose change when its rendered value is empty (the VBXTRACE_CHAIN_* variables) |
requires_facts | facts that must be true for the proposal to be offered: php_has_ffi (php -m lists FFI, or the Dockerfile or packages install it), php_opcache_enabled |
rank | the order among this package's proposals that all match: higher is proposed first (extension 30, preload 20, basic 15, ffi 5) |
warning | a sentence the hub shows next to the diff (the FFI variant) |
requires | other proposals (<package>:<file stem>) one of which must be applied too. H2's source changes require an H1 ini proposal |
source_patterns | regexes on the service's source files that locate the line a kind = "source" change edits (H2) |
What it records
| Event | Emitted from | Join key |
|---|---|---|
request_begin | prepend.php, before the script | REMOTE_ADDR:REMOTE_PORT; local port (nginx, php -S, or VBXTRACE_LOCAL_PORT; else 0); request index (nginx $connection_requests − 1, php -S 0, else unknown) |
request_end | the last shutdown function, with http_response_code() | context id |
The hook can't see proc_open, exec, fopen or fsockopen calls: PHP has no way to wrap built-in functions without an extension of its own. In the H1 models it doesn't need to. The kernel links child processes, files and connections to the worker, and the request boundaries place them in the right request. Code that wants explicit events can call \VbxTrace\Hook::spawnedProc($proc), connectedStream($socket) or opened($fd) (see H2).
Context following
One context per request, held in a static property of the hook's class. That is per process under NTS, and per thread under ZTS (FrankenPHP). Work the request starts in another process (exec, proc_open) is linked by the kernel through process lineage.
Known gaps
- Request index: unknown, except behind nginx with the
fastcgi_paramabove, and withphp -S. The peer address and port still name the connection. - Local port: 0, except behind nginx and with
php -S. php -Swith a router script (php -S 0:8080 router.php): PHP doesn't runauto_prepend_filefor the router, so the hook stays off. The kernel links everything anyway.REMOTE_ADDRrewritten by the web server (nginxreal_ip, Apachemod_remoteip, trusted-proxy settings) no longer names the socket's peer. The sensor checks every join key against the connections it saw and drops links that don't match.- CGI: CGI servers pass their programs only the CGI variables.
VBXTRACE=0must be passed through (PassEnv VBXTRACEin Apache,setenv.add-environmentin lighttpd) to take effect. - Per-directory overrides (
php_value,.user.ini) ofauto_prepend_fileswitch the hook off for those requests. - The 5-error limit counts per request in these models: PHP keeps no state between requests.
- Alpine's
php83-ffi8.3.29 crashes when an FFI call returns a pointer. The hook never declares one. Keep that in mind if you write FFI code yourself.
Safety
- Every call into the core is wrapped in a catch-all. The hook never changes arguments, return values, output, headers or the response status. It defines no global variables and registers no autoloader. It only adds two shutdown functions, and only while a tracer is attached.
- While no tracer is attached, a request costs one call to
vbx_v1_enabled()and nothing else. Without preloading, the hook's class isn't even compiled until a tracer attaches. - After 5 internal errors the hook switches itself off for the process.
VBXTRACE=0switches it off. The ini file copies the variable at startup, so it works behind FPM'sclear_envtoo.- The hook never gives FFI to the service's code (except in the FFI variant, by design), and never exposes a way to call arbitrary C functions.
- Every sample was run through the harness on x86_64 in three modes: events (the recorded events match the expected ones), chaos (the core forced to fail on every call) and baseline (without the hook). In both chaos and baseline mode the responses were byte-identical to the events run. The samples:
- nginx + PHP-FPM (
php:8.4-fpm-alpinewithext-ffi, preload variant); - Apache
mod_php(php:8.4-apache, Debian, preload variant); php -Son PHP 7.4 (FFI variant);- CGI with Alpine's own packages (lighttpd +
php-cgi83, FFI variant); - FPM on the plain official image without FFI (hook stays off, no events);
- FPM on the plain official image with the extension variant.
- nginx + PHP-FPM (
- The extension builds were loaded and checked in the official Alpine and Debian images of their PHP version (
hooks/php/h1/ext/test.sh): with the recording core, the production core, no core, andVBXTRACE=0, for every build on x86_64 and on aarch64 (under qemu).
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service.
Reverting removes the ini mounts (both, for the extension variant), the hook-directory mount and VBXTRACE_CHAIN_*. Any earlier auto_prepend_file or opcache.preload takes effect again by itself.
Troubleshooting
- Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too. - Is the hook loaded?
php --inilistszz-vbxtrace.ini.php -i | grep -E 'auto_prepend_file|vbxtrace'shows the settings. With the extension variant,php -mlistsvbxtrace.- Once a tracer is attached,
/proc/<worker pid>/mapsof a PHP worker containslibvbxtrace.
- Which links did a flow get? The hub shows each link's method; this package's are
hook:H1. - PHP doesn't start after the change: the preload variant with a missing
/opt/vbxtracemount, or anopcache.preload_userthat doesn't exist. Switch to the extension variant. - "Unable to load dynamic library vbxtrace-…" at startup: the hook-directory mount is missing (the ini file names a file under
/opt/vbxtracethat is not there), or, on musl, the extension was built for PHP 7.4 and the service runs 8.x (or the other way round). Check the mount andphp -v. - Extension variant,
php -mdoes not listvbxtraceand nothing was printed: the triple in the mountedzz-vbxtrace-ext.inidoesn't match the PHP minor version or NTS/ZTS (an image upgraded after the proposal), so the extension stayed off silently. Checkphp -vand ask the hub for a new proposal. - No events behind Apache or Caddy: expected for the index and local port (unknown and 0). If there are no events at all, check that
auto_prepend_fileisn't overridden per directory.