eBPF-based security platform
obs: record executable memory mappings
John Fastabend3180ac27f27a
diff --git a/upatches/kernel-obs/Beefile b/upatches/kernel-obs/Beefileindex 7feec079..e5ec48f6 100644--- a/upatches/kernel-obs/Beefile+++ b/upatches/kernel-obs/Beefile@@ -1,6 +1,6 @@[package]name = "kernel-obs"-description = "shirudo kernel observability: exec, network, DNS, listeners, watched destinations, privilege and sensitive file access"+description = "shirudo kernel observability: exec, network, DNS, listeners, watched destinations, executable mappings, privilege and sensitive file access"license = "GPL-2.0 OR BSD-2-Clause"authors = ["Authors of shirudo"]diff --git a/upatches/kernel-obs/README.md b/upatches/kernel-obs/README.mdindex 18957913..af80e891 100644--- a/upatches/kernel-obs/README.md+++ b/upatches/kernel-obs/README.md@@ -53,7 +53,7 @@ sudo shirudo upatch list| `listen` | executable, cgroup, protocol, local address and port | TCP listens and UDP binds to a fixed port, with backlog || `watch` | executable, cgroup, protocol, destination | attempts and packets towards addresses in `watch.list` || `dns` | executable, cgroup, answered address | up to four names that resolved to the address, with counts |-| `priv` | executable, cgroup, kind, subtype, euid | setuid to root, root without an exec or setuid to explain it, ptrace attach, mount, kernel file and module loads, user namespace creation, bpf() use |+| `priv` | executable, cgroup, kind, subtype, euid | setuid to root, root without an exec or setuid to explain it, ptrace attach, mount, executable mmap and mprotect requests, kernel file and module loads, user namespace creation, bpf() use || `file` | executable, cgroup, euid, open mode, path | opens matching `file-watch.list`, with the filesystem magic || `watch_stat` | per-CPU counters | metric: socket-level checks against the watch list and matches |@@ -62,6 +62,13 @@ cgroup id, and `first_seen_unix_nano` and `last_seen_unix_nano`. A taskthat started before the upatch loaded is attributed from its mappedexecutable and flagged `caller_inferred` on its next exec.+The `mmap_exec` privilege records report mapping requests that include+`PROT_EXEC`; `mprotect_exec` records transitions that newly grant it.+Their subtype is the requested protection bits; `mmap_exec` stores+mapping flags in `aux`, while `mprotect_exec` stores the VMA length.+`name` is the backing file name or `anonymous`. These hooks observe+requests, not whether the mapping succeeds or code runs.+Packet counts come from the cgroup hooks on the root cgroup and coverevery socket on the host, including sockets created before the load,which are attributed to their cgroup alone until their owner is known.diff --git a/upatches/kernel-obs/decoders/priv.lua b/upatches/kernel-obs/decoders/priv.luaindex 917a5a7b..86c9c4c6 100644--- a/upatches/kernel-obs/decoders/priv.lua+++ b/upatches/kernel-obs/decoders/priv.lua@@ -12,6 +12,8 @@ local KINDS = {[7] = "module_request",[8] = "userns_create",[9] = "bpf",+ [10] = "mmap_exec",+ [11] = "mprotect_exec",}local function header(slot)diff --git a/upatches/kernel-obs/obs_priv.bpf.c b/upatches/kernel-obs/obs_priv.bpf.cindex 995905af..86f16c5e 100644--- a/upatches/kernel-obs/obs_priv.bpf.c+++ b/upatches/kernel-obs/obs_priv.bpf.c@@ -3,9 +3,9 @@* obs_priv.bpf.c -- privilege transitions and persistence primitives.** Records credential changes to root, root privileges that no exec or- * setuid explains, ptrace attaches, mounts, kernel file and module- * loads, user namespace creation and bpf() use, keyed by the executable- * and cgroup that performed them.+ * setuid explains, ptrace attaches, mounts, executable mappings, kernel+ * file and module loads, user namespace creation and bpf() use, keyed by+ * the executable and cgroup that performed them.*/enum obs_priv_kind {@@ -18,10 +18,14 @@ enum obs_priv_kind {OBS_PRIV_MODULE_REQUEST = 7,OBS_PRIV_USERNS_CREATE = 8,OBS_PRIV_BPF = 9,+ OBS_PRIV_MMAP_EXEC = 10,+ OBS_PRIV_MPROTECT_EXEC = 11,};#define OBS_PRIV_DETAIL_LEN 96#define PTRACE_MODE_ATTACH 0x02+#define OBS_PROT_EXEC (1UL << 2)+#define OBS_VM_EXEC (1UL << 2)struct priv_key {struct obs_ident ident;@@ -114,6 +118,18 @@ static __always_inline void priv_set_detail(struct priv_entry __arena *e,obs_copy_in(e->val.detail, detail, OBS_PRIV_DETAIL_LEN / 8);}+static __always_inline void priv_set_mapping_name(+ struct priv_entry __arena *e, struct file *file)+{+ char name[OBS_NAME_LEN] = {};++ if (file)+ obs_name_from_file(file, name);+ else+ __builtin_memcpy(name, "anonymous", sizeof("anonymous"));+ priv_set_name(e, name);+}+/** A task whose effective uid is zero while its last exec or setuid left* it unprivileged gained root some other way, which is what a kernel@@ -312,6 +328,63 @@ int BPF_PROG(obs_module_request, char *kmod_name)return 0;}+SEC("lsm/mmap_file")+int BPF_PROG(obs_mmap_file, struct file *file, unsigned long reqprot,+ unsigned long prot, unsigned long flags)+{+ struct priv_entry __arena *e;+ struct obs_ident ident = {};+ __u64 now = obs_now();+ __u32 euid;+ int status;++ (void)reqprot;+ if (!(prot & OBS_PROT_EXEC))+ return 0;+ obs_ident_checked(&ident, now);+ euid = obs_current_euid();+ e = priv_record(&ident, OBS_PRIV_MMAP_EXEC, (__u32)prot, euid, now,+ &status);+ if (!e)+ return 0;+ if (status == OBS_TOUCH_CREATED)+ priv_set_mapping_name(e, file);+ e->val.aux = flags;+ return 0;+}++SEC("lsm/file_mprotect")+int BPF_PROG(obs_file_mprotect, struct vm_area_struct *vma,+ unsigned long reqprot, unsigned long prot)+{+ struct priv_entry __arena *e;+ struct obs_ident ident = {};+ unsigned long vm_flags;+ __u64 now = obs_now();+ struct file *file;+ __u32 euid;+ int status;++ (void)reqprot;+ if (!(prot & OBS_PROT_EXEC))+ return 0;+ vm_flags = BPF_CORE_READ(vma, vm_flags);+ if (vm_flags & OBS_VM_EXEC)+ return 0;+ file = BPF_CORE_READ(vma, vm_file);+ obs_ident_checked(&ident, now);+ euid = obs_current_euid();+ e = priv_record(&ident, OBS_PRIV_MPROTECT_EXEC, (__u32)prot, euid,+ now, &status);+ if (!e)+ return 0;+ if (status == OBS_TOUCH_CREATED)+ priv_set_mapping_name(e, file);+ e->val.aux = BPF_CORE_READ(vma, vm_end) -+ BPF_CORE_READ(vma, vm_start);+ return 0;+}+SEC("lsm/userns_create")int BPF_PROG(obs_userns_create, const struct cred *cred){