Skip to content

Commit 055fbb7

Browse files
authored
Merge pull request DeusData#2138 from DeusData/fix/decision-bd
fix(daemon): accept single-uid userns overflow-owned ancestors (DeusData#1830)
2 parents cdff08f + 8116672 commit 055fbb7

3 files changed

Lines changed: 338 additions & 16 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 200 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,191 @@ static char *private_log_directory_path_copy(const char *directory_path) {
13851385
return string_copy(directory_path);
13861386
}
13871387

1388-
static bool posix_directory_owner_trusted(uid_t owner) {
1389-
return owner == (uid_t)0 || owner == geteuid();
1388+
/* #1830 — single-uid user-namespace ancestors.
1389+
*
1390+
* Inside a 1:1 user namespace (for example `podman --userns=keep-id` or
1391+
* `unshare -U --map-current-user`), ancestors whose real owner is unmapped —
1392+
* the root-owned /, /tmp and the like — appear owned by the kernel overflow
1393+
* uid (`/proc/sys/kernel/overflowuid`, conventionally 65534). Refusing every
1394+
* overflow-owned ancestor made the daemon unusable in that legitimate setup.
1395+
*
1396+
* The overflow owner is tolerated for ANCESTORS ONLY, and ONLY when
1397+
* /proc/self/uid_map is a single-uid map whose sole inside id is our euid. In
1398+
* that shape no in-namespace principal can be the overflow owner, so an
1399+
* overflow-owned ancestor is exactly as safe as a root-owned one on the host:
1400+
* nobody reachable can have created it or can mutate it. A multi-uid map
1401+
* (rootless podman with subuids) still shows host root as overflow and is
1402+
* refused — conservative scope, not the safety argument. There is deliberately
1403+
* no environment escape hatch.
1404+
*
1405+
* The private directory itself is never tolerated as overflow: it is created by
1406+
* us (euid-owned) and re-checked `== geteuid()` by the created/final/snapshot
1407+
* gates, which stay strict. A host-squatted /tmp/cbm-daemon-<uid> appears
1408+
* overflow-owned and is refused there. */
1409+
#define POSIX_NO_OVERFLOW_UID ((uid_t) - 1)
1410+
1411+
#ifdef CBM_ENABLE_TEST_SEAMS
1412+
static bool g_posix_overflow_override_active;
1413+
static uid_t g_posix_overflow_override_uid = POSIX_NO_OVERFLOW_UID;
1414+
void cbm_daemon_ipc_posix_set_ancestor_overflow_uid_for_test(bool active,
1415+
unsigned long overflow_uid) {
1416+
g_posix_overflow_override_active = active;
1417+
g_posix_overflow_override_uid = active ? (uid_t)overflow_uid : POSIX_NO_OVERFLOW_UID;
1418+
}
1419+
#endif
1420+
1421+
#if defined(__linux__) || defined(CBM_ENABLE_TEST_SEAMS)
1422+
/* Parse a /proc/self/uid_map image. True iff it is exactly one mapping line
1423+
* "<inside> <outside> <count>" with count == 1 and inside == euid. Extra lines,
1424+
* a count other than 1, a different inside id, or malformed input all yield
1425+
* false (no tolerance). */
1426+
static bool posix_uid_map_is_single_uid(const char *uid_map, uid_t euid) {
1427+
if (!uid_map) {
1428+
return false;
1429+
}
1430+
unsigned long inside = 0;
1431+
unsigned long outside = 0;
1432+
unsigned long count = 0;
1433+
int consumed = 0;
1434+
if (sscanf(uid_map, " %lu %lu %lu %n", &inside, &outside, &count, &consumed) != 3) {
1435+
return false;
1436+
}
1437+
if (count != 1UL || (uid_t)inside != euid) {
1438+
return false;
1439+
}
1440+
(void)outside;
1441+
/* Reject a second mapping line: a single-uid map has exactly one. */
1442+
const char *rest = uid_map + consumed;
1443+
while (*rest == ' ' || *rest == '\t' || *rest == '\n' || *rest == '\r') {
1444+
rest++;
1445+
}
1446+
return *rest == '\0';
13901447
}
1448+
#endif
1449+
1450+
#if defined(__linux__)
1451+
static bool posix_read_small_proc_file(const char *path, char *buffer, size_t capacity) {
1452+
int fd = open(path, O_RDONLY | O_CLOEXEC);
1453+
if (fd < 0) {
1454+
return false;
1455+
}
1456+
size_t total = 0;
1457+
bool ok = true;
1458+
while (total + 1 < capacity) {
1459+
ssize_t got = read(fd, buffer + total, capacity - 1 - total);
1460+
if (got < 0) {
1461+
if (errno == EINTR) {
1462+
continue;
1463+
}
1464+
ok = false;
1465+
break;
1466+
}
1467+
if (got == 0) {
1468+
break;
1469+
}
1470+
total += (size_t)got;
1471+
}
1472+
(void)close(fd);
1473+
if (!ok) {
1474+
return false;
1475+
}
1476+
buffer[total] = '\0';
1477+
return true;
1478+
}
1479+
1480+
static uid_t posix_compute_ancestor_overflow_uid(void) {
1481+
char map[256];
1482+
if (!posix_read_small_proc_file("/proc/self/uid_map", map, sizeof(map)) ||
1483+
!posix_uid_map_is_single_uid(map, geteuid())) {
1484+
return POSIX_NO_OVERFLOW_UID;
1485+
}
1486+
char overflow_text[32];
1487+
/* Unreadable → no tolerance; never hardcode 65534. */
1488+
if (!posix_read_small_proc_file("/proc/sys/kernel/overflowuid", overflow_text,
1489+
sizeof(overflow_text))) {
1490+
return POSIX_NO_OVERFLOW_UID;
1491+
}
1492+
unsigned long overflow = 0;
1493+
if (sscanf(overflow_text, " %lu", &overflow) != 1) {
1494+
return POSIX_NO_OVERFLOW_UID;
1495+
}
1496+
return (uid_t)overflow;
1497+
}
1498+
1499+
static uid_t g_posix_overflow_cached = POSIX_NO_OVERFLOW_UID;
1500+
static pthread_once_t g_posix_overflow_once = PTHREAD_ONCE_INIT;
1501+
static void posix_overflow_init_once(void) {
1502+
g_posix_overflow_cached = posix_compute_ancestor_overflow_uid();
1503+
}
1504+
#endif /* __linux__ */
1505+
1506+
/* The overflow uid tolerated for ancestors in this process, or
1507+
* POSIX_NO_OVERFLOW_UID when none. Derived once from immutable /proc state. */
1508+
static uid_t posix_ancestor_overflow_uid(void) {
1509+
#ifdef CBM_ENABLE_TEST_SEAMS
1510+
if (g_posix_overflow_override_active) {
1511+
return g_posix_overflow_override_uid;
1512+
}
1513+
#endif
1514+
#if defined(__linux__)
1515+
(void)pthread_once(&g_posix_overflow_once, posix_overflow_init_once);
1516+
return g_posix_overflow_cached;
1517+
#else
1518+
return POSIX_NO_OVERFLOW_UID;
1519+
#endif
1520+
}
1521+
1522+
/* Ancestor owner acceptance. euid and root are always trusted; the namespace
1523+
* overflow uid is trusted only when overflow_uid != POSIX_NO_OVERFLOW_UID. */
1524+
static bool posix_ancestor_owner_ok(uid_t owner, uid_t euid, uid_t overflow_uid) {
1525+
return owner == (uid_t)0 || owner == euid ||
1526+
(overflow_uid != POSIX_NO_OVERFLOW_UID && owner == overflow_uid);
1527+
}
1528+
1529+
/* Pure ancestor accept/refuse over (owner, mode). Mirrors the owner + write-bit
1530+
* policy of posix_directory_parent_secure so the decision is unit-testable
1531+
* without a chown-able overflow-owned directory (unprivileged tests cannot
1532+
* create one). The ACL and fstat checks stay in the caller. */
1533+
static bool posix_ancestor_stat_ok(uid_t owner, mode_t mode, uid_t euid, uid_t overflow_uid) {
1534+
if (!posix_ancestor_owner_ok(owner, euid, overflow_uid)) {
1535+
return false;
1536+
}
1537+
bool owner_is_overflow = overflow_uid != POSIX_NO_OVERFLOW_UID && owner == overflow_uid &&
1538+
owner != euid && owner != (uid_t)0;
1539+
if ((mode & 0002) != 0) {
1540+
/* World-writable ancestor: only the root/overflow-owned sticky pattern
1541+
* (for example /tmp). A plain writable ancestor lets any local user swap
1542+
* a path component. */
1543+
return (owner == (uid_t)0 || owner_is_overflow) && (mode & S_ISVTX) != 0;
1544+
}
1545+
if ((mode & 0020) != 0 && owner_is_overflow) {
1546+
/* Group-writable is admitted with a warning for euid/root owners where
1547+
* the group is knowable (#1537); for an unmapped overflow owner the
1548+
* host-side group membership is unknown, so refuse. */
1549+
return false;
1550+
}
1551+
return true;
1552+
}
1553+
1554+
static bool posix_directory_ancestor_owner_trusted(uid_t owner) {
1555+
return posix_ancestor_owner_ok(owner, geteuid(), posix_ancestor_overflow_uid());
1556+
}
1557+
1558+
#ifdef CBM_ENABLE_TEST_SEAMS
1559+
bool cbm_daemon_ipc_posix_uid_map_is_single_uid_for_test(const char *uid_map, unsigned long euid) {
1560+
return posix_uid_map_is_single_uid(uid_map, (uid_t)euid);
1561+
}
1562+
bool cbm_daemon_ipc_posix_ancestor_stat_ok_for_test(unsigned long owner, unsigned int mode,
1563+
unsigned long euid, bool overflow_active,
1564+
unsigned long overflow_uid) {
1565+
return posix_ancestor_stat_ok((uid_t)owner, (mode_t)mode, (uid_t)euid,
1566+
overflow_active ? (uid_t)overflow_uid : POSIX_NO_OVERFLOW_UID);
1567+
}
1568+
#endif
13911569

13921570
static bool posix_directory_parent_secure(int directory_fd) {
13931571
struct stat status;
13941572
if (directory_fd < 0 || fstat(directory_fd, &status) != 0 || !S_ISDIR(status.st_mode) ||
1395-
!posix_directory_owner_trusted(status.st_uid) ||
13961573
!cbm_macos_extended_acl_fd_is_deny_only(directory_fd)) {
13971574
return false;
13981575
}
@@ -1403,14 +1580,18 @@ static bool posix_directory_parent_secure(int directory_fd) {
14031580
* with a shared primary group), and refusing it here made the daemon
14041581
* unusable with no way for the reader to see why.
14051582
*
1406-
* WORLD-writable is still refused: any local user could swap a path
1407-
* component. Group-writable is admitted for ancestors only — the private
1408-
* directory itself is chmod'd to 0700 and verified after this walk, so the
1409-
* thing that actually holds data stays owner-private either way. */
1410-
if ((status.st_mode & 0002) != 0) {
1411-
return status.st_uid == (uid_t)0 && (status.st_mode & S_ISVTX) != 0;
1412-
}
1413-
if ((status.st_mode & 0020) != 0) {
1583+
* WORLD-writable is still refused (apart from the root/overflow-owned sticky
1584+
* pattern such as /tmp): any local user could swap a path component.
1585+
* Group-writable is admitted for ancestors only — the private directory
1586+
* itself is chmod'd to 0700 and verified after this walk, so the thing that
1587+
* actually holds data stays owner-private either way. #1830 extends the
1588+
* accepted owner set to the single-uid user-namespace overflow uid; see
1589+
* posix_ancestor_stat_ok. */
1590+
if (!posix_ancestor_stat_ok(status.st_uid, status.st_mode, geteuid(),
1591+
posix_ancestor_overflow_uid())) {
1592+
return false;
1593+
}
1594+
if ((status.st_mode & 0020) != 0 && (status.st_mode & 0002) == 0) {
14141595
char mode_text[16];
14151596
(void)snprintf(mode_text, sizeof(mode_text), "%04o", (unsigned)(status.st_mode & 07777));
14161597
cbm_log_warn("daemon.private_dir_group_writable_ancestor", "mode", mode_text);
@@ -1420,17 +1601,20 @@ static bool posix_directory_parent_secure(int directory_fd) {
14201601

14211602
/* Validate a path transition only through the two already-open directory
14221603
* handles. A group/other-writable parent is unsafe unless it is the standard
1423-
* root-owned sticky-directory pattern (for example /tmp) and the selected
1424-
* child is itself root/current-user owned. Existing ancestors are observed,
1425-
* never chmod'd or ACL-rewritten. */
1604+
* root/overflow-owned sticky-directory pattern (for example /tmp) and both the
1605+
* parent and the selected child are trusted-owned (euid/root, or the single-uid
1606+
* user-namespace overflow uid for #1830). Existing ancestors are observed,
1607+
* never chmod'd or ACL-rewritten. The euid-only enforcement that stops a
1608+
* squatted private directory lives in the created/final/snapshot checks, not
1609+
* here — this only walks ancestors. */
14261610
static bool posix_directory_transition_secure(int parent_fd, int child_fd) {
14271611
struct stat parent;
14281612
struct stat child;
14291613
if (parent_fd < 0 || child_fd < 0 || !posix_directory_parent_secure(parent_fd) ||
14301614
fstat(parent_fd, &parent) != 0 || fstat(child_fd, &child) != 0 ||
14311615
!S_ISDIR(parent.st_mode) || !S_ISDIR(child.st_mode) ||
1432-
!posix_directory_owner_trusted(parent.st_uid) ||
1433-
!posix_directory_owner_trusted(child.st_uid)) {
1616+
!posix_directory_ancestor_owner_trusted(parent.st_uid) ||
1617+
!posix_directory_ancestor_owner_trusted(child.st_uid)) {
14341618
return false;
14351619
}
14361620
return true;

‎src/daemon/ipc.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ const char *cbm_daemon_ipc_validation_detail(void);
9494
#ifdef CBM_ENABLE_TEST_SEAMS
9595
/* #1537: seed the detail so a test can prove the CLI refusal surfaces it. */
9696
void cbm_daemon_ipc_set_validation_detail_for_testing(const char *detail);
97+
#ifndef _WIN32
98+
/* #1830 seams (POSIX). Override the single-uid user-namespace overflow uid that
99+
* ancestors may be owned by (active=false restores the real /proc-derived
100+
* value); and expose the pure uid_map-parse and ancestor accept/refuse decision
101+
* so they can be tested without a chown-able overflow-owned directory. */
102+
void cbm_daemon_ipc_posix_set_ancestor_overflow_uid_for_test(bool active,
103+
unsigned long overflow_uid);
104+
bool cbm_daemon_ipc_posix_uid_map_is_single_uid_for_test(const char *uid_map, unsigned long euid);
105+
bool cbm_daemon_ipc_posix_ancestor_stat_ok_for_test(unsigned long owner, unsigned int mode,
106+
unsigned long euid, bool overflow_active,
107+
unsigned long overflow_uid);
108+
#endif
97109
#endif
98110

99111
/* Create/validate an owner-only directory and securely open one regular

0 commit comments

Comments
 (0)