Debian Codegraph @

psx/psx.c

raw
1 /*
2 * Copyright (c) 2019-21,2024 Andrew G Morgan <[email protected]>
3 *
4 * This file contains a collection of routines that perform thread
5 * synchronization to ensure that a whole process is running as a
6 * single privilege entity - independent of the number of threads.
7 *
8 * The whole file would be unnecessary if glibc exported an explicit
9 * psx_syscall()-like function that leveraged the nptl:setxid
10 * mechanism to synchronize thread state over the whole process.
11 */
12
13 #ifndef _GNU_SOURCE
14 #define _GNU_SOURCE
15 #endif
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <pthread.h> /* pthread_atfork() */
20 #include <signal.h>
21 #include <stdarg.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include "psx_syscall.h"
32 #include "libpsx.h"
33
34 #ifdef _PSX_DEBUG_MEMORY
35
36 static void *_psx_calloc(const char *file, const int line,
37 size_t nmemb, size_t size) {
38 void *ptr = calloc(nmemb, size);
39 fprintf(stderr, "psx:%d:%s:%d: calloc(%ld, %ld) -> %p\n", _psx_gettid(),
40 file, line, (long int)nmemb, (long int)size, ptr);
41 return ptr;
42 }
43
44 static void _psx_free(const char *file, const int line, void *ptr) {
45 fprintf(stderr, "psx:%d:%s:%d: free(%p)\n",
46 _psx_gettid(), file, line, ptr);
47 return free(ptr);
48 }
49
50 #define calloc(a, b) _psx_calloc(__FILE__, __LINE__, a, b)
51 #define free(a) _psx_free(__FILE__, __LINE__, a)
52
53 #endif /* def _PSX_DEBUG_MEMORY */
54
55 /*
56 * psx_load_syscalls() can be weakly defined in dependent libraries to
57 * provide a mechanism for a library to optionally leverage this psx
58 * mechanism. Specifically, when libcap calls psx_load_sycalls() it
59 * provides a weakly declared default that maps its system calls to
60 * the regular system call functions. However, when linked with psx,
61 * this function here overrides the syscalls to be the psx ones.
62 */
63 void psx_load_syscalls(long int (**syscall_fn)(long int,
64 long int, long int, long int),
65 long int (**syscall6_fn)(long int,
66 long int, long int, long int,
67 long int, long int, long int))
68 {
69 *syscall_fn = psx_syscall3;
70 *syscall6_fn = psx_syscall6;
71 }
72
73 /*
74 * This global coordinates the PSX mechanism.
75 */
76 __attribute__((visibility ("hidden"))) psx_tracker_t psx_tracker;
77
78 /* psx_mix is our trivial hash mixing for the thread reference map */
79 __attribute__((visibility ("hidden"))) long psx_mix(long value) {
80 return value ^ (value >> 7) ^ (value >> 13) ^ (value >> 23);
81 }
82
83 static void psx_set_map(int size)
84 {
85 psx_tracker.map_entries = size;
86 psx_tracker.map_mask = size - 1;
87 psx_tracker.map = calloc(psx_tracker.map_entries, sizeof(psx_thread_ref_t));
88 }
89
90 /*
91 * Forward declaration
92 */
93 static void _psx_cleanup(void);
94
95 #define taskdir_fmt "/proc/%ld/task"
96
97 /*
98 * Every time we detect a new process, the first thread to recognize
99 * this resets some of the psx_tracker fields.
100 */
101 static void _psx_proc_start(void)
102 {
103 long pid = getpid();
104 psx_tracker.pid = pid;
105 if (psx_tracker.pid_path == NULL) {
106 psx_tracker.pid_path = calloc(1, 3*sizeof(pid) + sizeof(taskdir_fmt));
107 }
108 sprintf(psx_tracker.pid_path, taskdir_fmt, pid);
109 psx_tracker.state = _PSX_IDLE;
110 psx_tracker.cmd.active = 0;
111 }
112
113 static void _psx_new_proc(void)
114 {
115 _psx_mu_unlock(&psx_tracker.state_mu);
116 _psx_proc_start();
117 }
118
119 /*
120 * psx_syscall_start initializes the psx subsystem. It is called
121 * once and while locked.
122 */
123 static void psx_syscall_start(void)
124 {
125 /*
126 * All sorts of things are assumed by Linux and glibc and/or musl
127 * about signal handlers and which can be blocked. Go has its own
128 * idiosyncrasies too. We tried SIGRTMAX until
129 *
130 * https://bugzilla.kernel.org/show_bug.cgi?id=210533
131 *
132 * We tried SYGSYS until
133 *
134 * https://bugzilla.kernel.org/show_bug.cgi?id=219687
135 *
136 * Our current strategy is to aggressively intercept SIGNO=33,
137 * something that is confirmed to be the case each time _PSX_SETUP
138 * state is entered. Note, this signal is special and hidden by
139 * glibc and musl, but we inject our use of it via raw system
140 * calls and quietly cooperate with those library usages. Go
141 * treats this signal specially (avoiding blocking it for extended
142 * periods) because of its hidden glibc usage as discussed here:
143 *
144 * https://github.com/golang/go/issues/42494
145 */
146 psx_tracker.psx_sig = 33;
147 psx_tracker.actions = calloc(2, psx_actions_size());
148 psx_set_map(256);
149 atexit(_psx_cleanup);
150 pthread_atfork(NULL, NULL, _psx_new_proc);
151 psx_tracker.initialized = 1;
152 }
153
154 /*
155 * This is the only way this library globally locks. Note, this is not
156 * to be confused with psx_sig (interrupt) blocking - which is
157 * performed when the signal handler is being confirmed.
158 */
159 __attribute__((visibility ("hidden"))) void psx_lock(void)
160 {
161 _psx_mu_lock(&psx_tracker.state_mu);
162 if (!psx_tracker.initialized) {
163 _psx_proc_start();
164 psx_syscall_start();
165 }
166 }
167
168 /*
169 * This is the only way this library unlocks.
170 */
171 __attribute__((visibility ("hidden"))) void psx_unlock(void)
172 {
173 _psx_mu_unlock(&psx_tracker.state_mu);
174 }
175
176 /*
177 * psx_cond_wait unlocks and waits to obtain the lock again, allowing
178 * other code to run that may require the lock. This is the only way
179 * the psx code waits like this.
180 */
181 __attribute__((visibility ("hidden"))) void psx_cond_wait(void)
182 {
183 _psx_mu_cond_wait(&psx_tracker.state_mu);
184 }
185
186 /*
187 * _psx_cleanup its called when the program exits. It is used to free
188 * any memory used by the thread tracker.
189 */
190 static void _psx_cleanup(void) {
191 /*
192 * We enter the exiting state and never exit that. This cleanup is
193 * only done at program exit.
194 */
195 psx_lock();
196 while (psx_tracker.state != _PSX_IDLE) {
197 psx_cond_wait();
198 }
199 psx_tracker.state = _PSX_EXITING;
200 free(psx_tracker.actions);
201 free(psx_tracker.map);
202 free(psx_tracker.pid_path);
203 psx_unlock();
204 }
205
206 /*
207 * under lock perform a state transition. Changing state is generally
208 * done via this function. However, there is a single exception in
209 * _psx_cleanup().
210 */
211 static void psx_new_state(psx_tracker_state_t was, psx_tracker_state_t is)
212 {
213 psx_lock();
214 while (psx_tracker.state != was) {
215 psx_cond_wait();
216 }
217 psx_tracker.state = is;
218 psx_unlock();
219 }
220
221 long int psx_syscall3(long int syscall_nr,
222 long int arg1, long int arg2, long int arg3) {
223 return psx_syscall(syscall_nr, arg1, arg2, arg3);
224 }
225
226 long int psx_syscall6(long int syscall_nr,
227 long int arg1, long int arg2, long int arg3,
228 long int arg4, long int arg5, long int arg6) {
229 return psx_syscall(syscall_nr, arg1, arg2, arg3, arg4, arg5, arg6);
230 }
231
232 /*
233 * __psx_immediate_syscall does one syscall using the current
234 * process.
235 */
236 static long int __psx_immediate_syscall(long int syscall_nr,
237 int count, long int *arg) {
238 psx_tracker.cmd.syscall_nr = syscall_nr;
239 psx_tracker.cmd.arg1 = count > 0 ? arg[0] : 0;
240 psx_tracker.cmd.arg2 = count > 1 ? arg[1] : 0;
241 psx_tracker.cmd.arg3 = count > 2 ? arg[2] : 0;
242
243 if (count > 3) {
244 psx_tracker.cmd.six = 1;
245 psx_tracker.cmd.arg4 = arg[3];
246 psx_tracker.cmd.arg5 = count > 4 ? arg[4] : 0;
247 psx_tracker.cmd.arg6 = count > 5 ? arg[5] : 0;
248 return syscall(syscall_nr,
249 psx_tracker.cmd.arg1,
250 psx_tracker.cmd.arg2,
251 psx_tracker.cmd.arg3,
252 psx_tracker.cmd.arg4,
253 psx_tracker.cmd.arg5,
254 psx_tracker.cmd.arg6);
255 }
256
257 psx_tracker.cmd.six = 0;
258 return syscall(syscall_nr, psx_tracker.cmd.arg1,
259 psx_tracker.cmd.arg2, psx_tracker.cmd.arg3);
260 }
261
262 /*
263 * glibc diropen/readdir API uses malloc/free internally and
264 * empirically employ some sort of private mutex. The fact that psx
265 * interrupts threads in arbitrary places guarantees that occasionally
266 * the code in __psx_syscall() will interrupt functions in the middle
267 * of performing these calls from other threads. Thus (and observed
268 * with the libcap_psx_test) it's inevitable that this will interrupt
269 * those functions while they hold a private lock. The net effect is
270 * that we will fall into a deadlock condition if __psx_syscall() uses
271 * diropen/readdir. So, we have opted to use raw system calls to read
272 * directories instead. The whole of the psx functionality is really
273 * low level, and only aimed at supporting Linux with its non-POSIX
274 * LWP threading model, so we're OK with that.
275 */
276
277 #define BUF_SIZE 4096
278
279 struct psx_linux_dirent64 {
280 long long d_ino;
281 long long d_off;
282 unsigned short d_reclen;
283 unsigned char d_type;
284 char d_name[];
285 };
286
287 /*
288 * __psx_syscall performs the syscall on the current thread and if no
289 * error is detected it ensures that the syscall is also performed on
290 * all (other) registered threads. The return code is the value for
291 * the first invocation. It uses a trick to figure out how many
292 * arguments the user has supplied. The other half of the trick is
293 * provided by the macro psx_syscall() in the <sys/psx_syscall.h>
294 * file. The trick is the 7th optional argument (8th over all) to
295 * __psx_syscall is the count of arguments supplied to psx_syscall.
296 *
297 * User:
298 * psx_syscall(nr, a, b);
299 * Expanded by macro to:
300 * __psx_syscall(nr, a, b, 6, 5, 4, 3, 2, 1, 0);
301 * The eighth arg is now ------------------------------------^
302 */
303 long int __psx_syscall(long int syscall_nr, ...) {
304 long int arg[7];
305 long i;
306
307 va_list aptr;
308 va_start(aptr, syscall_nr);
309 for (i = 0; i < 7; i++) {
310 arg[i] = va_arg(aptr, long int);
311 }
312 va_end(aptr);
313
314 int count = arg[6];
315 if (count < 0 || count > 6) {
316 errno = EINVAL;
317 return -1;
318 }
319
320 psx_new_state(_PSX_IDLE, _PSX_SETUP);
321 psx_confirm_sigaction();
322
323 long int ret = __psx_immediate_syscall(syscall_nr, count, arg);
324 if (ret == -1) {
325 psx_new_state(_PSX_SETUP, _PSX_IDLE);
326 goto defer;
327 }
328
329 int restore_errno = errno;
330 psx_new_state(_PSX_SETUP, _PSX_SYSCALL);
331
332 /*
333 * cleaning up before we start helps a fork()ed child not inherit
334 * confusion from its parent.
335 */
336 memset(psx_tracker.map, 0,
337 psx_tracker.map_entries*sizeof(psx_thread_ref_t));
338
339 long self = _psx_gettid(), sweep = 1;
340 int some, incomplete, mismatch = 0, verified = 0;
341 do {
342 incomplete = 0; /* count threads to return from signal handler */
343 some = 0; /* count threads still pending */
344 sweep++;
345
346 int fd = open(psx_tracker.pid_path, O_RDONLY | O_DIRECTORY);
347 if (fd == -1) {
348 psx_lock();
349 fprintf(stderr, "failed to read %s - aborting\n", psx_tracker.pid_path);
350 kill(psx_tracker.pid, SIGKILL);
351 }
352
353 for (;;) {
354 char buf[BUF_SIZE];
355 size_t nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
356 if (nread == 0) {
357 break;
358 } else if (nread < 0) {
359 perror("getdents64 failed");
360 kill(psx_tracker.pid, SIGKILL);
361 }
362
363 size_t offset;
364 unsigned short reclen;
365 for (offset = 0; offset < nread; offset += reclen) {
366 /* deal with potential unaligned reads */
367 memcpy(&reclen, buf + offset +
368 offsetof(struct psx_linux_dirent64, d_reclen),
369 sizeof(reclen));
370 char *dir = (buf + offset +
371 offsetof(struct psx_linux_dirent64, d_name));
372 long tid = atoi(dir);
373 if (tid == 0 || tid == self) {
374 continue;
375 }
376 long mix = psx_mix(tid);
377 psx_thread_ref_t *x =
378 &psx_tracker.map[mix & psx_tracker.map_mask];
379 if (x->tid != tid) {
380 if (x->tid != 0) {
381 /* a collision */
382 long entries = psx_tracker.map_entries;
383 long oval, mask;
384 for (oval = psx_mix(x->tid); ; entries <<= 1) {
385 mask = entries - 1;
386 if (((oval ^ mix) & mask) != 0) {
387 /* no more collisions */
388 break;
389 }
390 }
391 psx_thread_ref_t *old = psx_tracker.map;
392 long old_entries = psx_tracker.map_entries;
393 psx_lock();
394 psx_set_map(entries);
395 long ok_sweep = sweep - 1;
396 for (i = 0; i < old_entries; i++) {
397 psx_thread_ref_t *y = &old[i];
398 if (y->sweep < ok_sweep) {
399 /* no longer care about this entry */
400 continue;
401 }
402 psx_thread_ref_t *z =
403 &psx_tracker.map[psx_mix(y->tid) & mask];
404 z->tid = y->tid;
405 z->pending = y->pending;
406 z->retval = y->retval;
407 z->sweep = y->sweep;
408 }
409 psx_unlock();
410 free(old);
411 x = &psx_tracker.map[mix & mask];
412 }
413 /*
414 * A new entry - this is where we will also
415 * (first) enable the PSX parts of our installed
416 * handler. This is, potentially racing with other
417 * users of the same signal, so we do this under
418 * lock.
419 */
420 psx_lock();
421 x->pending = 1;
422 x->tid = tid;
423 psx_tracker.cmd.active = 1;
424 psx_unlock();
425 /*
426 * There is a small chance that this signal may be
427 * racing with another user of this signal.
428 * Locking above should ensure both forks of the
429 * handler get invoked - perhaps out of order
430 * though...
431 */
432 syscall(SYS_tkill, tid, psx_tracker.psx_sig);
433 }
434 psx_lock();
435 x->sweep = sweep;
436 incomplete++;
437 if (x->pending) {
438 some++;
439 } else if (x->retval != ret) {
440 mismatch = 1;
441 }
442 psx_unlock();
443 }
444 }
445 close(fd);
446 if (some) {
447 verified = 0;
448 sched_yield();
449 } else {
450 verified++;
451 }
452 } while (verified < 2);
453
454 psx_lock();
455 psx_tracker.incomplete = incomplete;
456 psx_tracker.cmd.active = 0;
457 while (psx_tracker.incomplete != 0) {
458 psx_cond_wait();
459 }
460 psx_unlock();
461
462 if (mismatch) {
463 psx_lock();
464 switch (psx_tracker.sensitivity) {
465 case PSX_IGNORE:
466 break;
467 default:
468 fprintf(stderr, "psx_syscall result differs.\n");
469 if (psx_tracker.cmd.six) {
470 fprintf(stderr, "trap:%ld a123456=[%ld,%ld,%ld,%ld,%ld,%ld]\n",
471 psx_tracker.cmd.syscall_nr,
472 psx_tracker.cmd.arg1,
473 psx_tracker.cmd.arg2,
474 psx_tracker.cmd.arg3,
475 psx_tracker.cmd.arg4,
476 psx_tracker.cmd.arg5,
477 psx_tracker.cmd.arg6);
478 } else {
479 fprintf(stderr, "trap:%ld a123=[%ld,%ld,%ld]\n",
480 psx_tracker.cmd.syscall_nr,
481 psx_tracker.cmd.arg1,
482 psx_tracker.cmd.arg2,
483 psx_tracker.cmd.arg3);
484 }
485 fprintf(stderr, "results:");
486 for (i=0; i < psx_tracker.map_entries; i++) {
487 psx_thread_ref_t *ref = &psx_tracker.map[i];
488 if (ref->sweep != sweep) {
489 continue;
490 }
491 if (ret != ref->retval) {
492 fprintf(stderr, " %ld={%ld}", ref->tid, ref->retval);
493 }
494 }
495 fprintf(stderr, " wanted={%ld}\n", ret);
496 if (psx_tracker.sensitivity == PSX_WARNING) {
497 break;
498 }
499 kill(psx_tracker.pid, SIGKILL);
500 }
501 psx_unlock();
502 }
503 errno = restore_errno;
504 psx_new_state(_PSX_SYSCALL, _PSX_IDLE);
505
506 defer:
507 return ret;
508 }
509
510 /*
511 * Change the PSX sensitivity level. If the threads appear to have
512 * diverged in behavior, this can cause the library to notify the
513 * user.
514 */
515 int psx_set_sensitivity(psx_sensitivity_t level) {
516 if (level < PSX_IGNORE || level > PSX_ERROR) {
517 errno = EINVAL;
518 return -1;
519 }
520 psx_lock();
521 psx_tracker.sensitivity = level;
522 psx_unlock();
523 return 0;
524 }
525
526 /*
527 * The following is required for legacy linkage libcap-2.71 and
528 * earlier backward compatibility. The Go use of psx no longer has any
529 * need for this, and does not provide the define when building this
530 * code.
531 */
532 #ifdef _LIBPSX_PTHREAD_LINKAGE
533
534 /*
535 * psx requires this function to be provided by the linkage wrapping.
536 */
537 extern int __real_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
538 void *(*start_routine) (void *), void *arg);
539
540 /*
541 * forward declaration to keep the compiler happy.
542 */
543 int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
544 void *(*start_routine) (void *), void *arg);
545
546 /*
547 * __wrap_pthread_create is defined for legacy reasons, since whether
548 * or not you use this wrapper to reach the __real_ functionality or
549 * not isn't important to the psx mechanism any longer (since
550 * libpsx-2.72).
551 */
552 int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
553 void *(*start_routine) (void *), void *arg) {
554 return __real_pthread_create(thread, attr, start_routine, arg);
555 }
556
557 #endif /* _LIBPSX_PTHREAD_LINKAGE def */