eBPF-based security platform
seal: count only attach-mode ptrace access as SealPtrace
John Fastabenddb12f414e9e2
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 694f447e..dae2c355 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -442,7 +442,8 @@ signed config image.* - ``t6133-seal-init-xattr``- Labels at inode creation* - ``t6134-seal-task``- - ``ptrace`` and perf events on a running shirudo+ - ``ptrace``, ``/proc`` reads and perf events on a running+ shirudo; only attach mode counts* - ``t6135-task-descriptors``- Descriptors through ``/proc`` and ``pidfd_getfd``* - ``t6136-secure-exec``@@ -883,7 +884,7 @@ tests assert that a refusal moves its counter:* - ``SealCopyUp``- ``t6124``* - ``SealPtrace``- - ``t6135``+ - ``t6134``, ``t6135``* - ``SealSignal``- ``t6143``* - ``ExecTraced``diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex de469463..993731a9 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -481,10 +481,11 @@ when the record it needs is missing.count alike (R2).* - ``lsm/ptrace_access_check`` (``seal``)- Every ptrace access mode against a marked task, which covers- attach, ``/proc/<pid>/mem``, ``pidfd_getfd()`` and- ``process_vm_readv()``.+ attach, ``/proc/<pid>/mem``, ``pidfd_getfd()``,+ ``process_vm_readv()`` and the read-mode ``/proc`` files such as+ ``exe``, ``environ`` and ``maps``.- ``EACCES``- - ``SealPtrace``+ - ``SealPtrace``, attach mode only- Closed.* - ``lsm/perf_event_alloc`` (``seal``)- A perf event bound to a marked task. CPU-wide events have no taskdiff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex 5e256387..42695528 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -121,8 +121,11 @@ Protecting marked tasksA mark is only worth as much as the integrity of the process that holdsit. ``lsm/ptrace_access_check`` refuses every ptrace access to a marked-task (``SealPtrace``), which also covers ``/proc/<pid>/mem``,-``pidfd_getfd()`` and ``process_vm_readv()``. ``lsm/perf_event_alloc``+task. It counts one whose mode includes ``PTRACE_MODE_ATTACH``+(``SealPtrace``), which covers attach, ``/proc/<pid>/mem``,+``pidfd_getfd()`` and ``process_vm_readv()``, and refuses a read-mode+one uncounted, because journald reads ``/proc/<pid>/exe`` of every+process it logs for and a counted read would rise on an honest node. ``lsm/perf_event_alloc``refuses a perf event bound to a marked task (``SealPerf``).``lsm/task_kill`` lets a signal to a marked task through, since rootmay end a command that hangs and systemd stops the runs its timersdiff --git a/Documentation/operate/deploy/boot-integration.rst b/Documentation/operate/deploy/boot-integration.rstindex 7698de6a..ddb8d0b1 100644--- a/Documentation/operate/deploy/boot-integration.rst+++ b/Documentation/operate/deploy/boot-integration.rst@@ -461,7 +461,6 @@ Boot and verifyBpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):diff --git a/Documentation/operate/deploy/initialize.rst b/Documentation/operate/deploy/initialize.rstindex 04fc7212..5a633a88 100644--- a/Documentation/operate/deploy/initialize.rst+++ b/Documentation/operate/deploy/initialize.rst@@ -85,7 +85,6 @@ Check that init workedBpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):diff --git a/Documentation/operate/plan/interoperability.rst b/Documentation/operate/plan/interoperability.rstindex 4cbb3914..38053059 100644--- a/Documentation/operate/plan/interoperability.rst+++ b/Documentation/operate/plan/interoperability.rst@@ -381,14 +381,14 @@ Debuggers, tracing and /proc:Mechanism: The seal marks a task at its exec of a runtime file, and afork inherits the mark. ``lsm/ptrace_access_check`` refuses everyptrace access check against a marked task, both attach and read- modes, and ``lsm/perf_event_alloc`` refuses a perf event bound to one.+ modes, but counts only attach mode, and ``lsm/perf_event_alloc`` refuses a perf event bound to one.An exec of a runtime file by a traced task is refused. The ptracecheck runs after the kernel's own user and capability checks, so onlyroot, or a caller running as the same user, reaches it.:Symptom: See the table below.:What to do: Debug shirudo where it does not enforce, such as the test- VM (:doc:`/contribute/testing/testing`). Expect ``SealPtrace`` in the- baseline of a host that runs process monitors as root.+ VM (:doc:`/contribute/testing/testing`). A read-mode refusal shows+ only as ``EACCES`` to the caller, never in ``SealPtrace``... list-table:::header-rows: 1@@ -403,18 +403,21 @@ Debuggers, tracing and /proc* - Starting shirudo under ``strace`` or ``gdb``- The exec fails with ``EACCES``- ``ExecTraced``- * - Reading ``/proc/<pid>/exe``, ``environ``, ``maps``, ``mem`` or- the ``fd`` links of a shirudo command+ * - Reading ``/proc/<pid>/mem`` of a shirudo command- ``EACCES``- ``SealPtrace``+ * - Reading ``/proc/<pid>/exe``, ``environ``, ``maps`` or the ``fd``+ links of a shirudo command+ - ``EACCES``+ - None* - Reading ``/proc/<pid>/stat`` of a shirudo command, as ``ps`` and``top`` do- Succeeds, with the fields that need the check zeroed- - ``SealPtrace``+ - None* - journald receiving a log line from a shirudo command- journald cannot read ``/proc/<pid>/exe``, so the entry has no``_EXE=`` field- - ``SealPtrace``+ - None* - ``perf record --pid``, ``perf stat --pid`` on a shirudo command- ``EACCES``- ``SealPerf``diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex f2f2b100..9e397040 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -56,7 +56,6 @@ Print the reportBpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):@@ -109,7 +108,6 @@ line; a section unfolds on its own when its condition is not ``OK``.BpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):@@ -743,7 +741,6 @@ LSM returns. The report afterwards lists one more count underPinUnlink 1 unlink of a pin in the private bpffsBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 12 ptrace of a shirudo taskObjGet 1 pin, map or link opened by another executable...@@ -751,15 +748,16 @@ Read the counters with these rules:- Learn the baseline of your hosts. Software other than shirudo thatuses BPF or inspects processes is refused too. A freshly booted test- VM shows counts for ``LoadNotLoader``, ``MapCreate``, ``BpfCommand``- and ``SealPtrace`` before any upatch is loaded.+ VM shows counts for ``LoadNotLoader``, ``MapCreate`` and+ ``BpfCommand`` before any upatch is loaded.- Alert on a counter that moves beyond the baseline, and on any count ofa counter that stays at zero on your hosts.- Find the actor in the journal and on the host. The counter does notrecord who tried.- A zero count does not prove that nothing tried. Some refusals have no- counter, among them those of the runtime and config image mounts and- of device-mapper control commands.+ counter, among them those of the runtime and config image mounts, of+ device-mapper control commands and of a read-mode ptrace check, such+ as journald reading ``/proc/<pid>/exe`` of a shirudo command.:doc:`/security/tamper-counters` lists every counter and the guard pointthat raises it.diff --git a/Documentation/reference/cli/status.rst b/Documentation/reference/cli/status.rstindex f5a9208a..4accb2a9 100644--- a/Documentation/reference/cli/status.rst+++ b/Documentation/reference/cli/status.rst@@ -294,7 +294,6 @@ Show the state of a target:BpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):@@ -330,7 +329,6 @@ system as well:BpfCommand 8 detach, update, delete or unclassified bpf() commandBpffsUmount 1 unmount of the private bpffsSealWrite 1 write open of a sealed file- SealPtrace 7 ptrace of a shirudo taskSealEntry 2 entry into a sealed directory, or a sealed inode linkedLoaded upatches (1):diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rstindex 3caa7252..348a603a 100644--- a/Documentation/reference/formats/status-json.rst+++ b/Documentation/reference/formats/status-json.rst@@ -915,7 +915,7 @@ indented here:"bpffs_mounted": true},"tamper_attempts": {- "total": 35,+ "total": 28,"counters": {"LoadNotLoader": 15,"LoadUnsigned": 0,@@ -943,7 +943,7 @@ indented here:"SealSetattr": 0,"SealXattr": 0,"SealLabel": 0,- "SealPtrace": 7,+ "SealPtrace": 0,"SealPerf": 0,"ExecTraced": 0,"SealEntry": 2,@@ -998,7 +998,7 @@ of the same target read:.. code-block:: json{- "total": 51,+ "total": 44,"counters": {"LoadNotLoader": 27, "LoadUnsigned": 0, "LoadUnverified": 0, "LoadKeyringKind": 0,"LoadKeyring": 0, "MapCreate": 1, "MapWrite": 0, "MapFdWrite": 0,@@ -1006,7 +1006,7 @@ of the same target read:"ExeIdentity": 0, "KeyringWrite": 0, "PinUnlink": 1, "PinRename": 0,"PinXattr": 0, "BpffsUmount": 1, "BpffsUmountAbove": 0, "BpffsMount": 0,"SealWrite": 1, "SealUnlink": 0, "SealRename": 0, "SealSetattr": 0,- "SealXattr": 0, "SealLabel": 0, "SealPtrace": 7, "SealPerf": 0,+ "SealXattr": 0, "SealLabel": 0, "SealPtrace": 0, "SealPerf": 0,"ExecTraced": 0, "SealEntry": 0, "PerfAttach": 0, "PinEntry": 0,"SealCopyUp": 0, "Kexec": 0, "TpmExtend": 0, "ObjGet": 1,"SealSignal": 0diff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rstindex c6e76ca2..b511cb0c 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -156,7 +156,10 @@ counter.- ``EACCES``* - ``SealPtrace``- ptrace of a shirudo task- - ``lsm/ptrace_access_check``+ - ``lsm/ptrace_access_check``: an access mode that includes+ ``PTRACE_MODE_ATTACH``, such as ``ptrace(2)`` attach,+ ``/proc/<pid>/mem``, ``process_vm_readv(2)`` and+ ``pidfd_getfd(2)``- ``EACCES``* - ``SealPerf``- perf event bound to a shirudo task@@ -216,6 +219,17 @@ counter.Unless the row says every caller, a task with runtime authority isnot refused and moves nothing.+.. note::++ Every ptrace mode against a shirudo task is refused, but only an+ attach-mode access is counted. A read-mode probe, such as journald+ reading ``/proc/<pid>/exe`` for a log line or ``ps`` reading+ ``/proc/<pid>/stat``, or a read of ``environ``, ``maps`` or the+ ``fd`` links, is refused silently: the caller gets ``EACCES`` and may+ log it, but ``SealPtrace`` and the keepalive do not move. Honest+ system services make read-mode checks against every process that+ logs, so counting them would make a healthy node read as tampered.+Refusals without a counter--------------------------@@ -249,6 +263,10 @@ counter:* - Exec of a runtime file under a seccomp filter, and an exec forwhich task storage cannot be allocated- ``lsm.s/bprm_check_security``+ * - A read-mode ptrace access check against a shirudo task, such as+ a read of ``/proc/<pid>/exe``, ``environ``, ``maps`` or the+ ``fd`` links, ``kcmp(2)`` and ``process_madvise(2)``+ - ``lsm/ptrace_access_check``* - A fork that cannot copy a task mark- ``lsm/task_alloc``* - A new inode under a marked directory that cannot receive thediff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 89eeb4f7..9a5f9f06 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -792,8 +792,11 @@ bootstrap mark through a pidfd before the gates attach.An authorized process is protected against being taken over:- ``lsm/ptrace_access_check`` refuses every ptrace mode against a marked- task: attach, ``/proc/<pid>/mem``, ``/proc/<pid>/fd``,- ``pidfd_getfd(2)`` and ``process_vm_readv(2)`` (``SealPtrace``).+ task. It counts an attach-mode access, which covers attach,+ ``/proc/<pid>/mem``, ``pidfd_getfd(2)`` and ``process_vm_readv(2)``+ (``SealPtrace``), and refuses a read-mode one, which covers+ ``/proc/<pid>/exe``, ``environ``, ``maps`` and the ``fd`` links,+ without a counter.- ``lsm/perf_event_alloc`` refuses a perf event bound to a marked task(``SealPerf``). Events on all tasks of a CPU, and events opened on atask before the exec that marks it, are not checked (D9).@@ -817,6 +820,17 @@ An authorized process is protected against being taken over:host names without NSS modules and locks its memory, and refuses torun when the lock fails.+.. note::++ Every ptrace mode against a shirudo task is refused, but only an+ attach-mode access is counted. A read-mode probe, such as journald+ reading ``/proc/<pid>/exe`` for a log line or ``ps`` reading+ ``/proc/<pid>/stat``, or a read of ``environ``, ``maps`` or the+ ``fd`` links, is refused silently: the caller gets ``EACCES`` and may+ log it, but ``SealPtrace`` and the keepalive do not move. Honest+ system services make read-mode checks against every process that+ logs, so counting them would make a healthy node read as tampered.+.. rst-class:: facts:Adversary: ADV-B and ADV-Cdiff --git a/Documentation/start/quickstart.rst b/Documentation/start/quickstart.rstindex 50c3063c..253f7500 100644--- a/Documentation/start/quickstart.rst+++ b/Documentation/start/quickstart.rst@@ -183,7 +183,6 @@ Check enforcementLoadNotLoader 15 program load that is not a signed loaderMapCreate 1 map creation by another executableBpfCommand 8 detach, update, delete or unclassified bpf() command- SealPtrace 6 ptrace of a shirudo taskObjGet 1 pin, map or link opened by another executableLoaded upatches (0):diff --git a/bpf/lib/lsm.h b/bpf/lib/lsm.hindex 6679621d..73a40540 100644--- a/bpf/lib/lsm.h+++ b/bpf/lib/lsm.h@@ -21,6 +21,8 @@#define S_IFDIR 0040000#endif#define LSM_UNSAFE_PTRACE 2+/* include/linux/ptrace.h; a #define, so not in BTF. */+#define PTRACE_MODE_ATTACH 0x02#define PR_SET_MM 35#define PR_SET_MM_EXE_FILE 13#define PR_SET_MM_MAP 14diff --git a/bpf/seal.c b/bpf/seal.cindex 21a8d2f5..ab33058f 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -240,18 +240,23 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,return 0;}-/* Keep ptrace away from processes with committed exec marks. */+/*+ * Keep every ptrace mode away from processes with committed exec marks,+ * but count only attach mode: journald reads /proc/<pid>/exe of every+ * client it logs for, and ps reads /proc/<pid>/stat, so a counted read+ * mode would make an honest node look tampered.+ */SEC("lsm/ptrace_access_check")int BPF_PROG(shirudo_seal_ptrace_access_check,struct task_struct *child, unsigned int mode, int ret){if (ret)return ret;-- if (shirudo_marked(child))+ if (!shirudo_marked(child))+ return 0;+ if (mode & PTRACE_MODE_ATTACH)return shirudo_tamper_deny(TAMPER_SEAL_PTRACE, -EACCES);-- return 0;+ return -EACCES;}/*diff --git a/testing/functional/tamperproof/t6134-seal-task.sh b/testing/functional/tamperproof/t6134-seal-task.shindex dfeaadfd..f8a308a1 100755--- a/testing/functional/tamperproof/t6134-seal-task.sh+++ b/testing/functional/tamperproof/t6134-seal-task.sh@@ -4,9 +4,11 @@## t6134 -- the seal's task hooks (bpf/seal.c ptrace_access_check and# perf_event_alloc): a task that runs the runtime shirudo binary can-# neither be ptrace-attached nor have a perf event bound to it, even by-# root. The task is a shirudo blocked on opening a fifo, released at-# the end. Non-destructive.+# neither be ptrace-attached, read through /proc nor have a perf event+# bound to it, even by root. Only an attach-mode access counts as+# SealPtrace; a read-mode one is refused uncounted. The task is a+# shirudo blocked on opening a fifo, released at the end.+# Non-destructive.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -29,6 +31,13 @@ cleanup() {}trap cleanup EXIT+counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}+mkfifo "$W/fifo" || test_skip_all "cannot make a fifo""$SHIRUDO_BIN" bpm info "$W/fifo" >/dev/null 2>&1 &PID=$!@@ -42,6 +51,20 @@ while [ "$i" -lt 50 ]; dodone[ -d "/proc/$PID" ] || test_skip_all "shirudo did not stay around"+# A /proc access by kind, against pid $2: 0 granted, 1 refused with+# EACCES, 3 refused otherwise. exe and environ are read-mode checks,+# mem an attach-mode one.+PROC='+import errno, os, sys+kind, pid = sys.argv[1], sys.argv[2]+try:+ if kind == "exe":+ os.readlink("/proc/%s/exe" % pid)+ else:+ os.close(os.open("/proc/%s/%s" % (pid, kind), os.O_RDONLY))+except OSError as e:+ sys.exit(1 if e.errno == errno.EACCES else 3)+'# ptrace(PTRACE_ATTACH): 0 attached (and detached again), 1 refused by# the seal, 3 refused otherwise.ATTACH='@@ -71,10 +94,30 @@ if fd >= 0:sys.exit(1 if ctypes.get_errno() == 13 else 3)'+before=$(counter SealPtrace) || test_skip_all "no tamper counters"++test_expect_success 'root cannot read the exe link or environ of a running shirudo' '+ for kind in exe environ; do+ python3 -c "$PROC" "$kind" "$PID"; [ "$?" = 1 ] || return 1+ done+'++test_expect_success 'a read-mode refusal leaves SealPtrace unchanged' '+ [ "$(counter SealPtrace)" = "$before" ]+'+test_expect_success 'root cannot ptrace-attach to a running shirudo' 'python3 -c "$ATTACH" "$PID"; [ "$?" = 1 ]'+test_expect_success 'nor open its /proc mem' '+ python3 -c "$PROC" mem "$PID"; [ "$?" = 1 ]+'++test_expect_success 'each attach-mode refusal counts as SealPtrace' '+ [ "$(counter SealPtrace)" = "$((before + 2))" ]+'+if python3 -c "$PERF" self 2>/dev/null; thentest_expect_success 'root cannot bind a perf event to a running shirudo' 'python3 -c "$PERF" "$PID"; [ "$?" = 1 ]diff --git a/testing/functional/tamperproof/t6135-task-descriptors.sh b/testing/functional/tamperproof/t6135-task-descriptors.shindex b9096435..4b2fd5a7 100755--- a/testing/functional/tamperproof/t6135-task-descriptors.sh+++ b/testing/functional/tamperproof/t6135-task-descriptors.sh@@ -5,9 +5,10 @@# t6135 -- a running shirudo's descriptors are not root's to take. Opening# one through /proc/<pid>/fd and copying one with pidfd_getfd both need# ptrace access to the task, which the seal refuses for a task the runtime-# image started, counted as SealPtrace; another process's descriptors stay-# reachable as the control. Passing one with SCM_RIGHTS would need shirudo-# to send it, which it never does. Non-destructive.+# image started; pidfd_getfd needs attach mode and counts as SealPtrace,+# /proc/<pid>/fd only read mode and does not. Another process's+# descriptors stay reachable as the control. Passing one with SCM_RIGHTS+# would need shirudo to send it, which it never does. Non-destructive.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -98,8 +99,10 @@ test_expect_success 'nor copy one with pidfd_getfd' 'release-test_expect_success 'every refusal is counted as SealPtrace' '- [ "$(counter SealPtrace)" -ge $((before + 6)) ]+# status takes the config operation lock that the held shirudo keeps, so+# the counter is read only once it is released.+test_expect_success 'only the pidfd_getfd refusals count, not the /proc reads' '+ [ "$(counter SealPtrace)" = "$((before + 3))" ]'test_done