Isolating Untrusted Proprietary Binaries Using Bubblewrap and Systemd User Units

Privacy & Security10 min

A standalone proprietary binary belongs directly on the host inside a rootless bubblewrap user namespace that receives three private directories and no session bus, started only by a systemd user unit. Under this architecture, the binary never receives the real home directory, never connects to the real session bus, and never runs outside the control group of that user unit. Anything launched from an interactive shell, a desktop launcher, or an autostart directory bypasses that process boundary.

A Rootless Jail Belongs on the Host for One Closed Binary

Keeping a virtual machine or a long-lived container around for a single closed executable leaves a writable disk image and a broad mount table alive between runs. Every time the container restarts, whatever state the executable wrote on its previous invocation is still sitting there waiting for it. A bubblewrap user namespace constructs its root filesystem in memory at invocation time. The jail is empty until the systemd user unit starts and vanishes completely the instant the unit stops.

Administrators often treat a familiar vendor's closed utility as benign and wrap it only to tidy up dotfile clutter. Isolation requires the opposite posture. You treat the binary as an untrusted guest that gets a read-only view of system libraries, an isolated scratchpad for the exact files it must write, and zero visibility into your personal files or active desktop session.

Bubblewrap Isolation Boundary Reference
Resource Category Host Path / Source In-Jail Target Access Mode & Policy
System Binaries & Libraries /usr, /bin, /sbin, /lib, /lib64 /usr, /bin, /sbin, /lib, /lib64 Read-only (--ro-bind / --ro-bind-try with usr-merge symlinks)
Vendor Application /opt/vendor /opt/vendor Read-only (--ro-bind)
User Configuration & Cache $XDG_DATA_HOME/jails/<name>/{config,data,cache} /cfg, /data, /cache Read-write (--bind, mode 0700, replaces real $HOME)
Runtime Directory tmpfs at /run /run/user-jail (exported as XDG_RUNTIME_DIR) Ephemeral read-write (mode 0700)
D-Bus Session Socket $JAIL_RUNTIME/bus (via xdg-dbus-proxy --filter) $XDG_RUNTIME_DIR/bus Default-deny proxy bind (omitted unless binary fails without bus)
Display & Real Home $HOME, /tmp/.X11-unix, Wayland socket None Unmounted and excluded

The Shortest bwrap Line That Still Executes the File

Start with a single command you can paste into a shell to verify the mount layout, then tighten it into a wrapper script. The invocation opens with bwrap --unshare-all --die-with-parent --new-session. Unsharing all namespaces detaches user, mount, PID, IPC, UTS, and network views at once, while --new-session prevents the jailed binary from injecting keystrokes back into a controlling terminal via the TIOCSTI ioctl. As documented in the bwrap manual, --die-with-parent kills the sandboxed command when bwrap's parent dies, and --unshare-user maps UID 0 inside the namespace to the unprivileged calling user's UID on the host.

Next comes a read-only system view that works cleanly across both merged-usr distributions (like Fedora or Arch) and split-usr layouts. Bind /usr unconditionally with --ro-bind /usr /usr. Pair --ro-bind-try for /bin, /sbin, /lib, and /lib64 with conditional symlink flags only when those top-level paths are symlinks on the host, or let --ro-bind-try handle the resolution directly while ensuring standard loader paths exist:

bwrap \
  --unshare-all \
  --die-with-parent \
  --new-session \
  --ro-bind /usr /usr \
  --ro-bind-try /bin /bin \
  --ro-bind-try /sbin /sbin \
  --ro-bind-try /lib /lib \
  --ro-bind-try /lib64 /lib64 \
  --proc /proc \
  --dev /dev \
  --tmpfs /tmp \
  --tmpfs /run \
  --ro-bind /opt/vendor /opt/vendor \
  --clearenv \
  --setenv PATH /usr/bin:/bin \
  --setenv LANG C.UTF-8 \
  -- /opt/vendor/bin/closed-tool

Add --proc /proc for a fresh procfs reflecting only the unshared PID namespace, --dev /dev for standard character devices like /dev/null and /dev/urandom, plus --tmpfs /tmp and --tmpfs /run. Do not mount the real home directory, the host /run/user tree, /tmp/.X11-unix, or the Wayland socket. Any X11 client connected to /tmp/.X11-unix can observe keystrokes and window contents across the entire display; that socket stays out. Passing a Wayland socket is a separate, later decision for GUI tools and remains off in this baseline.

Kernel User Namespace Restrictions

If your kernel refuses unprivileged user namespace creation, install your distribution's packaged bubblewrap binary so it uses its audited setuid root helper. Never run the proprietary binary unsandboxed as a fallback, and never pass --uid of a different host account to work around namespace limits.

Before running the vendor binary after --, test the mount geometry by substituting /bin/true after --. When that command exits with status 0, the dynamic linker and base libraries are reachable inside the jail.

Kernel User Namespace Restrictions

Private XDG Directories Instead of a Bind of Home

Closed binaries almost always demand a writable place to drop configuration state, license tokens, or temporary cache blobs. Before the first launch, create a dedicated host tree at $XDG_DATA_HOME/jails/<name>/{config,data,cache} with mode 0700. I create those directories myself; I do not point them at ~/.config, ~/.local/share, or ~/.cache.

JAIL_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/jails/vendor-app"
mkdir -m 0700 -p "$JAIL_ROOT/config" "$JAIL_ROOT/data" "$JAIL_ROOT/cache" "$JAIL_ROOT/home"

Inside the bubblewrap invocation, bind each directory to a short in-jail path and export the matching XDG environment variable:

  • --bind "$JAIL_ROOT/config" /cfg paired with --setenv XDG_CONFIG_HOME /cfg
  • --bind "$JAIL_ROOT/data" /data paired with --setenv XDG_DATA_HOME /data
  • --bind "$JAIL_ROOT/cache" /cache paired with --setenv XDG_CACHE_HOME /cache
  • --dir /run/user-jail and --chmod 0700 /run/user-jail paired with --setenv XDG_RUNTIME_DIR /run/user-jail

Because /run is already a fresh tmpfs inside the namespace, creating a mode 0700 subdirectory there gives the binary a private runtime location for any internal UNIX sockets or lockfiles it creates during execution.

If the binary ignores the XDG specification and queries $HOME directly or reads getpwuid(), bind $JAIL_ROOT/home to /home/sandbox and pass --setenv HOME /home/sandbox. Copy in only a single configuration or license file that the binary has already proven it must read to start. Never bind your real $HOME, ~/.ssh, ~/.gnupg, or ~/Downloads.

Verify the environment binding before invoking the proprietary tool by running a quick check after --:

-- /bin/sh -c 'ls -ld "$HOME" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"'

Confirm that all three paths resolve to /home/sandbox, /cfg, and /cache. If a closed command-line tool writes a license token under XDG_CONFIG_HOME and cached data under XDG_CACHE_HOME, only those directories need to stay writable. If it later attempts to write outside those bounds and fails, stop the run and inspect what it wanted instead of widening the bind mount into your real home directory.

A Filtered Proxy Socket Instead of the Session Bus

The baseline posture for D-Bus is no bus at all. Launch the jail with the private XDG_RUNTIME_DIR and omit DBUS_SESSION_BUS_ADDRESS completely. Many command-line tools and background helpers initialize cleanly without ever touching inter-process communication. You add a proxy only when the binary aborts immediately on startup because it cannot open $XDG_RUNTIME_DIR/bus.

When a bus socket is required, never bind-mount the host's real $XDG_RUNTIME_DIR/bus into the container. Unfiltered access to the user session bus allows a confined process to talk to org.freedesktop.systemd1 and spawn arbitrary unpacked processes on the host. For a binary that aborts because it cannot open $XDG_RUNTIME_DIR/bus, the wrapper script runs xdg-dbus-proxy --filter on the host before calling bwrap. The proxy socket at $JAIL_RUNTIME/bus is bound to the jail's $XDG_RUNTIME_DIR/bus; the real session bus socket stays outside.

#!/bin/sh
set -eu

JAIL_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/jails/vendor-app"
JAIL_RUNTIME="${XDG_RUNTIME_DIR}/vendor-app-proxy"
mkdir -m 0700 -p "$JAIL_RUNTIME"

xdg-dbus-proxy \
  "unix:path=${XDG_RUNTIME_DIR}/bus" \
  "${JAIL_RUNTIME}/bus" \
  --filter \
  --talk=org.freedesktop.Notifications &
PROXY_PID=$!

trap 'kill "$PROXY_PID" 2>/dev/null || true' EXIT INT TERM

while [ ! -S "${JAIL_RUNTIME}/bus" ]; do
  sleep 0.05
done

bwrap \
  --unshare-all \
  --die-with-parent \
  --new-session \
  --ro-bind /usr /usr \
  --ro-bind-try /bin /bin \
  --ro-bind-try /sbin /sbin \
  --ro-bind-try /lib /lib \
  --ro-bind-try /lib64 /lib64 \
  --proc /proc \
  --dev /dev \
  --tmpfs /tmp \
  --tmpfs /run \
  --dir /run/user-jail \
  --chmod 0700 /run/user-jail \
  --ro-bind "${JAIL_RUNTIME}/bus" /run/user-jail/bus \
  --bind "$JAIL_ROOT/config" /cfg \
  --bind "$JAIL_ROOT/data" /data \
  --bind "$JAIL_ROOT/cache" /cache \
  --bind "$JAIL_ROOT/home" /home/sandbox \
  --ro-bind /opt/vendor /opt/vendor \
  --clearenv \
  --setenv PATH /usr/bin:/bin \
  --setenv LANG C.UTF-8 \
  --setenv HOME /home/sandbox \
  --setenv XDG_CONFIG_HOME /cfg \
  --setenv XDG_DATA_HOME /data \
  --setenv XDG_CACHE_HOME /cache \
  --setenv XDG_RUNTIME_DIR /run/user-jail \
  --setenv DBUS_SESSION_BUS_ADDRESS unix:path=/run/user-jail/bus \
  -- /opt/vendor/bin/closed-tool

Passing --filter switches xdg-dbus-proxy into default-deny mode: the binary can complete the initial bus handshake, which satisfies stubborn toolkit initialization routines, without seeing or calling any session services unless you explicitly list them. If a graphical utility only needs to post desktop alerts, add --talk=org.freedesktop.Notifications.

Forbidden D-Bus Portal Grants

Passing --talk=org.freedesktop.systemd1, granting --own on a portal name, or exposing the FileChooser or Documents portals to xdg-dbus-proxy lets the jailed binary execute host systemd units or read arbitrary host files outside your /cfg, /data, and /cache binds. If a closed binary refuses to run without a bus name that compromises the host session, drop the proxy and refuse to run the tool on the host.

The User Unit Is the Only Process Allowed to Start It

Save the wrapper script as ~/.local/bin/vendor-jail-launch with mode 0700, then hand complete control of its execution over to a systemd user unit at ~/.config/systemd/user/vendor-jail.service. Launching the script through systemd guarantees a clean environment, places both xdg-dbus-proxy and bwrap inside a dedicated cgroup slice, and routes all standard output and standard error straight to the journal.

The User Unit Is the Only Process Allowed to Start It
[Unit]
Description=Isolated Bubblewrap Jail for Vendor Binary

[Service]
Type=exec
ExecStart=%h/.local/bin/vendor-jail-launch
KillMode=control-group
Restart=no
NoNewPrivileges=yes

Set Type=exec, KillMode=control-group, and Restart=no. A crashing proprietary binary should never be restarted automatically by policy; if it faults or exits abnormally, you want the entire control group torn down immediately so you can inspect the failure in the journal. Setting NoNewPrivileges=yes ensures that neither the wrapper nor the payload can acquire elevated privileges through setuid bits inside the unit's lifecycle (note that if your distribution's bwrap relies on a setuid binary rather than unprivileged user namespaces, omit NoNewPrivileges=yes from the unit and rely on bwrap dropping privileges immediately after creating the namespace).

Be careful with systemd filesystem hardening directives that overlap with bubblewrap's setup phase. Enabling ProtectHome=yes in vendor-jail.service causes bubblewrap to fail before entering the user namespace unless ReadOnlyPaths=/opt/vendor and ReadWritePaths=%h/.local/share/jails/vendor-app are explicitly declared. Because bubblewrap opens the source paths on the host prior to unsharing the mount table, systemd's mount filter must permit access to those specific directories first.

Notice that the unit file omits an [Install] section and never declares WantedBy=default.target. An untrusted binary does not start at login. Reload the user manager and control the process explicitly when you actually need the tool:

systemctl --user daemon-reload
systemctl --user start vendor-jail.service
systemctl --user status vendor-jail.service
journalctl --user -u vendor-jail.service -b

When you finish your task, tear down the namespace and any helper proxy processes in one step with systemctl --user stop vendor-jail.service.

Test Whether This Binary May Open a Network Socket

With vendor-jail.service and the /cfg, /data, and /cache binds in place, you have one remaining choice to test against this binary: leave the network namespace isolated under --unshare-all, or append --share-net to the wrapper script.

Run systemctl --user start vendor-jail.service first with the network still unshared, where the only interface inside the namespace is an isolated loopback device. Read the output in journalctl --user -u vendor-jail.service -b. If the binary's job is local file conversion or offline analysis and it finishes cleanly, stop there. Do not add --share-net just because a later vendor release might try to phone home or check for updates.

If the binary's only job is an outbound HTTPS fetch and the journal shows a connection or DNS resolution error, edit ~/.local/bin/vendor-jail-launch to add --share-net along with read-only binds for resolver configuration and the host TLS certificate bundle:

  --share-net \
  --ro-bind /etc/resolv.conf /etc/resolv.conf \
  --ro-bind /etc/ssl/certs /etc/ssl/certs \
  --ro-bind-try /etc/ca-certificates /etc/ca-certificates \
  --ro-bind-try /etc/pki /etc/pki \

For example, suppose the outbound HTTPS fetch succeeds after these changes and systemctl --user restart vendor-jail.service, with the journal confirming a successful handshake. The fetch now works, but --share-net exposes the full host network stack and local LAN routing view rather than enforcing a per-host or per-port allow-list. For this binary, if that access is unacceptable and the fetch is required, the appropriate isolation boundary is a virtual machine rather than the host-level bubblewrap jail.

Stay Updated

Be the first to know.

We respect your privacy. No spam.

Your Thoughts

Nothing here yet. Add your opinion.

Write a Comment

Your cookie choices