Skip to content

Commit fa97ea5

Browse files
committed
Fix CodeQL TOCTOU: use fchmod before fclose on gate script
CodeQL cpp/toctou-race-condition #32: fopen then chmod on the same path allows a race where the file could be swapped between write and chmod. Fix: use fchmod(fileno(f)) before fclose on POSIX. Windows falls back to chmod (no fchmod).
1 parent aa5237b commit fa97ea5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/cli/cli.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,14 @@ static void cbm_install_hook_gate_script(const char *home) {
15591559
"is not indexed yet, call index_repository first. Fall back to Grep/Glob/Read "
15601560
"only for text content search. If you need Grep, retry.' >&2\n"
15611561
"exit 2\n");
1562+
/* fchmod before close to avoid TOCTOU race (CodeQL cpp/toctou-race-condition) */
1563+
#ifndef _WIN32
1564+
fchmod(fileno(f), 0755);
1565+
#endif
15621566
fclose(f);
1567+
#ifdef _WIN32
15631568
chmod(script_path, 0755);
1569+
#endif
15641570
}
15651571

15661572
#define GEMINI_HOOK_MATCHER "google_search|read_file|grep_search"

0 commit comments

Comments
 (0)