Skip to content

Commit 7cde54a

Browse files
Mossakaclaude
andcommitted
fix(cli): fix secure_getenv() bypass of one-shot token protection
secure_getenv() was calling get_token_index() before init_token_list() and without the mutex, causing all token protection to be bypassed when secure_getenv() was the first call into the library (empty token list returns -1 for all lookups). Added initialization, mutex acquisition, and recursion guard matching the getenv() implementation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ad7a4bc commit 7cde54a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

containers/agent/one-shot-token/one-shot-token.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,29 @@ char *secure_getenv(const char *name) {
404404
return getenv(name);
405405
}
406406

407+
/* Skip interception during recursive calls (e.g., fprintf -> secure_getenv) */
408+
if (in_getenv) {
409+
return real_secure_getenv(name);
410+
}
411+
in_getenv = 1;
412+
413+
/* Initialize token list on first call (thread-safe) */
414+
pthread_mutex_lock(&token_mutex);
415+
if (!tokens_initialized) {
416+
init_token_list();
417+
}
418+
419+
/* Get token index while holding mutex to avoid race with initialization */
407420
int token_idx = get_token_index(name);
408421

409-
/* Not a sensitive token - pass through to real secure_getenv */
422+
/* Not a sensitive token - release mutex and pass through */
410423
if (token_idx < 0) {
424+
pthread_mutex_unlock(&token_mutex);
425+
in_getenv = 0;
411426
return real_secure_getenv(name);
412427
}
413428

414-
/* Sensitive token - handle cached access with secure_getenv semantics */
415-
pthread_mutex_lock(&token_mutex);
429+
/* Sensitive token - handle cached access with secure_getenv semantics (mutex already held) */
416430

417431
char *result = NULL;
418432

@@ -445,6 +459,7 @@ char *secure_getenv(const char *name) {
445459
}
446460

447461
pthread_mutex_unlock(&token_mutex);
462+
in_getenv = 0;
448463

449464
return result;
450465
}

0 commit comments

Comments
 (0)