@@ -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. */
51375194TEST (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 );
0 commit comments