Skip to content

Commit 40dad61

Browse files
committed
fix(git): clear git's repository-local env for cbm's own git children (DeusData#2003)
Git exports repository-local variables (GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_COMMON_DIR, ...) into hooks, and some editors export them into their terminals. They take precedence over `git -C <dir>`, and every git child cbm spawned inherited them. A cbm started from such an environment therefore resolved every project against the CALLER's repository: git_context reported a plain directory as is_git=true with the caller's worktree root, branch and HEAD, the history pass attributed the caller's commits to it, and the watcher / detect_changes / artifact git calls read (and `config merge.ours.driver` could write) the wrong repository. Fix: one production helper, src/foundation/git_env.{c,h}, holds the 15-variable list `git rev-parse --local-env-vars` prints and builds the child environment with exactly those removed -- a POSIX envp array or a Windows CREATE_UNICODE_ENVIRONMENT block (case-insensitive names). It is applied per spawn; cbm's own environment is never modified (no unsetenv in the parent, which would race other threads): - cbm_popen_git(cmd): read-mode popen for git command lines. POSIX spawns /bin/sh -c via posix_spawn with the scrubbed envp (stdin /dev/null, pipe ends close-on-exec, reaped by cbm_pclose); Windows passes the block to the existing handle-isolated CreateProcessW spawn. Fails closed. - cbm_proc_opts_t.strip_git_repo_env: cbm_subprocess_spawn passes the scrubbed envp to posix_spawnp / the fork+exec child, or the block to CreateProcessW. Fails closed. - Every git spawn uses them: git_context, pass_githistory, artifact (4 sites), the watcher's git runner, and detect_changes' git commands. search_code's grep scan keeps the inherited environment. Tests (RED before, green after, RED on revert of the helper and of the call sites): - git_context::git_context_ignores_inherited_repo_env -- GIT_DIR and the hook set pointing at a decoy repo: a plain dir is is_git=false, a fixture repo reports its own HEAD and branch, the decoy is untouched. - git_context::githistory_ignores_inherited_repo_env -- the history pass on a plain dir sees 0 commits (was the decoy's 4). - subprocess::subprocess_strip_git_repo_env_is_per_child and popen_git_strips_repo_env_and_reports_exit_status -- all 15 variables are gone in the child, unrelated variables pass through, the parent environment is unchanged, and cbm_pclose keeps pclose() status semantics. Proof (macOS arm64, GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE/GIT_PREFIX exported at a decoy clone, `cli index_status verbose=true`): a plain dir went from is_git=true, worktree_root=<decoy>, branch=<decoy's> (v0.11.0) to is_git=false; a fixture repo went from the decoy's root/branch/HEAD to its own root and branch. Refs DeusData#2003 Signed-off-by: Martin Vogel <[email protected]>
1 parent 5958f54 commit 40dad61

14 files changed

Lines changed: 727 additions & 31 deletions

File tree

‎Makefile.cbm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ FOUNDATION_SRCS = \
327327
src/foundation/limits.c \
328328
src/foundation/index_policy.c \
329329
src/foundation/subprocess.c \
330+
src/foundation/git_env.c \
330331
src/foundation/sha256.c \
331332
src/foundation/secure_random.c \
332333
src/foundation/macos_acl.c \

‎src/foundation/compat_fs.c‎

Lines changed: 153 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "foundation/constants.h"
88
#include "foundation/compat_fs.h"
99
#include "foundation/compat_fs_internal.h"
10+
#include "foundation/git_env.h"
1011

1112
#include <stdio.h>
1213
#include <stdlib.h>
@@ -236,7 +237,9 @@ static wchar_t *cbm_resolve_comspec(void) {
236237

237238
/* On failure returns NULL with *stage naming the failing step and *gle the
238239
* GetLastError value captured at that step (0 when errno is the signal). */
239-
static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle) {
240+
/* env: a CREATE_UNICODE_ENVIRONMENT block for the child, or NULL to inherit. */
241+
static FILE *cbm_popen_isolated(const char *cmd, const wchar_t *env, const char **stage,
242+
DWORD *gle) {
240243
*stage = "";
241244
*gle = 0;
242245
InitOnceExecuteOnce(&g_popen_once, cbm_popen_init, NULL, NULL);
@@ -315,8 +318,11 @@ static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle)
315318
*stage = "cmdline";
316319
*gle = ERROR_NOT_ENOUGH_MEMORY;
317320
} else {
318-
created = CreateProcessW(app, wcmdline, NULL, NULL, TRUE,
319-
EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW, NULL, NULL,
321+
DWORD flags = EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW;
322+
if (env) {
323+
flags |= CREATE_UNICODE_ENVIRONMENT;
324+
}
325+
created = CreateProcessW(app, wcmdline, NULL, NULL, TRUE, flags, (LPVOID)env, NULL,
320326
&si.StartupInfo, &pi);
321327
if (!created) {
322328
*stage = "spawn";
@@ -372,29 +378,46 @@ static FILE *cbm_popen_isolated(const char *cmd, const char **stage, DWORD *gle)
372378
return NULL;
373379
}
374380

381+
static FILE *cbm_popen_read_isolated(const char *cmd, const wchar_t *env) {
382+
const char *stage = "";
383+
DWORD gle = 0;
384+
FILE *fp = cbm_popen_isolated(cmd, env, &stage, &gle);
385+
g_popen_last_isolated = (fp != NULL);
386+
if (!fp) {
387+
char glebuf[CBM_SZ_16];
388+
char errnobuf[CBM_SZ_16];
389+
snprintf(glebuf, sizeof(glebuf), "%lu", (unsigned long)gle);
390+
snprintf(errnobuf, sizeof(errnobuf), "%d", errno);
391+
cbm_log_warn("compat.popen_isolated_failed", "stage", stage, "gle", glebuf, "errno",
392+
errnobuf);
393+
}
394+
return fp;
395+
}
396+
375397
FILE *cbm_popen(const char *cmd, const char *mode) {
376398
/* Our git shell-outs are all read-mode; they MUST use the isolated
377399
* spawn. On failure, log and fail the call — never fall back to
378400
* _popen, whose full handle inheritance re-arms the UI hang (#798). */
379401
if (mode && mode[0] == 'r' && mode[1] == '\0') {
380-
const char *stage = "";
381-
DWORD gle = 0;
382-
FILE *fp = cbm_popen_isolated(cmd, &stage, &gle);
383-
g_popen_last_isolated = (fp != NULL);
384-
if (!fp) {
385-
char glebuf[CBM_SZ_16];
386-
char errnobuf[CBM_SZ_16];
387-
snprintf(glebuf, sizeof(glebuf), "%lu", (unsigned long)gle);
388-
snprintf(errnobuf, sizeof(errnobuf), "%d", errno);
389-
cbm_log_warn("compat.popen_isolated_failed", "stage", stage, "gle", glebuf, "errno",
390-
errnobuf);
391-
}
392-
return fp;
402+
return cbm_popen_read_isolated(cmd, NULL);
393403
}
394404
g_popen_last_isolated = 0;
395405
return _popen(cmd, mode);
396406
}
397407

408+
FILE *cbm_popen_git(const char *cmd) {
409+
/* Fail closed: without the scrubbed block the child would inherit a
410+
* caller's GIT_DIR and read the wrong repository (#2003). */
411+
wchar_t *env = cbm_git_child_env_block();
412+
if (!env) {
413+
cbm_log_warn("compat.popen_git_env_failed", "stage", "env_block");
414+
return NULL;
415+
}
416+
FILE *fp = cbm_popen_read_isolated(cmd, env);
417+
cbm_git_child_env_free(env); /* CreateProcessW copies the block into the child */
418+
return fp;
419+
}
420+
398421
int cbm_pclose(FILE *f) {
399422
InitOnceExecuteOnce(&g_popen_once, cbm_popen_init, NULL, NULL);
400423

@@ -759,6 +782,8 @@ int cbm_exec_no_shell(const char *const *argv) {
759782
#include <dirent.h>
760783
#include <errno.h>
761784
#include <fcntl.h>
785+
#include <pthread.h>
786+
#include <spawn.h>
762787
#include <sys/file.h>
763788
#include <sys/stat.h>
764789
#include <sys/wait.h>
@@ -863,8 +888,119 @@ FILE *cbm_popen(const char *cmd, const char *mode) {
863888
return popen(cmd, mode);
864889
}
865890

891+
/* Streams opened by cbm_popen_git: popen() cannot take an environment, so
892+
* those children are spawned here and reaped by cbm_pclose via this list. */
893+
typedef struct cbm_popen_git_entry {
894+
FILE *fp;
895+
pid_t pid;
896+
struct cbm_popen_git_entry *next;
897+
} cbm_popen_git_entry_t;
898+
899+
static cbm_popen_git_entry_t *g_popen_git_list = NULL;
900+
static pthread_mutex_t g_popen_git_lock = PTHREAD_MUTEX_INITIALIZER;
901+
902+
static bool cbm_fd_set_cloexec(int fd) {
903+
int flags = fcntl(fd, F_GETFD);
904+
return flags >= 0 && fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0;
905+
}
906+
907+
/* `/bin/sh -c cmd` with stdout on a pipe, stdin on /dev/null (never the MCP
908+
* transport), stderr inherited like popen(), and `envp` as the environment. */
909+
static pid_t cbm_popen_spawn_sh(const char *cmd, char **envp, int *read_fd) {
910+
int fds[2];
911+
if (pipe(fds) != 0) {
912+
return -1;
913+
}
914+
if (!cbm_fd_set_cloexec(fds[0]) || !cbm_fd_set_cloexec(fds[1])) {
915+
(void)close(fds[0]);
916+
(void)close(fds[1]);
917+
return -1;
918+
}
919+
posix_spawn_file_actions_t actions;
920+
if (posix_spawn_file_actions_init(&actions) != 0) {
921+
(void)close(fds[0]);
922+
(void)close(fds[1]);
923+
return -1;
924+
}
925+
/* dup2 clears close-on-exec on the child's stdout; both pipe ends
926+
* themselves stay close-on-exec, so no other child ever holds them. */
927+
bool configured =
928+
posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0) == 0 &&
929+
posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO) == 0;
930+
char *const argv[] = {(char *)"sh", (char *)"-c", (char *)cmd, NULL};
931+
pid_t pid = -1;
932+
int rc = configured ? posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) : -1;
933+
(void)posix_spawn_file_actions_destroy(&actions);
934+
(void)close(fds[1]);
935+
if (rc != 0 || pid <= 0) {
936+
(void)close(fds[0]);
937+
return -1;
938+
}
939+
*read_fd = fds[0];
940+
return pid;
941+
}
942+
943+
static int cbm_popen_git_wait(pid_t pid) {
944+
int status = 0;
945+
while (waitpid(pid, &status, 0) < 0) {
946+
if (errno != EINTR) {
947+
return -1;
948+
}
949+
}
950+
return status;
951+
}
952+
953+
FILE *cbm_popen_git(const char *cmd) {
954+
if (!cmd) {
955+
return NULL;
956+
}
957+
/* Fail closed: without the scrubbed environment the child would inherit
958+
* a caller's GIT_DIR and read the wrong repository (#2003). */
959+
char **envp = cbm_git_child_envp();
960+
if (!envp) {
961+
return NULL;
962+
}
963+
int read_fd = -1;
964+
pid_t pid = cbm_popen_spawn_sh(cmd, envp, &read_fd);
965+
cbm_git_child_env_free(envp); /* posix_spawn copied it into the child */
966+
if (pid < 0) {
967+
return NULL;
968+
}
969+
cbm_popen_git_entry_t *entry = (cbm_popen_git_entry_t *)malloc(sizeof(*entry));
970+
FILE *fp = entry ? fdopen(read_fd, "r") : NULL;
971+
if (!fp) {
972+
free(entry);
973+
(void)close(read_fd);
974+
(void)cbm_popen_git_wait(pid);
975+
return NULL;
976+
}
977+
entry->fp = fp;
978+
entry->pid = pid;
979+
pthread_mutex_lock(&g_popen_git_lock);
980+
entry->next = g_popen_git_list;
981+
g_popen_git_list = entry;
982+
pthread_mutex_unlock(&g_popen_git_lock);
983+
return fp;
984+
}
985+
866986
int cbm_pclose(FILE *f) {
867-
return pclose(f);
987+
cbm_popen_git_entry_t *found = NULL;
988+
pthread_mutex_lock(&g_popen_git_lock);
989+
for (cbm_popen_git_entry_t **link = &g_popen_git_list; *link; link = &(*link)->next) {
990+
if ((*link)->fp == f) {
991+
found = *link;
992+
*link = found->next;
993+
break;
994+
}
995+
}
996+
pthread_mutex_unlock(&g_popen_git_lock);
997+
if (!found) {
998+
return pclose(f); /* opened by popen() */
999+
}
1000+
pid_t pid = found->pid;
1001+
free(found);
1002+
(void)fclose(f);
1003+
return cbm_popen_git_wait(pid); /* the raw wait status, as pclose() */
8681004
}
8691005

8701006
FILE *cbm_fopen(const char *path, const char *mode) {

‎src/foundation/compat_fs.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ void cbm_closedir(cbm_dir_t *d);
6060
/* ── Portable popen/pclose ────────────────────────────────────── */
6161

6262
FILE *cbm_popen(const char *cmd, const char *mode);
63+
64+
/* Read-mode cbm_popen for a `git ...` command line: the child environment
65+
* omits git's repository-local variables (foundation/git_env.h), so an
66+
* inherited GIT_DIR/GIT_WORK_TREE/... can never override `git -C <repo>`.
67+
* The caller's own environment is untouched. Close with cbm_pclose. */
68+
FILE *cbm_popen_git(const char *cmd);
69+
6370
int cbm_pclose(FILE *f);
6471

6572
/* ── File operations ──────────────────────────────────────────── */

0 commit comments

Comments
 (0)