Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include "config.h" |
| 3 | #endif |
| 4 | #include <assert.h> |
Dmitry V. Levin | 530bed0 | 2014-12-14 13:30:54 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 6 | #include <unistd.h> |
Dmitry V. Levin | 04fcb99 | 2015-07-22 19:31:54 +0000 | [diff] [blame] | 7 | #include <fcntl.h> |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 8 | #include <sys/syscall.h> |
| 9 | |
| 10 | int |
| 11 | main(void) |
| 12 | { |
Dmitry V. Levin | 8ef5439 | 2015-07-16 09:09:11 +0000 | [diff] [blame] | 13 | #if (defined __NR_getuid || defined __NR_getxuid) \ |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 14 | && defined(__NR_setuid) \ |
| 15 | && defined(__NR_getresuid) \ |
| 16 | && defined(__NR_setreuid) \ |
| 17 | && defined(__NR_setresuid) \ |
Dmitry V. Levin | 68804b3 | 2015-03-16 18:10:21 +0000 | [diff] [blame] | 18 | && defined(__NR_fchown) \ |
Dmitry V. Levin | 530bed0 | 2014-12-14 13:30:54 +0000 | [diff] [blame] | 19 | && defined(__NR_getgroups) |
Dmitry V. Levin | 3a15bc8 | 2015-03-02 01:13:47 +0000 | [diff] [blame] | 20 | int uid; |
Dmitry V. Levin | 530bed0 | 2014-12-14 13:30:54 +0000 | [diff] [blame] | 21 | int size; |
| 22 | int *list = 0; |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 23 | |
Dmitry V. Levin | 8ef5439 | 2015-07-16 09:09:11 +0000 | [diff] [blame] | 24 | #ifndef __NR_getuid |
| 25 | # define __NR_getuid __NR_getxuid |
| 26 | #endif |
Dmitry V. Levin | 3a15bc8 | 2015-03-02 01:13:47 +0000 | [diff] [blame] | 27 | uid = syscall(__NR_getuid); |
Dmitry V. Levin | 04fcb99 | 2015-07-22 19:31:54 +0000 | [diff] [blame] | 28 | |
| 29 | (void) close(0); |
| 30 | if (open("/proc/sys/kernel/overflowuid", O_RDONLY) == 0) { |
| 31 | /* we trust the kernel */ |
| 32 | char buf[sizeof(int)*3]; |
| 33 | int n = read(0, buf, sizeof(buf) - 1); |
| 34 | if (n) { |
| 35 | buf[n] = '\0'; |
| 36 | n = atoi(buf); |
| 37 | if (uid == n) |
| 38 | return 77; |
| 39 | } |
| 40 | (void) close(0); |
| 41 | } |
| 42 | |
Dmitry V. Levin | 3a15bc8 | 2015-03-02 01:13:47 +0000 | [diff] [blame] | 43 | assert(syscall(__NR_setuid, uid) == 0); |
| 44 | { |
| 45 | /* |
| 46 | * uids returned by getresuid should be ignored |
| 47 | * to avoid 16bit vs 32bit issues. |
| 48 | */ |
| 49 | int r, e, s; |
| 50 | assert(syscall(__NR_getresuid, &r, &e, &s) == 0); |
| 51 | } |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 52 | assert(syscall(__NR_setreuid, -1, -1L) == 0); |
Dmitry V. Levin | 3a15bc8 | 2015-03-02 01:13:47 +0000 | [diff] [blame] | 53 | assert(syscall(__NR_setresuid, uid, -1, -1L) == 0); |
Dmitry V. Levin | 68804b3 | 2015-03-16 18:10:21 +0000 | [diff] [blame] | 54 | assert(syscall(__NR_fchown, 1, -1, -1L) == 0); |
Dmitry V. Levin | 530bed0 | 2014-12-14 13:30:54 +0000 | [diff] [blame] | 55 | assert((size = syscall(__NR_getgroups, 0, list)) >= 0); |
| 56 | assert(list = calloc(size + 1, sizeof(*list))); |
| 57 | assert(syscall(__NR_getgroups, size, list) == size); |
Dmitry V. Levin | 1da7c95 | 2014-12-13 18:24:13 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | #else |
| 60 | return 77; |
| 61 | #endif |
| 62 | } |