• 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 
add_user(char n)33 static void add_user(char n)
34 {
35 	char username[] = "ltp_add_key05_n";
36 	const char *const cmd_useradd[] = {"useradd", username, NULL};
37 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
38 	const char *const cmd_groupdel[] = {"groupdel", username, NULL};
39 	struct passwd *pw;
40 
41 	username[sizeof(username) - 2] = '0' + n;
42 
43 	tst_cmd(cmd_userdel, NULL, "/dev/null", TST_CMD_PASS_RETVAL);
44 	tst_cmd(cmd_groupdel, NULL, "/dev/null", TST_CMD_PASS_RETVAL);
45 
46 	SAFE_CMD(cmd_useradd, NULL, NULL);
47 	pw = SAFE_GETPWNAM(username);
48 	ltpuser[(unsigned int)n] = pw->pw_uid;
49 
50 	tst_res(TINFO, "Created user %s", pw->pw_name);
51 }
52 
clean_user(char n)53 static void clean_user(char n)
54 {
55 	char username[] = "ltp_add_key05_n";
56 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
57 
58 	username[sizeof(username) - 2] = '0' + n;
59 
60 	if (tst_cmd(cmd_userdel, NULL, NULL, TST_CMD_PASS_RETVAL))
61 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
62 }
63 
parse_proc_key_users(int * used_key,int * max_key,int * used_bytes,int * max_bytes)64 static inline void parse_proc_key_users(int *used_key, int *max_key, int *used_bytes, int *max_bytes)
65 {
66 	unsigned int val[4];
67 	char fmt[1024];
68 
69 	sprintf(fmt, "%5u: %%*5d %%*d/%%*d %%d/%%d %%d/%%d", ltpuser[useri]);
70 	SAFE_FILE_LINES_SCANF("/proc/key-users", fmt, &val[0], &val[1], &val[2], &val[3]);
71 
72 	if (used_key)
73 		*used_key = val[0];
74 	if (max_key)
75 		*max_key = val[1];
76 	if (used_bytes)
77 		*used_bytes = val[2];
78 	if (max_bytes)
79 		*max_bytes = val[3];
80 }
81 
verify_max_bytes(void)82 static void verify_max_bytes(void)
83 {
84 	char *buf;
85 	int plen, invalid_plen, delta;
86 	int used_bytes, max_bytes, tmp_used_bytes;
87 
88 	tst_res(TINFO, "test max bytes under unprivileged user");
89 
90 	parse_proc_key_users(NULL, NULL, &tmp_used_bytes, NULL);
91 	TEST(add_key("user", "test2", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
92 	if (TST_RET == -1) {
93 		tst_res(TFAIL | TTERRNO, "add key test2 failed");
94 		return;
95 	}
96 	parse_proc_key_users(NULL, NULL, &used_bytes, &max_bytes);
97 
98 	/*
99 	 * Compute delta between default datalen(in key_alloc) and actual
100 	 * datlen(in key_payload_reserve).
101 	 * more info see kernel code: security/keys/key.c
102 	 */
103 	delta = used_bytes - tmp_used_bytes - strlen("test2") - 1 - 64;
104 	invalid_plen = max_bytes - used_bytes - delta - strlen("test_xxx");
105 	buf = tst_alloc(invalid_plen);
106 
107 	TEST(add_key("user", "test_inv", buf, invalid_plen, KEY_SPEC_THREAD_KEYRING));
108 	if (TST_RET != -1) {
109 		tst_res(TFAIL, "add_key(test_inv) succeeded unexpectedltly");
110 		return;
111 	}
112 	if (TST_ERR == EDQUOT)
113 		tst_res(TPASS | TTERRNO, "add_key(test_inv) failed as expected");
114 	else
115 		tst_res(TFAIL | TTERRNO, "add_key(test_inv) failed expected EDQUOT got");
116 
117 	/*Reset delta*/
118 	TEST(add_key("user", "test3", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
119 	if (TST_RET == -1) {
120 		tst_res(TFAIL | TTERRNO, "add key test3 failed");
121 		return;
122 	}
123 	TEST(add_key("user", "test4", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
124 	if (TST_RET == -1) {
125 		tst_res(TFAIL | TTERRNO, "add key test4 failed");
126 		return;
127 	}
128 	parse_proc_key_users(NULL, NULL, &used_bytes, &max_bytes);
129 	plen = max_bytes - used_bytes - delta - strlen("test_xxx") - 1;
130 	TEST(add_key("user", "test_max", buf, plen, KEY_SPEC_THREAD_KEYRING));
131 	if (TST_RET == -1) {
132 		tst_res(TFAIL | TTERRNO, "add_key(test_max) failed unexpectedly");
133 		return;
134 	}
135 
136 	tst_res(TPASS, "add_key(test_max) succeeded as expected");
137 	parse_proc_key_users(NULL, NULL, &tmp_used_bytes, &max_bytes);
138 	if (tmp_used_bytes == max_bytes)
139 		tst_res(TPASS, "allow reaching the max bytes exactly");
140 	else
141 		tst_res(TFAIL, "max used bytes %u, key allow max bytes %u", tmp_used_bytes, max_bytes);
142 }
143 
verify_max_keys(void)144 static void verify_max_keys(void)
145 {
146 	int i, used_key, max_key;
147 	char desc[10];
148 
149 	tst_res(TINFO, "test max keys under unprivileged user");
150 	parse_proc_key_users(&used_key, &max_key, NULL, NULL);
151 
152 	for (i = used_key + 1; i <= max_key; i++) {
153 		sprintf(desc, "abc%d", i);
154 		TEST(add_key("user", desc, user_buf, 64, KEY_SPEC_THREAD_KEYRING));
155 		if (TST_RET == -1) {
156 			tst_res(TFAIL | TTERRNO, "add keyring key(%s) failed", desc);
157 			goto count;
158 		}
159 	}
160 
161 	TEST(add_key("user", "test_invalid_key", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
162 	if (TST_RET != -1) {
163 		tst_res(TFAIL, "add keyring key(test_invalid_key) succeeded unexpectedly");
164 		goto count;
165 	}
166 	if (TST_ERR == EDQUOT)
167 		tst_res(TPASS | TTERRNO, "add_key(test_invalid_key) failed as expected");
168 	else
169 		tst_res(TFAIL | TTERRNO, "add_key(test_invalid_key) failed expected EDQUOT got");
170 
171 count:
172 	parse_proc_key_users(&used_key, &max_key, NULL, NULL);
173 	if (used_key == max_key)
174 		tst_res(TPASS, "allow reaching the max key(%u) exactly", max_key);
175 	else
176 		tst_res(TFAIL, "max used key %u, key allow max key %u", used_key, max_key);
177 }
178 
do_test(unsigned int n)179 static void do_test(unsigned int n)
180 {
181 	if (usern < MAX_USERS)
182 		add_user(usern++);
183 
184 	if (useri >= MAX_USERS) {
185 		sleep(1);
186 		useri = 0;
187 	}
188 
189 	if (!SAFE_FORK()) {
190 		SAFE_SETUID(ltpuser[useri]);
191 		tst_res(TINFO, "User: %d, UID: %d", useri, ltpuser[useri]);
192 		TEST(add_key("user", "test1", user_buf, 64, KEY_SPEC_THREAD_KEYRING));
193 		if (TST_RET == -1) {
194 			tst_res(TFAIL | TTERRNO, "add key test1 failed");
195 			return;
196 		}
197 
198 		if (n)
199 			verify_max_keys();
200 		else
201 			verify_max_bytes();
202 	}
203 
204 	tst_reap_children();
205 	useri++;
206 
207 	return;
208 }
209 
cleanup(void)210 static void cleanup(void)
211 {
212 	while (usern--)
213 		clean_user(usern);
214 }
215 
216 static struct tst_test test = {
217 	.test = do_test,
218 	.tcnt = 2,
219 	.needs_root = 1,
220 	.forks_child = 1,
221 	.cleanup = cleanup,
222 	.save_restore = (const struct tst_path_val[]) {
223 		{"/proc/sys/kernel/keys/gc_delay", "1",
224 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
225 		{"/proc/sys/kernel/keys/maxkeys", "200",
226 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
227 		{"/proc/sys/kernel/keys/maxbytes", "20000",
228 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
229 		{}
230 	},
231 	.bufs = (struct tst_buffers []) {
232 		{&user_buf, .size = 64},
233 		{}
234 	},
235 	.needs_cmds = (const char *const []) {
236 		"useradd",
237 		"userdel",
238 		"groupdel",
239 		NULL
240 	},
241 	.tags = (const struct tst_tag[]) {
242 		{"linux-git", "a08bf91ce28"},
243 		{"linux-git", "2e356101e72"},
244 		{}
245 	}
246 };
247