1 // SPDX-License-Identifier: GPL-2.0
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include <bpf/bpf_core_read.h>
9 __u32 target_ancestor_level;
10 __u64 target_ancestor_cgid;
11 int target_pid, target_hid;
13 struct cgroup *bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) __ksym;
14 struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;
15 void bpf_cgroup_release(struct cgroup *cgrp) __ksym;
17 static int bpf_link_create_verify(int cmd)
19 struct cgroup *cgrp, *ancestor;
20 struct task_struct *task;
23 if (cmd != BPF_LINK_CREATE)
26 task = bpf_get_current_task_btf();
28 /* Then it can run in parallel with others */
29 if (task->pid != target_pid)
32 cgrp = bpf_task_get_cgroup1(task, target_hid);
36 /* Refuse it if its cgid or its ancestor's cgid is the target cgid */
37 if (cgrp->kn->id == target_ancestor_cgid)
40 ancestor = bpf_cgroup_ancestor(cgrp, target_ancestor_level);
44 if (ancestor->kn->id == target_ancestor_cgid)
46 bpf_cgroup_release(ancestor);
49 bpf_cgroup_release(cgrp);
54 int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
56 return bpf_link_create_verify(cmd);
60 int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size)
62 return bpf_link_create_verify(cmd);
66 int BPF_PROG(fentry_run)
71 char _license[] SEC("license") = "GPL";