Skip to content

Commit 3c854ae

Browse files
committed
fix(cli): bind a symlink's ownership judgement to the inode that is followed
Review of the previous head reproduced a check-then-use window in the parent-chain walk: the link was judged by name (fstatat) and then opened by name (openat), with nothing tying the two to the same entry. An LD_PRELOAD shim that swaps the link for one pointing elsewhere the moment the judgement returns made both earlier heads follow the swapped link, including one owned by a foreign uid under root; the post-follow target check only bounds where such a swap can land. The link is now never opened by name after it has been judged. On Linux an O_PATH|O_NOFOLLOW descriptor pins the inode, fstat on that descriptor makes the ownership decision and readlinkat(fd, "") reads the target text from the same inode; on other systems the link is stat'ed by name before and after the readlinkat and both must be the same inode with the same owner. The walk then opens the text from the parent directory, so a relative text resolves against the link's own directory exactly as the kernel resolves it, and the target check runs on what that opens. The readlinkat form is used rather than /proc/self/fd so the walk does not depend on /proc being mounted. What is still done by path is resolving the text itself, which the target check bounds; the comment says so. Test: relative, ../, absolute and chained link texts resolve from the link's directory with the process CWD moved elsewhere. The race itself is exercised out of tree with the harness from the review thread. Refs DeusData#1722 Signed-off-by: TheStranjer <791672+TheStranjer@users.noreply.github.com>
1 parent 8622d6a commit 3c854ae

2 files changed

Lines changed: 138 additions & 10 deletions

File tree

‎src/foundation/compat_fs.c‎

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,10 @@ FILE *cbm_fopen(const char *path, const char *mode) {
864864
* Inspecting the link and following it are two steps, so what the follow
865865
* lands on is checked as well: the opened target must be a directory owned by
866866
* root or the invoking user, and not world-writable unless sticky. An account
867-
* that can swap the link between the two steps (it needs write access to the
868-
* parent, which the walk holds open) therefore cannot steer the walk into a
869-
* directory it controls or into one where anyone can pre-plant entries. This
870-
* is the before/after idiom the activation transaction uses around its own
871-
* opens. */
867+
* that can write the parent cannot steer the walk into a directory it
868+
* controls or into one where anyone can pre-plant entries, and because the
869+
* judgement and the follow are bound to one inode (cbm_read_trusted_link) it
870+
* cannot substitute a link of its own between them either. */
872871
static bool cbm_walk_link_trusted(uid_t owner, bool follow_owned) {
873872
return owner == 0U || (follow_owned && owner == geteuid());
874873
}
@@ -880,18 +879,69 @@ static bool cbm_walk_target_trusted(const struct stat *target) {
880879
return S_ISDIR(target->st_mode) && trusted_owner && (!world_writable || sticky);
881880
}
882881

882+
/* Read the target text of the symlink at `component`, but only if the link's
883+
* owner is trusted -- and read it from the SAME inode the judgement was made
884+
* on. Judging by name and then opening by name is a check-then-use pair: an
885+
* account that can write the parent could swap the entry in between, so the
886+
* link that gets followed is never the one that was judged (demonstrated
887+
* against an earlier head with an LD_PRELOAD shim). The link is therefore
888+
* never opened by name after the judgement: on Linux an O_PATH|O_NOFOLLOW
889+
* descriptor pins the inode, and both the fstat and the readlinkat operate on
890+
* it; elsewhere the link is stat'ed by name before and after the readlinkat
891+
* and both must be the same inode with the same owner. What is followed
892+
* afterwards is the text this function returns, resolved from the parent. */
893+
static bool cbm_read_trusted_link(int parent, const char *component, bool follow_owned, char *text,
894+
size_t text_size) {
895+
ssize_t length = 0; /* nothing read yet: refused below unless a read succeeds */
896+
#if defined(__linux__) && defined(O_PATH)
897+
int link = openat(parent, component, O_PATH | O_NOFOLLOW | O_CLOEXEC);
898+
if (link < 0) {
899+
return false;
900+
}
901+
struct stat state;
902+
if (fstat(link, &state) == 0 && S_ISLNK(state.st_mode) &&
903+
cbm_walk_link_trusted(state.st_uid, follow_owned)) {
904+
/* An empty path names the link the descriptor itself refers to. */
905+
length = readlinkat(link, "", text, text_size);
906+
}
907+
(void)close(link);
908+
#else
909+
struct stat before;
910+
struct stat after;
911+
if (fstatat(parent, component, &before, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(before.st_mode) ||
912+
!cbm_walk_link_trusted(before.st_uid, follow_owned)) {
913+
return false;
914+
}
915+
length = readlinkat(parent, component, text, text_size);
916+
if (fstatat(parent, component, &after, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(after.st_mode) ||
917+
after.st_dev != before.st_dev || after.st_ino != before.st_ino ||
918+
after.st_uid != before.st_uid) {
919+
return false;
920+
}
921+
#endif
922+
/* Empty or truncated text is refused rather than guessed at. */
923+
if (length <= 0 || (size_t)length >= text_size) {
924+
return false;
925+
}
926+
text[length] = '\0';
927+
return true;
928+
}
929+
883930
static int cbm_open_directory_component(int parent, const char *component, int flags,
884931
bool follow_owned) {
885932
int descriptor = openat(parent, component, flags);
886933
#if defined(O_NOFOLLOW) && defined(AT_SYMLINK_NOFOLLOW)
887934
if (descriptor < 0) {
888935
/* The caller decides on errno from the FIRST open (ENOENT means
889-
* "create it"); a refused link must not leak fstatat's errno instead. */
936+
* "create it"); a refused link must not leak a later call's errno. */
890937
int open_errno = errno;
891-
struct stat state;
892-
if (fstatat(parent, component, &state, AT_SYMLINK_NOFOLLOW) == 0 &&
893-
S_ISLNK(state.st_mode) && cbm_walk_link_trusted(state.st_uid, follow_owned)) {
894-
int followed = openat(parent, component, flags & ~O_NOFOLLOW);
938+
char text[CBM_SZ_4K];
939+
if (cbm_read_trusted_link(parent, component, follow_owned, text, sizeof(text))) {
940+
/* The judged link's own text, resolved from the parent exactly as
941+
* the kernel would resolve it (relative texts against the link's
942+
* directory). Links inside the text are resolved by the kernel as
943+
* before; the target check bounds where the walk lands. */
944+
int followed = openat(parent, text, flags & ~O_NOFOLLOW);
895945
struct stat target;
896946
if (followed >= 0 && fstat(followed, &target) == 0 &&
897947
cbm_walk_target_trusted(&target)) {

‎tests/test_platform.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,83 @@ TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in) {
178178
#endif
179179
}
180180

181+
/* A followed link is resolved from its own text, so the text has to be
182+
* resolved the way the kernel resolves it: a relative text against the link's
183+
* directory (never the process's working directory), an absolute text as is,
184+
* and links inside the text followed in turn. The test changes into an
185+
* unrelated directory first so a CWD-relative resolution would miss. */
186+
TEST(platform_mkdir_p_resolves_link_text_from_the_link_directory) {
187+
#ifdef _WIN32
188+
SKIP_PLATFORM("POSIX symlink ownership contract");
189+
#else
190+
char base[CBM_SZ_512];
191+
int written = snprintf(base, sizeof(base), "/tmp/cbm-linktext-XXXXXX");
192+
ASSERT_TRUE(written > 0 && written < (int)sizeof(base));
193+
ASSERT_NOT_NULL(cbm_mkdtemp(base));
194+
char elsewhere[CBM_SZ_1K];
195+
char target[CBM_SZ_1K];
196+
char nested[CBM_SZ_1K];
197+
(void)snprintf(elsewhere, sizeof(elsewhere), "%s/elsewhere", base);
198+
(void)snprintf(target, sizeof(target), "%s/dir/target", base);
199+
(void)snprintf(nested, sizeof(nested), "%s/dir/nested", base);
200+
ASSERT_TRUE(cbm_mkdir_p(elsewhere, 0700));
201+
ASSERT_TRUE(cbm_mkdir_p(target, 0700));
202+
ASSERT_TRUE(cbm_mkdir_p(nested, 0700));
203+
204+
char saved_cwd[CBM_SZ_1K];
205+
ASSERT_NOT_NULL(getcwd(saved_cwd, sizeof(saved_cwd)));
206+
ASSERT_EQ(chdir(elsewhere), 0);
207+
208+
/* relative text, sibling: dir/relative -> target */
209+
char link[CBM_SZ_1K];
210+
char through[CBM_SZ_1K];
211+
char created[CBM_SZ_1K];
212+
(void)snprintf(link, sizeof(link), "%s/dir/relative", base);
213+
ASSERT_EQ(symlink("target", link), 0);
214+
(void)snprintf(through, sizeof(through), "%s/child-relative", link);
215+
(void)snprintf(created, sizeof(created), "%s/child-relative", target);
216+
ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
217+
ASSERT_TRUE(cbm_is_dir(created));
218+
ASSERT_EQ(cbm_rmdir(created), 0);
219+
ASSERT_EQ(cbm_unlink(link), 0);
220+
221+
/* relative text through the parent: dir/nested/up -> ../target */
222+
(void)snprintf(link, sizeof(link), "%s/up", nested);
223+
ASSERT_EQ(symlink("../target", link), 0);
224+
(void)snprintf(through, sizeof(through), "%s/child-up", link);
225+
(void)snprintf(created, sizeof(created), "%s/child-up", target);
226+
ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
227+
ASSERT_TRUE(cbm_is_dir(created));
228+
ASSERT_EQ(cbm_rmdir(created), 0);
229+
ASSERT_EQ(cbm_unlink(link), 0);
230+
231+
/* absolute text, and a chain whose text names another link */
232+
(void)snprintf(link, sizeof(link), "%s/absolute", base);
233+
ASSERT_EQ(symlink(target, link), 0);
234+
char chain[CBM_SZ_1K];
235+
(void)snprintf(chain, sizeof(chain), "%s/chain", base);
236+
ASSERT_EQ(symlink("absolute", chain), 0);
237+
(void)snprintf(through, sizeof(through), "%s/child-chain", chain);
238+
(void)snprintf(created, sizeof(created), "%s/child-chain", target);
239+
ASSERT_TRUE(cbm_mkdir_p_ex(through, 0700, CBM_MKDIR_FOLLOW_OWNED));
240+
ASSERT_TRUE(cbm_is_dir(created));
241+
ASSERT_EQ(cbm_rmdir(created), 0);
242+
ASSERT_EQ(cbm_unlink(chain), 0);
243+
ASSERT_EQ(cbm_unlink(link), 0);
244+
245+
ASSERT_EQ(chdir(saved_cwd), 0);
246+
ASSERT_EQ(cbm_rmdir(target), 0);
247+
ASSERT_EQ(cbm_rmdir(nested), 0);
248+
char *slash = strrchr(target, '/');
249+
ASSERT_NOT_NULL(slash);
250+
*slash = '\0';
251+
ASSERT_EQ(cbm_rmdir(target), 0); /* dir */
252+
ASSERT_EQ(cbm_rmdir(elsewhere), 0);
253+
ASSERT_EQ(cbm_rmdir(base), 0);
254+
PASS();
255+
#endif
256+
}
257+
181258
/* The trust an opted-in walk extends to a symlink is bounded on BOTH ends of
182259
* the link. What the follow lands on must be a directory owned by root or the
183260
* invoking user and not world-writable unless sticky; the mode legs bind on
@@ -1056,6 +1133,7 @@ SUITE(platform) {
10561133
RUN_TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in);
10571134
RUN_TEST(platform_mkdir_p_symlink_trust_is_bounded_by_owner_and_mode);
10581135
RUN_TEST(platform_mkdir_p_follow_owned_is_per_call_site);
1136+
RUN_TEST(platform_mkdir_p_resolves_link_text_from_the_link_directory);
10591137
RUN_TEST(platform_mkstemp_and_mkdtemp_survive_non_ascii_directory);
10601138
RUN_TEST(platform_mkdtemp_is_thread_safe);
10611139
RUN_TEST(platform_counter_scaling_avoids_intermediate_overflow);

0 commit comments

Comments
 (0)