xref: /linux/tools/testing/selftests/vDSO/vdso_test_getrandom.c (revision c7546e2c3cb739a3c1a2f5acaf9bb629d401afe5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022-2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #include <assert.h>
7 #include <pthread.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <sys/auxv.h>
16 #include <sys/mman.h>
17 #include <sys/random.h>
18 #include <sys/syscall.h>
19 #include <sys/ptrace.h>
20 #include <sys/wait.h>
21 #include <sys/types.h>
22 #include <linux/random.h>
23 #include <linux/compiler.h>
24 #include <linux/ptrace.h>
25 
26 #include "../kselftest.h"
27 #include "parse_vdso.h"
28 #include "vdso_config.h"
29 #include "vdso_call.h"
30 
31 #ifndef timespecsub
32 #define	timespecsub(tsp, usp, vsp)					\
33 	do {								\
34 		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
35 		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
36 		if ((vsp)->tv_nsec < 0) {				\
37 			(vsp)->tv_sec--;				\
38 			(vsp)->tv_nsec += 1000000000L;			\
39 		}							\
40 	} while (0)
41 #endif
42 
43 static struct {
44 	pthread_mutex_t lock;
45 	void **states;
46 	size_t len, cap;
47 	ssize_t(*fn)(void *, size_t, unsigned long, void *, size_t);
48 	struct vgetrandom_opaque_params params;
49 } vgrnd = {
50 	.lock = PTHREAD_MUTEX_INITIALIZER
51 };
52 
53 static void *vgetrandom_get_state(void)
54 {
55 	void *state = NULL;
56 
57 	pthread_mutex_lock(&vgrnd.lock);
58 	if (!vgrnd.len) {
59 		size_t page_size = getpagesize();
60 		size_t new_cap;
61 		size_t alloc_size, num = sysconf(_SC_NPROCESSORS_ONLN); /* Just a decent heuristic. */
62 		void *new_block, *new_states;
63 
64 		alloc_size = (num * vgrnd.params.size_of_opaque_state + page_size - 1) & (~(page_size - 1));
65 		num = (page_size / vgrnd.params.size_of_opaque_state) * (alloc_size / page_size);
66 		new_block = mmap(0, alloc_size, vgrnd.params.mmap_prot, vgrnd.params.mmap_flags, -1, 0);
67 		if (new_block == MAP_FAILED)
68 			goto out;
69 
70 		new_cap = vgrnd.cap + num;
71 		new_states = reallocarray(vgrnd.states, new_cap, sizeof(*vgrnd.states));
72 		if (!new_states)
73 			goto unmap;
74 		vgrnd.cap = new_cap;
75 		vgrnd.states = new_states;
76 
77 		for (size_t i = 0; i < num; ++i) {
78 			if (((uintptr_t)new_block & (page_size - 1)) + vgrnd.params.size_of_opaque_state > page_size)
79 				new_block = (void *)(((uintptr_t)new_block + page_size - 1) & (~(page_size - 1)));
80 			vgrnd.states[i] = new_block;
81 			new_block += vgrnd.params.size_of_opaque_state;
82 		}
83 		vgrnd.len = num;
84 		goto success;
85 
86 	unmap:
87 		munmap(new_block, alloc_size);
88 		goto out;
89 	}
90 success:
91 	state = vgrnd.states[--vgrnd.len];
92 
93 out:
94 	pthread_mutex_unlock(&vgrnd.lock);
95 	return state;
96 }
97 
98 static void vgetrandom_put_state(void *state)
99 {
100 	if (!state)
101 		return;
102 	pthread_mutex_lock(&vgrnd.lock);
103 	vgrnd.states[vgrnd.len++] = state;
104 	pthread_mutex_unlock(&vgrnd.lock);
105 }
106 
107 static void vgetrandom_init(void)
108 {
109 	const char *version = versions[VDSO_VERSION];
110 	const char *name = names[VDSO_NAMES][6];
111 	unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
112 	size_t ret;
113 
114 	if (!sysinfo_ehdr) {
115 		printf("AT_SYSINFO_EHDR is not present!\n");
116 		exit(KSFT_SKIP);
117 	}
118 	vdso_init_from_sysinfo_ehdr(sysinfo_ehdr);
119 	vgrnd.fn = (__typeof__(vgrnd.fn))vdso_sym(version, name);
120 	if (!vgrnd.fn) {
121 		printf("%s is missing!\n", name);
122 		exit(KSFT_FAIL);
123 	}
124 	ret = VDSO_CALL(vgrnd.fn, 5, NULL, 0, 0, &vgrnd.params, ~0UL);
125 	if (ret == -ENOSYS) {
126 		printf("unsupported architecture\n");
127 		exit(KSFT_SKIP);
128 	} else if (ret) {
129 		printf("failed to fetch vgetrandom params!\n");
130 		exit(KSFT_FAIL);
131 	}
132 }
133 
134 static ssize_t vgetrandom(void *buf, size_t len, unsigned long flags)
135 {
136 	static __thread void *state;
137 
138 	if (!state) {
139 		state = vgetrandom_get_state();
140 		if (!state) {
141 			printf("vgetrandom_get_state failed!\n");
142 			exit(KSFT_FAIL);
143 		}
144 	}
145 	return VDSO_CALL(vgrnd.fn, 5, buf, len, flags, state, vgrnd.params.size_of_opaque_state);
146 }
147 
148 enum { TRIALS = 25000000, THREADS = 256 };
149 
150 static void *test_vdso_getrandom(void *ctx)
151 {
152 	for (size_t i = 0; i < TRIALS; ++i) {
153 		unsigned int val;
154 		ssize_t ret = vgetrandom(&val, sizeof(val), 0);
155 		assert(ret == sizeof(val));
156 	}
157 	return NULL;
158 }
159 
160 static void *test_libc_getrandom(void *ctx)
161 {
162 	for (size_t i = 0; i < TRIALS; ++i) {
163 		unsigned int val;
164 		ssize_t ret = getrandom(&val, sizeof(val), 0);
165 		assert(ret == sizeof(val));
166 	}
167 	return NULL;
168 }
169 
170 static void *test_syscall_getrandom(void *ctx)
171 {
172 	for (size_t i = 0; i < TRIALS; ++i) {
173 		unsigned int val;
174 		ssize_t ret = syscall(__NR_getrandom, &val, sizeof(val), 0);
175 		assert(ret == sizeof(val));
176 	}
177 	return NULL;
178 }
179 
180 static void bench_single(void)
181 {
182 	struct timespec start, end, diff;
183 
184 	clock_gettime(CLOCK_MONOTONIC, &start);
185 	test_vdso_getrandom(NULL);
186 	clock_gettime(CLOCK_MONOTONIC, &end);
187 	timespecsub(&end, &start, &diff);
188 	printf("   vdso: %u times in %lu.%09lu seconds\n", TRIALS, diff.tv_sec, diff.tv_nsec);
189 
190 	clock_gettime(CLOCK_MONOTONIC, &start);
191 	test_libc_getrandom(NULL);
192 	clock_gettime(CLOCK_MONOTONIC, &end);
193 	timespecsub(&end, &start, &diff);
194 	printf("   libc: %u times in %lu.%09lu seconds\n", TRIALS, diff.tv_sec, diff.tv_nsec);
195 
196 	clock_gettime(CLOCK_MONOTONIC, &start);
197 	test_syscall_getrandom(NULL);
198 	clock_gettime(CLOCK_MONOTONIC, &end);
199 	timespecsub(&end, &start, &diff);
200 	printf("syscall: %u times in %lu.%09lu seconds\n", TRIALS, diff.tv_sec, diff.tv_nsec);
201 }
202 
203 static void bench_multi(void)
204 {
205 	struct timespec start, end, diff;
206 	pthread_t threads[THREADS];
207 
208 	clock_gettime(CLOCK_MONOTONIC, &start);
209 	for (size_t i = 0; i < THREADS; ++i)
210 		assert(pthread_create(&threads[i], NULL, test_vdso_getrandom, NULL) == 0);
211 	for (size_t i = 0; i < THREADS; ++i)
212 		pthread_join(threads[i], NULL);
213 	clock_gettime(CLOCK_MONOTONIC, &end);
214 	timespecsub(&end, &start, &diff);
215 	printf("   vdso: %u x %u times in %lu.%09lu seconds\n", TRIALS, THREADS, diff.tv_sec, diff.tv_nsec);
216 
217 	clock_gettime(CLOCK_MONOTONIC, &start);
218 	for (size_t i = 0; i < THREADS; ++i)
219 		assert(pthread_create(&threads[i], NULL, test_libc_getrandom, NULL) == 0);
220 	for (size_t i = 0; i < THREADS; ++i)
221 		pthread_join(threads[i], NULL);
222 	clock_gettime(CLOCK_MONOTONIC, &end);
223 	timespecsub(&end, &start, &diff);
224 	printf("   libc: %u x %u times in %lu.%09lu seconds\n", TRIALS, THREADS, diff.tv_sec, diff.tv_nsec);
225 
226 	clock_gettime(CLOCK_MONOTONIC, &start);
227 	for (size_t i = 0; i < THREADS; ++i)
228 		assert(pthread_create(&threads[i], NULL, test_syscall_getrandom, NULL) == 0);
229 	for (size_t i = 0; i < THREADS; ++i)
230 		pthread_join(threads[i], NULL);
231 	clock_gettime(CLOCK_MONOTONIC, &end);
232 	timespecsub(&end, &start, &diff);
233 	printf("   syscall: %u x %u times in %lu.%09lu seconds\n", TRIALS, THREADS, diff.tv_sec, diff.tv_nsec);
234 }
235 
236 static void fill(void)
237 {
238 	uint8_t weird_size[323929];
239 	for (;;)
240 		vgetrandom(weird_size, sizeof(weird_size), 0);
241 }
242 
243 static void kselftest(void)
244 {
245 	uint8_t weird_size[1263];
246 	pid_t child;
247 
248 	ksft_print_header();
249 	ksft_set_plan(2);
250 
251 	for (size_t i = 0; i < 1000; ++i) {
252 		ssize_t ret = vgetrandom(weird_size, sizeof(weird_size), 0);
253 		if (ret != sizeof(weird_size))
254 			exit(KSFT_FAIL);
255 	}
256 
257 	ksft_test_result_pass("getrandom: PASS\n");
258 
259 	unshare(CLONE_NEWUSER);
260 	assert(unshare(CLONE_NEWTIME) == 0);
261 	child = fork();
262 	assert(child >= 0);
263 	if (!child) {
264 		vgetrandom_init();
265 		child = getpid();
266 		assert(ptrace(PTRACE_TRACEME, 0, NULL, NULL) == 0);
267 		assert(kill(child, SIGSTOP) == 0);
268 		assert(vgetrandom(weird_size, sizeof(weird_size), 0) == sizeof(weird_size));
269 		_exit(0);
270 	}
271 	for (;;) {
272 		struct ptrace_syscall_info info = { 0 };
273 		int status, ret;
274 		assert(waitpid(child, &status, 0) >= 0);
275 		if (WIFEXITED(status)) {
276 			if (WEXITSTATUS(status) != 0)
277 				exit(KSFT_FAIL);
278 			break;
279 		}
280 		assert(WIFSTOPPED(status));
281 		if (WSTOPSIG(status) == SIGSTOP)
282 			assert(ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD) == 0);
283 		else if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
284 			assert(ptrace(PTRACE_GET_SYSCALL_INFO, child, sizeof(info), &info) > 0);
285 			if (info.op == PTRACE_SYSCALL_INFO_ENTRY && info.entry.nr == __NR_getrandom &&
286 			    info.entry.args[0] == (uintptr_t)weird_size && info.entry.args[1] == sizeof(weird_size))
287 				exit(KSFT_FAIL);
288 		}
289 		assert(ptrace(PTRACE_SYSCALL, child, 0, 0) == 0);
290 	}
291 
292 	ksft_test_result_pass("getrandom timens: PASS\n");
293 
294 	exit(KSFT_PASS);
295 }
296 
297 static void usage(const char *argv0)
298 {
299 	fprintf(stderr, "Usage: %s [bench-single|bench-multi|fill]\n", argv0);
300 }
301 
302 int main(int argc, char *argv[])
303 {
304 	vgetrandom_init();
305 
306 	if (argc == 1) {
307 		kselftest();
308 		return 0;
309 	}
310 
311 	if (argc != 2) {
312 		usage(argv[0]);
313 		return 1;
314 	}
315 	if (!strcmp(argv[1], "bench-single"))
316 		bench_single();
317 	else if (!strcmp(argv[1], "bench-multi"))
318 		bench_multi();
319 	else if (!strcmp(argv[1], "fill"))
320 		fill();
321 	else {
322 		usage(argv[0]);
323 		return 1;
324 	}
325 	return 0;
326 }
327