| 1 |
/* |
| 2 |
* Copyright (c) 2019,24 Andrew G. Morgan <[email protected]> |
| 3 |
* |
| 4 |
* This header, and the -lpsx library, provide a number of things to |
| 5 |
* support POSIX semantics for syscalls associated with the pthread |
| 6 |
* library. For any code that references the psx_syscall{3,6} |
| 7 |
* functions, linking this code is as follows: |
| 8 |
* |
| 9 |
* ld ... -lpsx -lpthread |
| 10 |
* or, |
| 11 |
* gcc ... -lpsx -lpthread |
| 12 |
* |
| 13 |
* NOTE: linking with -lcap to make it silently aware of the psx |
| 14 |
* mechanism is tricky. You have to envelope the -lpsx part with |
| 15 |
* the following linker flags: |
| 16 |
* |
| 17 |
* ld ... --no-as-needed --whole-archive -lpsx --no-whole-archive --as-needed -lpthread |
| 18 |
* or, |
| 19 |
* gcc ... -Wl,--no-as-needed -Wl,--whole-archive -lpsx -Wl,--no-whole-archive -Wl,--as-needed -lpthread |
| 20 |
* |
| 21 |
* These options are provided in the |
| 22 |
* https://en.wikipedia.org/wiki/Pkg-config libpsx.pc file |
| 23 |
* distributed with the library. |
| 24 |
* |
| 25 |
* FYI Earlier versions of libpsx relied on gcc |
| 26 |
* ... -Wl,--wrap=pthread_create linkage in all cases, but since |
| 27 |
* libpsx-2.72 the library can work with non pthread threads (LWP) |
| 28 |
* under Linux and such wrapping is no longer needed. That being |
| 29 |
* said, for compatibility reasons such linking is still supported. |
| 30 |
* |
| 31 |
* glibc provides a subset of this functionality natively through the |
| 32 |
* nptl:setxid mechanism and could implement psx_syscall() directly |
| 33 |
* using that style of functionality but, as of 2019-11-30, the setxid |
| 34 |
* mechanism is limited to 9 specific set*() syscalls that do not |
| 35 |
* support the syscall6 API (needed for prctl functions and the ambient |
| 36 |
* capabilities set for example). |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifndef _SYS_PSX_SYSCALL_H |
| 40 |
#define _SYS_PSX_SYSCALL_H |
| 41 |
|
| 42 |
#ifdef __cplusplus |
| 43 |
extern "C" { |
| 44 |
#endif |
| 45 |
|
| 46 |
/* |
| 47 |
* Programmatic way to recognize feature set. |
| 48 |
*/ |
| 49 |
#define LIBPSX_MAJOR 2 |
| 50 |
#define LIBPSX_MINOR 78 |
| 51 |
|
| 52 |
/* |
| 53 |
* psx_syscall performs the specified syscall on all psx registered |
| 54 |
* threads. The mechanism by which this occurs is much less efficient |
| 55 |
* than a standard system call on Linux, so it should only be used |
| 56 |
* when POSIX semantics are required to change process relevant |
| 57 |
* security state. |
| 58 |
* |
| 59 |
* Glibc has native support for POSIX semantics on setgroups() and the |
| 60 |
* 8 set*[gu]id() functions. So, there is no need to use psx_syscall() |
| 61 |
* for these calls. This call exists for all the other system calls |
| 62 |
* that need to maintain parity on all pthreads of a program. |
| 63 |
* |
| 64 |
* Some macrology is used to allow the caller to provide only as many |
| 65 |
* arguments as needed, thus psx_syscall() cannot be used as a |
| 66 |
* function pointer. For those situations, we define psx_syscall3() |
| 67 |
* and psx_syscall6(). |
| 68 |
*/ |
| 69 |
#define psx_syscall(syscall_nr, ...) \ |
| 70 |
__psx_syscall(syscall_nr, __VA_ARGS__, (long int) 6, (long int) 5, \ |
| 71 |
(long int) 4, (long int) 3, (long int) 2, \ |
| 72 |
(long int) 1, (long int) 0) |
| 73 |
long int __psx_syscall(long int syscall_nr, ...); |
| 74 |
long int psx_syscall3(long int syscall_nr, |
| 75 |
long int arg1, long int arg2, long int arg3); |
| 76 |
long int psx_syscall6(long int syscall_nr, |
| 77 |
long int arg1, long int arg2, long int arg3, |
| 78 |
long int arg4, long int arg5, long int arg6); |
| 79 |
|
| 80 |
/* |
| 81 |
* This function should be used by systems to obtain pointers to the |
| 82 |
* two syscall functions provided by the PSX library. A linkage trick |
| 83 |
* is to define this function as weak in a library that can optionally |
| 84 |
* use libpsx and then, should the caller link -lpsx, that library can |
| 85 |
* implicitly use these POSIX semantics syscalls. See libcap for an |
| 86 |
* example of this usage. |
| 87 |
*/ |
| 88 |
void psx_load_syscalls(long int (**syscall_fn)(long int, |
| 89 |
long int, long int, long int), |
| 90 |
long int (**syscall6_fn)(long int, |
| 91 |
long int, long int, long int, |
| 92 |
long int, long int, long int)); |
| 93 |
|
| 94 |
/* |
| 95 |
* psx_sensitivity_t holds the level of paranoia for non-POSIX syscall |
| 96 |
* behavior. The default is PSX_IGNORE: which is best effort - no |
| 97 |
* enforcement; PSX_WARNING will dump to stderr a warning when a |
| 98 |
* syscall's results differ; PSX_ERROR will dump info as per |
| 99 |
* PSX_WARNING and generate a SIGSYS. The current mode can be set with |
| 100 |
* psx_set_sensitivity(). |
| 101 |
*/ |
| 102 |
typedef enum { |
| 103 |
PSX_IGNORE = 0, |
| 104 |
PSX_WARNING = 1, |
| 105 |
PSX_ERROR = 2, |
| 106 |
} psx_sensitivity_t; |
| 107 |
|
| 108 |
/* |
| 109 |
* psx_set_sensitivity sets the current sensitivity of the PSX |
| 110 |
* mechanism. The function returns 0 on success and -1 if the |
| 111 |
* requested level is invalid. |
| 112 |
*/ |
| 113 |
int psx_set_sensitivity(psx_sensitivity_t level); |
| 114 |
|
| 115 |
#ifdef __cplusplus |
| 116 |
} |
| 117 |
#endif |
| 118 |
|
| 119 |
#endif /* _SYS_PSX_SYSCALL_H */ |