fix(discord-agent-workspace): confine the bot with bwrap in a user unit, not ProtectHome=
Where the user manager can't mount (Ubuntu's kernel.apparmor_restrict_unprivileged_userns=1), ProtectHome= and friends are dropped without an error and the service runs unconfined. Ship a generic unit, as cloros1 runs: tmux for /login, bwrap for the confinement. Generated-by: Claude Opus 5.5
This commit is contained in:
1 parent
cf83169064
commit
e16e856eca
2 files changed
+48
-1
No files matched your search
@@ -62,7 +62,11 @@ Whoever can message the bot can make it act with the session's powers: files, sh
|
|||||||
|
|
||||||
## 4. Keeping it running
|
## 4. Keeping it running
|
||||||
|
|
||||||
The session must stay open. Simplest: run it inside `tmux` (`tmux new -s bot`, start Claude, detach with Ctrl-b d; `tmux attach -t bot` to look in). On Linux, a systemd user unit can start it at boot and keep it out of secrets it doesn't need (`ProtectHome=read-only`, `ReadWritePaths=` the workspace and `~/.claude`, `InaccessiblePaths=` `~/.ssh` and other credential dirs).
|
The session must stay open. Simplest: run it inside `tmux` (`tmux new -s bot`, start Claude, detach with Ctrl-b d; `tmux attach -t bot` to look in). An expired login shows only in that terminal, never on Discord, so that's where `/login` happens.
|
||||||
|
|
||||||
|
On Linux, a systemd user unit can start it at boot and confine it: [claude-bot.service](claude-bot.service) runs Claude in tmux under `bwrap`, with everything read-only but the workspace, `~/.claude`, `~/.claude.json` and `~/.cache`, and credential dirs hidden. Hiding `$XDG_RUNTIME_DIR` also hides the systemd and D-Bus sockets, so `systemd-run` can't escape. Add `loginctl enable-linger` so it runs while they're logged out.
|
||||||
|
|
||||||
|
Use bwrap, not `ProtectHome=`, `ReadWritePaths=` and friends: where the user manager can't mount (Ubuntu's `kernel.apparmor_restrict_unprivileged_userns=1`), it silently runs the service unconfined. Check the live process: if `readlink /proc/<pid>/ns/mnt` matches the user's shell, it isn't confined.
|
||||||
|
|
||||||
## 5. First task
|
## 5. First task
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# claude-bot: Claude Code Discord bot, confined to one workspace folder.
|
||||||
|
# Install: ~/.config/systemd/user/claude-bot.service; systemctl --user enable --now claude-bot
|
||||||
|
# loginctl enable-linger $USER (so it runs without you logged in)
|
||||||
|
# Attach: tmux -S /run/user/$UID/claude-bot/tmux.sock attach (detach: C-b d; /login happens here)
|
||||||
|
|
||||||
|
[Unit]
|
||||||
|
Description=Claude Code Discord bot
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=forking
|
||||||
|
WorkingDirectory=%h/workspace
|
||||||
|
Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
|
||||||
|
Environment=TERM=xterm-256color
|
||||||
|
RuntimeDirectory=claude-bot
|
||||||
|
|
||||||
|
# Optional: GH_TOKEN=<a fine-grained token of the bot's own, for just the repos it needs>
|
||||||
|
EnvironmentFile=-%h/.config/claude-bot/env
|
||||||
|
|
||||||
|
# The user manager has no network-online.target.
|
||||||
|
ExecStartPre=/bin/sh -c 'until getent hosts gateway.discord.gg >/dev/null; do sleep 2; done'
|
||||||
|
|
||||||
|
# bwrap, not ProtectHome= and friends: on Ubuntu (kernel.apparmor_restrict_unprivileged_userns=1)
|
||||||
|
# the user manager can't mount, and silently runs the service unconfined.
|
||||||
|
# Everything is read-only except the --bind paths; --tmpfs hides credential dirs.
|
||||||
|
# Hiding %t removes the systemd and D-Bus sockets (systemd-run would escape) and the ssh/gpg agents.
|
||||||
|
ExecStart=/usr/bin/tmux -S %t/claude-bot/tmux.sock new-session -d -s claude-bot \
|
||||||
|
/usr/bin/bwrap --die-with-parent \
|
||||||
|
--ro-bind / / --dev /dev --proc /proc --tmpfs /tmp --tmpfs %t \
|
||||||
|
--bind %h/workspace %h/workspace \
|
||||||
|
--bind %h/.claude %h/.claude \
|
||||||
|
--bind %h/.claude.json %h/.claude.json \
|
||||||
|
--bind %h/.cache %h/.cache \
|
||||||
|
--tmpfs %h/.ssh --tmpfs %h/.gnupg --tmpfs %h/.config/gh \
|
||||||
|
--ro-bind /dev/null %h/.git-credentials \
|
||||||
|
claude -n claude-bot --permission-mode auto --channels plugin:discord@claude-plugins-official
|
||||||
|
ExecStop=-/usr/bin/tmux -S %t/claude-bot/tmux.sock kill-server
|
||||||
|
|
||||||
|
# tmux exits 0 when claude exits, so on-failure would never fire.
|
||||||
|
Restart=always
|
||||||
|
RestartSec=30
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
Reference in new issue
Block a user