eBPF-based security platform
upatches/app-sandbox: add an Apache with PHP to try the sandbox on
John Fastabend13dfedbb7273
diff --git a/upatches/app-sandbox/README.md b/upatches/app-sandbox/README.mdindex 44c9a4aa..498b02c4 100644--- a/upatches/app-sandbox/README.md+++ b/upatches/app-sandbox/README.md@@ -129,6 +129,17 @@ leaves the labels, which restrict nothing once the upatch is gone.Every refusal is `EACCES`.+## Try it with Apache++`demo/` in the source of this package holds a web server to try the+`apache` sandbox on: Apache httpd with PHP from the official+`php:8.4-apache` image, a site on port 8443 and `attack.php`, which+does from inside PHP what an attacker would next. `demo/build.sh` writes+the image's file system to an archive, and `demo/apache.sh` unpacks it+on a target and runs Apache from it in a chroot, with `/usr/sbin/apache2`+a hard link to its binary. `Documentation/start/guides/system-hardening.rst`+walks through it.+## Limits- A path that does not exist at the load is not labeled, and a filediff --git a/upatches/app-sandbox/demo/Dockerfile b/upatches/app-sandbox/demo/Dockerfilenew file mode 100644index 00000000..9bf03190--- /dev/null+++ b/upatches/app-sandbox/demo/Dockerfile@@ -0,0 +1,21 @@+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# Apache with mod_ssl and mod_php, as the Docker "official image" of PHP+# builds it on Debian: the web server the sandboxes of app-sandbox are+# shown around. The digest pins what was run: PHP 8.4, Apache 2.4,+# Debian 13, the prefork MPM.+FROM php:8.4-apache@sha256:884f64b47174de2c0e813270fe7b24a18605729fcebe201302d98c38805530d0++# A certificate for the demo alone, made when the image is built.+RUN openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \+ -nodes -keyout /etc/ssl/private/demo.key \+ -out /etc/ssl/certs/demo.pem -subj /CN=localhost -days 3650 \+ && a2enmod ssl >/dev/null \+ && a2dissite 000-default >/dev/null++COPY demo.conf /etc/apache2/sites-enabled/demo.conf+COPY htdocs/ /var/www/html/+RUN mkdir /var/www/html/uploads \+ && echo data >/var/www/html/uploads/data.txt \+ && chown -R www-data: /var/www/html/uploadsdiff --git a/upatches/app-sandbox/demo/apache.sh b/upatches/app-sandbox/demo/apache.shnew file mode 100755index 00000000..bffaca77--- /dev/null+++ b/upatches/app-sandbox/demo/apache.sh@@ -0,0 +1,82 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# usage: apache.sh install <archive> | start | stop | remove+#+# Runs the Apache of build.sh on a target, as root, in a chroot under+# /var/lib/app-sandbox-demo, serving https://127.0.0.1:8443. install+# unpacks the archive and links the server's binary at /usr/sbin/apache2,+# the path the package labels: a hard link is the same inode, so the+# label the load sets there is on the file the chroot executes.++set -eu++root=/var/lib/app-sandbox-demo+pidfile=/run/app-sandbox-demo.pid++usage()+{+ echo "usage: $0 install <archive> | start | stop | remove" >&2+ exit 2+}++stop()+{+ [ -s "$pidfile" ] || return 0+ kill "$(cat "$pidfile")" 2>/dev/null || true+ rm -f "$pidfile"+ sleep 1+}++unmount()+{+ for dir in proc dev tmp run; do+ umount -l "$root/$dir" 2>/dev/null || true+ done+}++[ $# -ge 1 ] || usage+case $1 in+install)+ [ $# -eq 2 ] || usage+ rm -rf "$root"+ mkdir -p "$root"+ tar -xf "$2" -C "$root" --exclude='dev/*'+ ln "$root/usr/sbin/apache2" /usr/sbin/apache2+ ;;+start)+ stop+ unmount+ mount -t tmpfs tmpfs "$root/run"+ mount -t tmpfs tmpfs "$root/tmp"+ chmod 1777 "$root/tmp"+ mount -t proc proc "$root/proc"+ mount --bind /dev "$root/dev"+ # In a session of its own: Apache signals its process group when+ # it stops.+ setsid chroot "$root" /usr/local/bin/apache2-foreground \+ >/var/log/app-sandbox-demo.log 2>&1 &+ echo $! >"$pidfile"+ for _ in $(seq 50); do+ curl -s -k -o /dev/null https://127.0.0.1:8443/index.php &&+ exit 0+ sleep 0.2+ done+ echo "$0: Apache does not answer, see /var/log/app-sandbox-demo.log" >&2+ exit 1+ ;;+stop)+ stop+ unmount+ ;;+remove)+ stop+ unmount+ rm -f /usr/sbin/apache2+ rm -rf "$root"+ ;;+*)+ usage+ ;;+esacdiff --git a/upatches/app-sandbox/demo/build.sh b/upatches/app-sandbox/demo/build.shnew file mode 100755index 00000000..a16618f8--- /dev/null+++ b/upatches/app-sandbox/demo/build.sh@@ -0,0 +1,23 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# usage: build.sh <archive>+#+# Builds the image of the Dockerfile beside this script and writes its+# file system to <archive>, a tar, for apache.sh to run Apache from on a+# target, which has no Docker. Needs Docker and, the first time, the+# network; no root.++set -eu++[ $# -eq 1 ] || { echo "usage: $0 <archive>" >&2; exit 2; }+here=$(cd "$(dirname "$0")" && pwd)+image=shirudo-app-sandbox-demo++docker build -q -t "$image" "$here" >/dev/null+container=$(docker create "$image")+trap 'docker rm "$container" >/dev/null' EXIT+# The target binds its own /dev there.+docker export "$container" >"$1"+echo "$image -> $1"diff --git a/upatches/app-sandbox/demo/demo.conf b/upatches/app-sandbox/demo/demo.confnew file mode 100644index 00000000..6be4e8e9--- /dev/null+++ b/upatches/app-sandbox/demo/demo.conf@@ -0,0 +1,13 @@+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# HTTP/1.1 over TLS on 8443, with the certificate the image made.+Listen 8443+ServerName localhost++<VirtualHost *:8443>+ DocumentRoot /var/www/html+ SSLEngine on+ SSLCertificateFile /etc/ssl/certs/demo.pem+ SSLCertificateKeyFile /etc/ssl/private/demo.key+</VirtualHost>diff --git a/upatches/app-sandbox/demo/htdocs/attack.php b/upatches/app-sandbox/demo/htdocs/attack.phpnew file mode 100644index 00000000..9a3dffd3--- /dev/null+++ b/upatches/app-sandbox/demo/htdocs/attack.php@@ -0,0 +1,42 @@+<?php+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// What an attacker who runs PHP in the server tries next, and what the+// site itself must still be able to do, one line each, in uploads/.+header('Content-Type: text/plain');+$dir = __DIR__ . '/uploads';++function say($what, $ok)+{+ echo $what, ': ', $ok ? 'ok' : 'refused', "\n";+}++function put($dir, $name)+{+ say("write $name", @file_put_contents("$dir/$name", "# x\n") !== false);+}++function reach($host, $port)+{+ $s = @fsockopen($host, $port, $errno, $errstr, 2);+ echo "connect $host port $port: ", $s ? 'ok' : "errno $errno", "\n";+ if ($s)+ fclose($s);+}++// What an earlier request left.+foreach (['note.txt', 'note.php', 'shell.php', 'ran-a-command'] as $name)+ @unlink("$dir/$name");+@rmdir("$dir/d.php");++put($dir, 'note.txt');+put($dir, 'shell.php');+say('rename note.txt note.php', @rename("$dir/note.txt", "$dir/note.php"));+say('mkdir d.php', @mkdir("$dir/d.php"));+@exec('/bin/touch ' . escapeshellarg("$dir/ran-a-command"));+clearstatcache();+echo 'run a command: ',+ file_exists("$dir/ran-a-command") ? 'ran' : 'did not run', "\n";+reach('127.0.0.1', 8443);+reach($_GET['far'] ?? '1.1.1.1', 80);diff --git a/upatches/app-sandbox/demo/htdocs/index.php b/upatches/app-sandbox/demo/htdocs/index.phpnew file mode 100644index 00000000..c7d2495d--- /dev/null+++ b/upatches/app-sandbox/demo/htdocs/index.php@@ -0,0 +1,8 @@+<?php+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// The site: says what PHP made of a request.+header('Content-Type: text/plain');+printf("%s get=%d post=%d\n", $_SERVER['REQUEST_METHOD'], count($_GET),+ count($_POST));