[Python] How to run nsjail on Google Cloud Run without prctl() errors?

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Summary


    I'm trying to deploy a Python code execution service using nsjail for sandboxing on Google Cloud Run, but nsjail fails with `prctl(PR_SET_SECUREBITS)` errors even with minimal configuration. Is there a working nsjail configuration for Cloud Run's gVisor environment?

    Environment


    Platform: Google Cloud Run (managed, gVisor-based)
    Base Image: python:3.11-slim
    nsjail: Latest from GitHub (built from source)
    Deployment: gcloud run deploy with 1GB memory, no special flags

    ## What I'm Trying To Do
    Execute untrusted Python scripts safely using nsjail for resource limiting (CPU, memory) and filesystem isolation.

    ## The Problem
    nsjail fails to start with this error:


    [E][2025-11-24T23:58:07+0000][11] initNsFromChild():265 prctl(PR_SET_SECUREBITS, SECBIT_KEEP_CAPS | SECBIT_NO_SETUID_FIXUP): Invalid argument
    ​
    What I've Tried

    Configuration 1: Minimal nsjail (all namespaces disabled)


    name: "python-sandbox"
    mode: ONCE
    time_limit: 5

    # All disabled to avoid namespace errors
    clone_newnet: false
    clone_newuser: false
    clone_newns: false
    clone_newpid: false
    clone_newipc: false
    clone_newuts: false
    clone_newcgroup: false

    # Only resource limits
    rlimit_as: 512
    rlimit_cpu: 10
    rlimit_nofile: 64
    rlimit_nproc: 32


    Result: Still fails with prctl() error

    Configuration 2: With disable_no_new_privs


    # ... same as above plus:
    keep_caps: false
    disable_no_new_privs: true


    Result: Same error

    Configuration 3: Full nsjail with namespaces


    clone_newnet: true
    clone_newuser: true
    # ... etc


    Result: Fails earlier with:

    [W] clone(flags=CLONE_NEWNS|CLONE_NEWCGROUP|...) failed: Invalid argument

    Dockerfile


    FROM python:3.11-slim

    # Build nsjail
    RUN apt-get update && apt-get install -y \
    build-essential pkg-config libnl-route-3-dev \
    libnl-3-dev libprotobuf-dev protobuf-compiler \
    bison flex git && \
    git clone --depth 1 https://github.com/google/nsjail.git /tmp/nsjail && \
    cd /tmp/nsjail && make && \
    cp /tmp/nsjail/nsjail /usr/bin/nsjail && \
    chmod +x /usr/bin/nsjail

    # ... rest of setup
    CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]

    Execution Command


    subprocess.run([
    '/usr/bin/nsjail',
    '--config', '/app/config/nsjail.cfg',
    '--',
    '/usr/local/bin/python3', 'script.py'
    ], timeout=5)

    What Works

    • Locally with Docker: Works perfectly with docker run --privileged
    • Basic execution: Python scripts run fine without nsjail wrapper
    Questions

    1. Is it possible to run nsjail on Cloud Run at all? I've been told it is, but can't find working configs
    2. Is there a Cloud Run deployment flag to allow prctl() syscalls?
    3. Is there a modified nsjail build that works without PR_SET_SECUREBITS?
    What I Need

    • A working nsjail configuration for Cloud Run
    References


    Continue reading...

Compartilhe esta Página