From e16e856ecac7c9b1bfa6adde595837580beb8383 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Mon, 23 Nov 2026 15:40:13 -0600 Subject: [PATCH] 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 --- discord-agent-workspace/SKILL.md | 6 ++- discord-agent-workspace/claude-bot.service | 43 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 discord-agent-workspace/claude-bot.service diff --git a/discord-agent-workspace/SKILL.md b/discord-agent-workspace/SKILL.md index fa88aed..98c21b9 100644 --- a/discord-agent-workspace/SKILL.md +++ b/discord-agent-workspace/SKILL.md @@ -62,7 +62,11 @@ Whoever can message the bot can make it act with the session's powers: files, sh ## 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//ns/mnt` matches the user's shell, it isn't confined. ## 5. First task diff --git a/discord-agent-workspace/claude-bot.service b/discord-agent-workspace/claude-bot.service new file mode 100644 index 0000000..9ae58c6 --- /dev/null +++ b/discord-agent-workspace/claude-bot.service @@ -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= +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