[FEATURE]: apply process.selinuxLabel (containers are not SELinux-confined under youki)
Background
process.selinuxLabel (runtime spec config.md, "Process": "specifies the SELinux label for the process") is how Podman, CRI-O and other engines confine containers on SELinux hosts. They set it to container_t with a per-container MCS pair.
youki doesn't apply it. At eb07b60173fca880e4287d5bc0de2580edec2c91:
featuresreports"selinux": {"enabled": false}(crates/youki/src/commands/features.rs:87, "SELinux is not supported in youki.").ContainerBuilderImpl::process_labelis marked// TODO: youki does not support selinux yet(crates/libcontainer/src/container/builder_impl.rs:66).youki exec --process-labelis parsed (crates/liboci-cli/src/exec.rs:39) but never passed on (crates/youki/src/commands/exec.rs:36sets apparmor, not the label).
The container process keeps the label of whoever started youki. A container the engine meant to run as container_t runs as unconfined_t.
Feature Request
Set the process label before the container program is executed, for create/run and for exec.
Is the request related to some problem running youki?
Yes. A container that is SELinux-confined with runc or crun isn't with youki, and nothing reports it. The repro below runs as root and rootless on an enforcing host (the rootfs is labelled container_file_t the way engines do):
#!/bin/sh
# repro.sh <runtime> [workdir]: a bundle with process.selinuxLabel set (no mountLabel), on an
# SELinux-enabled host (the rootfs is labelled container_file_t with chcon). Prints the label the container process runs with, then the label of a
# process started with `exec` (no --process-label, no process file).
set -eu
RT=$1; W=${2:-$(pwd)/work-$RT}
LABEL=${LABEL:-system_u:system_r:container_t:s0:c100,c200}
here=$(cd "$(dirname "$0")" && pwd)
rm -rf "$W"; mkdir -p "$W/bundle/rootfs"
tar -xzf "$here/alpine-minirootfs-3.24.2-x86_64.tar.gz" -C "$W/bundle/rootfs" 2>/dev/null
# the rootfs gets the file label that goes with the process label, as container engines do
chcon -R "$(echo "$LABEL" | sed 's/:system_r:container_t:/:object_r:container_file_t:/')" "$W/bundle/rootfs"
cd "$W/bundle"
if [ "$(id -u)" = 0 ]; then "$RT" spec; else "$RT" spec --rootless; fi
python3 - "$LABEL" <<'P'
import json, sys
s = json.load(open('config.json'))
s['process']['terminal'] = False
s['process']['args'] = ['sleep', '5']
s['process']['selinuxLabel'] = sys.argv[1]
json.dump(s, open('config.json', 'w'), indent=2)
P
echo "runtime runs as: $(cat /proc/self/attr/current | tr -d '\0')"
"$RT" --root "$W/state" run --detach --bundle "$W/bundle" c1 </dev/null >/dev/null 2>"$W/err" || { cat "$W/err"; exit 1; }
pid=$("$RT" --root "$W/state" state c1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["pid"])')
echo "container process: $(cat /proc/$pid/attr/current | tr -d '\0')"
echo "exec'd process: $("$RT" --root "$W/state" exec c1 cat /proc/self/attr/current | tr -d '\0')"
"$RT" --root "$W/state" kill c1 KILL >/dev/null 2>&1 || true
sleep 1; "$RT" --root "$W/state" delete --force c1 >/dev/null 2>&1 || trueyouki at eb07b601, as root:
runtime runs as: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
container process: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
exec'd process: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
exit=0crun 1.27, as root:
runtime runs as: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
container process: system_u:system_r:container_t:s0:c100,c200
exec'd process: system_u:system_r:container_t:s0:c100,c200
exit=0Rootless (as a regular user), crun gives the same result as root. youki again leaves the container process unconfined_t:
runtime runs as: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
container process: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
ERROR libcontainer::namespaces: failed to set namespace err=Nix(EPERM) namespace=LinuxNamespace { typ: Pid, path: Some("/proc/5439/ns/pid") }
ERROR youki: error in executing command: failed to create container: intermediate process error EPERM: Operation not permitted
exec'd process:
exit=0The rootless exec fails before any label matters: youki's setns into the container's pid namespace gets EPERM. That happens without any SELinux settings too, and isn't part of this request.
Proposed Solution
Do what runc and crun do:
- in the init process, write the label to
/proc/thread-self/attr/execjust before the execve of the container program (runc:selinux.SetExecLabelinlibcontainer/standard_init_linux.go:183-188; crun:libcrun_set_selinux_label(),src/libcrun/linux.c:4773, called fromcontainer.c:3454); - for
exec, use--process-label, else the process file's label, else the container's (runcexec.go:221,234-235; cruncontainer.c:3720-3721); - report SELinux in
features.
A PR follows.
Considerations
- SELinux detection:
/sys/fs/selinuxisn't mounted inside the container after pivot_root./proc/filesystemslistsselinuxfsexactly when SELinux is enabled. - The key label (runc's
SetKeyLabel) is not part of this change. - #2718 and #3021 are about a full SELinux library (opencontainers/selinux in Rust). This needs only the exec attribute and could move onto that library later.
Additional Context
Velrix OS rig VM: AlmaLinux 10.2 (bootc), kernel 6.12.0-211.56.1.el10_2.x86_64, SELinux enforcing, targeted policy, crun 1.27, Podman 5.8.2.
youki built from eb07b60173fca880e4287d5bc0de2580edec2c91 (youki version 0.7.0, static musl, features systemd,v2,seccomp).
Source: youki-dev/youki