Hello Proxmox community!
I would like to share the results of an experiment with application-specific AppArmor confinement inside an unprivileged LXC container.
My goal was to add an additional AppArmor security layer for nginx while preserving the original Proxmox-generated LXC profile, without granting the container additional privileges to manage AppArmor.
After several experiments, I found a working solution that also survives a reboot initiated from inside the container.
1. Environment
- Proxmox VE 9.2.26
- pve-container 6.1.18
- lxc-pve 7.0.0-2
- AppArmor 4.1.1-pmx1
- Debian 13 unprivileged LXC
- Nesting enabled
- nginx 1.26.3 running entirely as www-data, including the master process
The original LXC AppArmor confinement remains enabled. No securityfs mount or additional AppArmor management permissions are provided to the guest.
2. Loading an application-specific profile
The application profile is stored on the Proxmox host and loaded directly into the existing LXC AppArmor namespace.
For CT 109:
apparmor_parser -r -K -n 'lxc-109_<-var-lib-lxc>' /etc/lxc/ct109-nginx.apparmor
Inside the container, a systemd override for nginx specifies:
[Service]
AppArmorProfile=ct109-nginx
The profile is mandatory, without the optional "-" prefix.
All nginx processes subsequently report:
ct109-nginx (enforce)
3. AppArmor compatibility issues
During testing, I encountered problems involving disconnected paths, inherited file descriptors and UNIX socket permissions.
The following profile worked in my environment:
abi <abi/4.0>,
#include <tunables/global>
profile ct109-nginx flags=(enforce,attach_disconnected.path=/disconnected) {
#include <abstractions/base>
/usr/sbin/nginx rix,
/usr/bin/kill rix,
/etc/nginx/** r,
/srv/sites/** r,
/run/nginx/ rw,
/run/nginx/** rwk,
/var/log/nginx/*.log rw,
network inet stream,
network inet6 stream,
network unix stream,
signal (send,receive) peer=ct109-nginx,
signal (receive) peer=unconfined,
/disconnected/dev/null r,
/disconnected/run/systemd/journal/stdout rw,
}
This is a workload-specific profile for a static HTTP backend, not a universal nginx profile.
Using ABI 4.0 resolved the UNIX socket issues observed during my experiments with the newer ABI. I would appreciate feedback from anyone familiar with these compatibility issues.
4. The problem with Proxmox hookscripts
Initially, I used a standard Proxmox hookscript to load the profile during the post-start phase.
This worked perfectly when starting the container using pct start.
However, after executing systemctl reboot inside the container, the AppArmor namespace was recreated and the custom profile was missing.
The nginx service remained inactive.
After inspecting the installed Proxmox scripts, I found the following behavior in /usr/share/lxc/pve-container-stop-wrapper:
if (unlink("/var/lib/lxc/$vmid/reboot")) {
exec {'systemctl'} 'systemctl', '--no-block', 'restart', "pve-container\@${vmid}.service";
}
The guest-initiated reboot follows a different lifecycle path and, in my tests, does not execute the configured Proxmox post-start hook.
5. Event-driven solution using a systemd drop-in
Instead of modifying Proxmox package files or implementing a periodic watchdog, I moved the initialization logic into an instance-specific systemd drop-in on the host:
/etc/systemd/system/
pve-container@109.service.d/90-apparmor.conf
Contents:
[Service]
TimeoutStartSec=150s
ExecStartPre=/var/lib/vz/snippets/ct109-apparmor.sh %i pre-start
ExecStartPost=/usr/bin/lxc-wait -n %i -s RUNNING -t 30
ExecStartPost=/var/lib/vz/snippets/ct109-apparmor.sh %i post-start
The host-side initialization script performs the following operations:
- Validates the profile file before starting the container.
- Loads the profile into the container's AppArmor namespace.
- Waits for the guest systemd to become available.
- Checks the mandatory AppArmorProfile setting.
- Starts nginx.
- Verifies that the nginx master process is running in enforce mode.
The script is owned by root and is not writable from inside the container.
The nginx service is disabled for ordinary guest boot and is started by the initialization script only after the AppArmor profile has been loaded.
The original Proxmox hookscript registration is removed to avoid duplicate execution.
This solution works for both normal Proxmox startup and guest-initiated reboot.
6. Verified results
The following tests passed:
- Cold start using pct start.
- Reboot initiated from inside the container.
- Automatic restoration of the application-specific AppArmor profile.
- All nginx processes running as www-data.
- All nginx processes confined by the custom profile in enforce mode.
- HTTP responses from both static websites.
- nginx stub_status.
- Explicit denial of access to a file outside the permitted paths.
- Container startup refusal when the profile file is missing.
- Successful recovery after restoring the profile file.
- nginx log rotation and reopening of the error log.
- No unexpected AppArmor denials observed during the log rotation test.
No Proxmox package files were modified, no periodic watchdog was required, and the original LXC confinement was preserved.
7. Limitations
The systemd drop-in, initialization script and AppArmor profile are host-local files. They must be provisioned separately on potential migration targets and included in the host recovery procedure.
A full hypervisor reboot and container migration have not yet been tested.
This approach is intended as an additional application security layer, not as a replacement for the existing LXC security model.
8. Question for the Proxmox developers
Is there a more appropriate, officially supported mechanism for loading host-managed application-specific AppArmor profiles into an unprivileged container's namespace?
Would it be possible to introduce a lifecycle hook that executes on every actual container startup, including a reboot initiated from inside the guest?
Native support for additional host-managed AppArmor profiles would be particularly useful for administrators who want application-level mandatory access control without granting AppArmor management privileges to their containers.
I would be interested in hearing your opinions on this approach, its limitations and possible improvements.
Thank you!
Disclosure: ChatGPT assisted with technical research, troubleshooting, and preparation of this post. All experiments were performed on my own infrastructure, and the reported results were verified through actual testing.