Skip to content

Commit 469c3dd

Browse files
committed
fix(test): exec the DeusData#1830 userns probe instead of forking a primed process
The single-uid userns smoke test forked a child, unshared CLONE_NEWUSER, wrote a 1:1 uid_map, then called cbm_daemon_ipc_private_directory_secure() directly in that child. The call cannot give the right answer there. posix_ancestor_overflow_uid() in src/daemon/ipc.c derives the overflow uid once per process under pthread_once, and the comment above it calls the /proc state "immutable". unshare(CLONE_NEWUSER) is precisely what changes /proc/self/uid_map, and pthread_once state survives fork(). Seven earlier call sites in this suite run the derivation first, so by the time the test forks, the child has already inherited the HOST answer -- no overflow uid -- and refuses the directory no matter what its namespace says. The test therefore asserted a product behaviour it never executed, and failed as a product refusal (exit 1) while the product was correct: production daemons start inside their namespace and derive the right answer on first use. Re-exec /proc/self/exe into a new --userns-secure-probe mode so the decision is made by a process that STARTED in the namespace, which is also the only shape production ever takes. A failed exec gets its own exit code 3 and a named FAIL, so a broken harness can never be misread as a quiet refusal. Verified both directions on linux/arm64 in the container leg as uid 1001: reverted, 48 passed / 1 failed on "expected WEXITSTATUS(status) == 1"; with the fix, 49 passed, 0 skipped. The non-root half of that recipe is now written down at the test, because it is the part that fooled me first: as root the mapped range covers uid 0, so root-owned /tmp stays 1:1 inside the namespace and never turns overflow. The first run of this reproduction passed with the fix REVERTED for exactly that reason. Only a non-root uid reaches the branch the test exists to cover. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0b51555 commit 469c3dd

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎tests/test_daemon_ipc.c‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,7 +5133,16 @@ TEST(daemon_ipc_posix_overflow_ancestor_tolerated_only_in_single_uid_ns_issue183
51335133
* blocks unshare(CLONE_NEWUSER) on the Colima container leg; some kernels ship
51345134
* user namespaces disabled. WHAT WAS TRIED when it skips: fork + unshare
51355135
* CLONE_NEWUSER + a 1:1 uid_map write, which the sandbox denied (EPERM). The
5136-
* deterministic decision coverage above is what binds the fix. */
5136+
* deterministic decision coverage above is what binds the fix.
5137+
*
5138+
* TO REPRODUCE IT LOCALLY two things are needed, and the second is easy to
5139+
* miss: run the container with --security-opt seccomp=unconfined so unshare is
5140+
* permitted, AND run the suite as a NON-ROOT uid. As root the mapped range
5141+
* covers uid 0, so root-owned /tmp stays 1:1 inside the namespace, never turns
5142+
* overflow, and this test passes without ever reaching the branch it exists to
5143+
* cover -- it passes just as happily with the fix reverted. Under `su tester`
5144+
* (uid 1001, the shape CI's runner user has) the ancestors do go overflow and
5145+
* the assertion becomes real. */
51375146
TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51385147
#if defined(__linux__)
51395148
uid_t host_uid = geteuid();
@@ -5164,9 +5173,20 @@ TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51645173
_exit(2);
51655174
}
51665175
/* 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);
5176+
* daemon can still build its private tree there; without it, refused.
5177+
*
5178+
* EXEC, do not just call. The overflow uid is derived once per process
5179+
* (pthread_once) from /proc/self/uid_map, and that state survives
5180+
* fork(): seven earlier call sites in this suite prime it with the HOST
5181+
* answer, so a forked child keeps "no overflow uid" and refuses no
5182+
* matter what its namespace says. That made this test fail on the only
5183+
* platform where it actually runs (ubuntu-22.04; macOS compile-gates it
5184+
* out, Colima's seccomp blocks unshare, and 23.10+ restricts
5185+
* unprivileged userns) -- while looking like a product regression.
5186+
* Re-exec so the decision is made by a process that STARTED here, which
5187+
* is also the only shape production ever takes. */
5188+
(void)execl("/proc/self/exe", "test-runner", "--userns-secure-probe", probe, (char *)NULL);
5189+
_exit(3); /* exec failed -- distinct from secure(0)/refused(1)/skip(2) */
51705190
}
51715191
if (child < 0) {
51725192
FAIL("fork failed for userns smoke");
@@ -5178,6 +5198,11 @@ TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830) {
51785198
if (WIFEXITED(status) && WEXITSTATUS(status) == 2) {
51795199
SKIP_PLATFORM("user namespaces unavailable (no CLONE_NEWUSER / seccomp-blocked)");
51805200
}
5201+
/* A failed re-exec must never read as a product refusal: 3 is its own code
5202+
* so a broken probe is a loud harness failure, not a quiet "refused". */
5203+
if (WIFEXITED(status) && WEXITSTATUS(status) == 3) {
5204+
FAIL("userns smoke could not re-exec /proc/self/exe for the probe");
5205+
}
51815206
ASSERT_TRUE(WIFEXITED(status));
51825207
ASSERT_EQ(0, WEXITSTATUS(status));
51835208
PASS();

‎tests/test_main.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,22 @@ 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__)
958+
if (argc == 3 && strcmp(argv[1], "--userns-secure-probe") == 0) {
959+
return cbm_daemon_ipc_private_directory_secure(argv[2]) ? 0 : 1;
960+
}
961+
#endif
946962
int mcp_idxfailclosed_rc = tf_maybe_run_mcp_idxfailclosed_probe(argc, argv);
947963
if (mcp_idxfailclosed_rc >= 0) {
948964
return mcp_idxfailclosed_rc;

0 commit comments

Comments
 (0)