Skip to content

Commit

Permalink
Introduce INVOKING_USER.mkdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeMeyer committed Mar 31, 2024
1 parent 000b81c commit 0fe78eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 1 addition & 11 deletions mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
format_rlimit,
make_executable,
one_zero,
parents_below,
read_env_file,
round_up,
scopedenv,
Expand Down Expand Up @@ -4223,16 +4222,7 @@ def run_build(args: Args, config: Config, *, resources: Path) -> None:
if not p or p.exists():
continue

p.mkdir(parents=True, exist_ok=True)

# If we created the directory in a parent directory owned by the invoking user, make sure the directories we
# just created are owned by the invoking user as well.
if (
INVOKING_USER.is_regular_user() and
(q := next((parent for parent in p.parents if parent.stat().st_uid == INVOKING_USER.uid), None))
):
for parent in parents_below(p, q):
os.chown(parent, INVOKING_USER.uid, INVOKING_USER.gid)
INVOKING_USER.mkdir(p)

# Discard setuid/setgid bits as these are inherited and can leak into the image.
if config.build_dir:
Expand Down
17 changes: 16 additions & 1 deletion mkosi/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from mkosi.log import die
from mkosi.run import run, spawn
from mkosi.util import flock
from mkosi.util import flock, parents_below

SUBRANGE = 65536

Expand Down Expand Up @@ -85,6 +85,21 @@ def rchown(cls, path: Path) -> None:
if cls.is_regular_user() and any(p.stat().st_uid == cls.uid for p in path.parents) and path.exists():
run(["chown", "--recursive", f"{INVOKING_USER.uid}:{INVOKING_USER.gid}", path])

@classmethod
def mkdir(cls, path: Path) -> None:
path.mkdir(parents=True, exist_ok=True)

# If we created the directory in a parent directory owned by the invoking user, make sure the directories we
# just created are owned by the invoking user as well.
if (
cls.is_regular_user() and
(q := next((parent for parent in path.parents if parent.stat().st_uid == cls.uid), None))
):
os.chown(path, INVOKING_USER.uid, INVOKING_USER.gid)

for parent in parents_below(path, q):
os.chown(parent, INVOKING_USER.uid, INVOKING_USER.gid)


def read_subrange(path: Path) -> int:
uid = str(os.getuid())
Expand Down

0 comments on commit 0fe78eb

Please sign in to comment.