1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2020 Google LLC.
9 #include <bpf/bpf_core_read.h>
10 #include <bpf/bpf_helpers.h>
11 #include <bpf/bpf_tracing.h>
15 __uint(type, BPF_MAP_TYPE_ARRAY);
16 __uint(max_entries, 1);
22 __uint(type, BPF_MAP_TYPE_HASH);
23 __uint(max_entries, 1);
29 __uint(type, BPF_MAP_TYPE_LRU_HASH);
30 __uint(max_entries, 1);
33 } lru_hash SEC(".maps");
36 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
37 __uint(max_entries, 1);
40 } percpu_array SEC(".maps");
43 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
44 __uint(max_entries, 1);
47 } percpu_hash SEC(".maps");
50 __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
51 __uint(max_entries, 1);
54 } lru_percpu_hash SEC(".maps");
57 __uint(type, BPF_MAP_TYPE_ARRAY);
58 __uint(max_entries, 1);
61 } inner_map SEC(".maps");
64 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
65 __uint(max_entries, 1);
66 __uint(key_size, sizeof(int));
67 __uint(value_size, sizeof(int));
68 __array(values, struct inner_map);
69 } outer_arr SEC(".maps") = {
70 .values = { [0] = &inner_map },
74 __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
75 __uint(max_entries, 1);
76 __uint(key_size, sizeof(int));
77 __array(values, struct inner_map);
78 } outer_hash SEC(".maps") = {
79 .values = { [0] = &inner_map },
82 char _license[] SEC("license") = "GPL";
84 int monitored_pid = 0;
85 int mprotect_count = 0;
88 SEC("lsm/file_mprotect")
89 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
90 unsigned long reqprot, unsigned long prot, int ret)
95 __u32 pid = bpf_get_current_pid_tgid() >> 32;
98 is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
99 vma->vm_end >= vma->vm_mm->start_stack);
101 if (is_stack && monitored_pid == pid) {
109 SEC("lsm.s/bprm_committed_creds")
110 int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
112 __u32 pid = bpf_get_current_pid_tgid() >> 32;
113 struct inner_map *inner_map;
118 if (monitored_pid == pid)
121 bpf_copy_from_user(args, sizeof(args), (void *)bprm->vma->vm_mm->arg_start);
122 bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start);
124 value = bpf_map_lookup_elem(&array, &key);
127 value = bpf_map_lookup_elem(&hash, &key);
130 value = bpf_map_lookup_elem(&lru_hash, &key);
133 value = bpf_map_lookup_elem(&percpu_array, &key);
136 value = bpf_map_lookup_elem(&percpu_hash, &key);
139 value = bpf_map_lookup_elem(&lru_percpu_hash, &key);
142 inner_map = bpf_map_lookup_elem(&outer_arr, &key);
144 value = bpf_map_lookup_elem(inner_map, &key);
148 inner_map = bpf_map_lookup_elem(&outer_hash, &key);
150 value = bpf_map_lookup_elem(inner_map, &key);
157 SEC("lsm/task_free") /* lsm/ is ok, lsm.s/ fails */
158 int BPF_PROG(test_task_free, struct task_struct *task)
165 SEC("fentry.s/" SYS_PREFIX "sys_setdomainname")
166 int BPF_PROG(test_sys_setdomainname, struct pt_regs *regs)
168 void *ptr = (void *)PT_REGS_PARM1_SYSCALL(regs);
169 int len = PT_REGS_PARM2_SYSCALL(regs);
173 ret = bpf_copy_from_user(&buf, sizeof(buf), ptr);
174 if (len == -2 && ret == 0 && buf == 1234)
176 if (len == -3 && ret == -EFAULT)
178 if (len == -4 && ret == -EFAULT)