Skip to content

Commit 1df4a26

Browse files
authored
Improve user namespace detection (#689)
Detect Ubuntu restrictions...
2 parents 954d5a2 + 95f1f7d commit 1df4a26

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

doc/installation.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ not permitted" error when building. Add the line ::
190190

191191
to your ``/etc/sysctl.conf`` (or wherever your distro stores that).
192192

193+
Ubuntu is further restricting user namespaces through AppArmor. Either set
194+
``/proc/sys/kernel/apparmor_restrict_unprivileged_userns`` to ``0`` or create
195+
an AppArmor profile for the ``bob-namespace-sandbox`` binary that allows user
196+
namespaces for Bob.
197+
193198
If you are using Docker, you must relax the security settings as they block
194199
nested containers by default. Adding ``--security-opt seccomp=unconfined
195200
--security-opt apparmor=unconfined`` to the ``docker run`` command should

pym/bob/invoker.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,23 @@ def ensureSandboxUsable():
450450
ret = subprocess.run([getSandboxHelperPath(), "-C"], stdout=subprocess.DEVNULL,
451451
stderr=subprocess.DEVNULL).returncode
452452
if ret != 0:
453+
knobs = [("/proc/sys/kernel/unprivileged_userns_clone", "1"),
454+
("/proc/sys/kernel/apparmor_restrict_unprivileged_userns", "0")]
455+
for knob, expected_val in knobs:
456+
try:
457+
with open(knob) as f:
458+
if f.read().strip() != expected_val:
459+
msg = f"Make sure {knob} is {expected_val}."
460+
break
461+
except FileNotFoundError:
462+
pass
463+
except OSError:
464+
msg = f"Make sure {knob} is {expected_val}."
465+
break
466+
else:
467+
msg = f"Not sure why that is exactly. Please consider opening a bug report at https://github.com/BobBuildTool/bob."
453468
raise BuildError("Your system does not support unprivileged containers! You need to build without sandbox (--no-sandbox).",
454-
help="Make sure /proc/sys/kernel/unprivileged_userns_clone is 1."
455-
" In case you use Docker, consider using the 'unconfined' seccomp profile."
469+
help=f"{msg} In case you use Docker, consider using the 'unconfined' seccomp profile."
456470
" See https://bob-build-tool.readthedocs.io/en/latest/installation.html#sandbox-capabilities for more details.",
457471
returncode=3)
458472
except OSError as e:

src/namespace-sandbox/namespace-sandbox.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ struct Options {
7474
static int CreateTarget(const char *path, bool is_directory);
7575

7676
// Child function used by CheckNamespacesSupported() in call to clone().
77-
static int CheckNamespacesSupportedChild(void *arg) { return 0; }
77+
static int CheckNamespacesSupportedChild(void *arg) {
78+
// Check that we can make our mount namespace private. This fails on Ubuntu if
79+
// /proc/sys/kernel/apparmor_restrict_unprivileged_userns_complain is enabled.
80+
return mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) == 0
81+
? EXIT_SUCCESS : EXIT_FAILURE;
82+
}
7883

7984
// Check whether the required namespaces are supported.
8085
static int CheckNamespacesSupported() {
@@ -100,9 +105,13 @@ static int CheckNamespacesSupported() {
100105
CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWUTS |
101106
CLONE_NEWIPC | CLONE_NEWNET | SIGCHLD,
102107
NULL));
103-
CHECK_CALL(waitpid(pid, NULL, 0));
108+
int status = 0;
109+
CHECK_CALL(waitpid(pid, &status, 0));
104110

105-
return EXIT_SUCCESS;
111+
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
112+
return EXIT_SUCCESS;
113+
else
114+
return EXIT_FAILURE;
106115
}
107116

108117
// Print out a usage error. argc and argv are the argument counter and vector,

test/run-tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ if [[ -n $RUN_COVERAGE ]] ; then
227227
fi
228228
fi
229229

230+
# Make sure sandbox helper is updated
231+
$RUN_PYTHON3 -c "from bob.develop.make import makeSandboxHelper
232+
makeSandboxHelper()
233+
"
234+
230235
# execute everything if nothing was specified
231236
if [[ -z ${RUN_UNITTEST_PAT+isset} && -z ${RUN_BLACKBOX_PAT+isset} &&
232237
-z ${RUN_INTEGRATION_PAT+isset} ]] ; then

0 commit comments

Comments
 (0)