Rebuilding a never-started container makes it boot without a user namespace, running as privileged
Description
Reported from github.com/
A container that is rebuilt before it has ever been started boots with no user namespace while its configuration still says it is unprivileged. In-container root is then uid 0 on the host, and because the privileged-container hardening is keyed on the security.privileged configuration (not on the actual ID map), none of it is applied. The result is a container the operator believes is unprivileged but which has full host root, retained dangerous capabilities, no device cgroup restriction, /dev/zfs exposed, and only the permissive "unprivileged" AppArmor profile.
Details
incus create allocates and persists an ID map at create time for an unprivileged instance:
internal/server/instance/drivers/driver_lxc.go:331 lxcCreate sets volatile.idmap.next (and volatile.idmap.base)volatile.idmap.current is only written on start, so a never-started instance has next but no current.
incus rebuild resets the map, deleting both the pending map and the on-disk map state:
internal/server/instance/drivers/driver_common.go:750 delete(instLocalConfig, "volatile.idmap.next")
internal/server/instance/drivers/driver_common.go:751 delete(instLocalConfig, "volatile.last_state.idmap")After a rebuild of a never-started instance, all of next, current and last_state.idmap are absent. NextIdmap() resolves through a fallback chain that then yields nil:
driver_lxc.go:9372 NextIdmap() -> next absent -> CurrentIdmap()
driver_lxc.go:9351 CurrentIdmap() -> current absent -> DiskIdmap()
driver_lxc.go:9360 DiskIdmap() -> last_state absent -> (nil, nil)On first start, the map is only (re)allocated when one already exists:
driver_lxc.go:2124 if nextMap != nil && d.state.OS.IdmapSet != nil && !d.state.OS.IdmapSet.Includes(nextMap) { ... findIdmap ... }With nextMap == nil the block is skipped, no map is allocated, and initLXC emits no lxc.idmap entries (it only does so for a non-nil set, driver_lxc.go:1096). The container therefore starts with an identity map.
The hardening is then gated on IsPrivileged() (the config), which is false, so:
lxc.cap.dropis not applied (driver_lxc.go, underIsPrivileged()) — container root keepssys_module,sys_rawio,sys_time,mac_admin/mac_override;- the device cgroup
deny ais not installed (underIsPrivileged()); /dev/zfsis bind-mounted (the!IsPrivileged()branch);- the "unprivileged" AppArmor profile is selected, which permits
mountandpivot_root.
The state is sticky: the first (unmapped) start writes volatile.idmap.current to an empty set, so later starts keep resolving to no map until a config change forces reallocation.
Proof of concept
incus create --empty poc
incus rebuild images:alpine/3.21 poc
incus start poc
# in the container, root is uid 0 on the host with full capabilities:
incus exec poc -- cat /proc/self/status | grep Cap # CapEff/CapBnd = 000001ffffffffff
incus exec poc -- id # uid=0 mapped to host uid 0The trigger is "rebuild before first start"; the --empty create is one way to reach it, but any rebuild of a never-started unprivileged instance behaves the same. An instance that was started at least once before rebuild is unaffected (its volatile.idmap.current survives and the fallback uses it).
Impact
A container the operator configured as unprivileged runs with no user namespace, i.e. as a privileged container without even the privileged-container hardening. Any root process inside it — the whole point of unprivileged confinement being untrusted workloads — is host root with sys_module and raw I/O, so it can load a kernel module or write host devices and fully compromise the host. It is silent: nothing in the config or status indicates the instance is unmapped. Long-standing behavior, not a recent regression.