Skip to content

Commit 339b3f4

Browse files
authored
Merge pull request DeusData#2195 from DeusData/fix/userns-smoke-spawn
fix(daemon,test,ci): make the DeusData#1830 userns guard actually run, and delete the cache behind it
2 parents 0b51555 + ac8f5b8 commit 339b3f4

5 files changed

Lines changed: 192 additions & 18 deletions

File tree

‎.github/workflows/_test.yml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ jobs:
107107
if: startsWith(matrix.os, 'ubuntu')
108108
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev ccache
109109

110+
# Ubuntu 23.10+ ships kernel.apparmor_restrict_unprivileged_userns=1, which
111+
# blocks unshare(CLONE_NEWUSER) for unprivileged users. Without this, the
112+
# #1830 single-uid-userns smoke test SKIPS on every 24.04 leg, and its only
113+
# real-namespace coverage lived on the two ubuntu-22.04 legs -- which are
114+
# broad-matrix only (never PR CI) and which GitHub retires on 2027-04-17.
115+
# A deliberate relaxation of a security check was therefore guarded by a
116+
# test that ran in no PR and was on a clock. Hosted runners have
117+
# passwordless sudo, so one sysctl puts it on the CORE matrix instead.
118+
# Best-effort: a runner that refuses simply returns to skipping.
119+
- name: Permit unprivileged user namespaces (Ubuntu)
120+
if: startsWith(matrix.os, 'ubuntu')
121+
run: |
122+
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 || \
123+
echo "could not lift the userns restriction; namespace tests will skip"
124+
# Then SAY whether it worked. The runner prints per-shard aggregates
125+
# ("N passed, M skipped") and never individual test names, so a
126+
# namespace test that silently returns to skipping is invisible in a
127+
# green log -- the same false assurance that let #1830 ship with a
128+
# test that never executed. This line is the evidence. Probed exactly
129+
# as the test does it: unprivileged, no sudo.
130+
if unshare -U -r true 2>/dev/null; then
131+
echo "CBM_USERNS_AVAILABLE=yes"
132+
else
133+
echo "CBM_USERNS_AVAILABLE=no (namespace-dependent tests will skip)"
134+
fi
135+
110136
- name: Install ccache (macOS)
111137
if: startsWith(matrix.os, 'macos')
112138
run: command -v ccache >/dev/null 2>&1 || brew install ccache

‎src/daemon/ipc.c‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,9 +1463,21 @@ static char *private_log_directory_path_copy(const char *directory_path) {
14631463
*
14641464
* The overflow owner is tolerated for ANCESTORS ONLY, and ONLY when
14651465
* /proc/self/uid_map is a single-uid map whose sole inside id is our euid. In
1466-
* that shape no in-namespace principal can be the overflow owner, so an
1467-
* overflow-owned ancestor is exactly as safe as a root-owned one on the host:
1468-
* nobody reachable can have created it or can mutate it. A multi-uid map
1466+
* that shape no IN-NAMESPACE principal can be the overflow owner.
1467+
*
1468+
* Be precise about what that does and does not buy, because an earlier version
1469+
* of this comment overstated it. The overflow uid is what EVERY unmapped host
1470+
* uid maps to, not only host root (user_namespaces(7)), so "overflow-owned"
1471+
* does NOT prove "created by root". On a shared host, a directory owned by
1472+
* another local user is indistinguishable from root-owned /tmp once you are
1473+
* inside the namespace -- and there is no in-namespace discriminator that could
1474+
* tell them apart, which is precisely why the tolerance is scoped the way it
1475+
* is rather than made smarter. The real guarantee is narrower and still
1476+
* sufficient: no principal REACHABLE FROM INSIDE the namespace can create or
1477+
* mutate such an ancestor, and the private directory itself is never tolerated
1478+
* as overflow (see below), so a hostile host-side owner of an ancestor is
1479+
* bounded to denial of service and socket-path control. It cannot reach the
1480+
* leaf, which stays 0700 and euid-rechecked. A multi-uid map
14691481
* (rootless podman with subuids) still shows host root as overflow and is
14701482
* refused — conservative scope, not the safety argument. There is deliberately
14711483
* no environment escape hatch.
@@ -1545,7 +1557,20 @@ static bool posix_read_small_proc_file(const char *path, char *buffer, size_t ca
15451557
return true;
15461558
}
15471559

1560+
#ifdef CBM_ENABLE_TEST_SEAMS
1561+
/* Counts real derivations. A cache here was a security hazard once (see the
1562+
* note above posix_ancestor_overflow_uid); the contract test asserts this
1563+
* climbs on EVERY call so re-introducing one fails loudly. */
1564+
static unsigned g_posix_overflow_compute_count;
1565+
unsigned cbm_daemon_ipc_posix_overflow_compute_count_for_test(void) {
1566+
return g_posix_overflow_compute_count;
1567+
}
1568+
#endif
1569+
15481570
static uid_t posix_compute_ancestor_overflow_uid(void) {
1571+
#ifdef CBM_ENABLE_TEST_SEAMS
1572+
g_posix_overflow_compute_count++;
1573+
#endif
15491574
char map[256];
15501575
if (!posix_read_small_proc_file("/proc/self/uid_map", map, sizeof(map)) ||
15511576
!posix_uid_map_is_single_uid(map, geteuid())) {
@@ -1564,24 +1589,35 @@ static uid_t posix_compute_ancestor_overflow_uid(void) {
15641589
return (uid_t)overflow;
15651590
}
15661591

1567-
static uid_t g_posix_overflow_cached = POSIX_NO_OVERFLOW_UID;
1568-
static pthread_once_t g_posix_overflow_once = PTHREAD_ONCE_INIT;
1569-
static void posix_overflow_init_once(void) {
1570-
g_posix_overflow_cached = posix_compute_ancestor_overflow_uid();
1571-
}
15721592
#endif /* __linux__ */
15731593

1574-
/* The overflow uid tolerated for ancestors in this process, or
1575-
* POSIX_NO_OVERFLOW_UID when none. Derived once from immutable /proc state. */
1594+
/* The overflow uid tolerated for ancestors, or POSIX_NO_OVERFLOW_UID when none.
1595+
*
1596+
* DERIVED FRESH ON EVERY CALL, deliberately. This used to memoise via
1597+
* pthread_once behind a comment claiming "immutable /proc state". That claim
1598+
* was false in both halves: unshare(CLONE_NEWUSER) rewrites
1599+
* /proc/self/uid_map, and pthread_once state survives a forked child already
1600+
* marked done -- so a process that forked and then changed namespace kept the
1601+
* parent answer and refused a directory it should have accepted.
1602+
* (Spelled without the call syntax on purpose: scripts/security-audit.sh
1603+
* blocks that literal in src/, and an allow-list entry to let a COMMENT pass
1604+
* would weaken a real check on a real file.) It happened to be
1605+
* harmless because every caller today runs in a freshly exec'd process, but
1606+
* that made a security decision depend on an invariant nothing enforced, and
1607+
* the next fork-without-exec caller would have silently inherited a stale
1608+
* verdict.
1609+
*
1610+
* The cost of not caching is two small /proc reads per ancestor check, against
1611+
* an openat + fstat + fchmod + ACL check per path component in the same walk.
1612+
* Do not re-introduce a cache here; the contract test counts derivations. */
15761613
static uid_t posix_ancestor_overflow_uid(void) {
15771614
#ifdef CBM_ENABLE_TEST_SEAMS
15781615
if (g_posix_overflow_override_active) {
15791616
return g_posix_overflow_override_uid;
15801617
}
15811618
#endif
15821619
#if defined(__linux__)
1583-
(void)pthread_once(&g_posix_overflow_once, posix_overflow_init_once);
1584-
return g_posix_overflow_cached;
1620+
return posix_compute_ancestor_overflow_uid();
15851621
#else
15861622
return POSIX_NO_OVERFLOW_UID;
15871623
#endif

‎src/daemon/ipc.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ bool cbm_daemon_ipc_posix_uid_map_is_single_uid_for_test(const char *uid_map, un
131131
bool cbm_daemon_ipc_posix_ancestor_stat_ok_for_test(unsigned long owner, unsigned int mode,
132132
unsigned long euid, bool overflow_active,
133133
unsigned long overflow_uid);
134+
#if defined(__linux__)
135+
/* Number of REAL overflow-uid derivations so far. The value is meaningless on
136+
* its own; the point is that it must rise on every ancestor check, proving no
137+
* cache has crept back in. */
138+
unsigned cbm_daemon_ipc_posix_overflow_compute_count_for_test(void);
139+
#endif
134140
#endif
135141
#endif
136142

‎tests/test_daemon_ipc.c‎

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,16 +5124,73 @@ TEST(daemon_ipc_posix_overflow_ancestor_tolerated_only_in_single_uid_ns_issue183
51245124
PASS();
51255125
}
51265126

5127+
/* #1830 regression guard, and the test that would have caught the original bug
5128+
* with no namespace at all. The overflow uid used to be memoised per process
5129+
* via pthread_once. That made a SECURITY decision sticky: pthread_once state
5130+
* survives fork(), unshare(CLONE_NEWUSER) rewrites /proc/self/uid_map, so a
5131+
* process that forked and then entered a namespace kept the parent verdict.
5132+
* Nothing in the suite could see it, because every deterministic #1830 test
5133+
* drives the override seam rather than the real derivation -- they bind the
5134+
* decision TABLE, not the WIRING.
5135+
*
5136+
* So assert the wiring directly: two ancestor checks must perform two real
5137+
* derivations. Re-introduce any cache and the second call derives zero times
5138+
* and this fails by name, on every platform, with no namespace required. */
5139+
TEST(daemon_ipc_posix_overflow_uid_is_never_cached_issue1830) {
5140+
#if defined(__linux__)
5141+
char parent[TEST_PATH_CAP];
5142+
if (!ipc_test_parent_new(parent, "overflow-nocache")) {
5143+
FAIL("could not create the probe parent directory");
5144+
}
5145+
char probe[TEST_PATH_CAP];
5146+
(void)snprintf(probe, sizeof(probe), "%s/x", parent);
5147+
5148+
/* The override seam short-circuits derivation, so it must be OFF here or
5149+
* the test would measure nothing. */
5150+
cbm_daemon_ipc_posix_set_ancestor_overflow_uid_for_test(false, 0);
5151+
5152+
unsigned before = cbm_daemon_ipc_posix_overflow_compute_count_for_test();
5153+
(void)cbm_daemon_ipc_private_directory_secure(probe);
5154+
unsigned after_first = cbm_daemon_ipc_posix_overflow_compute_count_for_test();
5155+
(void)cbm_daemon_ipc_private_directory_secure(probe);
5156+
unsigned after_second = cbm_daemon_ipc_posix_overflow_compute_count_for_test();
5157+
5158+
(void)rmdir(probe);
5159+
ipc_test_remove_flat_dir(parent);
5160+
5161+
ASSERT_TRUE(after_first > before);
5162+
/* The decisive half: the SECOND call must derive again. */
5163+
ASSERT_TRUE(after_second > after_first);
5164+
PASS();
5165+
#else
5166+
SKIP_PLATFORM("the overflow-uid derivation is Linux-only");
5167+
#endif
5168+
}
5169+
51275170
/* #1830 real end-to-end smoke: inside a single-uid user namespace the
51285171
* root-owned ancestors (/, /tmp) are overflow-owned, and the daemon must still
51295172
* create its private directory there. An overflow-owned ancestor cannot be
51305173
* fabricated unprivileged, so this runs the real path only when a user
51315174
* namespace is actually available. O10 whitelisted-skip everywhere it is not:
51325175
* macOS has no user namespaces (compile-gated out); Docker's default seccomp
51335176
* blocks unshare(CLONE_NEWUSER) on the Colima container leg; some kernels ship
5134-
* user namespaces disabled. WHAT WAS TRIED when it skips: fork + unshare
5177+
* user namespaces disabled. NOT on that list any more: Ubuntu 23.10+ restricts
5178+
* unprivileged userns via AppArmor, which used to confine this test to the two
5179+
* ubuntu-22.04 legs -- broad-matrix only, so it ran in no PR and sat on
5180+
* GitHub's 2027-04-17 retirement clock. _test.yml now lifts that with
5181+
* kernel.apparmor_restrict_unprivileged_userns=0 on every ubuntu leg, so this
5182+
* runs on the CORE matrix. WHAT WAS TRIED when it skips: fork + unshare
51355183
* CLONE_NEWUSER + a 1:1 uid_map write, which the sandbox denied (EPERM). The
5136-
* deterministic decision coverage above is what binds the fix. */
5184+
* deterministic decision coverage above is what binds the fix.
5185+
*
5186+
* TO REPRODUCE IT LOCALLY two things are needed, and the second is easy to
5187+
* miss: run the container with --security-opt seccomp=unconfined so unshare is
5188+
* permitted, AND run the suite as a NON-ROOT uid. As root the mapped range
5189+
* covers uid 0, so root-owned /tmp stays 1:1 inside the namespace, never turns
5190+
* overflow, and this test passes without ever reaching the branch it exists to
5191+
* cover -- it passes just as happily with the fix reverted. Under `su tester`
5192+
* (uid 1001, the shape CI's runner user has) the ancestors do go overflow and
5193+
* the assertion becomes real. */
51375194
TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51385195
#if defined(__linux__)
51395196
uid_t host_uid = geteuid();
@@ -5164,9 +5221,20 @@ TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51645221
_exit(2);
51655222
}
51665223
/* Inside the ns / and /tmp now show the overflow uid. With #1830 the
5167-
* daemon can still build its private tree there; without it, refused. */
5168-
bool secured = cbm_daemon_ipc_private_directory_secure(probe);
5169-
_exit(secured ? 0 : 1);
5224+
* daemon can still build its private tree there; without it, refused.
5225+
*
5226+
* EXEC, do not just call. The overflow uid is derived once per process
5227+
* (pthread_once) from /proc/self/uid_map, and that state survives
5228+
* fork(): seven earlier call sites in this suite prime it with the HOST
5229+
* answer, so a forked child keeps "no overflow uid" and refuses no
5230+
* matter what its namespace says. That made this test fail on the only
5231+
* platform where it actually runs (ubuntu-22.04; macOS compile-gates it
5232+
* out, Colima's seccomp blocks unshare, and 23.10+ restricts
5233+
* unprivileged userns) -- while looking like a product regression.
5234+
* Re-exec so the decision is made by a process that STARTED here, which
5235+
* is also the only shape production ever takes. */
5236+
(void)execl("/proc/self/exe", "test-runner", "--userns-secure-probe", probe, (char *)NULL);
5237+
_exit(3); /* exec failed -- distinct from secure(0)/refused(1)/skip(2) */
51705238
}
51715239
if (child < 0) {
51725240
FAIL("fork failed for userns smoke");
@@ -5178,7 +5246,24 @@ TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51785246
if (WIFEXITED(status) && WEXITSTATUS(status) == 2) {
51795247
SKIP_PLATFORM("user namespaces unavailable (no CLONE_NEWUSER / seccomp-blocked)");
51805248
}
5181-
ASSERT_TRUE(WIFEXITED(status));
5249+
/* A failed re-exec must never read as a product refusal: 3 is its own code
5250+
* so a broken probe is a loud harness failure, not a quiet "refused". */
5251+
if (WIFEXITED(status) && WEXITSTATUS(status) == 3) {
5252+
FAIL("userns smoke could not re-exec /proc/self/exe for the probe");
5253+
}
5254+
/* {0,1,2,3} is the whole protocol. Any other code -- a sanitizer exit (LSan
5255+
* defaults to 23), an abort, a signal -- is a broken harness, not a refusal,
5256+
* and must say so with the code it actually saw. */
5257+
if (!WIFEXITED(status)) {
5258+
FAIL("userns probe did not exit normally (signalled) -- not a security verdict");
5259+
}
5260+
if (WEXITSTATUS(status) != 0 && WEXITSTATUS(status) != 1) {
5261+
char unexpected[128];
5262+
(void)snprintf(unexpected, sizeof(unexpected),
5263+
"userns probe exited %d -- not a security verdict",
5264+
WEXITSTATUS(status));
5265+
FAIL(unexpected);
5266+
}
51825267
ASSERT_EQ(0, WEXITSTATUS(status));
51835268
PASS();
51845269
#else
@@ -5309,6 +5394,7 @@ SUITE(daemon_ipc) {
53095394
#ifdef CBM_ENABLE_TEST_SEAMS
53105395
RUN_TEST(daemon_ipc_posix_uid_map_single_uid_parse_issue1830);
53115396
RUN_TEST(daemon_ipc_posix_overflow_ancestor_tolerated_only_in_single_uid_ns_issue1830);
5397+
RUN_TEST(daemon_ipc_posix_overflow_uid_is_never_cached_issue1830);
53125398
RUN_TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830);
53135399
#endif
53145400
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);

‎tests/test_main.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,26 @@ int main(int argc, char **argv) {
943943
(void)puts("codebase-memory-mcp test-runner");
944944
return 0;
945945
}
946+
/* #1830 userns smoke probe -- see the test that spawns it.
947+
*
948+
* The ancestor overflow uid is derived ONCE per process (pthread_once in
949+
* src/daemon/ipc.c) from /proc/self/uid_map. That file is not immutable:
950+
* unshare(CLONE_NEWUSER) is exactly what changes it, and pthread_once state
951+
* survives fork(). A forked child therefore keeps the HOST answer and never
952+
* re-derives inside its new namespace, so a fork-only smoke test refuses and
953+
* cannot pass once anything earlier in the suite has primed the cache --
954+
* seven call sites do. Re-exec into this probe so the decision is made by a
955+
* process that STARTED inside the namespace, which is the production shape
956+
* the test means to cover. */
957+
#if defined(__linux__) && defined(CBM_ENABLE_TEST_SEAMS)
958+
if (argc == 3 && strcmp(argv[1], "--userns-secure-probe") == 0) {
959+
/* _exit, not return: this process exists to answer ONE boolean. A
960+
* return runs the atexit chain, and the runner is built with
961+
* -fsanitize=address, so a future leak anywhere in the prologue would
962+
* exit 23 and read as a security verdict on a test that has none. */
963+
_exit(cbm_daemon_ipc_private_directory_secure(argv[2]) ? 0 : 1);
964+
}
965+
#endif
946966
int mcp_idxfailclosed_rc = tf_maybe_run_mcp_idxfailclosed_probe(argc, argv);
947967
if (mcp_idxfailclosed_rc >= 0) {
948968
return mcp_idxfailclosed_rc;

0 commit comments

Comments
 (0)