1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2020 Google LLC.
8 #include <bpf/bpf_helpers.h>
9 #include <bpf/bpf_tracing.h>
13 __uint(type
, BPF_MAP_TYPE_ARRAY
);
14 __uint(max_entries
, 1);
20 __uint(type
, BPF_MAP_TYPE_HASH
);
21 __uint(max_entries
, 1);
27 __uint(type
, BPF_MAP_TYPE_LRU_HASH
);
28 __uint(max_entries
, 1);
31 } lru_hash
SEC(".maps");
33 char _license
[] SEC("license") = "GPL";
35 int monitored_pid
= 0;
36 int mprotect_count
= 0;
39 SEC("lsm/file_mprotect")
40 int BPF_PROG(test_int_hook
, struct vm_area_struct
*vma
,
41 unsigned long reqprot
, unsigned long prot
, int ret
)
46 __u32 pid
= bpf_get_current_pid_tgid() >> 32;
49 is_stack
= (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
50 vma
->vm_end
>= vma
->vm_mm
->start_stack
);
52 if (is_stack
&& monitored_pid
== pid
) {
60 SEC("lsm.s/bprm_committed_creds")
61 int BPF_PROG(test_void_hook
, struct linux_binprm
*bprm
)
63 __u32 pid
= bpf_get_current_pid_tgid() >> 32;
68 if (monitored_pid
== pid
)
71 bpf_copy_from_user(args
, sizeof(args
), (void *)bprm
->vma
->vm_mm
->arg_start
);
72 bpf_copy_from_user(args
, sizeof(args
), (void *)bprm
->mm
->arg_start
);
74 value
= bpf_map_lookup_elem(&array
, &key
);
77 value
= bpf_map_lookup_elem(&hash
, &key
);
80 value
= bpf_map_lookup_elem(&lru_hash
, &key
);
86 SEC("lsm/task_free") /* lsm/ is ok, lsm.s/ fails */
87 int BPF_PROG(test_task_free
, struct task_struct
*task
)
94 SEC("fentry.s/__x64_sys_setdomainname")
95 int BPF_PROG(test_sys_setdomainname
, struct pt_regs
*regs
)
97 void *ptr
= (void *)PT_REGS_PARM1(regs
);
98 int len
= PT_REGS_PARM2(regs
);
102 ret
= bpf_copy_from_user(&buf
, sizeof(buf
), ptr
);
103 if (len
== -2 && ret
== 0 && buf
== 1234)
105 if (len
== -3 && ret
== -EFAULT
)
107 if (len
== -4 && ret
== -EFAULT
)