1bdbee82bSHou Tao // SPDX-License-Identifier: GPL-2.0
2bdbee82bSHou Tao /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3bdbee82bSHou Tao #include <stdbool.h>
4bdbee82bSHou Tao #include <linux/types.h>
5bdbee82bSHou Tao #include <linux/bpf.h>
6bdbee82bSHou Tao #include <bpf/bpf_helpers.h>
7bdbee82bSHou Tao #include <bpf/bpf_tracing.h>
8bdbee82bSHou Tao 
9bdbee82bSHou Tao #define STRNCMP_STR_SZ 8
10bdbee82bSHou Tao 
11bdbee82bSHou Tao const char target[STRNCMP_STR_SZ] = "EEEEEEE";
12bdbee82bSHou Tao char str[STRNCMP_STR_SZ];
13bdbee82bSHou Tao int cmp_ret = 0;
14bdbee82bSHou Tao int target_pid = 0;
15bdbee82bSHou Tao 
16bdbee82bSHou Tao const char no_str_target[STRNCMP_STR_SZ] = "12345678";
17bdbee82bSHou Tao char writable_target[STRNCMP_STR_SZ];
18bdbee82bSHou Tao unsigned int no_const_str_size = STRNCMP_STR_SZ;
19bdbee82bSHou Tao 
20bdbee82bSHou Tao char _license[] SEC("license") = "GPL";
21bdbee82bSHou Tao 
22*0d7fefebSAndrii Nakryiko SEC("?tp/syscalls/sys_enter_nanosleep")
do_strncmp(void * ctx)23bdbee82bSHou Tao int do_strncmp(void *ctx)
24bdbee82bSHou Tao {
25bdbee82bSHou Tao 	if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
26bdbee82bSHou Tao 		return 0;
27bdbee82bSHou Tao 
28bdbee82bSHou Tao 	cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, target);
29bdbee82bSHou Tao 	return 0;
30bdbee82bSHou Tao }
31bdbee82bSHou Tao 
32*0d7fefebSAndrii Nakryiko SEC("?tp/syscalls/sys_enter_nanosleep")
strncmp_bad_not_const_str_size(void * ctx)33bdbee82bSHou Tao int strncmp_bad_not_const_str_size(void *ctx)
34bdbee82bSHou Tao {
35bdbee82bSHou Tao 	/* The value of string size is not const, so will fail */
36bdbee82bSHou Tao 	cmp_ret = bpf_strncmp(str, no_const_str_size, target);
37bdbee82bSHou Tao 	return 0;
38bdbee82bSHou Tao }
39bdbee82bSHou Tao 
40*0d7fefebSAndrii Nakryiko SEC("?tp/syscalls/sys_enter_nanosleep")
strncmp_bad_writable_target(void * ctx)41bdbee82bSHou Tao int strncmp_bad_writable_target(void *ctx)
42bdbee82bSHou Tao {
43bdbee82bSHou Tao 	/* Compared target is not read-only, so will fail */
44bdbee82bSHou Tao 	cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, writable_target);
45bdbee82bSHou Tao 	return 0;
46bdbee82bSHou Tao }
47bdbee82bSHou Tao 
48*0d7fefebSAndrii Nakryiko SEC("?tp/syscalls/sys_enter_nanosleep")
strncmp_bad_not_null_term_target(void * ctx)49bdbee82bSHou Tao int strncmp_bad_not_null_term_target(void *ctx)
50bdbee82bSHou Tao {
51bdbee82bSHou Tao 	/* Compared target is not null-terminated, so will fail */
52bdbee82bSHou Tao 	cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, no_str_target);
53bdbee82bSHou Tao 	return 0;
54bdbee82bSHou Tao }
55