docker_ownership.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. """Hand root-written sandbox files back to the host user when a root Docker container is removed.
  2. The Docker backend runs the image's default user (root) unless ``docker_run_as_host_user`` is set,
  3. so everything the container writes into the Hermes-created bind mounts (``<sandbox>/home`` at
  4. ``/root``, ``<sandbox>/workspace`` at ``/workspace``) lands on the host owned by uid 0, and the user
  5. cannot delete it. Until the container is removed, a root container with its volumes can ``chown``
  6. those trees back; once it is gone only ``sudo`` can. The handback runs after it stopped, so files a
  7. process writes on shutdown are included.
  8. Persistent sandbox dirs outlive their container, and root refuses some host-owned files: git stops
  9. at "dubious ownership", ssh at "Bad owner" on ``~/.ssh/config``. So the handback records the exact
  10. paths it changes beside the dirs (on the host, outside every mount), and the next container that
  11. mounts them gives exactly those paths back to root before its first command. Files the user added
  12. on the host in between keep their owner. Every walk is ``find -P`` (no symlinks followed) and prunes
  13. each mount Docker reports nested under the sandbox paths (read-only skills, credentials, caches,
  14. ``docker_volumes`` and ``docker_extra_args`` binds), so host data outside the sandbox dir is never touched.
  15. After the handback, a root pass strips setuid/setgid bits (``chmod ug-s``) from the sandbox files
  16. whatever their owner (a host-user-owned group-root setgid file runs with host egid 0). A container
  17. that could write file capabilities (SETFCAP, ALL, privileged) instead gets every regular file
  18. re-chowned to its own uid, which clears the bits and ``security.capability`` and changes no owner.
  19. A persistent container is never removed, so it gets this strip when a session detaches from it,
  20. before reattaching and before the stale-container sweep removes it. The strip reads the sandbox
  21. binds from the container itself (a container shared across profiles has its creator's dirs) but
  22. only keeps those under a sandbox root this host derives from its own Hermes root.
  23. """
  24. from __future__ import annotations
  25. import json
  26. import logging
  27. import os
  28. import re
  29. import secrets
  30. import subprocess
  31. import sys
  32. import time
  33. from pathlib import Path
  34. from typing import Optional
  35. from hermes_constants import get_default_hermes_root
  36. from tools.environments.base import get_sandbox_dir
  37. from tools.environments.remote_common import run_capture
  38. logger = logging.getLogger(__name__)
  39. # Bounded so a huge workspace cannot hold teardown past the atexit drain (15s). Paths are recorded
  40. # before any chown, so a timeout mid-chown never leaves a changed path unrecorded.
  41. _CHOWN_TIMEOUT_S = 10
  42. HANDBACK_RECORD = ".hermes-owner-handback"
  43. # Root runs agent-writable trees: name tools by absolute path (an image without them skips the pass)
  44. # and reset PATH and loader env so nothing planted under /root or /workspace runs instead.
  45. _SAFE_ENV = ["-e", "PATH=/usr/sbin:/usr/bin:/sbin:/bin", "-e", "LD_PRELOAD=", "-e", "LD_LIBRARY_PATH=", "-w", "/"]
  46. # The throwaway gets only what find and chown need, so even a planted binary gains no host power.
  47. _THROWAWAY_CONFINEMENT = ["--cap-drop", "ALL", "--cap-add", "CHOWN", "--cap-add", "DAC_OVERRIDE", "--cap-add", "FOWNER",
  48. "--security-opt", "no-new-privileges", "--pids-limit", "64", "--label", "hermes-handback=1"]
  49. _remapping_cache: dict[str, bool] = {}
  50. def _daemon_remaps_users(docker_exe: str) -> bool:
  51. """True when container uid 0 is not host uid 0 (rootless Docker, userns-remap) or when that
  52. cannot be told. Chowning to the host uid there maps to a subordinate id and makes the files
  53. less reachable, so the handback is skipped. Cached per executable for the process."""
  54. if docker_exe not in _remapping_cache:
  55. try:
  56. result = run_capture([docker_exe, "info", "--format", "{{.SecurityOptions}}"], timeout=10)
  57. options = result.stdout.lower() if result.returncode == 0 else ""
  58. except (subprocess.TimeoutExpired, OSError) as e:
  59. logger.debug("docker info for ownership handback failed: %s", e)
  60. options = ""
  61. _remapping_cache[docker_exe] = not options.strip() or "rootless" in options or "userns" in options
  62. return _remapping_cache[docker_exe]
  63. def _handback_ids(docker_exe: str) -> Optional[tuple[int, int]]:
  64. """Host ``(uid, gid)`` to hand sandbox files to, or ``None`` when no handback applies: the
  65. host is not Linux (Docker Desktop bind mounts are not root-owned on the host), Hermes itself
  66. runs as root, or the daemon remaps users."""
  67. if sys.platform != "linux" or os.geteuid() == 0: # windows-footgun: ok — linux-gated
  68. return None
  69. return None if _daemon_remaps_users(docker_exe) else (os.geteuid(), os.getegid()) # windows-footgun: ok
  70. def _record_path(mounts: list[tuple[str, str]]) -> Optional[Path]:
  71. """The record lives in the per-task sandbox dir, the parent of every Hermes bind source."""
  72. return Path(mounts[0][0]).parent / HANDBACK_RECORD if mounts else None
  73. def _inspect(docker_exe: str, container_id: str) -> Optional[dict]:
  74. """The container's ``docker inspect`` record (its real user, image id and every mount,
  75. ``docker_extra_args`` binds and named volumes included), or ``None`` when Docker cannot say
  76. (then nothing is walked: a same-filesystem bind is invisible to ``find -xdev``)."""
  77. try:
  78. result = run_capture([docker_exe, "inspect", container_id], timeout=10)
  79. if result.returncode == 0:
  80. return json.loads(result.stdout)[0]
  81. except (subprocess.TimeoutExpired, OSError, ValueError, IndexError, KeyError) as e:
  82. logger.debug("docker inspect of %s failed: %s", container_id[:12], e)
  83. return None
  84. def _runs_as_root(info: dict) -> bool:
  85. """The container's effective user (``--user`` from any source, else the image ``USER``) is root."""
  86. return ((info.get("Config") or {}).get("User") or "").split(":")[0] in ("", "root", "0")
  87. def _may_set_file_caps(info: dict) -> bool:
  88. """Only a container granted SETFCAP (or privileged) can write file capabilities; the base
  89. args drop it. ``find`` cannot test xattrs, so then every regular file is re-chowned."""
  90. host = info.get("HostConfig") or {}
  91. return bool(host.get("Privileged")) or any(
  92. c.upper().removeprefix("CAP_") in ("SETFCAP", "ALL") for c in host.get("CapAdd") or [])
  93. def _sandbox_binds(info: dict) -> list[tuple[str, str]]:
  94. """The container's Hermes sandbox binds (``<root>/<task>/{home,workspace}`` at ``/root`` and
  95. ``/workspace``) as ``(realpath, destination)``. *root* is derived on this host only, never from
  96. container metadata (labels and binds are settable via extra args or the image): this process's
  97. ``<sandbox>/docker`` or the default one of any profile of this Hermes root (shared containers)."""
  98. hermes_root = get_default_hermes_root()
  99. roots = {os.path.realpath(d / "sandboxes" / "docker") for d in [hermes_root, *hermes_root.glob("profiles/*")]}
  100. roots.add(os.path.realpath(get_sandbox_dir() / "docker"))
  101. binds = [(os.path.realpath(m.get("Source") or "/nonexistent"), m.get("Destination")) for m in info.get("Mounts") or []
  102. if m.get("Type") == "bind" and m.get("Destination") in ("/root", "/workspace")]
  103. return [(src, dst) for src, dst in binds if os.path.isdir(src) and os.path.dirname(os.path.dirname(src)) in roots]
  104. def _owned(owner: int) -> list[str]:
  105. # Device nodes and FIFOs stay root's: a host-owned block device is raw disk access.
  106. return ["(", "-type", "f", "-o", "-type", "d", "-o", "-type", "l", ")", "-user", str(owner), "-print0"]
  107. def walk_argv(mounts: list[tuple[str, str]], mount_points: list[str], tail: list[str]) -> list[str]:
  108. """``find`` argv applying *tail* to every entry under the sandbox mounts. It never follows
  109. symlinks (``-P``) and prunes each mount point nested below them."""
  110. targets = [container for _, container in mounts]
  111. prune: list[str] = []
  112. for point in sorted({p.rstrip("/") for p in mount_points}):
  113. if any(point.startswith(t.rstrip("/") + "/") for t in targets):
  114. prune += [*(["-o"] if prune else []), "-path", re.sub(r"([][*?\\])", r"\\\1", point)]
  115. return ["/usr/bin/find", "-P", *targets, "-xdev", *(["(", *prune, ")", "-prune", "-o"] if prune else []), *tail]
  116. def _walk(docker_exe: str, target: list[str], mounts: list[tuple[str, str]], info: dict, owner: int,
  117. deadline: float) -> Optional[list[bytes]]:
  118. """Entries owned by *owner*, or ``None`` when the walk failed (its output may be partial)."""
  119. points = [m["Destination"] for m in info.get("Mounts") or []]
  120. result = _docker(docker_exe, target, walk_argv(mounts, points, _owned(owner)), deadline)
  121. if result.returncode != 0:
  122. logger.debug("ownership walk exited %d: %s", result.returncode,
  123. result.stderr.decode("utf-8", "replace").strip()[:500])
  124. return None
  125. return [p for p in result.stdout.split(b"\0") if p]
  126. def _docker(docker_exe: str, target: list[str], argv: list[str], deadline: float, **kwargs):
  127. """Run *argv* in *target*. A timeout only kills the CLI, so a named throwaway is removed then
  128. instead of running on with the sandbox volumes."""
  129. try:
  130. stdin = kwargs.pop("stdin", None if "input" in kwargs else subprocess.DEVNULL)
  131. return subprocess.run([docker_exe, *target, *argv], capture_output=True, stdin=stdin,
  132. timeout=max(1.0, deadline - time.monotonic()), check=False, **kwargs)
  133. except subprocess.TimeoutExpired:
  134. if "--name" in target:
  135. run_capture([docker_exe, "rm", "-f", target[target.index("--name") + 1]], timeout=10)
  136. raise
  137. def _chown(docker_exe: str, target: list[str], paths: list[bytes], owner: str, deadline: float) -> bool:
  138. result = _docker(docker_exe, target, ["/usr/bin/xargs", "-0", "-r", "/bin/chown", "-h", owner, "--"], deadline,
  139. input=b"".join(p + b"\0" for p in paths))
  140. if result.returncode != 0:
  141. logger.debug("ownership chown to %s exited %d", owner, result.returncode)
  142. return result.returncode == 0
  143. def _throwaway(container_id: str, info: dict, volumes: list[str]) -> list[str]:
  144. return ["run", "--rm", "-i", "--pull", "never", "--network", "none", "--user", "0:0", *_THROWAWAY_CONFINEMENT,
  145. *_SAFE_ENV, "--name", f"hermes-handback-{container_id[:12]}-{secrets.token_hex(4)}",
  146. *volumes, "--entrypoint", "", info["Image"]]
  147. # Re-chown each regular file to its own uid, one walk per distinct owner (uids read once; busybox
  148. # find has no -printf, and stat sits in /usr/bin on some images and /bin on others).
  149. _SAME_OWNER_CHOWN = ('uids=$("$@" -type f -exec stat -c %u -- {} +) || exit; '
  150. 'for u in $(printf "%s\\n" "$uids" | sort -u); do "$@" -type f -user "$u" -exec chown -h "$u" {} + '
  151. '|| exit; done')
  152. _strip_warned: set[str] = set()
  153. def strip_sandbox_privileges(docker_exe: str, container_id: str, info: Optional[dict] = None) -> None:
  154. """Strip setuid/setgid bits (and file caps, when the container could set them) from the root
  155. container's existing sandbox binds in ONE throwaway that mounts only those dirs: owners stay,
  156. so a running container's git/ssh checks keep passing, and a path swapped mid-walk can only
  157. lead to sandbox files, never other host data. ``--mount`` refuses a missing source instead
  158. of creating it root-owned. A pass that cannot run warns once per sandbox dir."""
  159. if info is None:
  160. info = _inspect(docker_exe, container_id) if _handback_ids(docker_exe) else None
  161. if not (mounts := _sandbox_binds(info) if info and _runs_as_root(info) else []):
  162. return
  163. target = _throwaway(container_id, info, [a for src, dst in mounts
  164. for a in ("--mount", f"type=bind,src={src},dst={dst}")])
  165. # Only the sandbox dirs are mounted, so PATH (reset to system dirs) finds the image's own tools.
  166. walk = ["find", *walk_argv(mounts, [m["Destination"] for m in info.get("Mounts") or []], [])[1:]]
  167. argv = (["/bin/sh", "-c", _SAME_OWNER_CHOWN, "sh", *walk] if _may_set_file_caps(info)
  168. else [*walk, "-type", "f", "-perm", "/6000", "-exec", "chmod", "ug-s", "{}", "+"])
  169. try:
  170. result = _docker(docker_exe, target, argv, time.monotonic() + _CHOWN_TIMEOUT_S)
  171. error = result.returncode and f"exit {result.returncode}: {result.stderr.decode('utf-8', 'replace')[:300]}"
  172. except (subprocess.TimeoutExpired, OSError) as e:
  173. error = str(e)
  174. if error:
  175. where = os.path.dirname(mounts[0][0])
  176. (logger.debug if where in _strip_warned else logger.warning)(
  177. "Could not strip setuid/setgid bits and file capabilities from Docker sandbox %s (%s); "
  178. "files there may still run with elevated rights on the host", where, error.strip())
  179. _strip_warned.add(where)
  180. def hand_back_sandbox_files(docker_exe: str, container_id: str, mounts: list[tuple[str, str]]) -> None:
  181. """After a root container has stopped (so nothing writes any more) and before it is removed:
  182. give its root-owned sandbox files (``mounts`` are ``(host_dir, container_path)``) to the host
  183. user from a throwaway container of the same local image with the same volumes, and record
  184. which, then strip what is left (the handback chown already cleared the bits it touched); each
  185. step has its own time budget. The paths are recorded before any of them changes owner, so a
  186. timeout mid-chown still leaves a complete record. Best-effort: a gone container, an image
  187. without ``find`` or a timeout logs at debug."""
  188. record = _record_path(mounts)
  189. ids = _handback_ids(docker_exe) if record else None
  190. info = _inspect(docker_exe, container_id) if ids else None
  191. if not info or not _runs_as_root(info):
  192. return
  193. target = _throwaway(container_id, info, ["--volumes-from", container_id])
  194. deadline = time.monotonic() + _CHOWN_TIMEOUT_S
  195. try:
  196. if paths := _walk(docker_exe, target, mounts, info, 0, deadline):
  197. known = set(record.read_bytes().split(b"\0")) if record.is_file() else set()
  198. with record.open("ab") as fh: # append: an unreclaimed earlier record still applies
  199. fh.write(b"".join(p + b"\0" for p in paths if p not in known))
  200. _chown(docker_exe, target, paths, f"{ids[0]}:{ids[1]}", deadline)
  201. except (subprocess.TimeoutExpired, OSError, KeyError) as e:
  202. logger.debug("ownership handback of %s failed: %s", container_id[:12], e)
  203. strip_sandbox_privileges(docker_exe, container_id, info)
  204. def reclaim_sandbox_files(docker_exe: str, container_id: str, mounts: list[tuple[str, str]]) -> None:
  205. """After a container mounts sandbox dirs an earlier teardown handed back: when it runs as root,
  206. return the recorded paths to root (so git/ssh ownership checks pass again), then drop the
  207. record. A container running as any other user keeps the handed-back owner and just drops the
  208. record. Only recorded paths a fresh no-symlink walk of this container's sandbox mounts still
  209. finds (outside every nested mount) are touched, so a symlinked directory or a real host dir now
  210. bound inside is never chowned. A failed walk or chown keeps the record for the next container."""
  211. record = _record_path(mounts)
  212. if record is None or not record.is_file():
  213. return
  214. info = _inspect(docker_exe, container_id) if sys.platform == "linux" else {}
  215. if info is None:
  216. return
  217. if not info or not _runs_as_root(info):
  218. record.unlink(missing_ok=True)
  219. return
  220. target = ["exec", "-i", "-u", "0:0", *_SAFE_ENV, container_id]
  221. deadline = time.monotonic() + _CHOWN_TIMEOUT_S
  222. try:
  223. recorded = set(record.read_bytes().split(b"\0"))
  224. owned = _walk(docker_exe, target, mounts, info, os.geteuid(), deadline) # windows-footgun: ok — linux-gated
  225. paths = [p for p in owned or [] if p in recorded]
  226. if owned is None or (paths and not _chown(docker_exe, target, paths, "0:0", deadline)):
  227. return
  228. except (subprocess.TimeoutExpired, OSError) as e:
  229. logger.debug("ownership reclaim in %s failed: %s", container_id[:12], e)
  230. return
  231. record.unlink(missing_ok=True)