1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
4 #include <linux/types.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
9 #define STRNCMP_STR_SZ 8
11 const char target[STRNCMP_STR_SZ] = "EEEEEEE";
12 char str[STRNCMP_STR_SZ];
16 const char no_str_target[STRNCMP_STR_SZ] = "12345678";
17 char writable_target[STRNCMP_STR_SZ];
18 unsigned int no_const_str_size = STRNCMP_STR_SZ;
20 char _license[] SEC("license") = "GPL";
22 SEC("?tp/syscalls/sys_enter_nanosleep")
23 int do_strncmp(void *ctx)
25 if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
28 cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, target);
32 SEC("?tp/syscalls/sys_enter_nanosleep")
33 int strncmp_bad_not_const_str_size(void *ctx)
35 /* The value of string size is not const, so will fail */
36 cmp_ret = bpf_strncmp(str, no_const_str_size, target);
40 SEC("?tp/syscalls/sys_enter_nanosleep")
41 int strncmp_bad_writable_target(void *ctx)
43 /* Compared target is not read-only, so will fail */
44 cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, writable_target);
48 SEC("?tp/syscalls/sys_enter_nanosleep")
49 int strncmp_bad_not_null_term_target(void *ctx)
51 /* Compared target is not null-terminated, so will fail */
52 cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, no_str_target);