Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2017 Facebook
0003 
0004 #include "vmlinux.h"
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007 #include <bpf/bpf_core_read.h>
0008 #include "bpf_misc.h"
0009 
0010 int kprobe_res = 0;
0011 int kprobe2_res = 0;
0012 int kretprobe_res = 0;
0013 int kretprobe2_res = 0;
0014 int uprobe_res = 0;
0015 int uretprobe_res = 0;
0016 int uprobe_byname_res = 0;
0017 int uretprobe_byname_res = 0;
0018 int uprobe_byname2_res = 0;
0019 int uretprobe_byname2_res = 0;
0020 int uprobe_byname3_sleepable_res = 0;
0021 int uprobe_byname3_res = 0;
0022 int uretprobe_byname3_sleepable_res = 0;
0023 int uretprobe_byname3_res = 0;
0024 void *user_ptr = 0;
0025 
0026 SEC("kprobe")
0027 int handle_kprobe(struct pt_regs *ctx)
0028 {
0029     kprobe_res = 1;
0030     return 0;
0031 }
0032 
0033 SEC("ksyscall/nanosleep")
0034 int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
0035 {
0036     kprobe2_res = 11;
0037     return 0;
0038 }
0039 
0040 /**
0041  * This program will be manually made sleepable on the userspace side
0042  * and should thus be unattachable.
0043  */
0044 SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
0045 int handle_kprobe_sleepable(struct pt_regs *ctx)
0046 {
0047     kprobe_res = 2;
0048     return 0;
0049 }
0050 
0051 SEC("kretprobe")
0052 int handle_kretprobe(struct pt_regs *ctx)
0053 {
0054     kretprobe_res = 2;
0055     return 0;
0056 }
0057 
0058 SEC("kretsyscall/nanosleep")
0059 int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
0060 {
0061     kretprobe2_res = 22;
0062     return ret;
0063 }
0064 
0065 SEC("uprobe")
0066 int handle_uprobe(struct pt_regs *ctx)
0067 {
0068     uprobe_res = 3;
0069     return 0;
0070 }
0071 
0072 SEC("uretprobe")
0073 int handle_uretprobe(struct pt_regs *ctx)
0074 {
0075     uretprobe_res = 4;
0076     return 0;
0077 }
0078 
0079 SEC("uprobe")
0080 int handle_uprobe_byname(struct pt_regs *ctx)
0081 {
0082     uprobe_byname_res = 5;
0083     return 0;
0084 }
0085 
0086 /* use auto-attach format for section definition. */
0087 SEC("uretprobe//proc/self/exe:trigger_func2")
0088 int handle_uretprobe_byname(struct pt_regs *ctx)
0089 {
0090     uretprobe_byname_res = 6;
0091     return 0;
0092 }
0093 
0094 SEC("uprobe")
0095 int handle_uprobe_byname2(struct pt_regs *ctx)
0096 {
0097     unsigned int size = PT_REGS_PARM1(ctx);
0098 
0099     /* verify malloc size */
0100     if (size == 1)
0101         uprobe_byname2_res = 7;
0102     return 0;
0103 }
0104 
0105 SEC("uretprobe")
0106 int handle_uretprobe_byname2(struct pt_regs *ctx)
0107 {
0108     uretprobe_byname2_res = 8;
0109     return 0;
0110 }
0111 
0112 static __always_inline bool verify_sleepable_user_copy(void)
0113 {
0114     char data[9];
0115 
0116     bpf_copy_from_user(data, sizeof(data), user_ptr);
0117     return bpf_strncmp(data, sizeof(data), "test_data") == 0;
0118 }
0119 
0120 SEC("uprobe.s//proc/self/exe:trigger_func3")
0121 int handle_uprobe_byname3_sleepable(struct pt_regs *ctx)
0122 {
0123     if (verify_sleepable_user_copy())
0124         uprobe_byname3_sleepable_res = 9;
0125     return 0;
0126 }
0127 
0128 /**
0129  * same target as the uprobe.s above to force sleepable and non-sleepable
0130  * programs in the same bpf_prog_array
0131  */
0132 SEC("uprobe//proc/self/exe:trigger_func3")
0133 int handle_uprobe_byname3(struct pt_regs *ctx)
0134 {
0135     uprobe_byname3_res = 10;
0136     return 0;
0137 }
0138 
0139 SEC("uretprobe.s//proc/self/exe:trigger_func3")
0140 int handle_uretprobe_byname3_sleepable(struct pt_regs *ctx)
0141 {
0142     if (verify_sleepable_user_copy())
0143         uretprobe_byname3_sleepable_res = 11;
0144     return 0;
0145 }
0146 
0147 SEC("uretprobe//proc/self/exe:trigger_func3")
0148 int handle_uretprobe_byname3(struct pt_regs *ctx)
0149 {
0150     uretprobe_byname3_res = 12;
0151     return 0;
0152 }
0153 
0154 
0155 char _license[] SEC("license") = "GPL";