sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

Concurrent posix_spawn calls break subprocess execution under sandlock learn

Open ghazariann opened this issue 2 months ago • 0 comments

How this was found

Running sandlock learn -- claude -p "create a file called /tmp/ps_output.txt and write the output of the ps command into it", claude's own answer was:

The environment doesn't permit running shell commands. Neither the ps command nor shell redirects are available in this sandbox — I'm unable to complete this task.

ps was in fact installed. The actual cause seems to be in sandlock's own stderr for that run:

sandlock: argv-safety freeze failed for pid 17875: Operation not permitted (os error 1) — denying execve to preserve TOCTOU invariant

repeated many times across distinct pids in the same run. This is I assume to be the real issue. Once the failure starts, it repeats for the rest of the run and never recovers.

This comes from sandlock's policy_fn machinery, specifically an "argv-safety freeze" meant to protect against a TOCTOU (time-of-check-to-time-of-use) race around execve calls. For this workload, that protection itself fails to complete, and once it does, it starts denying every later execve too.

As I understand it: if sandlock can't successfully pause every other thread/process it needs to before letting an exec through, it has no way to guarantee the freeze actually worked. Rather than let the exec proceed unprotected, it takes the safe option and denies it, that's where denying execve to preserve TOCTOU invariant comes from.

Why the freeze itself fails to pause everything needs more investigation. I assume some kind of race, but that's not confirmed.

Environment

  • Kernel 6.17.0+, glibc 2.39-0ubuntu8.8

Repro

Reproduced without Node.js or Claude Code: a small C program (below) spawning subprocesses via posix_spawn from multiple threads at once hits the same argv-safety freeze failed error.

#include <spawn.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

extern char **environ;

static void *spawner_thread(void *arg) {
    (void)arg;
    for (int i = 0; i < 6; i++) {
        pid_t pid;
        char *argv[] = {"/bin/echo", "t2", NULL};
        posix_spawn(&pid, "/bin/echo", NULL, NULL, argv, environ);
        int status;
        waitpid(pid, &status, 0);
        fprintf(stderr, "thread2 spawn %d: pid=%d\n", i, pid);
    }
    return NULL;
}

int main(void) {
    pthread_t t2;
    pthread_create(&t2, NULL, spawner_thread, NULL);

    for (int i = 0; i < 6; i++) {
        pthread_t helper;
        pthread_create(&helper, NULL, spawner_thread, NULL);
        pid_t pid;
        char *argv[] = {"/bin/echo", "t1", NULL};
        posix_spawn(&pid, "/bin/echo", NULL, NULL, argv, environ);
        int status;
        waitpid(pid, &status, 0);
        fprintf(stderr, "main spawn %d: pid=%d\n", i, pid);
        pthread_join(helper, NULL);
    }

    pthread_join(t2, NULL);
    return 0;
}
gcc -O2 -pthread -o spawn_repro spawn_repro.c
sandlock learn -o /tmp/spawn-repro.toml -- ./spawn_repro

Running this spawns subprocesses concurrently from 2 threads. Under sandlock learn, most of those spawns will fail to actually run /bin/echo, expect repeated argv-safety freeze failed lines and no hi output, instead of the clean run you'd get unsandboxed.

ghazariann avatar Sep 08 '26 16:09 ghazariann