Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/bpf.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 int probe_res;
0007 
0008 char input[4] = {};
0009 int test_pid;
0010 
0011 SEC("tracepoint/syscalls/sys_enter_nanosleep")
0012 int probe(void *ctx)
0013 {
0014     /* This BPF program performs variable-offset reads and writes on a
0015      * stack-allocated buffer.
0016      */
0017     char stack_buf[16];
0018     unsigned long len;
0019     unsigned long last;
0020 
0021     if ((bpf_get_current_pid_tgid() >> 32) != test_pid)
0022         return 0;
0023 
0024     /* Copy the input to the stack. */
0025     __builtin_memcpy(stack_buf, input, 4);
0026 
0027     /* The first byte in the buffer indicates the length. */
0028     len = stack_buf[0] & 0xf;
0029     last = (len - 1) & 0xf;
0030 
0031     /* Append something to the buffer. The offset where we write is not
0032      * statically known; this is a variable-offset stack write.
0033      */
0034     stack_buf[len] = 42;
0035 
0036     /* Index into the buffer at an unknown offset. This is a
0037      * variable-offset stack read.
0038      *
0039      * Note that if it wasn't for the preceding variable-offset write, this
0040      * read would be rejected because the stack slot cannot be verified as
0041      * being initialized. With the preceding variable-offset write, the
0042      * stack slot still cannot be verified, but the write inhibits the
0043      * respective check on the reasoning that, if there was a
0044      * variable-offset to a higher-or-equal spot, we're probably reading
0045      * what we just wrote.
0046      */
0047     probe_res = stack_buf[last];
0048     return 0;
0049 }
0050 
0051 char _license[] SEC("license") = "GPL";