xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/keyctl/keyctl04.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 Google, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2017-2024
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Regression test for commit c9f838d104fe ("KEYS: fix
11*49cdfc7eSAndroid Build Coastguard Worker  * keyctl_set_reqkey_keyring() to not leak thread keyrings"), a.k.a.
12*49cdfc7eSAndroid Build Coastguard Worker  * CVE-2017-7472.  This bug could be used to exhaust kernel memory, though it
13*49cdfc7eSAndroid Build Coastguard Worker  * would take a while to do that and it would grind the test suite to a halt.
14*49cdfc7eSAndroid Build Coastguard Worker  * Instead we do a quick check for whether the existing thread keyring is
15*49cdfc7eSAndroid Build Coastguard Worker  * replaced when the default request-key destination is set to the thread
16*49cdfc7eSAndroid Build Coastguard Worker  * keyring.  It shouldn't be, but before the fix it was (and the old thread
17*49cdfc7eSAndroid Build Coastguard Worker  * keyring was leaked).
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/keyctl.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)25*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	key_serial_t tid_keyring;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	TEST(keyctl(KEYCTL_GET_KEYRING_ID, KEY_SPEC_THREAD_KEYRING, 1));
30*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "failed to create thread keyring");
32*49cdfc7eSAndroid Build Coastguard Worker 	tid_keyring = TST_RET;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	TEST(keyctl(KEYCTL_SET_REQKEY_KEYRING, KEY_REQKEY_DEFL_THREAD_KEYRING));
35*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "failed to set reqkey keyring");
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	TEST(keyctl(KEYCTL_GET_KEYRING_ID, KEY_SPEC_THREAD_KEYRING, 0));
39*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "failed to get thread keyring ID");
41*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == tid_keyring)
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "thread keyring was not leaked");
43*49cdfc7eSAndroid Build Coastguard Worker 	else
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "thread keyring was leaked!");
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
48*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
49*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
50*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2017-7472"},
51*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "c9f838d104fe"},
52*49cdfc7eSAndroid Build Coastguard Worker 		{}
53*49cdfc7eSAndroid Build Coastguard Worker 	}
54*49cdfc7eSAndroid Build Coastguard Worker };
55