eBPF-based security platform
build: compile the BPF objects and upatches with -Wshadow
Daniel Borkmannf6c673f6a007
diff --git a/Documentation/reference/cli/build.rst b/Documentation/reference/cli/build.rstindex c823ae6a..8015fd96 100644--- a/Documentation/reference/cli/build.rst+++ b/Documentation/reference/cli/build.rst@@ -61,11 +61,11 @@ Building a upatch``vmlinux.h`` (``/usr/share/shirudo/vmlinux.h`` in the operatorpackage, ``/usr/local/share/shirudo/vmlinux.h`` after ``makeinstall``). It then runs ``/usr/bin/clang -target bpf -O2 -g -Wall- -Wextra`` with the ``vmlinux.h`` directory, the libbpf headers and the- shirudo BPF headers on the include path, plus ``--extra-cflags``. The- clang path is fixed when the helper is built, and ``PATH`` is not- searched. The object goes to a temporary file in ``/tmp``, or with- ``--emit-artifacts`` beside the output.+ -Wextra -Wshadow`` with the ``vmlinux.h`` directory, the libbpf+ headers and the shirudo BPF headers on the include path, plus+ ``--extra-cflags``. The clang path is fixed when the helper is built,+ and ``PATH`` is not searched. The object goes to a temporary file in+ ``/tmp``, or with ``--emit-artifacts`` beside the output.#. A ``.bpf.o`` or ``.o`` input is packaged as it is. Any other input isrefused.#. It names the upatch: ``--name``, else the input file name withoutdiff --git a/Makefile.shirudo b/Makefile.shirudoindex 617cb396..11dd1783 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -162,7 +162,7 @@ endif# -Wmissing-declarations; the BPF_PROG() / BPF_KPROBE() macros# generate auto-named params that often go unused. Neither is# fixable upstream of us, so silence both for BPF compilation.-CLANG_BPF := $(CLANG) -target bpf -O2 -g -Wall -Wextra -Wvla \+CLANG_BPF := $(CLANG) -target bpf -O2 -g -Wall -Wextra -Wvla -Wshadow \-Wno-missing-declarations -Wno-unused-parameter \-Wdeclaration-after-statement $(WERROR_CFLAGS)diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex bd2dab14..17e3e60a 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -612,6 +612,35 @@ static __always_inline bool shirudo_umount_takes_bpffs(struct mount *m,return true;}+/*+ * From m, the last mount of a subtree walked, the next one in+ * first-child, next-sibling order: the next sibling of m or of its+ * nearest ancestor below t that has one. 1 with *m set to it, 0 when the+ * walk is back at t, -1 when the tree cannot be walked within the bounds.+ */+static __always_inline int shirudo_mount_next_sibling(struct mount *t,+ struct mount **m)+{+ struct mount *parent, *cur = *m;+ __u64 next;+ int j;++ bpf_for(j, 0, MOUNT_DEPTH_MAX) {+ if (cur == t)+ return 0;+ parent = BPF_CORE_READ(cur, mnt_parent);+ next = (__u64)BPF_CORE_READ(cur, mnt_child.next);+ if (!parent || !next)+ return -1;+ if (next != shirudo_mount_children(parent)) {+ *m = shirudo_mount_child(next);+ return 1;+ }+ cur = parent;+ }+ return -1;+}+/** The pins hold the enforcement links through the bpffs superblock,* which goes with the last mount of it: a lazy unmount of /run took the@@ -625,12 +654,11 @@ static __always_inline bool shirudo_umount_takes_bpffs(struct mount *m,*/static __always_inline int shirudo_umount_above(struct vfsmount *mnt){+ struct mount *t, *m, *bpffs;struct mount_record *rec;- struct mount *t, *m, *bpffs, *parent;__u32 zero = 0;- bool sibling;__u64 next;- int i, j;+ int i, ret;rec = bpf_map_lookup_elem(&shirudo_mnt_rec, &zero);if (!rec || !rec->mount)@@ -648,22 +676,10 @@ static __always_inline int shirudo_umount_above(struct vfsmount *mnt)m = shirudo_mount_child(next);continue;}- sibling = false;- bpf_for(j, 0, MOUNT_DEPTH_MAX) {- if (m == t)- return 0;- parent = BPF_CORE_READ(m, mnt_parent);- next = (__u64)BPF_CORE_READ(m, mnt_child.next);- if (!parent || !next)- goto deny;- if (next != shirudo_mount_children(parent)) {- m = shirudo_mount_child(next);- sibling = true;- break;- }- m = parent;- }- if (!sibling)+ ret = shirudo_mount_next_sibling(t, &m);+ if (!ret)+ return 0;+ if (ret < 0)goto deny;}deny:diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.cindex b0690a41..9df45cb3 100644--- a/tools/shirudo-build/compile.c+++ b/tools/shirudo-build/compile.c@@ -109,6 +109,7 @@ int build_compile(const char *source, const char *output,push(argv, &n, "-g");push(argv, &n, "-Wall");push(argv, &n, "-Wextra");+ push(argv, &n, "-Wshadow");/** vmlinux.h dumps anonymous struct forward declarations that* trip -Wmissing-declarations -- unfixable upstream of us;diff --git a/upatches/kernel-obs/obs_exec.bpf.c b/upatches/kernel-obs/obs_exec.bpf.cindex 4ca8d22c..4f1c768d 100644--- a/upatches/kernel-obs/obs_exec.bpf.c+++ b/upatches/kernel-obs/obs_exec.bpf.c@@ -83,7 +83,7 @@ int BPF_PROG(obs_exec, struct linux_binprm *bprm)struct exec_key key = {};char path[OBS_PATH_LEN] = {};char name[OBS_NAME_LEN] = {};- struct file *file;+ struct file *filp;__u64 now = obs_now();__u32 euid, uid;__u64 caps;@@ -91,8 +91,8 @@ int BPF_PROG(obs_exec, struct linux_binprm *bprm)int status;long len;- file = bprm->file;- if (!file || obs_exe_from_file(file, &key.ident.exe))+ filp = bprm->file;+ if (!filp || obs_exe_from_file(filp, &key.ident.exe))return 0;key.ident.cgroup = bpf_get_current_cgroup_id();interp = bprm->interp != bprm->filename;@@ -116,9 +116,9 @@ int BPF_PROG(obs_exec, struct linux_binprm *bprm)e = exec_touch(&key, &status);if (e) {if (status == OBS_TOUCH_CREATED) {- obs_name_from_file(file, name);+ obs_name_from_file(filp, name);obs_copy_in(e->val.name, name, OBS_NAME_LEN / 8);- len = bpf_d_path(&file->f_path, path, sizeof(path));+ len = bpf_d_path(&filp->f_path, path, sizeof(path));if (len > 0)obs_copy_in(e->val.path, path, OBS_PATH_LEN / 8);elsediff --git a/upatches/kernel-obs/obs_file.bpf.c b/upatches/kernel-obs/obs_file.bpf.cindex e5750bc8..4fed94b0 100644--- a/upatches/kernel-obs/obs_file.bpf.c+++ b/upatches/kernel-obs/obs_file.bpf.c@@ -107,7 +107,7 @@ static __always_inline int obs_file_match(const char *name,}SEC("lsm.s/file_open")-int BPF_PROG(obs_file_open, struct file *file)+int BPF_PROG(obs_file_open, struct file *filp){struct file_entry __arena *e;struct file_key key = {};@@ -121,9 +121,9 @@ int BPF_PROG(obs_file_open, struct file *file)int status;int rule;- if (!file)+ if (!filp)return 0;- dentry = file->f_path.dentry;+ dentry = filp->f_path.dentry;if (!dentry)return 0;src = BPF_CORE_READ(dentry, d_name.name);@@ -135,7 +135,7 @@ int BPF_PROG(obs_file_open, struct file *file)if (src)bpf_probe_read_kernel_str(parent, sizeof(parent), src);}- fmode = BPF_CORE_READ(file, f_mode);+ fmode = BPF_CORE_READ(filp, f_mode);write = (fmode & FMODE_WRITE_BIT) != 0;rule = obs_file_match(name, parent, write);if (rule < 0)@@ -145,7 +145,7 @@ int BPF_PROG(obs_file_open, struct file *file)obs_ident_checked(&key.ident, now);key.euid = obs_current_euid();key.mode = fmode & (FMODE_READ_BIT | FMODE_WRITE_BIT);- if (bpf_d_path(&file->f_path, key.path, sizeof(key.path)) <= 0) {+ if (bpf_d_path(&filp->f_path, key.path, sizeof(key.path)) <= 0) {/* A path too long leaves its tail in the key; the name alone is the key. */__builtin_memset(key.path, 0, sizeof(key.path));__builtin_memcpy(key.path, name, OBS_NAME_LEN);@@ -157,7 +157,7 @@ int BPF_PROG(obs_file_open, struct file *file)if (status == OBS_TOUCH_CREATED) {e->val.first_ns = now;e->val.rule = (__u32)rule;- e->val.magic = BPF_CORE_READ(file, f_inode, i_sb, s_magic);+ e->val.magic = BPF_CORE_READ(filp, f_inode, i_sb, s_magic);}__sync_fetch_and_add(&e->val.count, 1);e->val.last_ns = now;diff --git a/upatches/kernel-obs/obs_priv.bpf.c b/upatches/kernel-obs/obs_priv.bpf.cindex 6941233f..56a18429 100644--- a/upatches/kernel-obs/obs_priv.bpf.c+++ b/upatches/kernel-obs/obs_priv.bpf.c@@ -119,12 +119,12 @@ static __always_inline void priv_set_detail(struct priv_entry __arena *e,}static __always_inline void priv_set_mapping_name(struct priv_entry __arena *e,- struct file *file)+ struct file *filp){char name[OBS_NAME_LEN] = {};- if (file)- obs_name_from_file(file, name);+ if (filp)+ obs_name_from_file(filp, name);else__builtin_memcpy(name, "anonymous", sizeof("anonymous"));priv_set_name(e, name);@@ -268,7 +268,7 @@ int BPF_PROG(obs_mount, const char *dev_name, const struct path *path,}SEC("lsm.s/kernel_read_file")-int BPF_PROG(obs_kernel_read_file, struct file *file,+int BPF_PROG(obs_kernel_read_file, struct file *filp,enum kernel_read_file_id id, bool contents){struct priv_entry __arena *e;@@ -284,9 +284,9 @@ int BPF_PROG(obs_kernel_read_file, struct file *file,&status);if (!e || status != OBS_TOUCH_CREATED)return 0;- obs_name_from_file(file, name);+ obs_name_from_file(filp, name);priv_set_name(e, name);- if (file && bpf_d_path(&file->f_path, detail, sizeof(detail)) > 0)+ if (filp && bpf_d_path(&filp->f_path, detail, sizeof(detail)) > 0)priv_set_detail(e, detail);return 0;}@@ -325,7 +325,7 @@ int BPF_PROG(obs_module_request, char *kmod_name)}SEC("lsm/mmap_file")-int BPF_PROG(obs_mmap_file, struct file *file, unsigned long reqprot,+int BPF_PROG(obs_mmap_file, struct file *filp, unsigned long reqprot,unsigned long prot, unsigned long flags){struct priv_entry __arena *e;@@ -343,7 +343,7 @@ int BPF_PROG(obs_mmap_file, struct file *file, unsigned long reqprot,if (!e)return 0;if (status == OBS_TOUCH_CREATED)- priv_set_mapping_name(e, file);+ priv_set_mapping_name(e, filp);e->val.aux = flags;return 0;}@@ -356,7 +356,7 @@ int BPF_PROG(obs_file_mprotect, struct vm_area_struct *vma,struct obs_ident ident = {};unsigned long vm_flags;__u64 now = obs_now();- struct file *file;+ struct file *filp;__u32 euid;int status;@@ -365,7 +365,7 @@ int BPF_PROG(obs_file_mprotect, struct vm_area_struct *vma,vm_flags = BPF_CORE_READ(vma, vm_flags);if (vm_flags & OBS_VM_EXEC)return 0;- file = BPF_CORE_READ(vma, vm_file);+ filp = 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,@@ -373,7 +373,7 @@ int BPF_PROG(obs_file_mprotect, struct vm_area_struct *vma,if (!e)return 0;if (status == OBS_TOUCH_CREATED)- priv_set_mapping_name(e, file);+ priv_set_mapping_name(e, filp);e->val.aux = BPF_CORE_READ(vma, vm_end) -BPF_CORE_READ(vma, vm_start);return 0;