Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 
0004 #include "vmlinux.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007 #include <errno.h>
0008 
0009 int my_tid;
0010 
0011 __u64 kprobe_res;
0012 __u64 kprobe_multi_res;
0013 __u64 kretprobe_res;
0014 __u64 uprobe_res;
0015 __u64 uretprobe_res;
0016 __u64 tp_res;
0017 __u64 pe_res;
0018 __u64 fentry_res;
0019 __u64 fexit_res;
0020 __u64 fmod_ret_res;
0021 __u64 lsm_res;
0022 
0023 static void update(void *ctx, __u64 *res)
0024 {
0025     if (my_tid != (u32)bpf_get_current_pid_tgid())
0026         return;
0027 
0028     *res |= bpf_get_attach_cookie(ctx);
0029 }
0030 
0031 SEC("kprobe/sys_nanosleep")
0032 int handle_kprobe(struct pt_regs *ctx)
0033 {
0034     update(ctx, &kprobe_res);
0035     return 0;
0036 }
0037 
0038 SEC("kretprobe/sys_nanosleep")
0039 int handle_kretprobe(struct pt_regs *ctx)
0040 {
0041     update(ctx, &kretprobe_res);
0042     return 0;
0043 }
0044 
0045 SEC("uprobe")
0046 int handle_uprobe(struct pt_regs *ctx)
0047 {
0048     update(ctx, &uprobe_res);
0049     return 0;
0050 }
0051 
0052 SEC("uretprobe")
0053 int handle_uretprobe(struct pt_regs *ctx)
0054 {
0055     update(ctx, &uretprobe_res);
0056     return 0;
0057 }
0058 
0059 /* bpf_prog_array, used by kernel internally to keep track of attached BPF
0060  * programs to a given BPF hook (e.g., for tracepoints) doesn't allow the same
0061  * BPF program to be attached multiple times. So have three identical copies
0062  * ready to attach to the same tracepoint.
0063  */
0064 SEC("tp/syscalls/sys_enter_nanosleep")
0065 int handle_tp1(struct pt_regs *ctx)
0066 {
0067     update(ctx, &tp_res);
0068     return 0;
0069 }
0070 SEC("tp/syscalls/sys_enter_nanosleep")
0071 int handle_tp2(struct pt_regs *ctx)
0072 {
0073     update(ctx, &tp_res);
0074     return 0;
0075 }
0076 SEC("tp/syscalls/sys_enter_nanosleep")
0077 int handle_tp3(void *ctx)
0078 {
0079     update(ctx, &tp_res);
0080     return 1;
0081 }
0082 
0083 SEC("perf_event")
0084 int handle_pe(struct pt_regs *ctx)
0085 {
0086     update(ctx, &pe_res);
0087     return 0;
0088 }
0089 
0090 SEC("fentry/bpf_fentry_test1")
0091 int BPF_PROG(fentry_test1, int a)
0092 {
0093     update(ctx, &fentry_res);
0094     return 0;
0095 }
0096 
0097 SEC("fexit/bpf_fentry_test1")
0098 int BPF_PROG(fexit_test1, int a, int ret)
0099 {
0100     update(ctx, &fexit_res);
0101     return 0;
0102 }
0103 
0104 SEC("fmod_ret/bpf_modify_return_test")
0105 int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
0106 {
0107     update(ctx, &fmod_ret_res);
0108     return 1234;
0109 }
0110 
0111 SEC("lsm/file_mprotect")
0112 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
0113          unsigned long reqprot, unsigned long prot, int ret)
0114 {
0115     if (my_tid != (u32)bpf_get_current_pid_tgid())
0116         return ret;
0117     update(ctx, &lsm_res);
0118     return -EPERM;
0119 }
0120 
0121 char _license[] SEC("license") = "GPL";