xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/keyctl/keyctl02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2017-2024
5*49cdfc7eSAndroid Build Coastguard Worker  * Ported: Guangwen Feng <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Regression test for the race between keyctl_read() and
12*49cdfc7eSAndroid Build Coastguard Worker  * keyctl_revoke(), if the revoke happens between keyctl_read()
13*49cdfc7eSAndroid Build Coastguard Worker  * checking the validity of a key and the key's semaphore being taken,
14*49cdfc7eSAndroid Build Coastguard Worker  * then the key type read method will see a revoked key.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * This causes a problem for the user-defined key type because it
17*49cdfc7eSAndroid Build Coastguard Worker  * assumes in its read method that there will always be a payload
18*49cdfc7eSAndroid Build Coastguard Worker  * in a non-revoked key and doesn't check for a NULL pointer.
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  * Bug was fixed in commit
21*49cdfc7eSAndroid Build Coastguard Worker  * b4a1b4f5047e ("KEYS: Fix race between read and revoke")
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "tst_kconfig.h"
31*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/keyctl.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define LOOPS	20000
34*49cdfc7eSAndroid Build Coastguard Worker #define MAX_WAIT_FOR_GC_MS 5000
35*49cdfc7eSAndroid Build Coastguard Worker #define PATH_KEY_COUNT_QUOTA	"/proc/sys/kernel/keys/root_maxkeys"
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker static int orig_maxkeys;
38*49cdfc7eSAndroid Build Coastguard Worker static int realtime_kernel;
39*49cdfc7eSAndroid Build Coastguard Worker 
do_read(void * arg)40*49cdfc7eSAndroid Build Coastguard Worker static void *do_read(void *arg)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	key_serial_t key = (unsigned long)arg;
43*49cdfc7eSAndroid Build Coastguard Worker 	char buffer[4] = { 0 };
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	keyctl(KEYCTL_READ, key, buffer, 4);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
do_revoke(void * arg)50*49cdfc7eSAndroid Build Coastguard Worker static void *do_revoke(void *arg)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	key_serial_t key = (unsigned long)arg;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	keyctl(KEYCTL_REVOKE, key);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)59*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret;
62*49cdfc7eSAndroid Build Coastguard Worker 	key_serial_t key, key_inv;
63*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t pth[4];
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < LOOPS; i++) {
66*49cdfc7eSAndroid Build Coastguard Worker 		key = add_key("user", "ltptestkey", "foo", 3,
67*49cdfc7eSAndroid Build Coastguard Worker 			KEY_SPEC_PROCESS_KEYRING);
68*49cdfc7eSAndroid Build Coastguard Worker 		if (key == -1)
69*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TERRNO, "Failed to add key");
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_CREATE(&pth[0], NULL, do_read,
72*49cdfc7eSAndroid Build Coastguard Worker 			(void *)(unsigned long)key);
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_CREATE(&pth[1], NULL, do_revoke,
74*49cdfc7eSAndroid Build Coastguard Worker 			(void *)(unsigned long)key);
75*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_CREATE(&pth[2], NULL, do_read,
76*49cdfc7eSAndroid Build Coastguard Worker 			(void *)(unsigned long)key);
77*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_CREATE(&pth[3], NULL, do_revoke,
78*49cdfc7eSAndroid Build Coastguard Worker 			(void *)(unsigned long)key);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(pth[0], NULL);
81*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(pth[1], NULL);
82*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(pth[2], NULL);
83*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(pth[3], NULL);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 		if (!tst_remaining_runtime()) {
86*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "Runtime exhausted, exiting after %d loops", i);
87*49cdfc7eSAndroid Build Coastguard Worker 			break;
88*49cdfc7eSAndroid Build Coastguard Worker 		}
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 		/*
91*49cdfc7eSAndroid Build Coastguard Worker 		 * Realtime kernel has deferred post-join thread cleanup which
92*49cdfc7eSAndroid Build Coastguard Worker 		 * may result in exhaustion of cgroup thread limit. Add delay
93*49cdfc7eSAndroid Build Coastguard Worker 		 * to limit the maximum number of stale threads to 4000
94*49cdfc7eSAndroid Build Coastguard Worker 		 * even with CONFIG_HZ=100.
95*49cdfc7eSAndroid Build Coastguard Worker 		 */
96*49cdfc7eSAndroid Build Coastguard Worker 		if (realtime_kernel)
97*49cdfc7eSAndroid Build Coastguard Worker 			usleep(100);
98*49cdfc7eSAndroid Build Coastguard Worker 	}
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	/*
101*49cdfc7eSAndroid Build Coastguard Worker 	 * Kernel should start garbage collect when last reference to key
102*49cdfc7eSAndroid Build Coastguard Worker 	 * is removed (see key_put()). Since we are adding keys with identical
103*49cdfc7eSAndroid Build Coastguard Worker 	 * description and type, each replacement should schedule a gc run,
104*49cdfc7eSAndroid Build Coastguard Worker 	 * see comment at __key_link().
105*49cdfc7eSAndroid Build Coastguard Worker 	 *
106*49cdfc7eSAndroid Build Coastguard Worker 	 * We create extra key here, to remove reference to last revoked key.
107*49cdfc7eSAndroid Build Coastguard Worker 	 */
108*49cdfc7eSAndroid Build Coastguard Worker 	key_inv = add_key("user", "ltptestkey", "foo", 3,
109*49cdfc7eSAndroid Build Coastguard Worker 		KEY_SPEC_PROCESS_KEYRING);
110*49cdfc7eSAndroid Build Coastguard Worker 	if (key_inv == -1)
111*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "Failed to add key");
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	/*
114*49cdfc7eSAndroid Build Coastguard Worker 	 * If we have invalidate, we can drop extra key immediately as well,
115*49cdfc7eSAndroid Build Coastguard Worker 	 * which also schedules gc.
116*49cdfc7eSAndroid Build Coastguard Worker 	 */
117*49cdfc7eSAndroid Build Coastguard Worker 	if (keyctl(KEYCTL_INVALIDATE, key_inv) == -1 && errno != EOPNOTSUPP)
118*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "Failed to invalidate key");
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	/*
121*49cdfc7eSAndroid Build Coastguard Worker 	 * At this point we are quite confident that gc has been scheduled,
122*49cdfc7eSAndroid Build Coastguard Worker 	 * so we wait and periodically check for last test key to be removed.
123*49cdfc7eSAndroid Build Coastguard Worker 	 */
124*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAX_WAIT_FOR_GC_MS; i += 100) {
125*49cdfc7eSAndroid Build Coastguard Worker 		ret = keyctl(KEYCTL_REVOKE, key);
126*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == -1 && errno == ENOKEY)
127*49cdfc7eSAndroid Build Coastguard Worker 			break;
128*49cdfc7eSAndroid Build Coastguard Worker 		usleep(100*1000);
129*49cdfc7eSAndroid Build Coastguard Worker 	}
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	if (i)
132*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "waiting for key gc took: %d ms", i);
133*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Bug not reproduced");
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)136*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
139*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_kconfig_var rt_kconfigs[] = {
140*49cdfc7eSAndroid Build Coastguard Worker 		TST_KCONFIG_INIT("CONFIG_PREEMPT_RT"),
141*49cdfc7eSAndroid Build Coastguard Worker 		TST_KCONFIG_INIT("CONFIG_PREEMPT_RT_FULL")
142*49cdfc7eSAndroid Build Coastguard Worker 	};
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(PATH_KEY_COUNT_QUOTA, "%d", &orig_maxkeys);
145*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(PATH_KEY_COUNT_QUOTA, "%d", orig_maxkeys + LOOPS + 1);
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	tst_kconfig_read(rt_kconfigs, ARRAY_SIZE(rt_kconfigs));
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(rt_kconfigs); i++)
150*49cdfc7eSAndroid Build Coastguard Worker 		realtime_kernel |= rt_kconfigs[i].choice == 'y';
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)153*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker 	if (orig_maxkeys > 0)
156*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PATH_KEY_COUNT_QUOTA, "%d", orig_maxkeys);
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
160*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
161*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
162*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
163*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 60,
164*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
165*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
166*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "b4a1b4f5047e"},
167*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2015-7550"},
168*49cdfc7eSAndroid Build Coastguard Worker 		{}
169*49cdfc7eSAndroid Build Coastguard Worker 	}
170*49cdfc7eSAndroid Build Coastguard Worker };
171