Hi all,

I'd like to propose a small, opt-in module for trunk that confines
httpd's unprivileged processes with Landlock, the Linux LSM that lets
an unprivileged process restrict its own filesystem access. I have a
working prototype and would like your view on the design before I turn
it into a pull request.

The problem
-----------
After the privilege drop, a child can still open anything the User
account can open. On a typical server that is much more than the
content it serves: application configs and credentials owned by
www-data, other sites on the same box, the server config, /tmp, and
any interpreter it cares to exec. A memory-safety bug in a module (or
in a library such as OpenSSL, nghttp2 or libxml2) therefore turns into
"read every secret the web user can read, plant a web shell, run a
shell". SELinux/AppArmor can close this, but few deployments write a
policy for it, and the policy lives far away from the httpd config
that defines what the server actually needs.

The idea
--------
A new module next to mod_privileges, modules/arch/unix/mod_landlock.c
(about 450 lines, built only with --enable-landlock and when
linux/landlock.h exists):

LandlockSandbox On
LandlockReadPath /usr/share/apache2/icons /etc/apache2/htpasswd
LandlockWritePath /var/lib/apache2/dav /var/lib/apache2/davlock
LandlockExecPath /usr/lib/cgi-bin /usr/bin

- It hooks drop_privileges at APR_HOOK_REALLY_LAST, after mod_unixd.
All MPMs call that hook in the new child before child_init and
before any thread is created, so every thread (and every process a
child forks, CGI scripts included) inherits the restriction. The
mod_cgid daemon goes through the same hook, so CGI scripts are
confined too.
- The parent is untouched: config parsing, log and listener setup,
certificate loading and graceful restarts work as before. File
descriptors opened before the drop (logs, listeners, scoreboard, TLS
keys already loaded) keep working, since Landlock only checks new
opens.
- Automatically allowed (read-only): every vhost's DocumentRoot, plus
shared libraries and resolver/time zone files (/usr/lib,
/etc/resolv.conf, /etc/localtime, ...). /dev/null is writable. The
dynamic loader gets execute rights only when some LandlockExecPath
is configured. Everything else must be listed.
- Rights by directive: Read = read files and list directories; Write =
also create, modify, rename and delete; Exec = read and execute.
All filesystem rights the running kernel knows (ABI 1 to 7) are
handled, so anything not granted is denied.
- Fail closed: with LandlockSandbox On, startup fails if the kernel
has no Landlock, and a child that cannot apply the ruleset exits
with APEXIT_CHILDFATAL rather than serving unconfined. A listed path
that does not exist is reported once at startup and skipped.
- Landlock needs no_new_privs, so suEXEC cannot switch users in a
sandboxed process; the module warns about that at startup.

Results so far
--------------
Tested on trunk (bd50573), Linux 6.18 (Landlock ABI 7), with the
event, worker and prefork MPMs. With the sandbox on, the following
all behave as without it: static files, autoindex, .htaccess with
mod_rewrite, 404s, CGI through mod_cgid, WebDAV
PUT/MKCOL/MOVE/DELETE/LOCK, graceful restart, HTTPS with HTTP/2 and
-X.

To check what it buys, I attached gdb to a worker running as the web
user and made it call open()/system() as an attacker would:

sandbox off sandbox on
read a file in the DocumentRoot allowed allowed
read the app's DB password (owned by web allowed EACCES
user, outside the allow-list)
read the server config allowed EACCES
create a web shell in the DocumentRoot allowed EACCES
write to /tmp allowed EACCES
start /bin/sh (no LandlockExecPath) allowed EACCES
... with CGI paths allowed, the shell steals the can read and
starts but password write nothing

Open questions
--------------
1. Is a separate module the right home, or would you prefer a
directive in mod_unixd (next to ChrootDir)?
2. Explicit allow-list vs. deriving more from the config. Only
DocumentRoot is derived today. <Directory> and Alias targets could
be added too, but `<Directory />` in the default config shows the
risk of deriving too much. Files that modules read per request
(AuthUserFile, RewriteMap txt:, ErrorDocument files outside the
DocumentRoot) currently have to be listed by hand; the error log
shows EACCES for anything missing.
3. One ruleset for all sandboxed processes, or separate rules for the
mod_cgid daemon, so that MPM children never get exec rights?
4. Follow-ups once the basics are agreed: Landlock TCP rules
(restrict which ports children may connect to, which limits SSRF
through mod_proxy) and scoping of abstract unix sockets and
signals.

If the direction looks acceptable, I'll send a pull request against
trunk with the module, a configure check, docs (mod_landlock.xml) and
a test in the pytest suite, using whatever shape you prefer from the
questions above.

Disclosure: I wrote this with help from an AI coding assistant
(Claude). I have reviewed and tested every line, I understand the
code, and I'm responsible for it. Commits will carry a "Generated-by:"
line as the ASF generative tooling guidance recommends.

Thanks,
Robson Pereira

Reply via email to