Skip to content

fix(service): run the Linux system unit in the installer's SELinux context - #17151

Merged
chenmingwei23 merged 1 commit into
mainfrom
fix/selinux-unit-context
Oct 7, 2026
Merged

chenmingwei23 merged 1 commit into
mainfrom
fix/selinux-unit-context

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: On an SELinux host, files the Linux system service writes under the user's home carry the user's own SELinux user, so the user's shell can keep using those caches.

The system unit runs the gateway as User=<you> but in a system SELinux domain, so its SELinux user is system_u. Every cache it or its agents create under $HOME (~/.npm/_cacache, ~/.gradle, ~/.cache/pip) is labelled system_u, and the user's unconfined_u shell is then refused hardlinks inside it: npm fails with EPERM / syscall link and calls it "root-owned files".

Why it matters

Any RHEL/Fedora/AL2023 user who installs the service gets their npm, Brazil, Gradle or pip caches broken by the agent's builds, with a misleading error, and wiping the cache does not stick while the agent keeps building.

Not a goal

  • A user-scope unit at install time (an install-model change, tracked by feat(service): prefer systemd user units #10813).
  • Confined SELinux users (staff_u, user_u): local policy may refuse PID 1 the switch into their context; the unit then fails to start and the existing start-failure hint points at the per-user unit.
  • Relabelling caches that are already system_u; the install guide says how.
  • Hosts where policy forbids PID 1 from executing the binary under $HOME: the existing pre-flight refusal still handles them.

What changed (motivation → approach → change)

SELinux on the host Policy check Unit gets
off — no SELinuxContext= line (unchanged)
on (enforcing or permissive) PID 1 may switch into the installer's context, and that context may start the binary SELinuxContext=<installer context>
on either answer is no, or the policy cannot be asked no line (unchanged), one INFO log line
on installer context unreadable, malformed or system_u no line (unchanged)
any user-scope unit no line

A new file takes its SELinux user from the process that creates it, so the fix is to start the gateway in the user's own context. installer_context() in src/kiro_crew/service/selinux.py reads the installing process's context from /proc/self/attr/current (the user's login context, kept through sudo). Before handing it out it asks the loaded policy, through the same /sys/fs/selinux/access query the existing pre-flight uses, whether PID 1 may process transition into it and whether it has file entrypoint on the binary and its shebang interpreter (the same file list the pre-flight checks, _exec_candidates). Any no or unanswered question leaves the line out, so a unit that starts today keeps starting.

render_unit() in src/kiro_crew/service/linux.py writes the context into the system unit as SELinuxContext=. The install guide (docs/guides/install.md) explains the line, when it is left out, and how to clean an already-poisoned cache.

Scope this reaches, from this AL2023 host's targeted policy: a kirocrew labelled bin_t/usr_t (e.g. /usr/local/bin) gets the line; one labelled home_bin_t (~/.local/bin) or initrc_exec_t does not, because unconfined_t has no entrypoint there, and keeps today's behaviour.

Backwards compatibility

Compatible: the line is written only when the loaded policy says the unit can start with it; everywhere else the unit renders byte-for-byte as before, and an existing install keeps its old unit until kirocrew service install is re-run.

Removes nothing: no directive, key or path is taken away.

Tests

test/test_service_selinux_context.py points the module at a tmp selinuxfs, a fake /proc/self/attr/current and a fake policy answer (_compute_av, the one kernel call), so the result never depends on the CI host. It pins: policy allows → the context (enforcing and permissive); process transition denied, file entrypoint denied on the binary or on its interpreter, an unanswered query, and a missing /sys/fs/selinux/access node (the real query path) → no context; one INFO line naming why; no SELinux, unreadable, system_u and malformed contexts → none; the rendered system unit carries the line inside [Service] only when allowed, and the user-scope unit never does. A gate that always answers "allowed" fails 7 of them.

Manual verification

On this AL2023 host (SELinux permissive, not switched) the real policy was queried through /sys/fs/selinux/access: init_t -> unconfined_u:unconfined_r:unconfined_t process transition is allowed; unconfined_t file entrypoint is allowed on bin_t, usr_t, user_home_t and denied on home_bin_t, initrc_exec_t, default_t. Not installed and started on an enforcing host (none available); the policy gate is what keeps that untested path from stopping a unit.

Related Issues

Closes #17150

Internal ticket: Mesh-3268

Pattern harvest

Not generalizable: the defect is specific to how a systemd system unit picks the SELinux user for its process; no other code path here launches a long-lived process through PID 1.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@iamwhatever
iamwhatever requested a review from a team as a code owner October 5, 2026 21:44
@iamwhatever
iamwhatever requested a review from CrysisDeu October 5, 2026 21:44
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ human override accepted

@iamwhatever overrode this lane for a7de4330286043a881adc6a61e1779238df9ade2 via /ai-review override (this commit only).

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ human override accepted

@iamwhatever overrode this lane for a7de4330286043a881adc6a61e1779238df9ade2 via /ai-review override (this commit only).

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ human override accepted

Human judgment by @iamwhatever overrides the GPT 6.1 finding for a7de4330286043a881adc6a61e1779238df9ade2; the recorded reason is authoritative for this commit.

[GPT-OVERRIDE] a7de433

This comment is updated in place on each push.

The model was not re-run because an authorized human decision supersedes it.

False positive or not applicable? A repository writer can comment:
/ai-review override gpt a7de4330286043a881adc6a61e1779238df9ade2: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ human override accepted

Reviewed a7de4330286043a881adc6a61e1779238df9ade2 — this comment is updated in place on each push.

Human judgment by @iamwhatever overrides the Opus 5.5 finding for a7de4330286043a881adc6a61e1779238df9ade2; the recorded reason is authoritative for this commit.

[OPUS-OVERRIDE] a7de433

Verdict recorded from an authorized human decision for commit a7de4330286043a881adc6a61e1779238df9ade2.

False positive or not applicable? A repository writer can comment:
/ai-review override fable a7de4330286043a881adc6a61e1779238df9ade2: <one-sentence reason>

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Fix: ask the policy before emitting the line span=c824f79e3971 — fixed

selinux.installer_context(exec_path) now asks the loaded policy (the same /sys/fs/selinux/access query blocks_system_unit uses) whether PID 1 may process transition into the context and whether the context has file entrypoint on the binary and its shebang interpreter.
Any denial or unanswered query omits the line and logs one INFO line, so a unit that starts today renders byte-for-byte as before.
Real policy on an AL2023 host: init_t -> unconfined_t transition allowed; unconfined_t entrypoint allowed on bin_t/usr_t, denied on home_bin_t/initrc_exec_t, so those installs keep today's unit.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Unverified premise on a core availability path (gateway boot) span=75a0e50977c2 — fixed

The premise is no longer assumed: the line is emitted only when the loaded policy answers that PID 1 may transition into the context and the context may enter the binary; otherwise the unit is unchanged.
No enforcing host is available, so the start itself is not exercised; the gate is what keeps that path from stopping a unit that boots today.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Item 1 — premise risk: gateway-boot path span=b588a6ab2392 — fixed

Same remedy as the blocker: installer_context gates the line on two policy queries (process transition from PID 1, file entrypoint on the binary and interpreter) and omits it on any no or unanswered query.
The description now names the scope this reaches (bin_t/usr_t installs) and the cases that keep today's unit (home_bin_t, initrc_exec_t), measured against a real targeted policy.

@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from da4e84b to 15ba083 Compare October 5, 2026 22:46
@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • The gate may pass a unit that cannot start span=2ffb29eea9bc — rebutted

The two queried permissions are the ones the observed policy can deny: on a real AL2023 targeted policy unconfined_t file entrypoint is denied on home_bin_t/initrc_exec_t, and the gate omits the line there.
The rest of an exec under SELinuxContext= (process setexec on PID 1, inherited fd use, rlimit/sig inheritance) is what systemd already needs for every unit that sets SELinuxContext= and for user@.service, which PID 1 starts in unconfined_t on these hosts; a policy that refused it would not boot user sessions.
Residual risk stated in the description: the start itself is not exercised on an enforcing host (none available).

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Ask the kernel end to end with a throwaway systemd-run at install span=0b602196edc2 — rebutted

A transient systemd-run --property=SELinuxContext=<ctx> /bin/true runs /bin/true (bin_t), not the kirocrew binary, so it cannot see the entrypoint denial that actually differs by install; running the real binary would start a second gateway.
On a permissive host it exits 0 whatever the policy says, so it adds no signal where the static query already answers, and it starts a root-owned unit as a side effect of rendering a file.
The policy query stays read-only and is the same interface the existing pre-flight trusts.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

Backend Tests (Windows) (2) is red here for a reason outside this diff: test/test_slot_ownership_checkpoint.py::TestTranscriptOwnershipWithoutALiveSlot fails with resume_conflict ("this session changed while resuming"), on 15ba08350c13 (same as PR #17052). Main a92dde1a6c failed the same shard with the same 409 (test_dashboard_chat.py::TestFolderCRUD::test_resume_session_unhides_folder).

This is #17117 (a transient Windows sharing violation on the post-hook identity re-read). The fix is open as #17130. This PR waits for it, then rebases onto it; no override is being asked for this red.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 5, 2026
@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from 15ba083 to 81bd945 Compare October 6, 2026 00:32
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Rebased: main moved since this head (the coverage baseline row for src/kiro_crew/apps/hooks_integration.py was dropped in #17114, which this head's Coverage Gate tripped on) (15ba083 -> 81bd945, base 782dedd); CI re-running. No change to this PR's own files.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Item 4 — duplicate of blocks_system_unit candidate resolution span=36a60222e7d3 — fixed

One copy remains: _exec_candidates(exec_path) in src/kiro_crew/service/selinux.py returns the resolved binary and its resolved shebang interpreter, and both blocks_system_unit and installer_context call it.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Unverified premise on a core availability path (gateway boot) span=80244141245d — needs-a-decision

No enforcing host is available to start the re-rendered unit, so this cannot be cleared by a code change.
What stands: the line is written only after a read-only policy query says PID 1 may transition into the context and the context may enter the binary and its interpreter; any no or unanswered query leaves the unit byte-for-byte as today.
Maintainer question: accept the policy-gated line without an enforcing-host start, or hold this PR until someone runs it on one.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Item 1 — premise risk: the policy gate stands in for an enforcing-host start span=85103c10a803 — needs-a-decision

Same question as the blocker on this head: the description states the start was not exercised on an enforcing host; the policy gate is what keeps that path from stopping a unit that boots today.
Left to a maintainer ruling; no override is posted by the author.

@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from 81bd945 to c3b4a49 Compare October 6, 2026 01:08
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 6, 2026
@github-actions github-actions Bot removed the readiness: checking Automated validation is still running label Oct 6, 2026
@github-actions github-actions Bot added the readiness: action required A blocking check or review needs attention label Oct 6, 2026
@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from c3b4a49 to c9f3476 Compare October 6, 2026 07:30
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from c9f3476 to 4cc9b3d Compare October 7, 2026 01:13
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 7, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Autouse isolation uses the shared test undo stack span=840716e918c5 — fixed in a7de433

fixed: the autouse _user fixture in test/test_service_selinux_context.py now patches USER / SUDO_USER through _floor_monkeypatch, the isolation floor's own MonkeyPatch, so a test calling monkeypatch.undo() no longer lifts it.
Same change clears SAST rule semgrep.kirocrew.test-autouse-shared-monkeypatch at line 168.
No production code changed; test/test_service_selinux_context.py + test/test_service.py: 440 passed, 3 skipped.

@iamwhatever
iamwhatever force-pushed the fix/selinux-unit-context branch from 4cc9b3d to a7de433 Compare October 7, 2026 08:32
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 7, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

/ai-review override all a7de433: Maintainer accepts the policy-gated SELinuxContext= line without an enforcing-host start; the line is emitted only when a libselinux policy query allows the transition and the entrypoint, and on no/unknown the unit renders exactly as today, so the start-failure mode the reviewers describe is not reachable.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Human judgment recorded

@iamwhatever marked the all AI finding as false positive, not applicable, or explicitly accepted for a7de4330286043a881adc6a61e1779238df9ade2.

Maintainer accepts the policy-gated SELinuxContext= line without an enforcing-host start; the line is emitted only when a libselinux policy query allows the transition and the entrypoint, and on no/unknown the unit renders exactly as today, so the start-failure mode the reviewers describe is not reachable.

This decision applies only to this commit. A new push requires a new judgment.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ human override accepted

@iamwhatever overrode this lane for a7de4330286043a881adc6a61e1779238df9ade2 via /ai-review override (this commit only).

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 7, 2026
@chenmingwei23
chenmingwei23 merged commit 460300a into main Oct 7, 2026
108 of 110 checks passed
@chenmingwei23
chenmingwei23 deleted the fix/selinux-unit-context branch October 7, 2026 19:42
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux service: gateway files under $HOME get the system_u SELinux user and break the user's caches

2 participants