Fix #4233: mkosi runs into permission errors even when running as root#4268
Open
JiwaniZakir wants to merge 1 commit intosystemd:mainfrom
Open
Fix #4233: mkosi runs into permission errors even when running as root#4268JiwaniZakir wants to merge 1 commit intosystemd:mainfrom
mkosi runs into permission errors even when running as root#4268JiwaniZakir wants to merge 1 commit intosystemd:mainfrom
Conversation
When mkosi runs as root with acquire_privileges(foreign=True, delegate=3), the `and not delegate` guard forced entering a user namespace even though root already has all necessary host privileges. Inside that namespace root lost host-filesystem access, causing PermissionError in ensure_directories_exist() for paths under another user's home directory. Fix by returning early (no user namespace) when the calling process is already true root (UID=0, GID=0), regardless of the delegate count. Fixes: systemd#4233 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
behrmann
reviewed
Apr 5, 2026
Comment on lines
+21
to
+22
| result = acquire_privileges(foreign=True, delegate=3) | ||
| assert result is False |
Contributor
There was a problem hiding this comment.
Suggested change
| result = acquire_privileges(foreign=True, delegate=3) | |
| assert result is False | |
| assert not acquire_privileges(foreign=True, delegate=3) |
Contributor
There was a problem hiding this comment.
The regression test is a nice thought, but I don't see it surviving the next refactor. The internal API of sandbox.py changes as requirements for it change. The relevant thing is the behaviour as seen from the outside.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4233
Skip user namespace entry in
acquire_privilegeswhen running as real root with a non-zerodelegatevalue.Changes
mkosi/sandbox.py—acquire_privileges()The condition on line 802 that gates early-exit (returning
False, meaning "don't enter a user namespace") previously blocked that exit wheneverdelegatewas truthy. It now allows the early exit when bothos.getuid() == 0andos.getgid() == 0, so a real-root process is not forced into a user namespace solely because of a non-zerodelegateargument.tests/test_sandbox.py(new file)Adds
test_acquire_privileges_root_with_delegate_skips_userns, which mockshave_effective_capto returnTrue, patchesos.getuid/os.getgidto return0, and asserts thatacquire_privileges(foreign=True, delegate=3)returnsFalse.Motivation
When mkosi is invoked as root, the sandbox set up before
ensure_directories_exist()was entering a user namespace becausedelegatewas non-zero. Inside that namespace the process no longer held host-root privileges, making directories under paths like/home/user/...(mode0700) inaccessible. The result was aPermissionError: [Errno 13] Permission deniedon amkdircall for the build directory, even though the caller was UID 0 on the host. The regression was introduced in #4160, which added thedelegateparameter path throughacquire_privileges.Real root does not benefit from a user namespace for privilege escalation purposes — it already has the capabilities it needs — so skipping namespace entry when
uid == gid == 0is both correct and safe.Testing
Unit test added in
tests/test_sandbox.pyvalidates the fixed code path directly:Manual verification: running
mkosias root with a build directory inside a0700home directory no longer raisesPermissionErrorand proceeds throughensure_directories_exist()successfully.This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.