Skip to content

Commit c0dc307

Browse files
committed
default to using the root user in the namespace
map the user as root, so it has privileges to user owned files outside the namespace Signed-off-by: Zen <[email protected]>
1 parent 325875a commit c0dc307

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/zenlib/util/namespace.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from multiprocessing import Event, Pipe, Process, Queue
2-
from os import CLONE_NEWNS, CLONE_NEWUSER, chroot, getlogin, setgid, setuid, unshare
2+
from os import CLONE_NEWNS, CLONE_NEWUSER, chroot, getlogin, setgid, setuid, getuid, getgid, unshare
33
from subprocess import CalledProcessError, run
44

55

@@ -20,16 +20,16 @@ def get_id_map(username=None, id_type="uid"):
2020
raise ValueError(f"User {username} not found in /etc/sub{id_type}")
2121

2222

23-
def new_id_map(id_type, pid, id, nsid, count=2**16, failures=0):
23+
def new_id_map(id_type, pid, id, nsid, count=1, *args, failures=0):
2424
if id_type not in ("uid", "gid"):
2525
raise ValueError("id_type must be 'uid' or 'gid")
26-
args = [f"new{id_type}map", str(pid), str(id), str(nsid), str(count)]
26+
cmd_args = [f"new{id_type}map", str(pid), str(id), str(nsid), str(count), *map(str, args)]
2727
try:
28-
return run(args, check=True)
28+
return run(cmd_args, check=True)
2929
except CalledProcessError as e:
3030
if failures > 5:
3131
raise e
32-
new_id_map(id_type, pid, id, nsid, count, failures + 1)
32+
new_id_map(id_type, pid, id, nsid, count, *args, failures=failures + 1)
3333

3434

3535
class NamespaceProcess(Process):
@@ -38,21 +38,21 @@ class NamespaceProcess(Process):
3838
"""
3939

4040
def __init__(self, target=None, args=None, kwargs=None, **ekwargs):
41-
self.uid = int(kwargs.pop("uid", 0))
42-
self.gid = int(kwargs.pop("gid", 0))
4341
self.target_root = kwargs.pop("target_root", "/")
4442
namespace_user = kwargs.pop("namespace_user", getlogin())
4543
self.subuid_start, self.subuid_count = get_id_map(namespace_user, "uid")
4644
self.subgid_start, self.subgid_count = get_id_map(namespace_user, "gid")
45+
self.orig_uid = getuid()
46+
self.orig_gid = getgid()
4747
self.uidmapped = Event()
4848
self.completed = Event()
4949
self.exception_recv, self.exception_send = Pipe()
5050
self.function_queue = Queue()
5151
super().__init__(target=target, args=args, kwargs=kwargs, **ekwargs)
5252

5353
def map_ids(self):
54-
new_id_map("uid", self.pid, self.uid, self.subuid_start, self.subuid_count)
55-
new_id_map("gid", self.pid, self.gid, self.subgid_start, self.subgid_count)
54+
new_id_map("uid", self.pid, 0, self.orig_uid, 1, 1, self.subuid_start, self.subuid_count)
55+
new_id_map("gid", self.pid, 0, self.orig_gid, 1, 1, self.subgid_start, self.subgid_count)
5656

5757
def map_unshare_uids(self):
5858
self.start()
@@ -62,8 +62,8 @@ def map_unshare_uids(self):
6262
def run(self):
6363
unshare_namespace()
6464
self.uidmapped.wait()
65-
setuid(self.uid)
66-
setgid(self.gid)
65+
setuid(0)
66+
setgid(0)
6767
chroot(self.target_root)
6868
try:
6969
self.function_queue.put(self._target(*self._args, **self._kwargs))

tests/test_namespace.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ def test_user_namespace_func(self):
2828
def test_user_namespace_uid_gid(self):
2929
self.assertEqual(nsexec(test_uid_gid), (0, 0))
3030

31-
def test_user_namespace_alt_uid_gid(self):
32-
self.assertEqual(nsexec(test_uid_gid, uid=9999, gid=9999), (9999, 9999))
33-
3431

3532
if __name__ == "__main__":
3633
main()

0 commit comments

Comments
 (0)