BWRAP(1)

๐Ÿ’– BWRAP(1) ๐Ÿ’–

Manual name: bwrap ยท section: User Commands ๐Ÿซถ

โœจ NAME โœจ

bwrap โ€” container setup utility ๐Ÿ› ๏ธ

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

bwrap [OPTION...] [COMMAND]

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

okay so bwrap is an unprivileged low-level sandboxing tool. bestie, you're probably not gonna use it straight from the commandline every day, but you can โ€” it's totally possible. ๐Ÿ’…

It works by making a brand-new, completely empty filesystem namespace where the root lives on a tmpfs that the host can't see, and that whole thing gets automatically cleaned up when the last process exits. โœจ Then you use commandline options to build the root filesystem and the process environment for the command that's gonna run in that namespace. It's giving custom tiny universe energy. ๐ŸŒŒ

By default, bwrap creates a new mount namespace for the sandbox. Optionally it also sets up new user, ipc, pid, network and uts namespaces โ€” but heads up, the user namespace is required if bwrap is not run as root. The application in the sandbox can be made to run with a different UID and GID. ๐Ÿ‘‘

If needed (e.g. when using a PID namespace), bwrap runs a minimal pid 1 process in the sandbox that is responsible for reaping zombies. It also detects when the initial application process (pid 2) dies and reports its exit status back to the original spawner. The pid 1 process exits to clean up the sandbox when there are no other processes in the sandbox left. no cap, that little pid 1 is doing main character cleanup duty. ๐Ÿงน

๐Ÿ’… OPTIONS ๐Ÿ’…

When options are used multiple times, the last option wins, unless otherwise specified.

An empty string is not a valid path, so options that take a path reject it with an error. ๐Ÿšจ

๐ŸŽ€ General options ๐ŸŽ€

  • --help โ€” print help and exit. cute, quick, done. โœจ
  • --version โ€” print version. ๐Ÿท๏ธ
  • --args FD โ€” parse nul-separated arguments from the given file descriptor. This option can be used multiple times to parse options from multiple sources. ๐Ÿซถ
  • --argv0 VALUE โ€” set argv[0] to the value VALUE before running the program. ๐ŸŽ€
  • --level-prefix โ€” prefix each line of diagnostic output with a numeric severity level enclosed in angle brackets. The severity levels used are based on the constants used by syslog(3): for example, <4> indicates a warning, because LOG_WARNING has numeric value 4. Numbers smaller than 4 indicate fatal errors, and numbers larger than 4 indicate informational messages. These prefixes can be parsed by tools compatible with logger --prio-prefix (see logger(1)) or systemd-cat --level-prefix=1 (see systemd-cat(1)). ๐Ÿชต
  • --unshare-user โ€” create a new user namespace. ๐Ÿ‘ค
  • --unshare-user-try โ€” create a new user namespace if possible, else skip it. ๐Ÿ™ˆ
  • --unshare-ipc โ€” create a new ipc namespace. ๐Ÿ“ฌ
  • --unshare-pid โ€” create a new pid namespace. ๐Ÿงธ
  • --unshare-net โ€” create a new network namespace. ๐ŸŒ
  • --unshare-uts โ€” create a new uts namespace. ๐Ÿท๏ธ
  • --unshare-cgroup โ€” create a new cgroup namespace. ๐Ÿงบ
  • --unshare-cgroup-try โ€” create a new cgroup namespace if possible, else skip it. ๐Ÿซง
  • --unshare-all โ€” unshare all possible namespaces. Currently equivalent with: --unshare-user-try --unshare-ipc --unshare-pid --unshare-net --unshare-uts --unshare-cgroup-try. โœจ
  • --share-net โ€” retain the network namespace, overriding an earlier --unshare-all or --unshare-net. ๐ŸŒˆ
  • --userns FD โ€” use an existing user namespace instead of creating a new one. The namespace must fulfil the permission requirements for setns(), which generally means that it must be a descendant of the currently active user namespace, owned by the same user. ๐Ÿ”‘
  • --userns2 FD โ€” after setting up the new namespace, switch into the specified namespace. For this to work the specified namespace must be a descendant of the user namespace used for the setup, so this is only useful in combination with --userns.

This is useful because sometimes bubblewrap itself creates nested user namespaces (to work around some kernel issues) and --userns2 can be used to enter these. ๐ŸŒ€

  • --disable-userns โ€” prevent the process in the sandbox from creating further user namespaces, so that it cannot rearrange the filesystem namespace or do other more complex namespace modification. This is currently implemented by setting the user.max_user_namespaces sysctl to 1, and then entering a nested user namespace which is unable to raise that limit in the outer namespace. This option requires --unshare-user. ๐Ÿ”’
  • --assert-userns-disabled โ€” confirm that the process in the sandbox has been prevented from creating further user namespaces, but without taking any particular action to prevent that. For example, this can be combined with --userns to check that the given user namespace has already been set up to prevent the creation of further user namespaces. โœ…
  • --pidns FD โ€” use an existing pid namespace instead of creating one. This is often used with --userns, because the pid namespace must be owned by the same user namespace that bwrap uses.

Note that this can be combined with --unshare-pid, and in that case it means that the sandbox will be in its own pid namespace, which is a child of the passed in one. ๐Ÿงธ

  • --uid UID โ€” use a custom user id in the sandbox (requires --unshare-user). ๐Ÿชช
  • --gid GID โ€” use a custom group id in the sandbox (requires --unshare-user). ๐Ÿซถ
  • --hostname HOSTNAME โ€” use a custom hostname in the sandbox (requires --unshare-uts). ๐Ÿท๏ธ

๐ŸŒธ Options about environment setup ๐ŸŒธ

  • --chdir DIR โ€” change directory to DIR. ๐Ÿ“
  • --setenv VAR VALUE โ€” set an environment variable. ๐ŸŒฑ
  • --unsetenv VAR โ€” unset an environment variable. ๐Ÿƒ
  • --clearenv โ€” unset all environment variables, except for PWD and any that are subsequently set by --setenv. ๐Ÿงผ

๐Ÿฆ‹ Options for monitoring the sandbox from the outside ๐Ÿฆ‹

  • --lock-file DEST โ€” take a lock on DEST while the sandbox is running. This option can be used multiple times to take locks on multiple files. ๐Ÿ”
  • --sync-fd FD โ€” keep this file descriptor open while the sandbox is running. ๐Ÿ“ก

These are all operations that modify the filesystem directly, or mounts stuff in the filesystem. These are applied in the order they are given as arguments. ๐Ÿงš

Any missing parent directories that are required to create a specified destination are automatically created as needed. Their permissions are normally set to 0755 (rwxr-xr-x). However, if a --perms option is in effect, and it sets the permissions for group or other to zero, then newly-created parent directories will also have their corresponding permission set to zero. --size modifies the size of the created mount when preceding a --tmpfs action; --perms and --size can be combined. โœจ

  • --perms OCTAL โ€” this option does nothing on its own, and must be followed by one of the options that it affects. It sets the permissions for the next operation to OCTAL. Subsequent operations are not affected: for example, --perms 0700 --tmpfs /a --tmpfs /b will mount /a with permissions 0700, then return to the default permissions for /b. Note that --perms and --size can be combined: --perms 0700 --size 10485760 --tmpfs /s will apply permissions as well as a maximum size to the created tmpfs. ๐Ÿ”
  • --size BYTES โ€” this option does nothing on its own, and must be followed by --tmpfs. It sets the size in bytes for the next tmpfs. For example, --size 10485760 --tmpfs /tmp will create a tmpfs at /tmp of size 10MiB. Subsequent operations are not affected: for example, --size 10485760 --tmpfs /a --tmpfs /b will mount /a with size 10MiB, then return to the default size for /b. Note that --perms and --size can be combined: --size 10485760 --perms 0700 --tmpfs /s will apply permissions as well as a maximum size to the created tmpfs. ๐Ÿ“
  • --bind SRC DEST โ€” bind mount the host path SRC on DEST. ๐Ÿ”—
  • --bind-try SRC DEST โ€” equal to --bind but ignores non-existent SRC. ๐Ÿ™ˆ
  • --dev-bind SRC DEST โ€” bind mount the host path SRC on DEST, allowing device access. ๐ŸŽ›๏ธ
  • --dev-bind-try SRC DEST โ€” equal to --dev-bind but ignores non-existent SRC. ๐Ÿซง
  • --ro-bind SRC DEST โ€” bind mount the host path SRC readonly on DEST. ๐ŸงŠ
  • --ro-bind-try SRC DEST โ€” equal to --ro-bind but ignores non-existent SRC. ๐Ÿ“
  • --remount-ro DEST โ€” remount the path DEST as readonly. It works only on the specified mount point, without changing any other mount point under the specified path. ๐Ÿ”’
  • --overlay-src SRC โ€” this option does nothing on its own, and must be followed by one of the other overlay options. It specifies a host path from which files should be read if they aren't present in a higher layer.

This option can be used multiple times to provide multiple sources. The sources are overlaid in the order given, with the first source on the command line at the bottom of the stack: if a given path to be read exists in more than one source, the file is read from the last such source specified. ๐Ÿฅž

(For readers familiar with overlayfs, note that this is the reverse of the order used by the kernel's lowerdir mount option.) ๐Ÿ™ƒ

  • --overlay RWSRC WORKDIR DEST โ€” writable overlay vibes: writes go to RWSRC, reads prefer RWSRC, then the --overlay-src paths. WORKDIR must be an empty directory on the same filesystem as RWSRC, and is used internally by the kernel. ๐Ÿง
  • --tmp-overlay DEST โ€” overlay where writes go to the tmpfs that hosts the sandbox root, in a location not accessible from either the host or the child process. Writes will therefore not be persisted across multiple runs. ๐Ÿซง
  • --ro-overlay DEST โ€” overlay mounted read-only. This option requires at least two --overlay-src to precede it. ๐ŸงŠ

All three overlay options use overlayfs to mount the host paths specified by RWSRC and all immediately preceding --overlay-src on DEST. DEST will contain the union of all the files in all the layers. ๐ŸŒˆ

Using --ro-overlay or providing more than one --overlay-src requires a Linux kernel version of 4.0 or later. ๐Ÿง

overlayfs limitation alert ๐Ÿšจ due to limitations of overlayfs, no host directory given via --overlay-src or --overlay may be an ancestor of another, after resolving symlinks. Depending on version, the Linux kernel may or may not enforce this, but if not then overlayfs's behavior is undefined.

For more information see the Overlay Filesystem documentation in the Linux kernel at https://www.kernel.org/doc/Documentation/filesystems/overlayfs.txt. ๐Ÿ“š

  • --proc DEST โ€” mount procfs on DEST. ๐Ÿ—‚๏ธ
  • --dev DEST โ€” mount new devtmpfs on DEST. ๐ŸŽ›๏ธ
  • --tmpfs DEST โ€” mounts a brand-new tmpfs at DEST ๐Ÿ’– if the option right before it was --perms, that sets the mode of the tmpfs (cute!). Otherwise the tmpfs gets mode 0755. And if the previous option was --size, that sets the size in bytes of the tmpfs. Otherwise it just rocks the default size, no cap โœจ
  • --mqueue DEST โ€” mounts a fresh mqueue at DEST ๐ŸŽ€
  • --dir DEST โ€” makes a directory at DEST ๐Ÿฆ‹ real talk though: if the directory already exists, its permissions stay unmodified and --perms gets straight-up ignored (use --chmod if you actually need to change perms on a directory that's already there, iykyk). If it's newly created and the previous option was --perms, that sets the mode. Otherwise newly-created directories get mode 0755. periodt ๐Ÿ’…
  • --file FD DEST โ€” copies from the file descriptor FD to DEST ๐ŸŒธ if the previous option was --perms, it sets the mode of the new file. Otherwise the file gets mode 0666 (and yes, note that this is not the same as --bind-data, don't @ me ๐Ÿ˜ญ)
  • --bind-data FD DEST โ€” copies from the file descriptor FD to a file which is bind-mounted on DEST ๐Ÿ‘‘ if the previous option was --perms, it sets the mode of the new file. Otherwise the file gets mode 0600 (note that this is not the same as --file, they're literally different beasts ๐Ÿซถ)
  • --ro-bind-data FD DEST โ€” copies from the file descriptor FD to a file which is bind-mounted read-only on DEST ๐Ÿงš if the previous option was --perms, it sets the mode of the new file. Otherwise the file gets mode 0600 (again: note that this is not the same as --file โญ)
  • --symlink SRC DEST โ€” creates a symlink at DEST with target SRC ๐Ÿ’œ

Since version 0.9.0, it is not considered an error if DEST already exists as a symbolic link and its target is exactly SRC ๐ŸŽ‰

Before version 0.9.0, if DEST already existed, this would be treated as an error โ€” even if its target was identical to SRC ๐Ÿ’€ historic L tbh

  • --chmod OCTAL PATH โ€” sets the permissions of PATH (which must already exist, no exceptions bestie) to OCTAL ๐ŸŒˆ

๐Ÿ”’ Lockdown options ๐Ÿ”’

  • --seccomp FD โ€” loads and uses seccomp rules from FD ๐Ÿ›ก๏ธ the rules need to be in the form of a compiled cBPF program, as generated by seccomp_export_bpf. If this option is given more than once, only the last one is used (rude but true ๐Ÿ™ˆ). Use --add-seccomp-fd if multiple seccomp programs are needed.
  • --add-seccomp-fd FD โ€” loads and uses seccomp rules from FD โœจ rules need to be a compiled cBPF program, as generated by seccomp_export_bpf. This option can be repeated, in which case all the seccomp programs get loaded in the order given โ€” though heads up, the kernel evaluates them in reverse order, so the last program on the bwrap command-line is evaluated first ๐Ÿ”„. All of them, except possibly the last, must allow use of the PR_SET_SECCOMP prctl. Also: this option cannot be combined with --seccomp ๐Ÿšซ
  • --exec-label LABEL โ€” exec label from the sandbox ๐Ÿ’… on an SELinux system you can specify the SELinux context for the sandbox process(s). iconic.
  • --file-label LABEL โ€” file label for temporary sandbox content ๐ŸŽ€ on an SELinux system you can specify the SELinux context for the sandbox content.
  • --block-fd FD โ€” blocks the sandbox on reading from FD until some data is available โณ (the sandbox is literally waiting on you, main character energy)
  • --userns-block-fd FD โ€” does not initialize the user namespace but waits on FD until it is ready ๐Ÿฆ‹ this lets external processes (like newuidmap/newgidmap) set up the user namespace before it gets used by the sandbox process. teamwork fr fr.
  • --info-fd FD โ€” writes information in JSON format about the sandbox to FD ๐ŸŒธ
  • --json-status-fd FD โ€” multiple JSON documents are written to FD, one per line ("JSON lines" format[1] โœจ). Each line is a single JSON object. After bwrap has started the child process inside the sandbox, it writes an object with a child-pid member to the --json-status-fd (this duplicates the older --info-fd). The corresponding value is the process ID of the child process in the pid namespace from which bwrap was run. If available, the namespace IDs are also included in the object with the child-pid; again, this duplicates the older --info-fd. When the child process inside the sandbox exits, bwrap writes an object with an exit-code member, and then closes the --json-status-fd. The value corresponding to exit-code is the exit status of the child, in the usual shell encoding (n if it exited normally with status n, or 128+n if it was killed by signal n) ๐Ÿงฎ Other members may be added to those objects in future versions of bwrap, and other JSON objects may be added before or after the current objects โ€” so readers must ignore members and objects that they do not understand. no cap, forward-compatible or bust ๐Ÿ’œ
  • --new-session โ€” creates a new terminal session for the sandbox (calls setsid()) ๐Ÿ‘‘ this disconnects the sandbox from the controlling terminal, which means the sandbox can't for instance inject input into the terminal ๐Ÿ”’
Note: in a general sandbox, if you don't use --new-session, it is recommended to use seccomp to disallow the TIOCSTI ioctl ๐Ÿ˜ญ otherwise the application can feed keyboard input to the terminal which can e.g. lead to out-of-sandbox command execution (see CVE-2017-5226). that one is genuinely scary, don't skip it ๐Ÿ’€
  • --die-with-parent โ€” ensures the child process (COMMAND) dies when bwrap's parent dies ๐Ÿชฆ it kills (SIGKILL) all bwrap sandbox processes in sequence from parent to child, including the COMMAND process, when bwrap or bwrap's parent dies. See prctl, PR_SET_PDEATHSIG. loyal to the end fr ๐Ÿซถ
  • --as-pid-1 โ€” does not create a process with PID=1 in the sandbox to reap child processes ๐Ÿ™ˆ (so yes, you have to deal with the zombie reaping, good luck bestie)
  • --cap-add CAP โ€” adds the specified capability CAP, e.g. CAP_DAC_READ_SEARCH, when running as privileged user โœจ it accepts the special value ALL to add all the permitted caps ๐Ÿ‘‘
  • --cap-drop CAP โ€” drops the specified capability when running as privileged user ๐Ÿ”ฅ it accepts the special value ALL to drop all the caps. By default no caps are left in the sandboxed process. The --cap-add and --cap-drop options are processed in the order they are specified on the command line โ€” so please be careful about the order they are specified in โš ๏ธ (order matters, that's the whole vibe)
  • --not-a-security-boundary โ€” declares that this invocation of bwrap is not intended to create a security boundary between the sandbox and the host system ๐Ÿšง when this option is given, certain non-fatal sandbox setup failures (such as a subdirectory remount failing because an automounter did not respond in time) will produce a warning and be skipped, rather than causing bwrap to exit with an error. In future releases of bubblewrap the effect of this option might be extended to make other sandbox setup operations non-fatal ๐Ÿ‘€

This option is intended for callers such as xdg-dbus-proxy or Steam that use bwrap to adjust the filesystem layout for a process, but do not rely on it to create a security boundary ๐ŸŽฎ

Other operations that are fundamental to establishing the sandbox (creating namespaces, pivot_root, dropping capabilities) will still cause a hard failure regardless of this option ๐Ÿ’€ so no, this isn't a magic "ignore everything" button, bestie.

๐ŸŒธ ENVIRONMENT ๐ŸŒธ

  • HOME โ€” used as the cwd in the sandbox if --chdir has not been explicitly specified and the current cwd is not present inside the sandbox ๐Ÿ  the --setenv option can be used to override the value that is used here ๐Ÿ’–

โœจ EXIT STATUS โœจ

The bwrap command returns the exit status of the initial application process (pid 2 in the sandbox) ๐Ÿซถ so like, whatever your app said on the way out is what bwrap echoes back. periodt.


๐Ÿ“ NOTES ๐Ÿ“

  1. "JSON lines" format https://jsonlines.org/ ๐Ÿ”—

and that's a wrap on the bwrap(1) page, bestie ๐ŸŽ€ source is the Containers section, page BWRAP(1) โ€” go forth and sandbox responsibly, mwah ๐Ÿ’…โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from