systemd icon indicating copy to clipboard operation
systemd copied to clipboard

Type=oneshot + User= service process stays in init_t domain (SELinux) despite policy allowing transition to initrc_t/unconfined_service_t

Open BenkiNew opened this issue 3 months ago • 4 comments

Description

For Type=oneshot + User=<unprivileged user> service units, the spawned process appears to stay in the init_t SELinux domain for the entire duration of its execution (observed for a multi-minute real workload, not just a brief startup window), even though SELinux policy grants everything needed for the expected automatic domain transition and no denial is ever logged (permissive mode).

For comparison, a Type=simple + User=<same user> unit on the same host transitions correctly to unconfined_service_t.

Environment

  • systemd 257 (257-24.el10-g2897882)
  • CentOS Stream 10 (Coughlan), kernel 6.12.0-224.el10.x86_64
  • SELinux: permissive, targeted policy

Reproduction

Unit A (does not transition, stays init_t):

[Unit]
Description=oneshot repro

[Service]
Type=oneshot
User=someuser
ExecStart=/home/someuser/projects/x/scripts/somejob.sh

somejob.sh starts with #!/bin/bash and is labeled bin_t (also tried user_home_t, etc_t — no difference in outcome).

Unit B (transitions correctly to unconfined_service_t):

[Unit]
Description=simple repro

[Service]
Type=simple
User=someuser
ExecStart=/home/someuser/projects/y/venv/bin/python main.py

venv/bin/python resolves (symlink) to /usr/bin/python3.12, labeled bin_t.

Checked while unit A's process was still running, several minutes into a real (long) job:

$ ps -eZ | grep somejob
system_u:system_r:init_t:s0  <pid>  ?  00:0X:XX somejob.sh

What I checked (all consistent with the transition being expected to happen)

$ sesearch --type_trans -s init_t -t bin_t
type_transition init_t bin_t:process unconfined_service_t;

$ sesearch --type_trans -s init_t -t shell_exec_t
type_transition init_t shell_exec_t:process initrc_t;

$ sesearch --allow -s init_t -t initrc_t -c process
allow init_t initrc_t:process transition;

$ sesearch --allow -s init_t -t unconfined_service_t -c process
allow init_t unconfined_service_t:process { siginh transition };

$ sesearch --allow -s initrc_t -t bin_t -c file -p entrypoint
allow initrc_t bin_t:file entrypoint;

$ sesearch --allow -s initrc_t -t shell_exec_t -c file -p entrypoint
allow initrc_t shell_exec_t:file entrypoint;

$ sesearch --neverallow -s init_t -c process
(no output)
  • NoNewPrivileges=no on both units (explicitly checked — understood that NNP blocks kernel-level SELinux exec transitions, ruled out as the cause).
  • PAMName= empty on both units.
  • /home is a plain xfs mount with seclabel, no context= mount override.
  • ausearch -m avc shows no tclass=process (transition) or tclass=file (entrypoint) denials at all for unit A's PID — i.e. the kernel doesn't appear to even be attempting/logging a transition decision for this process.

Question

Given the policy fully permits the transition and no denial is logged, is there a known systemd-side reason a Type=oneshot + User= unit's process would not go through the expected SELinux domain transition on exec, while a Type=simple + User= unit does? Is this expected/by-design (e.g. something about how systemd sets up the child for oneshot units specifically), or worth a closer look?

Happy to provide strace/more detail if useful, though I'd want to be careful poking at PID 1 further on this host without more guidance.

BenkiNew avatar Aug 20 '26 06:08 BenkiNew

we generally dont have enough selinux knowledge to address selinux issues, and rely on patches being submitted for any selinux issues.

poettering avatar Aug 20 '26 16:08 poettering

Additional production evidence for this, from two separate hosts, surfacing through a different code path than my original repro (log/output I/O rather than the long-running-job case I originally reported).

New symptom: StandardOutput=append:/file writes from Type=oneshot + User= units also stay in init_t

Several Type=oneshot + User=<unprivileged> units using StandardOutput=append:/path/to/log (systemd's own log-append redirection, not something the script itself opens) hit SELinux denials because the process performing the write is still in init_t at that point — same non-transition as my original report, just observed via a different symptom (log output setup / a script's own file write) instead of the whole job's lifetime.

Host A (8 services observed over 24h, 3 examples shown):

type=AVC msg=audit(...): avc: denied { open } for pid=673442 comm="(python)" path="/home/.../superautobot.log" scontext=system_u:system_r:init_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=1
type=AVC msg=audit(...): avc: denied { append } for pid=673442 comm="(python)" name="superautobot.log" scontext=system_u:system_r:init_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=1
type=AVC msg=audit(...): avc: denied { append } for pid=947635 comm="(python)" name="reindex_daily.log" scontext=system_u:system_r:init_t:s0 tcontext=system_u:object_r:etc_t:s0 tclass=file permissive=1

Corresponding unit (representative):

[Service]
Type=oneshot
User=someuser
ExecStart=/home/someuser/projects/x/venv/bin/python main.py
StandardOutput=append:/home/someuser/projects/x/logs/x.log
StandardError=append:/home/someuser/projects/x/logs/x.log

Host B (different machine, same systemd/policy generation, different symptom — a shell script that is itself the ExecStart of a Type=oneshot + User= unit writing an output file):

type=AVC msg=audit(...): avc: denied { open } for pid=1039590 comm="render_verified" path="/home/.../reports/.codescan-report.XXXXXX" scontext=system_u:system_r:init_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file permissive=1
type=AVC msg=audit(...): avc: denied { write } for pid=1039590 comm="render_verified" name=".codescan-report.XXXXXX" scontext=system_u:system_r:init_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file permissive=1

Here comm="render_verified" exe="/usr/bin/bash" subj=system_u:system_r:init_t:s0 — the ExecStart script itself, not just a pre-exec I/O setup step, running the whole way through in init_t. This matches my original repro's ps -eZ observation more directly than the log-append case above.

Both hosts: SELinux permissive (targeted policy), systemd 257 (257-24.el10-g2897882), CentOS Stream 10, kernel 6.12.0-224.el10.x86_64 / 6.12.0-264.el10.x86_64.

Why this matters beyond the abstract repro

In enforcing mode this would silently drop application log lines (StandardOutput=append: writes failing) and break output files these jobs are meant to produce (the second example), for any Type=oneshot + User= unit — a fairly common systemd unit shape for cron-like scheduled jobs. We're staying in permissive specifically because of gaps like this one, so it's not causing us active harm today, but it's a real trap for anyone who flips to enforcing without knowing to look for it.

Happy to provide the full multi-line audit records (PROCTITLE/PATH/SYSCALL/AVC) for either example, or sesearch/seinfo output from either host, if that's useful for narrowing down where in the fork/exec sequence the transition is supposed to happen but doesn't.

BenkiNew avatar Aug 28 '26 05:08 BenkiNew

Correction: my original diagnosis in this issue was wrong

Coming back to this with a proper reproduction matrix, I have to retract the premise in the title. Type=oneshot + User= is not the relevant variable, and the process does not stay in init_t for its lifetime.

Disproof of the original claim

Tested on two hosts (systemd 257-33.el10, selinux-policy-targeted 42.1.27-2, CentOS Stream 10, SELinux permissive), via systemd-run with an ExecStart script that prints /proc/self/attr/current:

Type User= executable label resulting domain
oneshot yes bin_t unconfined_service_t
oneshot yes user_home_t unconfined_service_t
oneshot no bin_t unconfined_service_t
simple yes bin_t unconfined_service_t
simple yes user_home_t unconfined_service_t
simple no bin_t unconfined_service_t

All six transition correctly. My original ps -eZ observation must have caught the pre-exec child rather than the running payload; I did not distinguish the two at the time.

What is actually happening

The AVCs I reported are real, but they are not a failed domain transition. They come from the file StandardOutput=append: points at being opened while the process is still being set up — before execve(), and therefore before the domain transition. src/core/exec-invoke.c makes the ordering explicit: setup_output() (which opens the target via acquire_path()) runs at the top of exec_invoke(), setexeccon() roughly 800 lines further down, and execve() after that.

Single unit demonstrating both sides at once — Type=oneshot, User=, StandardOutput=append: and StandardError=append: pointing at a file labelled httpd_sys_content_t, whose ExecStart script also appends to that same file itself:

# systemd's setup of stdout/stderr — pre-exec, manager's context:
avc: denied { open }   comm="(probe.sh)" path=".../target.log" scontext=system_u:system_r:init_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file permissive=1
avc: denied { append } comm="(probe.sh)" name="target.log"     scontext=system_u:system_r:init_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file permissive=1

# the script's own append to the very same file — post-exec, no denial:
SCRIPT-OWN-WRITE ctx=system_u:system_r:unconfined_service_t:s0

Note the parenthesised comm="(probe.sh)" on the denials: that is the child before execve(). Reproduced identically on both hosts.

So the transition works; what is checked against init_t is only the manager-side open of the redirection target. This also explains the StandardOutput=append: denials in my earlier comment, and why the payload itself never showed a denial.

Consequence and proposed resolution

This is inherent to the design — the fd has to exist before execve(), and the domain cannot change before execve() — so there is nothing to reorder in the code, and #14385 shows the fd-passing behaviour is deliberate. The gap is that systemd.exec(5) mentions the manager's privileges only for creating the file, and says nothing about an existing file being opened in the manager's security context. In enforcing mode that silently drops a unit's log output when the file is labelled only for the domain the service transitions into.

I have opened a documentation PR for that: https://github.com/systemd/systemd/pull/43646

Happy for this issue to be closed in favour of the PR, or retitled if you would rather keep it as the tracking item.

BenkiNew avatar Sep 03 '26 17:09 BenkiNew

Follow-up: #43646 merged (05.09.2026), documenting exactly this ordering. Closing in favour of the PR.

BenkiNew avatar Sep 05 '26 14:09 BenkiNew