Skip to content

Commit d21e6cc

Browse files
committed
fix(daemon,ci): stop caching the overflow uid, and run the userns guard on the core matrix
Three maintainer decisions from the challenger read on the DeusData#1830 unit. 1. DELETE THE CACHE (not just fix its comment). posix_ancestor_overflow_uid() memoised via pthread_once behind a comment claiming "immutable /proc state". Both halves were false: unshare(CLONE_NEWUSER) rewrites /proc/self/uid_map, and pthread_once state survives fork() already marked done. A process that forked and then entered a namespace kept the parent verdict and refused a directory it should have accepted. It was harmless only because every caller today runs in a freshly exec'd process -- the daemon double-fork execs, workers go through cbm_subprocess_spawn's fork+exec, worker_pool.c never forks, and src/ contains no setns or unshare at all. That made a security decision depend on an invariant nothing enforced. The next fork-without-exec caller would have inherited a stale verdict silently. Now derived fresh per call: two small /proc reads against an openat + fstat + fchmod + ACL check per path component in the same walk. With a test that would have caught the original bug and needs no namespace. daemon_ipc_posix_overflow_uid_is_never_cached_issue1830 counts real derivations through the production entry point and asserts the count rises on every ancestor check. Re-introducing a cache fails it by name on every platform. Proven both ways on linux/arm64 as uid 1001: cache re-introduced 49 passed 1 failed; removed 50 passed 0 skipped. Worth knowing why that test is not redundant: with the re-exec probe in place, the userns smoke test PASSES even with the cache restored, because the probe is a fresh process and immune to it by construction. The two tests are complementary -- the smoke test binds the namespace wiring, this one binds the absence of the cache. 2. RUN THE GUARD ON THE CORE MATRIX. Ubuntu 23.10+ ships kernel.apparmor_restrict_unprivileged_userns=1, so the DeusData#1830 smoke test skipped on every 24.04 leg and its only real-namespace coverage sat on the two ubuntu-22.04 legs. Those are broad-matrix only, so the test ran in NO pull request, and GitHub retires those images on 2027-04-17 -- after which it would have become a permanent silent skip, and a skip is not a red. A deliberate relaxation of a security check was guarded by a test that ran nowhere a change would be reviewed, on a clock. _test.yml now lifts the restriction with one sysctl on every ubuntu leg (hosted runners have passwordless sudo), best-effort so a runner that refuses simply returns to skipping. The O10 skip-whitelist comment no longer presents the AppArmor restriction as immovable. 3. KEEP THE TOLERANCE, CORRECT THE SAFETY ARGUMENT. The comment claimed that in a single-uid map "nobody reachable can have created it or can mutate it". The overflow uid is what EVERY unmapped host uid maps to, not only host root (user_namespaces(7)), so overflow-owned does not prove root-created: on a shared host another local user's sticky directory is indistinguishable from root-owned /tmp from inside the namespace. The tolerance stays, because there is no in-namespace discriminator that could tell them apart -- and narrowing it would refuse the legitimate root-owned 01777 /tmp that DeusData#1830 exists to accept, i.e. it would revert the fix in practice. What changes is the claim: the real guarantee is that no principal reachable from inside the namespace can create or mutate such an ancestor, and the leaf is never tolerated as overflow (0700, euid-rechecked), so a hostile host-side ancestor owner is bounded to denial of service and socket-path control. macOS daemon_ipc 53 passed 2 skipped; linux/arm64 50 passed 0 skipped; lint-ci clean. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent f9038b8 commit d21e6cc

4 files changed

Lines changed: 114 additions & 21 deletions

File tree

‎.github/workflows/_test.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ 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+
110125
- name: Install ccache (macOS)
111126
if: startsWith(matrix.os, 'macos')
112127
run: command -v ccache >/dev/null 2>&1 || brew install ccache

‎src/daemon/ipc.c‎

Lines changed: 43 additions & 20 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,34 +1589,32 @@ 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.
1594+
/* The overflow uid tolerated for ancestors, or POSIX_NO_OVERFLOW_UID when none.
15761595
*
1577-
* Derived once per process. NOT from immutable state, whatever an earlier
1578-
* revision of this comment claimed: unshare(CLONE_NEWUSER) rewrites
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
15791599
* /proc/self/uid_map, and pthread_once state survives fork() already marked
1580-
* done. A child that forks WITHOUT exec and then changes namespace therefore
1581-
* keeps the parent answer. That is safe here only because every caller runs in
1582-
* a freshly exec'd process -- the daemon double-fork execs
1583-
* (daemon/bootstrap.c), workers go through cbm_subprocess_spawn's fork+exec,
1584-
* and src/ contains no setns/unshare at all. If you ever add a fork-without-exec
1585-
* path that reaches this check, this cache is WRONG and must go. */
1600+
* done -- so a process that forked and then changed namespace kept the parent
1601+
* answer and refused a directory it should have accepted. It happened to be
1602+
* harmless because every caller today runs in a freshly exec'd process, but
1603+
* that made a security decision depend on an invariant nothing enforced, and
1604+
* the next fork-without-exec caller would have silently inherited a stale
1605+
* verdict.
1606+
*
1607+
* The cost of not caching is two small /proc reads per ancestor check, against
1608+
* an openat + fstat + fchmod + ACL check per path component in the same walk.
1609+
* Do not re-introduce a cache here; the contract test counts derivations. */
15861610
static uid_t posix_ancestor_overflow_uid(void) {
15871611
#ifdef CBM_ENABLE_TEST_SEAMS
15881612
if (g_posix_overflow_override_active) {
15891613
return g_posix_overflow_override_uid;
15901614
}
15911615
#endif
15921616
#if defined(__linux__)
1593-
(void)pthread_once(&g_posix_overflow_once, posix_overflow_init_once);
1594-
return g_posix_overflow_cached;
1617+
return posix_compute_ancestor_overflow_uid();
15951618
#else
15961619
return POSIX_NO_OVERFLOW_UID;
15971620
#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: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5124,14 +5124,62 @@ 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
51365184
* deterministic decision coverage above is what binds the fix.
51375185
*
@@ -5346,6 +5394,7 @@ SUITE(daemon_ipc) {
53465394
#ifdef CBM_ENABLE_TEST_SEAMS
53475395
RUN_TEST(daemon_ipc_posix_uid_map_single_uid_parse_issue1830);
53485396
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);
53495398
RUN_TEST(daemon_ipc_posix_single_uid_userns_real_smoke_issue1830);
53505399
#endif
53515400
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);

0 commit comments

Comments
 (0)