1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
5  *
6  * Author: Roberto Sassu <roberto.sassu@huawei.com>
7  */
8 
9 #include <linux/keyctl.h>
10 #include <test_progs.h>
11 
12 #include "test_lookup_key.skel.h"
13 
14 #define KEY_LOOKUP_CREATE	0x01
15 #define KEY_LOOKUP_PARTIAL	0x02
16 
17 static bool kfunc_not_supported;
18 
19 static int libbpf_print_cb(enum libbpf_print_level level, const char *fmt,
20 			   va_list args)
21 {
22 	char *func;
23 
24 	if (strcmp(fmt, "libbpf: extern (func ksym) '%s': not found in kernel or module BTFs\n"))
25 		return 0;
26 
27 	func = va_arg(args, char *);
28 
29 	if (strcmp(func, "bpf_lookup_user_key") && strcmp(func, "bpf_key_put") &&
30 	    strcmp(func, "bpf_lookup_system_key"))
31 		return 0;
32 
33 	kfunc_not_supported = true;
34 	return 0;
35 }
36 
37 void test_lookup_key(void)
38 {
39 	libbpf_print_fn_t old_print_cb;
40 	struct test_lookup_key *skel;
41 	__u32 next_id;
42 	int ret;
43 
44 	skel = test_lookup_key__open();
45 	if (!ASSERT_OK_PTR(skel, "test_lookup_key__open"))
46 		return;
47 
48 	old_print_cb = libbpf_set_print(libbpf_print_cb);
49 	ret = test_lookup_key__load(skel);
50 	libbpf_set_print(old_print_cb);
51 
52 	if (ret < 0 && kfunc_not_supported) {
53 		printf("%s:SKIP:bpf_lookup_*_key(), bpf_key_put() kfuncs not supported\n",
54 		       __func__);
55 		test__skip();
56 		goto close_prog;
57 	}
58 
59 	if (!ASSERT_OK(ret, "test_lookup_key__load"))
60 		goto close_prog;
61 
62 	ret = test_lookup_key__attach(skel);
63 	if (!ASSERT_OK(ret, "test_lookup_key__attach"))
64 		goto close_prog;
65 
66 	skel->bss->monitored_pid = getpid();
67 	skel->bss->key_serial = KEY_SPEC_THREAD_KEYRING;
68 
69 	/* The thread-specific keyring does not exist, this test fails. */
70 	skel->bss->flags = 0;
71 
72 	ret = bpf_prog_get_next_id(0, &next_id);
73 	if (!ASSERT_LT(ret, 0, "bpf_prog_get_next_id"))
74 		goto close_prog;
75 
76 	/* Force creation of the thread-specific keyring, this test succeeds. */
77 	skel->bss->flags = KEY_LOOKUP_CREATE;
78 
79 	ret = bpf_prog_get_next_id(0, &next_id);
80 	if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
81 		goto close_prog;
82 
83 	/* Pass both lookup flags for parameter validation. */
84 	skel->bss->flags = KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL;
85 
86 	ret = bpf_prog_get_next_id(0, &next_id);
87 	if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
88 		goto close_prog;
89 
90 	/* Pass invalid flags. */
91 	skel->bss->flags = UINT64_MAX;
92 
93 	ret = bpf_prog_get_next_id(0, &next_id);
94 	if (!ASSERT_LT(ret, 0, "bpf_prog_get_next_id"))
95 		goto close_prog;
96 
97 	skel->bss->key_serial = 0;
98 	skel->bss->key_id = 1;
99 
100 	ret = bpf_prog_get_next_id(0, &next_id);
101 	if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
102 		goto close_prog;
103 
104 	skel->bss->key_id = UINT32_MAX;
105 
106 	ret = bpf_prog_get_next_id(0, &next_id);
107 	ASSERT_LT(ret, 0, "bpf_prog_get_next_id");
108 
109 close_prog:
110 	skel->bss->monitored_pid = 0;
111 	test_lookup_key__destroy(skel);
112 }
113