Skip to content

Commit 6c28345

Browse files
DaanDeMeyerbehrmann
authored andcommitted
Check for CAP_SYS_ADMIN instead of root
Even if we're running as root, we might not have CAP_SYS_ADMIN, so let's always check for CAP_SYS_ADMIN.
1 parent 64495fd commit 6c28345

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

mkosi/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
workdir,
115115
)
116116
from mkosi.sandbox import (
117+
CAP_SYS_ADMIN,
117118
CLONE_NEWNS,
118119
MOUNT_ATTR_NODEV,
119120
MOUNT_ATTR_NOEXEC,
@@ -123,6 +124,7 @@
123124
MS_SLAVE,
124125
__version__,
125126
acquire_privileges,
127+
have_effective_cap,
126128
join_new_session_keyring,
127129
mount,
128130
mount_rbind,
@@ -4888,12 +4890,11 @@ def run_build(
48884890
metadata_dir: Path,
48894891
package_dir: Optional[Path] = None,
48904892
) -> None:
4891-
if os.getuid() != 0:
4893+
if not have_effective_cap(CAP_SYS_ADMIN):
48924894
acquire_privileges()
4893-
4894-
unshare(CLONE_NEWNS)
4895-
4896-
if os.getuid() == 0:
4895+
unshare(CLONE_NEWNS)
4896+
else:
4897+
unshare(CLONE_NEWNS)
48974898
mount("", "/", "", MS_SLAVE | MS_REC, "")
48984899

48994900
# For extra safety when running as root, remount a bunch of directories read-only unless the output

mkosi/sandbox.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def become_user(uid: int, gid: int) -> None:
452452

453453

454454
def acquire_privileges(*, become_root: bool = False) -> bool:
455-
if os.getuid() == 0 or (not become_root and have_effective_cap(CAP_SYS_ADMIN)):
455+
if have_effective_cap(CAP_SYS_ADMIN) and (os.getuid() == 0 or not become_root):
456456
return False
457457

458458
if become_root:

0 commit comments

Comments
 (0)