Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _codeql_detected_source_root
22 changes: 14 additions & 8 deletions containers/agent/one-shot-token/one-shot-token.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ static pthread_mutex_t token_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Pointer to the real getenv function */
static char *(*real_getenv)(const char *name) = NULL;

/* Initialize the real getenv pointer */
static void init_real_getenv(void) {
/* pthread_once control for thread-safe initialization */
static pthread_once_t getenv_init_once = PTHREAD_ONCE_INIT;

/* Initialize the real getenv pointer (called exactly once via pthread_once) */
static void init_real_getenv_once(void) {
real_getenv = dlsym(RTLD_NEXT, "getenv");
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning the void * result of dlsym() directly to a function pointer is not portable ISO C and can trigger warnings (even if it works on many POSIX platforms). Prefer assigning via an intermediate void * and memcpy, or the common POSIX pattern *(void **)(&real_getenv) = dlsym(...); to avoid undefined-behavior concerns and compiler diagnostics.

See below for a potential fix:

    void *sym = dlsym(RTLD_NEXT, "getenv");
    if (sym == NULL) {
        fprintf(stderr, "[one-shot-token] FATAL: Could not find real getenv: %s\n", dlerror());
        /* Cannot recover - abort to prevent undefined behavior */
        abort();
    }
    memcpy(&real_getenv, &sym, sizeof(real_getenv));

Copilot uses AI. Check for mistakes.
if (real_getenv == NULL) {
real_getenv = dlsym(RTLD_NEXT, "getenv");
if (real_getenv == NULL) {
fprintf(stderr, "[one-shot-token] ERROR: Could not find real getenv: %s\n", dlerror());
/* Fall back to a no-op to prevent crash */
abort();
}
fprintf(stderr, "[one-shot-token] FATAL: Could not find real getenv: %s\n", dlerror());
/* Cannot recover - abort to prevent undefined behavior */
abort();
}
Comment on lines +52 to 58
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dlsym() errors should be checked via dlerror(), not by comparing the returned pointer to NULL. Per dlsym() semantics, NULL can be a valid symbol value, and dlerror() may also return a stale error unless you clear it before calling dlsym(). Clear dlerror() first, call dlsym(), then call dlerror() and abort only if it reports an error.

See below for a potential fix:

    const char *err;

    /* Clear any existing error */
    (void)dlerror();

    real_getenv = dlsym(RTLD_NEXT, "getenv");
    err = dlerror();
    if (err != NULL) {
        fprintf(stderr, "[one-shot-token] FATAL: Could not find real getenv: %s\n", err);

Copilot uses AI. Check for mistakes.
}

/* Ensure real_getenv is initialized (thread-safe) */
static void init_real_getenv(void) {
pthread_once(&getenv_init_once, init_real_getenv_once);
}

/* Check if a variable name is a sensitive token */
static int get_token_index(const char *name) {
if (name == NULL) return -1;
Expand Down
Loading