Skip to content

Commit cc6743e

Browse files
authored
Merge pull request DeusData#2060 from TheStranjer/fix/follow-own-symlinked-agent-roots
fix(cli): follow agent-root symlinks owned by the invoking user
2 parents 48f2698 + 3c854ae commit cc6743e

15 files changed

Lines changed: 709 additions & 67 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ static bool cli_activation_production_context_init(cli_activation_production_con
647647
cbm_canonical_path(requested_cache, context->canonical_cache,
648648
sizeof(context->canonical_cache));
649649
if (!cache_ready && requested_cache && requested_cache[0] &&
650-
cbm_mkdir_p(requested_cache, 0700)) {
650+
cbm_mkdir_p_ex(requested_cache, 0700, CBM_MKDIR_FOLLOW_OWNED)) {
651651
cache_ready = cbm_canonical_path(requested_cache, context->canonical_cache,
652652
sizeof(context->canonical_cache));
653653
}
@@ -1418,8 +1418,12 @@ const char *cbm_get_codex_instructions(void) {
14181418

14191419
/* ── Recursive mkdir (via compat_fs) ──────────────────────────── */
14201420

1421+
/* Every caller here creates a directory under the user's own configuration
1422+
* (skills, instruction files, the cache), so a symlink the user owns on the
1423+
* way there is the user's own arrangement and is followed. Repository-derived
1424+
* paths never come through this wrapper. */
14211425
static int mkdirp(const char *path, int mode) {
1422-
return (int)cbm_mkdir_p(path, mode) ? 0 : CLI_ERR;
1426+
return (int)cbm_mkdir_p_ex(path, mode, CBM_MKDIR_FOLLOW_OWNED) ? 0 : CLI_ERR;
14231427
}
14241428

14251429
/* Legacy migration may remove an empty directory, but never recursively
@@ -5430,7 +5434,7 @@ bool cbm_install_hook_gate_script(const char *home, const char *binary_path) {
54305434
if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) {
54315435
return false;
54325436
}
5433-
if (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM)) {
5437+
if (!cbm_mkdir_p_ex(hooks_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED)) {
54345438
return false;
54355439
}
54365440

@@ -5483,7 +5487,7 @@ static bool cbm_install_session_reminder_script(const char *home, const char *bi
54835487
if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) {
54845488
return false;
54855489
}
5486-
if (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM)) {
5490+
if (!cbm_mkdir_p_ex(hooks_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED)) {
54875491
return false;
54885492
}
54895493

@@ -5718,7 +5722,7 @@ static bool cbm_install_subagent_reminder_script(const char *home, const char *b
57185722
if (hooks_written <= 0 || (size_t)hooks_written >= sizeof(hooks_dir)) {
57195723
return false;
57205724
}
5721-
if (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM)) {
5725+
if (!cbm_mkdir_p_ex(hooks_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED)) {
57225726
return false;
57235727
}
57245728

@@ -7936,7 +7940,9 @@ static bool prepare_config_parent(const char *path) {
79367940
return slash != NULL;
79377941
}
79387942
*slash = '\0';
7939-
return cbm_mkdir_p(parent, CLI_OCTAL_PERM);
7943+
/* Agent roots live under HOME / XDG / the client's own config-dir
7944+
* variable, so a symlink the user owns on the way is followed (#1722). */
7945+
return cbm_mkdir_p_ex(parent, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED);
79407946
}
79417947

79427948
typedef struct {
@@ -8494,7 +8500,7 @@ static void install_copilot_durable_context(const char *home, const char *binary
84948500
return;
84958501
}
84968502
bool hook_ok = true;
8497-
if (!dry_run && (!cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM) ||
8503+
if (!dry_run && (!cbm_mkdir_p_ex(hooks_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED) ||
84988504
cbm_upsert_copilot_hooks(binary_path, hook_path) != CLI_OK)) {
84998505
hook_ok = false;
85008506
record_agent_config_error(false, "Copilot", "lifecycle_hook_install", hook_path);
@@ -9248,7 +9254,7 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
92489254
if (!dry_run && !g_install_plan) {
92499255
char cfg_dir[CLI_BUF_1K];
92509256
snprintf(cfg_dir, sizeof(cfg_dir), "%s/.gemini/config", home);
9251-
cbm_mkdir_p(cfg_dir, CLI_OCTAL_PERM);
9257+
cbm_mkdir_p_ex(cfg_dir, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED);
92529258
}
92539259
install_generic_agent_config("Antigravity", binary_path, cp, ip, dry_run,
92549260
cbm_upsert_antigravity_mcp);
@@ -9557,7 +9563,7 @@ static void install_editor_agent_configs(const cbm_detected_agents_t *agents, co
95579563
snprintf(skills_dir, sizeof(skills_dir), "%s/.junie/skills", home);
95589564
snprintf(agent_path, sizeof(agent_path), "%s/.junie/agents/codebase-memory.md", home);
95599565
if (!dry_run && !g_install_plan) {
9560-
cbm_mkdir_p(sd, CLI_OCTAL_PERM);
9566+
cbm_mkdir_p_ex(sd, CLI_OCTAL_PERM, CBM_MKDIR_FOLLOW_OWNED);
95619567
}
95629568
bool direct_profiles_ready = install_generic_agent_config("Junie", binary_path, cp, NULL,
95639569
dry_run, cbm_upsert_junie_mcp);

‎src/cli/config_json_like.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,12 +1498,17 @@ static char *jl_parent_directory(const char *path) {
14981498
return cbm_strndup(path, (size_t)(separator - path));
14991499
}
15001500

1501+
/* Every document written through this editor is a client's own configuration
1502+
* file under HOME / XDG / the client's config-dir variable, so a symlink the
1503+
* user owns on the way to it is followed. Repository-derived paths are never
1504+
* edited here. */
15011505
static int jl_ensure_parent(const char *path) {
15021506
char *parent = jl_parent_directory(path);
15031507
if (!parent) {
15041508
return -1;
15051509
}
1506-
int result = strcmp(parent, ".") == 0 || cbm_mkdir_p(parent, 0755) ? 0 : -1;
1510+
int result =
1511+
strcmp(parent, ".") == 0 || cbm_mkdir_p_ex(parent, 0755, CBM_MKDIR_FOLLOW_OWNED) ? 0 : -1;
15071512
free(parent);
15081513
return result;
15091514
}

‎src/cli/hook_augment.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static void ha_open_crumb_log(int deadline_ms) {
115115
}
116116
char dir[CBM_SZ_1K];
117117
snprintf(dir, sizeof(dir), "%s/.cache/codebase-memory-mcp/logs", home);
118-
cbm_mkdir_p(dir, 0755);
118+
cbm_mkdir_p_ex(dir, 0755, CBM_MKDIR_FOLLOW_OWNED);
119119
snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir);
120120
}
121121
g_ha_crumb_fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);

‎src/daemon/application.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,8 @@ static bool application_unique_recovery_file(char out[APPLICATION_PATH_CAP], con
639639
int written;
640640
if (application_cache_dir(cache)) {
641641
written = snprintf(directory, sizeof(directory), "%s/logs", cache);
642-
if (written <= 0 || written >= (int)sizeof(directory) || !cbm_mkdir_p(directory, 0700)) {
642+
if (written <= 0 || written >= (int)sizeof(directory) ||
643+
!cbm_mkdir_p_ex(directory, 0700, CBM_MKDIR_FOLLOW_OWNED)) {
643644
return false;
644645
}
645646
} else {

‎src/foundation/compat_fs.c‎

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,13 @@ bool cbm_mkdir_p(const char *path, int mode) {
561561
return ok;
562562
}
563563

564+
bool cbm_mkdir_p_ex(const char *path, int mode, unsigned int policy) {
565+
/* The Windows walk has its own reparse-point policy (see
566+
* cbm_windows_mkdir_component); the POSIX symlink policy does not apply. */
567+
(void)policy;
568+
return cbm_mkdir_p(path, mode);
569+
}
570+
564571
int cbm_unlink(const char *path) {
565572
wchar_t *wpath = cbm_path_to_wide(path);
566573
if (!wpath) {
@@ -841,24 +848,131 @@ FILE *cbm_fopen(const char *path, const char *mode) {
841848
return fopen(path, mode);
842849
}
843850

844-
static int cbm_open_directory_component(int parent, const char *component, int flags) {
851+
/* Symlink policy for the parent-chain walk. Every component is opened with
852+
* O_NOFOLLOW; a symlink is followed only when its OWNER is trusted, never by
853+
* default. Root-owned links are trusted everywhere (distro /home indirection,
854+
* macOS /tmp: only root can create those, so they are outside the attacker
855+
* model). A link owned by the invoking account is trusted only where the
856+
* caller opted in with CBM_MKDIR_FOLLOW_OWNED: for a path rooted in the user's
857+
* own configuration such a link is the user's own arrangement (a dotfile
858+
* manager, ~/.config/opencode -> /mnt/...), and refusing it made every
859+
* agent-config write under such a root fail with an opaque agent_config
860+
* error. It is not trusted for a path derived from a repository, where git
861+
* creates symlinks owned by whoever cloned, so "user-owned" says nothing about
862+
* "user-intended". The rule is the one the Linux kernel's
863+
* fs.protected_symlinks applies, and the ancestor policy the activation
864+
* transaction already uses. A link owned by any OTHER account (planted in a
865+
* group- or world-writable ancestor) stays refused, and a privileged walk
866+
* (euid 0) still refuses user-owned links.
867+
*
868+
* Inspecting the link and following it are two steps, so what the follow
869+
* lands on is checked as well: the opened target must be a directory owned by
870+
* root or the invoking user, and not world-writable unless sticky. An account
871+
* that can write the parent cannot steer the walk into a directory it
872+
* controls or into one where anyone can pre-plant entries, and because the
873+
* judgement and the follow are bound to one inode (cbm_read_trusted_link) it
874+
* cannot substitute a link of its own between them either. */
875+
static bool cbm_walk_link_trusted(uid_t owner, bool follow_owned) {
876+
return owner == 0U || (follow_owned && owner == geteuid());
877+
}
878+
879+
static bool cbm_walk_target_trusted(const struct stat *target) {
880+
bool trusted_owner = target->st_uid == 0U || target->st_uid == geteuid();
881+
bool world_writable = (target->st_mode & S_IWOTH) != 0;
882+
bool sticky = (target->st_mode & S_ISVTX) != 0;
883+
return S_ISDIR(target->st_mode) && trusted_owner && (!world_writable || sticky);
884+
}
885+
886+
/* Read the target text of the symlink at `component`, but only if the link's
887+
* owner is trusted -- and read it from the SAME inode the judgement was made
888+
* on. Judging by name and then opening by name is a check-then-use pair: an
889+
* account that can write the parent could swap the entry in between, so the
890+
* link that gets followed is never the one that was judged (demonstrated
891+
* against an earlier head with an LD_PRELOAD shim). The link is therefore
892+
* never opened by name after the judgement: on Linux an O_PATH|O_NOFOLLOW
893+
* descriptor pins the inode, and both the fstat and the readlinkat operate on
894+
* it; elsewhere the link is stat'ed by name before and after the readlinkat
895+
* and both must be the same inode with the same owner. What is followed
896+
* afterwards is the text this function returns, resolved from the parent. */
897+
static bool cbm_read_trusted_link(int parent, const char *component, bool follow_owned, char *text,
898+
size_t text_size) {
899+
ssize_t length = 0; /* nothing read yet: refused below unless a read succeeds */
900+
#if defined(__linux__) && defined(O_PATH)
901+
int link = openat(parent, component, O_PATH | O_NOFOLLOW | O_CLOEXEC);
902+
if (link < 0) {
903+
return false;
904+
}
905+
struct stat state;
906+
if (fstat(link, &state) == 0 && S_ISLNK(state.st_mode) &&
907+
cbm_walk_link_trusted(state.st_uid, follow_owned)) {
908+
/* An empty path names the link the descriptor itself refers to. */
909+
length = readlinkat(link, "", text, text_size);
910+
}
911+
(void)close(link);
912+
#else
913+
struct stat before;
914+
struct stat after;
915+
if (fstatat(parent, component, &before, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(before.st_mode) ||
916+
!cbm_walk_link_trusted(before.st_uid, follow_owned)) {
917+
return false;
918+
}
919+
length = readlinkat(parent, component, text, text_size);
920+
if (fstatat(parent, component, &after, AT_SYMLINK_NOFOLLOW) != 0 || !S_ISLNK(after.st_mode) ||
921+
after.st_dev != before.st_dev || after.st_ino != before.st_ino ||
922+
after.st_uid != before.st_uid) {
923+
return false;
924+
}
925+
#endif
926+
/* Empty or truncated text is refused rather than guessed at. */
927+
if (length <= 0 || (size_t)length >= text_size) {
928+
return false;
929+
}
930+
text[length] = '\0';
931+
return true;
932+
}
933+
934+
static int cbm_open_directory_component(int parent, const char *component, int flags,
935+
bool follow_owned) {
845936
int descriptor = openat(parent, component, flags);
846937
#if defined(O_NOFOLLOW) && defined(AT_SYMLINK_NOFOLLOW)
847938
if (descriptor < 0) {
848-
struct stat state;
849-
if (fstatat(parent, component, &state, AT_SYMLINK_NOFOLLOW) == 0 &&
850-
S_ISLNK(state.st_mode) && state.st_uid == 0U) {
851-
descriptor = openat(parent, component, flags & ~O_NOFOLLOW);
939+
/* The caller decides on errno from the FIRST open (ENOENT means
940+
* "create it"); a refused link must not leak a later call's errno. */
941+
int open_errno = errno;
942+
char text[CBM_SZ_4K];
943+
if (cbm_read_trusted_link(parent, component, follow_owned, text, sizeof(text))) {
944+
/* The judged link's own text, resolved from the parent exactly as
945+
* the kernel would resolve it (relative texts against the link's
946+
* directory). Links inside the text are resolved by the kernel as
947+
* before; the target check bounds where the walk lands. */
948+
int followed = openat(parent, text, flags & ~O_NOFOLLOW);
949+
struct stat target;
950+
if (followed >= 0 && fstat(followed, &target) == 0 &&
951+
cbm_walk_target_trusted(&target)) {
952+
descriptor = followed;
953+
} else if (followed >= 0) {
954+
(void)close(followed);
955+
}
956+
}
957+
if (descriptor < 0) {
958+
errno = open_errno;
852959
}
853960
}
961+
#else
962+
(void)follow_owned;
854963
#endif
855964
return descriptor;
856965
}
857966

858967
bool cbm_mkdir_p(const char *path, int mode) {
968+
return cbm_mkdir_p_ex(path, mode, 0U);
969+
}
970+
971+
bool cbm_mkdir_p_ex(const char *path, int mode, unsigned int policy) {
859972
if (!path || path[0] == '\0') {
860973
return false;
861974
}
975+
bool follow_owned = (policy & CBM_MKDIR_FOLLOW_OWNED) != 0U;
862976
char *tmp = strdup(path);
863977
if (!tmp) {
864978
return false;
@@ -891,12 +1005,12 @@ bool cbm_mkdir_p(const char *path, int mode) {
8911005
*separator = '\0';
8921006
}
8931007
if (cursor[0] != '\0' && strcmp(cursor, ".") != 0) {
894-
int next = cbm_open_directory_component(directory, cursor, flags);
1008+
int next = cbm_open_directory_component(directory, cursor, flags, follow_owned);
8951009
if (next < 0 && errno == ENOENT) {
8961010
if (mkdirat(directory, cursor, (mode_t)mode) != 0 && errno != EEXIST) {
8971011
ok = false;
8981012
} else {
899-
next = cbm_open_directory_component(directory, cursor, flags);
1013+
next = cbm_open_directory_component(directory, cursor, flags, follow_owned);
9001014
}
9011015
}
9021016
if (ok && next < 0) {

‎src/foundation/compat_fs.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,25 @@ int cbm_pclose(FILE *f);
6464

6565
/* ── File operations ──────────────────────────────────────────── */
6666

67-
/* Create directory (and parents). mode is ignored on Windows. Returns true on success. */
67+
/* Create directory (and parents). mode is ignored on Windows. Returns true on success.
68+
* A symlink component is followed only when root owns it (cbm_mkdir_p_ex). */
6869
bool cbm_mkdir_p(const char *path, int mode);
6970

71+
/* cbm_mkdir_p with a per-call-site symlink policy.
72+
*
73+
* CBM_MKDIR_FOLLOW_OWNED also follows a symlink component owned by the invoking
74+
* user. Opt in ONLY for paths rooted in the user's own configuration -- HOME,
75+
* XDG, CBM_CACHE_DIR: agent roots, config, cache and log directories -- where a
76+
* link the user owns is the user's own arrangement (a dotfile manager, a config
77+
* tree kept on another volume). Never for a path derived from a repository:
78+
* git creates symlinks owned by whoever cloned, so a checked-in
79+
* `.codebase-memory -> ~/.ssh` is user-owned without being user-intended.
80+
* Whatever a followed link lands on must itself be a directory owned by root
81+
* or the invoking user and not world-writable unless sticky. Windows ignores
82+
* the policy. */
83+
enum { CBM_MKDIR_FOLLOW_OWNED = 1U << 0 };
84+
bool cbm_mkdir_p_ex(const char *path, int mode, unsigned int policy);
85+
7086
/* Delete a file. Returns 0 on success. */
7187
int cbm_unlink(const char *path);
7288
/* Remove <db_path>-wal/-shm/-journal. MUST be called by any path installing a fresh

‎src/main.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ static main_build_identity_status_t main_build_identity(cbm_daemon_build_identit
13721372
* only the resulting canonical path, so retargeting the original alias
13731373
* cannot move storage after cohort admission. */
13741374
bool cache_ready = cbm_canonical_path(cache, canonical_cache, sizeof(canonical_cache));
1375-
if (!cache_ready && cbm_mkdir_p(cache, 0700)) {
1375+
if (!cache_ready && cbm_mkdir_p_ex(cache, 0700, CBM_MKDIR_FOLLOW_OWNED)) {
13761376
cache_ready = cbm_canonical_path(cache, canonical_cache, sizeof(canonical_cache));
13771377
}
13781378
if (!cache_ready || !cbm_is_dir(canonical_cache)) {

‎src/mcp/index_supervisor.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ static bool worker_unique_file(char *out, size_t out_size, const char *kind) {
564564
if (have_cache) {
565565
char directory[INDEX_WORKER_PATH_CAP];
566566
written = snprintf(directory, sizeof(directory), "%s/logs", cache_copy);
567-
if (written <= 0 || written >= (int)sizeof(directory) || !cbm_mkdir_p(directory, 0700)) {
567+
if (written <= 0 || written >= (int)sizeof(directory) ||
568+
!cbm_mkdir_p_ex(directory, 0700, CBM_MKDIR_FOLLOW_OWNED)) {
568569
return false;
569570
}
570571
written = snprintf(out, out_size, "%s/.worker-%s-XXXXXX", directory, kind);

‎src/mcp/mcp.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10262,7 +10262,7 @@ static bool write_skip_logfile(const char *project, const cbm_file_error_t *errs
1026210262
}
1026310263
char logdir[CBM_SZ_1K];
1026410264
snprintf(logdir, sizeof(logdir), "%s/logs", cdir);
10265-
cbm_mkdir_p(logdir, 0755);
10265+
cbm_mkdir_p_ex(logdir, 0755, CBM_MKDIR_FOLLOW_OWNED);
1026610266
snprintf(path, sizeof(path), "%s/%s-%lld.log", logdir, project ? project : "index",
1026710267
(long long)time(NULL));
1026810268
}
@@ -10481,7 +10481,7 @@ static void supervisor_tmp_path(char *out, size_t out_sz, const char *suffix) {
1048110481
if (cdir && cdir[0]) {
1048210482
char logdir[CBM_SZ_1K];
1048310483
snprintf(logdir, sizeof(logdir), "%s/logs", cdir);
10484-
cbm_mkdir_p(logdir, 0755);
10484+
cbm_mkdir_p_ex(logdir, 0755, CBM_MKDIR_FOLLOW_OWNED);
1048510485
snprintf(out, out_sz, "%s/.supervisor-%d%s", logdir, (int)getpid(), suffix);
1048610486
} else {
1048710487
snprintf(out, out_sz, ".supervisor-%d%s", (int)getpid(), suffix);
@@ -14860,7 +14860,8 @@ static bool mcp_command_output_path(char out[CBM_SZ_2K]) {
1486014860
int written;
1486114861
if (cache && cache[0]) {
1486214862
written = snprintf(directory, sizeof(directory), "%s/logs", cache);
14863-
if (written <= 0 || written >= (int)sizeof(directory) || !cbm_mkdir_p(directory, 0700)) {
14863+
if (written <= 0 || written >= (int)sizeof(directory) ||
14864+
!cbm_mkdir_p_ex(directory, 0700, CBM_MKDIR_FOLLOW_OWNED)) {
1486414865
return false;
1486514866
}
1486614867
} else {

‎src/pipeline/artifact.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,10 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
967967
if (dir_len < 0 || (size_t)dir_len >= sizeof(art_dir)) {
968968
return artifact_export_fail("prepare_artifact_dir", repo_path, "path_too_long", 0);
969969
}
970+
/* Deliberately the STRICT walk: art_dir is inside the checkout being
971+
* indexed, where a symlink is a checked-in file owned by whoever cloned.
972+
* A repository shipping `.codebase-memory -> ~/.ssh` must not redirect
973+
* this write, so user-owned links are not followed here. */
970974
errno = 0;
971975
if (!cbm_mkdir_p(art_dir, ART_DIR_PERMS)) {
972976
return artifact_export_fail("prepare_artifact_dir", art_dir, "mkdir_or_not_directory",
@@ -1141,7 +1145,7 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
11411145
char *last_slash = strrchr(cache_dir, '/');
11421146
if (last_slash) {
11431147
*last_slash = '\0';
1144-
cbm_mkdir_p(cache_dir, ART_DIR_PERMS);
1148+
cbm_mkdir_p_ex(cache_dir, ART_DIR_PERMS, CBM_MKDIR_FOLLOW_OWNED);
11451149
}
11461150

11471151
artifact_file_error_t ioerr;

0 commit comments

Comments
 (0)