Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cli): bind a symlink's ownership judgement to the inode that is followed

TheStranjer3c854ae217c4
diff --git a/src/foundation/compat_fs.c b/src/foundation/compat_fs.c
index c28f2a542..6502bca75 100644
--- a/src/foundation/compat_fs.c
+++ b/src/foundation/compat_fs.c
@@ -864,11 +864,10 @@ FILE *cbm_fopen(const char *path, const char *mode) {
* Inspecting the link and following it are two steps, so what the follow
* lands on is checked as well: the opened target must be a directory owned by
* root or the invoking user, and not world-writable unless sticky. An account
- * that can swap the link between the two steps (it needs write access to the
- * parent, which the walk holds open) therefore cannot steer the walk into a
- * directory it controls or into one where anyone can pre-plant entries. This
- * is the before/after idiom the activation transaction uses around its own
- * opens. */
+ * that can write the parent cannot steer the walk into a directory it
+ * controls or into one where anyone can pre-plant entries, and because the
+ * judgement and the follow are bound to one inode (cbm_read_trusted_link) it
+ * cannot substitute a link of its own between them either. */
static bool cbm_walk_link_trusted(uid_t owner, bool follow_owned) {
return owner == 0U || (follow_owned && owner == geteuid());
}
@@ -880,18 +879,69 @@ static bool cbm_walk_target_trusted(const struct stat *target) {
return S_ISDIR(target->st_mode) && trusted_owner && (!world_writable || sticky);
}
+/* Read the target text of the symlink at `component`, but only if the link's
+ * owner is trusted -- and read it from the SAME inode the judgement was made
+ * on. Judging by name and then opening by name is a check-then-use pair: an
+ * account that can write the parent could swap the entry in between, so the
+ * link that gets followed is never the one that was judged (demonstrated
+ * against an earlier head with an LD_PRELOAD shim). The link is therefore
+ * never opened by name after the judgement: on Linux an O_PATH|O_NOFOLLOW
+ * descriptor pins the inode, and both the fstat and the readlinkat operate on
+ * it; elsewhere the link is stat'ed by name before and after the readlinkat
+ * and both must be the same inode with the same owner. What is followed
+ * afterwards is the text this function returns, resolved from the parent. */
+static bool cbm_read_trusted_link(int parent, const char *component, bool follow_owned, char *text,
+ size_t text_size) {
+ ssize_t length = 0; /* nothing read yet: refused below unless a read succeeds */
+#if defined(__linux__) && defined(O_PATH)
+ int link = openat(parent, component, O_PATH | O_NOFOLLOW | O_CLOEXEC);
+ if (link < 0) {
+ return false;
+ }
+ struct stat state;
+ if (fstat(link, &state) == 0 && S_ISLNK(state.st_mode) &&
+ cbm_walk_link_trusted(state.st_uid, follow_owned)) {
+ /* An empty path names the link the descriptor itself refers to. */
+ length = readlinkat(link, "", text, text_size);
+ }
+ (void)close(link);
+#else
+ struct stat before;
+ struct stat after;
+ if (fstatat(parent, component, &before, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(before.st_mode) ||
+ !cbm_walk_link_trusted(before.st_uid, follow_owned)) {
+ return false;
+ }
+ length = readlinkat(parent, component, text, text_size);
+ if (fstatat(parent, component, &after, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(after.st_mode) ||
+ after.st_dev != before.st_dev || after.st_ino != before.st_ino ||
+ after.st_uid != before.st_uid) {
+ return false;
+ }
+#endif
+ /* Empty or truncated text is refused rather than guessed at. */
+ if (length <= 0 || (size_t)length >= text_size) {
+ return false;
+ }
+ text[length] = '\0';
+ return true;
+}
+
static int cbm_open_directory_component(int parent, const char *component, int flags,
bool follow_owned) {
int descriptor = openat(parent, component, flags);
#if defined(O_NOFOLLOW) && defined(AT_SYMLINK_NOFOLLOW)
if (descriptor < 0) {
/* The caller decides on errno from the FIRST open (ENOENT means
- * "create it"); a refused link must not leak fstatat's errno instead. */
+ * "create it"); a refused link must not leak a later call's errno. */
int open_errno = errno;
- struct stat state;
- if (fstatat(parent, component, &state, AT_SYMLINK_NOFOLLOW) == 0 &&
- S_ISLNK(state.st_mode) && cbm_walk_link_trusted(state.st_uid, follow_owned)) {
- int followed = openat(parent, component, flags & ~O_NOFOLLOW);
+ char text[CBM_SZ_4K];
+ if (cbm_read_trusted_link(parent, component, follow_owned, text, sizeof(text))) {
+ /* The judged link's own text, resolved from the parent exactly as
+ * the kernel would resolve it (relative texts against the link's
+ * directory). Links inside the text are resolved by the kernel as
+ * before; the target check bounds where the walk lands. */
+ int followed = openat(parent, text, flags & ~O_NOFOLLOW);
struct stat target;
if (followed >= 0 && fstat(followed, &target) == 0 &&
cbm_walk_target_trusted(&target)) {
diff --git a/tests/test_platform.c b/tests/test_platform.c
index 849ad083b..5995a4b01 100644
--- a/tests/test_platform.c
+++ b/tests/test_platform.c
@@ -178,6 +178,83 @@ TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in) {
#endif
}
+/* A followed link is resolved from its own text, so the text has to be
+ * resolved the way the kernel resolves it: a relative text against the link's
+ * directory (never the process's working directory), an absolute text as is,
+ * and links inside the text followed in turn. The test changes into an
+ * unrelated directory first so a CWD-relative resolution would miss. */
+TEST(platform_mkdir_p_resolves_link_text_from_the_link_directory) {
+#ifdef _WIN32
+ SKIP_PLATFORM("POSIX symlink ownership contract");
+#else
+ char base[CBM_SZ_512];
+ int written = snprintf(base, sizeof(base), "/tmp/cbm-linktext-XXXXXX");
+ ASSERT_TRUE(written > 0 && written < (int)sizeof(base));
+ ASSERT_NOT_NULL(cbm_mkdtemp(base));
+ char elsewhere[CBM_SZ_1K];
+ char target[CBM_SZ_1K];
+ char nested[CBM_SZ_1K];
+ (void)snprintf(elsewhere, sizeof(elsewhere), "%s/elsewhere", base);
+ (void)snprintf(target, sizeof(target), "%s/dir/target", base);
+ (void)snprintf(nested, sizeof(nested), "%s/dir/nested", base);
+ ASSERT_TRUE(cbm_mkdir_p(elsewhere, 0700));
+ ASSERT_TRUE(cbm_mkdir_p(target, 0700));
+ ASSERT_TRUE(cbm_mkdir_p(nested, 0700));
+
+ char saved_cwd[CBM_SZ_1K];
+ ASSERT_NOT_NULL(getcwd(saved_cwd, sizeof(saved_cwd)));
+ ASSERT_EQ(chdir(elsewhere), 0);
+
+ /* relative text, sibling: dir/relative -> target */
+ char link[CBM_SZ_1K];
+ char through[CBM_SZ_1K];
+ char created[CBM_SZ_1K];
+ (void)snprintf(link, sizeof(link), "%s/dir/relative", base);
+ ASSERT_EQ(symlink("target", link), 0);
+ (void)snprintf(through, sizeof(through), "%s/child-relative", link);
+ (void)snprintf(created, sizeof(created), "%s/child-relative", target);
+ ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
+ ASSERT_TRUE(cbm_is_dir(created));
+ ASSERT_EQ(cbm_rmdir(created), 0);
+ ASSERT_EQ(cbm_unlink(link), 0);
+
+ /* relative text through the parent: dir/nested/up -> ../target */
+ (void)snprintf(link, sizeof(link), "%s/up", nested);
+ ASSERT_EQ(symlink("../target", link), 0);
+ (void)snprintf(through, sizeof(through), "%s/child-up", link);
+ (void)snprintf(created, sizeof(created), "%s/child-up", target);
+ ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
+ ASSERT_TRUE(cbm_is_dir(created));
+ ASSERT_EQ(cbm_rmdir(created), 0);
+ ASSERT_EQ(cbm_unlink(link), 0);
+
+ /* absolute text, and a chain whose text names another link */
+ (void)snprintf(link, sizeof(link), "%s/absolute", base);
+ ASSERT_EQ(symlink(target, link), 0);
+ char chain[CBM_SZ_1K];
+ (void)snprintf(chain, sizeof(chain), "%s/chain", base);
+ ASSERT_EQ(symlink("absolute", chain), 0);
+ (void)snprintf(through, sizeof(through), "%s/child-chain", chain);
+ (void)snprintf(created, sizeof(created), "%s/child-chain", target);
+ ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
+ ASSERT_TRUE(cbm_is_dir(created));
+ ASSERT_EQ(cbm_rmdir(created), 0);
+ ASSERT_EQ(cbm_unlink(chain), 0);
+ ASSERT_EQ(cbm_unlink(link), 0);
+
+ ASSERT_EQ(chdir(saved_cwd), 0);
+ ASSERT_EQ(cbm_rmdir(target), 0);
+ ASSERT_EQ(cbm_rmdir(nested), 0);
+ char *slash = strrchr(target, '/');
+ ASSERT_NOT_NULL(slash);
+ *slash = '\0';
+ ASSERT_EQ(cbm_rmdir(target), 0); /* dir */
+ ASSERT_EQ(cbm_rmdir(elsewhere), 0);
+ ASSERT_EQ(cbm_rmdir(base), 0);
+ PASS();
+#endif
+}
+
/* The trust an opted-in walk extends to a symlink is bounded on BOTH ends of
* the link. What the follow lands on must be a directory owned by root or the
* invoking user and not world-writable unless sticky; the mode legs bind on
@@ -1056,6 +1133,7 @@ SUITE(platform) {
RUN_TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in);
RUN_TEST(platform_mkdir_p_symlink_trust_is_bounded_by_owner_and_mode);
RUN_TEST(platform_mkdir_p_follow_owned_is_per_call_site);
+ RUN_TEST(platform_mkdir_p_resolves_link_text_from_the_link_directory);
RUN_TEST(platform_mkstemp_and_mkdtemp_survive_non_ascii_directory);
RUN_TEST(platform_mkdtemp_is_thread_safe);
RUN_TEST(platform_counter_scaling_avoids_intermediate_overflow);