• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5  *
6  * Test unprivileged user can support the number of keys and the
7  * number of bytes consumed in payloads of the keys. The default
8  * value is 200 and 20000.
9  *
10  * This is also a regression test for
11  * commit a08bf91ce28e ("KEYS: allow reaching the keys quotas exactly")
12  * commit 2e356101e72a ("KEYS: reaching the keys quotas correctly")
13  *
14  * If you run this test with -i > 5 then expect to see some sporadic failures
15  * where add_key fails with EDQUOT. Keys are freed asynchronously and we only
16  * create up to 10 users to avoid race conditions.
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pwd.h>
22 #include "tst_test.h"
23 #include "lapi/keyctl.h"
24 
25 #define MAX_USERS 10
26 
27 static char *user_buf;
28 static uid_t ltpuser[MAX_USERS];
29 
30 static unsigned int usern;
31 static unsigned int useri;
32 
33 static const char *const save_restore[] = {
34 	"?/proc/sys/kernel/keys/gc_delay",
35 	"?/proc/sys/kernel/keys/maxkeys",
36 	"?/proc/sys/kernel/keys/maxbytes",
37 	NULL,
38 };
39 
add_user(char n)40 static void add_user(char n)
41 {
42 	char username[] = "ltp_add_key05_n";
43 	const char *const cmd_useradd[] = {"useradd", username, NULL};
44 	struct passwd *pw;
45 
46 	username[sizeof(username) - 2] = '0' + n;
47 
48 	SAFE_CMD(cmd_useradd, NULL, NULL);
49 	pw = SAFE_GETPWNAM(username);
50 	ltpuser[(unsigned int)n] = pw->pw_uid;
51 
52 	tst_res(TINFO, "Created user %s", pw->pw_name);
53 }
54 
clean_user(char n)55 static void clean_user(char n)
56 {
57 	char username[] = "ltp_add_key05_n";
58 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
59 
60 	username[sizeof(username) - 2] = '0' + n;
61 
62 	if (tst_cmd(cmd_userdel, NULL, NULL, TST_CMD_PASS_RETVAL))
63 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
64 }
65 
parse_proc_key_users(int * used_key,int * max_key,int * used_bytes,int * max_bytes)66 static inline void parse_proc_key_users(int *used_key, int *max_key, int *used_bytes, int *max_bytes)
67 {
68 	unsigned int val[4];
69 	char fmt[1024];
70 
71 	sprintf(fmt, "%5u: %%*5d %%*d/%%*d %%d/%%d %%d/%%d", ltpuser[useri]);
72 	SAFE_FILE_LINES_SCANF("/proc/key-users", fmt, &val[0], &val[1], &val[2], &val[3]);
73 
74 	if (used_key)
75 		*used_key = val[0];
76 	if (max_key)
77 		*max_key = val[1];
78 	if (used_bytes)
79 		*used_bytes = val[2];
80 	if (max_bytes)
81 		*max_bytes = val[3];
82 }
83 
verify_max_bytes(void)84 static void verify_max_bytes(void)
85 {
86 	char *buf;
87 	int plen, invalid_plen, delta;
88 	int used_bytes, max_bytes, tmp_used_bytes;
89 
90 	tst_res(TINFO, "test max bytes under unprivileged user");
91 
92 	parse_proc_key_users(NULL, NULL, &tmp_used_bytes, NULL);
93 	TEST(add_key("user", "test2", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
94 	if (TST_RET == -1) {
95 		tst_res(TFAIL | TTERRNO, "add key test2 failed");
96 		return;
97 	}
98 	parse_proc_key_users(NULL, NULL, &used_bytes, &max_bytes);
99 
100 	/*
101 	 * Compute delta between default datalen(in key_alloc) and actual
102 	 * datlen(in key_payload_reserve).
103 	 * more info see kernel code: security/keys/key.c
104 	 */
105 	delta = used_bytes - tmp_used_bytes - strlen("test2") - 1 - 64;
106 	invalid_plen = max_bytes - used_bytes - delta - strlen("test_xxx");
107 	buf = tst_alloc(invalid_plen);
108 
109 	TEST(add_key("user", "test_inv", buf, invalid_plen, KEY_SPEC_THREAD_KEYRING));
110 	if (TST_RET != -1) {
111 		tst_res(TFAIL, "add_key(test_inv) succeeded unexpectedltly");
112 		return;
113 	}
114 	if (TST_ERR == EDQUOT)
115 		tst_res(TPASS | TTERRNO, "add_key(test_inv) failed as expected");
116 	else
117 		tst_res(TFAIL | TTERRNO, "add_key(test_inv) failed expected EDQUOT got");
118 
119 	/*Reset delta*/
120 	TEST(add_key("user", "test3", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
121 	if (TST_RET == -1) {
122 		tst_res(TFAIL | TTERRNO, "add key test3 failed");
123 		return;
124 	}
125 	TEST(add_key("user", "test4", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
126 	if (TST_RET == -1) {
127 		tst_res(TFAIL | TTERRNO, "add key test4 failed");
128 		return;
129 	}
130 	parse_proc_key_users(NULL, NULL, &used_bytes, &max_bytes);
131 	plen = max_bytes - used_bytes - delta - strlen("test_xxx") - 1;
132 	TEST(add_key("user", "test_max", buf, plen, KEY_SPEC_THREAD_KEYRING));
133 	if (TST_RET == -1) {
134 		tst_res(TFAIL | TTERRNO, "add_key(test_max) failed unexpectedly");
135 		return;
136 	}
137 
138 	tst_res(TPASS, "add_key(test_max) succeeded as expected");
139 	parse_proc_key_users(NULL, NULL, &tmp_used_bytes, &max_bytes);
140 	if (tmp_used_bytes == max_bytes)
141 		tst_res(TPASS, "allow reaching the max bytes exactly");
142 	else
143 		tst_res(TFAIL, "max used bytes %u, key allow max bytes %u", tmp_used_bytes, max_bytes);
144 }
145 
verify_max_keys(void)146 static void verify_max_keys(void)
147 {
148 	int i, used_key, max_key;
149 	char desc[10];
150 
151 	tst_res(TINFO, "test max keys under unprivileged user");
152 	parse_proc_key_users(&used_key, &max_key, NULL, NULL);
153 
154 	for (i = used_key + 1; i <= max_key; i++) {
155 		sprintf(desc, "abc%d", i);
156 		TEST(add_key("user", desc, user_buf, 64, KEY_SPEC_THREAD_KEYRING));
157 		if (TST_RET == -1) {
158 			tst_res(TFAIL | TTERRNO, "add keyring key(%s) failed", desc);
159 			goto count;
160 		}
161 	}
162 
163 	TEST(add_key("user", "test_invalid_key", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
164 	if (TST_RET != -1) {
165 		tst_res(TFAIL, "add keyring key(test_invalid_key) succeeded unexpectedly");
166 		goto count;
167 	}
168 	if (TST_ERR == EDQUOT)
169 		tst_res(TPASS | TTERRNO, "add_key(test_invalid_key) failed as expected");
170 	else
171 		tst_res(TFAIL | TTERRNO, "add_key(test_invalid_key) failed expected EDQUOT got");
172 
173 count:
174 	parse_proc_key_users(&used_key, &max_key, NULL, NULL);
175 	if (used_key == max_key)
176 		tst_res(TPASS, "allow reaching the max key(%u) exactly", max_key);
177 	else
178 		tst_res(TFAIL, "max used key %u, key allow max key %u", used_key, max_key);
179 }
180 
do_test(unsigned int n)181 static void do_test(unsigned int n)
182 {
183 	if (usern < MAX_USERS)
184 		add_user(usern++);
185 
186 	if (useri >= MAX_USERS) {
187 		sleep(1);
188 		useri = 0;
189 	}
190 
191 	if (!SAFE_FORK()) {
192 		SAFE_SETUID(ltpuser[useri]);
193 		tst_res(TINFO, "User: %d, UID: %d", useri, ltpuser[useri]);
194 		TEST(add_key("user", "test1", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
195 		if (TST_RET == -1) {
196 			tst_res(TFAIL | TTERRNO, "add key test1 failed");
197 			return;
198 		}
199 
200 		if (n)
201 			verify_max_keys();
202 		else
203 			verify_max_bytes();
204 	}
205 
206 	tst_reap_children();
207 	useri++;
208 
209 	return;
210 }
211 
setup(void)212 static void setup(void)
213 {
214 	SAFE_FILE_PRINTF("/proc/sys/kernel/keys/gc_delay", "1");
215 	SAFE_FILE_PRINTF("/proc/sys/kernel/keys/maxkeys", "200");
216 	SAFE_FILE_PRINTF("/proc/sys/kernel/keys/maxbytes", "20000");
217 }
218 
cleanup(void)219 static void cleanup(void)
220 {
221 	while (usern--)
222 		clean_user(usern);
223 }
224 
225 static struct tst_test test = {
226 	.test = do_test,
227 	.tcnt = 2,
228 	.needs_root = 1,
229 	.forks_child = 1,
230 	.setup = setup,
231 	.cleanup = cleanup,
232 	.save_restore = save_restore,
233 	.bufs = (struct tst_buffers []) {
234 		{&user_buf, .size = 64},
235 		{}
236 	},
237 	.needs_cmds = (const char *const []) {
238 		"useradd",
239 		"userdel",
240 		NULL
241 	},
242 	.tags = (const struct tst_tag[]) {
243 		{"linux-git", "a08bf91ce28"},
244 		{"linux-git", "2e356101e72"},
245 		{}
246 	}
247 };
248