これは、なにをしたくて書いたもの?
以前、debootstrapおよびmmemdebstrapを使ってローカル内にDebian・Ubuntu Linux環境を分離環境として作ってみました。
Ubuntu Linux 24.04 LTSでdebootstrapを使って、Debian・Ubuntu Linux環境を作ってみる - CLOVER🍀
Ubuntu Linux 24.04 LTSでmmdebstrapを使って、Debian・Ubuntu Linux環境を作ってみる - CLOVER🍀
もうひとつの環境分離として、Bubblewrapを試してみます。
Bubblewrap
BubblewrapのGitHubリポジトリーはこちら。
Bubblewrapを使うと、管理者権限、いわゆるroot権限なしでサンドボックス環境を作れるようです。サンドボックス環境では
OSやユーザーデータへのアクセスが制限されたり、PIDやネットワークも制限できるようになります。
Claude Codeのサンドボックス環境でも使われています。
Choose a sandbox environment - Claude Code Docs
これにはLinuxのuser namespaceという機能を使っています。
A capability set for user namespaces [LWN.net]
他の参考になりそうなエントリーはこちら。
第686回 Bubblewrap/bwrapを使って管理者権限なしで非特権コンテナーを作る | gihyo.jp
こちらを見ると、ですくとっぷ版のUbuntu LinuxであればデフォルトでBubblewrapはインストールされているようです。
Ubuntu Linuxでのmanページはこちら。コマンドとしては「bwrap」になるようです。
Ubuntu Manpage: bwrap - container setup utility
少し試してみましょう。
環境
今回の環境はこちら。Ubuntu Linux 24.04 LTSです。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.4 LTS Release: 24.04 Codename: noble $ uname -srvmpio Linux 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Bubblewrapを使ってみる
Bubblewrapはデスクトップ版のUbuntu Linuxにはインストールされているという話でした。
確認してみます。
$ bwrap --version bubblewrap 0.9.0 $ bwrap --help usage: bwrap [OPTIONS...] [--] COMMAND [ARGS...] --help Print this help --version Print version --args FD Parse NUL-separated args from FD --argv0 VALUE Set argv[0] to the value VALUE before running the program --unshare-all Unshare every namespace we support by default --share-net Retain the network namespace (can only combine with --unshare-all) --unshare-user Create new user namespace (may be automatically implied if not setuid) --unshare-user-try Create new user namespace if possible else continue by skipping it --unshare-ipc Create new ipc namespace --unshare-pid Create new pid namespace --unshare-net Create new network namespace --unshare-uts Create new uts namespace --unshare-cgroup Create new cgroup namespace --unshare-cgroup-try Create new cgroup namespace if possible else continue by skipping it --userns FD Use this user namespace (cannot combine with --unshare-user) --userns2 FD After setup switch to this user namespace, only useful with --userns --disable-userns Disable further use of user namespaces inside sandbox --assert-userns-disabled Fail unless further use of user namespace inside sandbox is disabled --pidns FD Use this pid namespace (as parent namespace if using --unshare-pid) --uid UID Custom uid in the sandbox (requires --unshare-user or --userns) --gid GID Custom gid in the sandbox (requires --unshare-user or --userns) --hostname NAME Custom hostname in the sandbox (requires --unshare-uts) --chdir DIR Change directory to DIR --clearenv Unset all environment variables --setenv VAR VALUE Set an environment variable --unsetenv VAR Unset an environment variable --lock-file DEST Take a lock on DEST while sandbox is running --sync-fd FD Keep this fd open while sandbox is running --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 --bind-fd FD DEST Bind open directory or path fd on DEST --ro-bind-fd FD DEST Bind open directory or path fd read-only on DEST --remount-ro DEST Remount DEST as readonly; does not recursively remount --exec-label LABEL Exec label for the sandbox --file-label LABEL File label for temporary sandbox content --proc DEST Mount new procfs on DEST --dev DEST Mount new dev on DEST --tmpfs DEST Mount new tmpfs on DEST --mqueue DEST Mount new mqueue on DEST --dir DEST Create dir at DEST --file FD DEST Copy from FD to destination DEST --bind-data FD DEST Copy from FD to file which is bind-mounted on DEST --ro-bind-data FD DEST Copy from FD to file which is readonly bind-mounted on DEST --symlink SRC DEST Create symlink at DEST with target SRC --seccomp FD Load and use seccomp rules from FD (not repeatable) --add-seccomp-fd FD Load and use seccomp rules from FD (repeatable) --block-fd FD Block on FD until some data to read is available --userns-block-fd FD Block on FD until the user namespace is ready --info-fd FD Write information about the running container to FD --json-status-fd FD Write container status to FD as multiple JSON documents --new-session Create a new terminal session --die-with-parent Kills with SIGKILL child process (COMMAND) when bwrap or bwrap's parent dies. --as-pid-1 Do not install a reaper process with PID=1 --cap-add CAP Add cap CAP when running as privileged user --cap-drop CAP Drop cap CAP when running as privileged user --perms OCTAL Set permissions of next argument (--bind-data, --file, etc.) --size BYTES Set size of next argument (only for --tmpfs) --chmod OCTAL PATH Change permissions of PATH (must already exist)
確かにインストールされています。
パッケージの情報を確認。
$ apt show bubblewrap Package: bubblewrap Version: 0.9.0-1ubuntu0.1 Priority: optional Section: admin Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Utopia Maintenance Team <pkg-utopia-maintainers@lists.alioth.debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 133 kB Depends: libc6 (>= 2.38), libcap2 (>= 1:2.10), libselinux1 (>= 3.1~) Recommends: procps Homepage: https://github.com/containers/bubblewrap Task: ubuntu-desktop-minimal, ubuntu-desktop, ubuntu-desktop-raspi, kubuntu-desktop, xubuntu-minimal, xubuntu-desktop, lubuntu-desktop, ubuntustudio-desktop-core, ubuntustudio-desktop, ubuntukylin-desktop, ubuntukylin-desktop-minimal, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop-minimal, ubuntu-budgie-desktop, ubuntu-budgie-desktop-raspi, ubuntu-unity-desktop, edubuntu-desktop-gnome-minimal, edubuntu-desktop-gnome-raspi, ubuntucinnamon-desktop-minimal, ubuntucinnamon-desktop-raspi Download-Size: 50.2 kB APT-Manual-Installed: no APT-Sources: http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages Description: utility for unprivileged chroot and namespace manipulation bubblewrap uses Linux namespaces to launch unprivileged containers. These containers can be used to sandbox semi-trusted applications such as Flatpak apps, image/video thumbnailers and web browser components, or to run programs in a different library stack such as a Flatpak runtime or a different Debian release. . By default, this package relies on a kernel with user namespaces enabled. Official Debian and Ubuntu kernels are suitable. . On kernels without user namespaces, system administrators can make the bwrap executable setuid root, allowing it to create unprivileged containers even though ordinary user processes cannot. N: 追加レコードが 1 件あります。表示するには '-a' スイッチを付けてください。
GitHubのREADME.mdに従ってコマンドを実行してみます。
$ bwrap \ --ro-bind /usr /usr \ --symlink usr/lib64 /lib64 \ --proc /proc \ --dev /dev \ --unshare-pid \ --new-session \ bash
すると、権限でエラーになりました。
bwrap: setting up uid map: Permission denied
sudo journalctl -fで見てみると、こういう感じになりどうもAppArmorに阻まれているようです。
7月 11 18:09:57 ubuntu-desktop kernel: audit: type=1400 audit(1783760997.703:171): apparmor="AUDIT" operation="userns_create" class="namespace" info="Userns create - transitioning profile" profile="unconfined" pid=15068 comm="bwrap" requested="userns_create" target="unprivileged_userns" 7月 11 18:09:57 ubuntu-desktop kernel: audit: type=1400 audit(1783760997.704:172): apparmor="DENIED" operation="open" class="file" info="Failed name lookup - disconnected path" error=-13 profile="unprivileged_userns" name="proc/15069/uid_map" pid=15069 comm="bwrap" requested_mask="wr" denied_mask="wr" fsuid=1000 ouid=1000
こちらを見ながら、Bubblewrap向けにAppArmorの設定を追加。
/etc/apparmor.d/bwrap
abi <abi/4.0>,
include <tunables/global>
profile bwrap /usr/bin/bwrap flags=(unconfined) {
userns,
# Site-specific additions and overrides. See local/README for details.
include if exists <local/bwrap>
}
AppArmorを再起動。
$ sudo systemctl restart apparmor
警告が出ていますが、動くようになりました。
$ bwrap \ --ro-bind /usr /usr \ --symlink usr/lib64 /lib64 \ --proc /proc \ --dev /dev \ --unshare-pid \ --new-session \ bash bash: 端末プロセスグループを設定できません (1): デバイスに対する不適切なioctlです bash: このシェルではジョブ制御が無効になっています bash-5.2$
あと、こちらも設定しておいた方がよさそうなのですが、自分の環境では特に変わりませんでした…。
$ sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=1 $ sudo sysctl -w kernel.unprivileged_userns_clone=1
指定したオプションの意味を見てみましょう。
最終的にはnamespaceを作成してBashを起動します。
ホスト側の/usrを/usrに読み取り専用でマウントします。
--ro-bind /usr /usr \
usr/lib64からlib64へシンボリックリンクを作成します。
--symlink usr/lib64 /lib64 \
/procをprocfsとしてマウントします。
--proc /proc \
/devをdevtmpfsとしてマウントします。
--dev /dev \
新しいPID namespaceを作成します。
--unshare-pid \
サンドボックスに新しいターミナルセッションを作成します。
--new-session \
これで環境分離ができるにはできたのですが、すぐにターミナルの挙動がおかしくなるのでこれくらいにしておきます…。
Bubblewrapがすでにインストール済みという、意外と身近な存在でちょっと驚きました。