Makefile/all: make libnl3 optional
Linux process isolation tool using namespaces, resource limits, and seccomp-bpf syscall filters.
chroot()
pivot_root()
/proc
tmpfs
# Install dependencies (Debian/Ubuntu) sudo apt-get install autoconf bison flex gcc g++ git libprotobuf-dev libnl-route-3-dev libtool make pkg-config protobuf-compiler git clone https://github.com/google/nsjail.git cd nsjail make
docker build -t nsjail . docker run --privileged --rm -it nsjail nsjail --user 99999 --group 99999 --chroot / -- /bin/bash
./nsjail -Mo --chroot / --user 99999 --group 99999 -- /bin/bash
./nsjail -Ml --port 9000 --chroot /chroot --user 99999 --group 99999 -- /bin/sh -i
./nsjail -Mr --chroot / -- /bin/echo "test"
-Ml
LISTEN
-Mo
ONCE
-Me
EXECVE
-Mr
RERUN
# Filesystem -c, --chroot DIR Chroot directory (default: /) -R, --bindmount_ro SRC Read-only bind mount (SRC or SRC:DST) -B, --bindmount SRC Read-write bind mount (SRC or SRC:DST) -T, --tmpfsmount DST Tmpfs mount at DST -m, --mount SRC:DST:TYPE:OPTS Arbitrary mount # User/Group -u, --user UID User ID inside jail (default: current) -g, --group GID Group ID inside jail (default: current) -U, --uid_mapping I:O:C Custom UID mapping (requires newuidmap) -G, --gid_mapping I:O:C Custom GID mapping (requires newgidmap) # Namespaces -N, --disable_clone_newnet Disable network namespace --disable_clone_newuser Disable user namespace --disable_clone_newpid Disable PID namespace --enable_clone_newtime Enable time namespace (kernel >= 5.3) # Resource Limits -t, --time_limit SEC Wall-time limit in seconds (default: 600) --rlimit_as MB Address space limit in MB --rlimit_cpu SEC CPU time limit in seconds --rlimit_nofile N Max open files --oom_score_adj VALUE OOM score adjustment for the sandbox (-1000 to 1000) # Security -P, --seccomp_policy FILE Seccomp-bpf policy file (Kafel syntax) --seccomp_string POLICY Inline seccomp policy --cap CAP_NAME Retain capability (can specify multiple) --keep_caps Retain all capabilities # Networking --iface_own IFACE Move interface into jail --macvlan_iface IFACE Clone interface as MACVLAN --use_pasta Enable userland networking (pasta) # Configuration -C, --config FILE Load protobuf config file
NsJail uses Protocol Buffers for configuration. Schema: config.proto
name: "bash-jail" mode: ONCE hostname: "JAILED" cwd: "/tmp" time_limit: 100 max_cpus: 1 rlimit_as: 512 rlimit_cpu: 10 rlimit_nofile: 32 clone_newnet: true clone_newuser: true clone_newns: true clone_newpid: true clone_newipc: true clone_newuts: true uidmap { inside_id: "0" outside_id: "" count: 1 } gidmap { inside_id: "0" outside_id: "" count: 1 } mount { src: "/" dst: "/" is_bind: true rw: false } mount { dst: "/proc" fstype: "proc" } mount { dst: "/tmp" fstype: "tmpfs" rw: true } seccomp_string: "ALLOW { read, write, exit, exit_group } DEFAULT KILL"
Load with:
./nsjail --config myconfig.cfg
Override command:
./nsjail --config myconfig.cfg -- /usr/bin/id
Isolate networked services for security challenges:
./nsjail -Ml --port 8000 \ --chroot /srv/ctf \ --user 65534 --group 65534 \ --time_limit 60 \ --rlimit_as 128 \ --rlimit_cpu 10 \ -- /srv/ctf/challenge
Continuously re-run potentially crashing programs:
./nsjail -Mr \ --chroot / \ --user 99999 \ --time_limit 10 \ --rlimit_as 512 \ --seccomp_string 'ALLOW { ... } DEFAULT KILL' \ -- /path/to/target @@
Run untrusted GUI applications:
./nsjail --config configs/firefox-with-net-wayland.cfg
Run program with minimal filesystem access:
./nsjail -Mo \ -R /lib/x86_64-linux-gnu \ -R /lib64 \ -R /usr/bin/find \ -R /dev/urandom \ -- /usr/bin/find /
Uses pasta for userspace networking without root privileges:
./nsjail --user 1000 --group 1000 \ --use_pasta \ --chroot / \ -- /usr/bin/curl https://example.com
Or via config:
user_net { enable: true ip: "10.255.255.2" gw: "10.255.255.1" ip6: "fc00::2" gw6: "fc00::1" tcp_ports: "80,443" enable_dns: true }
Clone physical interface (requires root):
sudo ./nsjail \ --macvlan_iface eth0 \ --macvlan_vs_ip 192.168.1.100 \ --macvlan_vs_nm 255.255.255.0 \ --macvlan_vs_gw 192.168.1.1 \ -- /bin/bash
Kafel policy syntax:
./nsjail --seccomp_string ' POLICY example { ALLOW { read, write, open, close, mmap, munmap, brk, exit_group } DEFAULT KILL } USE example DEFAULT KILL ' -- /bin/program
./nsjail \ --cgroup_mem_max $((512*1024*1024)) \ --cgroup_pids_max 32 \ --cgroup_cpu_ms_per_sec 800 \ -- /bin/cpu_intensive_program
CLONE_NEWUSER required: Run with --disable_clone_newuser (requires root) or ensure user namespaces are enabled:
--disable_clone_newuser
sysctl kernel.unprivileged_userns_clone # Should be 1
Mount errors: Check that /proc is not overmounted:
cat /proc/mounts | grep /proc
Check available namespaces:
ls -la /proc/self/ns/
Disable unsupported namespaces (e.g., --disable_clone_newcgroup for kernel < 4.6).
--disable_clone_newcgroup
Enable verbose logging:
./nsjail -v --config myconfig.cfg
This is not an official Google product.
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802032778号
NsJail
Linux process isolation tool using namespaces, resource limits, and seccomp-bpf syscall filters.
Features
chroot(),pivot_root(), read-only mounts, custom/procandtmpfsInstallation
Build from source
Docker
Quick Start
Basic isolated shell
Network service (inetd-style)
Re-running process (useful for fuzzing)
Usage
Execution Modes
-MlLISTEN-MoONCE-MeEXECVE-MrRERUNCommon Options
Configuration Files
NsJail uses Protocol Buffers for configuration. Schema: config.proto
Example configuration
Load with:
Override command:
Example Configs
Use Cases
CTF Challenge Hosting
Isolate networked services for security challenges:
Fuzzing
Continuously re-run potentially crashing programs:
Desktop Application Sandboxing
Run untrusted GUI applications:
Minimal Environment Execution
Run program with minimal filesystem access:
Advanced Features
Userland Networking (pasta)
Uses pasta for userspace networking without root privileges:
Or via config:
MACVLAN Network Isolation
Clone physical interface (requires root):
Seccomp-bpf Filtering
Kafel policy syntax:
Cgroups Resource Control
Troubleshooting
Permission Denied Errors
CLONE_NEWUSER required: Run with
--disable_clone_newuser(requires root) or ensure user namespaces are enabled:Mount errors: Check that
/procis not overmounted:Resource Not Available
Check available namespaces:
Disable unsupported namespaces (e.g.,
--disable_clone_newcgroupfor kernel < 4.6).Debugging
Enable verbose logging:
Documentation
Contact
This is not an official Google product.