Skip to content

Commit afa4417

Browse files
committed
unshare in two steps
https: //github.com//issues/13952 Signed-off-by: Caolán McNamara <[email protected]> Change-Id: I7ce87bf7455f6f99155baa11030f3e57e0ff2b16
1 parent f209539 commit afa4417

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

common/JailUtil.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,31 @@ bool enterMountingNS(uid_t uid, gid_t gid)
6363
{
6464
#ifdef __linux__
6565
// Put this process into its own user and mount namespace.
66-
if (unshare(CLONE_NEWNS | CLONE_NEWUSER) != 0)
66+
// Note: Having multiple threads at unshare time is a known source of failure.
67+
if (unshare(CLONE_NEWUSER) != 0)
6768
{
68-
// having multiple threads is a source of failure f.e.
69-
LOG_SYS("enterMountingNS, unshare failed");
69+
LOG_SYS("enterMountingNS, CLONE_NEWUSER unshare failed");
7070
return false;
7171
}
7272

7373
setdeny();
7474

75+
// Map this user as the root user of the new namespace
76+
mapuser(uid, 0, gid, 0);
77+
78+
if (unshare(CLONE_NEWNS) != 0)
79+
{
80+
LOG_SYS("enterMountingNS, CLONE_NEWNS unshare failed");
81+
return false;
82+
}
83+
7584
// Do not propagate any mounts from this new namespace to the system.
7685
if (mount("none", "/", nullptr, MS_REC | MS_PRIVATE, nullptr) != 0)
7786
{
7887
LOG_SYS("enterMountingNS, root mount failed");
79-
// set to original uid so coolmount check isn't surprised by 'nobody'
80-
mapuser(uid, uid, gid, gid);
8188
return false;
8289
}
8390

84-
// Map this user as the root user of the new namespace
85-
mapuser(uid, 0, gid, 0);
86-
8791
return true;
8892
#else
8993
(void)uid;

0 commit comments

Comments
 (0)