summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/sandbox/keeper.py
blob: 69c959cb81f2d926f6e670d6bc297f540bf5cb69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""The supervisor's side of a sandbox on this machine: the keeper process and its messages."""

from __future__ import annotations

import os
import pwd
import secrets
import socket
import subprocess
import sys
from contextlib import suppress
from typing import TYPE_CHECKING, final

from .. import plugin
from . import kernel, landlock, seccomp, streams
from .kernel import Prctl
from .landlock import Access
from .plan import SYSTEM_PATH, Message, SandboxError, receive, send_plan

if TYPE_CHECKING:
    from collections.abc import Generator

    from .plan import Plan


def _connect(*, sudo: bool) -> tuple[subprocess.Popen[bytes], socket.socket]:
    """Start the keeper, with none of this process's environment, and take its call. Its socket has
    no path that another user can change. Any user can read its name, so a caller with another
    user id is turned away."""
    name = f"claude-sandbox-{secrets.token_hex(16)}"
    program = str(plugin.ROOT / "scripts/sandbox_keeper.py")
    keeper = (sys.executable, "-ISB", "-X", "utf8", program, name)
    keeper_user = 0 if sudo else os.geteuid()
    with socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) as listener:
        listener.bind(f"\0{name}")
        listener.listen(1)
        listener.settimeout(0.2)
        job = subprocess.Popen(
            ("sudo", "--", *keeper) if sudo else keeper,
            cwd="/",
            env={"PATH": os.pathsep.join(SYSTEM_PATH)},
            stdin=subprocess.DEVNULL,
            stdout=subprocess.DEVNULL,
        )
        while job.poll() is None:
            with suppress(socket.timeout):
                call = listener.accept()[0]
                if kernel.peer_uid(call) == keeper_user:
                    return job, call
                call.close()
    msg = f"the sandbox's keeper did not start (exit {job.returncode})"
    raise SandboxError(msg)


def user(name: str) -> pwd.struct_passwd:
    """The entry of the user `name`. ValueError if there is none."""
    try:
        return pwd.getpwnam(name)
    except KeyError as error:
        raise ValueError(name) from error


def harden() -> None:
    """Go on behind the sandbox's system call filter, with no new privileges and not dumpable. No
    thread may be running."""
    kernel.prctl(Prctl.NO_NEW_PRIVILEGES, 1)
    # This process holds the credential: no core file and no tracer may read it.
    kernel.prctl(Prctl.DUMPABLE, 0)
    seccomp.install()


def become(entry: pwd.struct_passwd, out: str) -> None:
    """Go on as the user of `entry` for good, hardened, with Landlock's rights to write only below
    `out` and at /dev/null. Needs root and Landlock version 3, and no thread may be running."""
    if landlock.version() < landlock.TRUNCATE_SINCE:
        msg = f"--user needs Landlock version {landlock.TRUNCATE_SINCE} (Linux 6.2) or newer"
        raise SandboxError(msg)
    kernel.prctl(Prctl.NO_NEW_PRIVILEGES, 1)
    landlock.restrict({
        "/": Access.READ,
        "/dev/null": Access.DEVICE,
        out: Access.WRITE,
    })
    os.setgroups([])
    os.setresgid(entry.pw_gid, entry.pw_gid, entry.pw_gid)
    os.setresuid(entry.pw_uid, entry.pw_uid, entry.pw_uid)
    harden()


class Door(socket.socket):
    """The outer end of the socket on which process 1 hands over the calls that it lets in."""

    def accept(self) -> tuple[socket.socket, None]:  # pyright: ignore[reportImplicitOverride]
        """The next call. EOFError once an end is shut and no call waits, OSError if a call lost
        its socket."""
        try:
            while message := receive(self):
                if message.files:
                    return socket.socket(fileno=message.files[0]), None
                streams.say("refused a call that was not shown to be of the command's own process")
        except SandboxError as error:
            raise OSError(str(error)) from error
        raise EOFError


@final
class Keeper:
    """Builds the sandbox of `plan`."""

    def __init__(self, plan: Plan, *, sudo: bool) -> None:
        self._plan: Plan = plan
        self._job, self._channel = _connect(sudo=sudo)
        kernel.raise_file_limit()

    def messages(self) -> Generator[Message, None, None]:
        with suppress(BrokenPipeError):
            send_plan(self._channel, self._plan)
        try:
            while message := self._received():
                yield message
        except SandboxError as error:
            yield Message("error", str(error), [])

    def _received(self) -> Message | None:
        try:
            return receive(self._channel)
        except ConnectionResetError:
            return receive(self._channel)

    def stop(self) -> None:
        """End the command. Its reports still come."""
        with suppress(OSError):
            self._channel.shutdown(socket.SHUT_WR)

    def wait(self) -> int:
        self._channel.close()
        return self._job.wait()