Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/x86/syscall: Update and extend syscall_numbering_64

Update the syscall_numbering_64 selftest to reflect that a system call is
to be extended from 32 bits. Add a mix of tests for valid and invalid
system calls in 64-bit and x32 space.

Use an explicit system call instruction, because the glibc syscall()
wrapper might intercept instructions, extend the system call number
independently, or anything similar.

Use long long instead of long to make it possible to compile this test
on x32 as well as 64 bits.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20210518191303.4135296-2-hpa@zytor.com

authored by

H. Peter Anvin (Intel) and committed by
Thomas Gleixner
15c82d98 3cba325b

+228 -58
+228 -58
tools/testing/selftests/x86/syscall_numbering.c
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* 3 - * syscall_arg_fault.c - tests faults 32-bit fast syscall stack args 3 + * syscall_numbering.c - test calling the x86-64 kernel with various 4 + * valid and invalid system call numbers. 5 + * 4 6 * Copyright (c) 2018 Andrew Lutomirski 5 7 */ 6 8 ··· 13 11 #include <stdbool.h> 14 12 #include <errno.h> 15 13 #include <unistd.h> 16 - #include <syscall.h> 14 + #include <string.h> 15 + #include <fcntl.h> 16 + #include <limits.h> 17 17 18 - static int nerrs; 18 + /* Common system call numbers */ 19 + #define SYS_READ 0 20 + #define SYS_WRITE 1 21 + #define SYS_GETPID 39 22 + /* x64-only system call numbers */ 23 + #define X64_IOCTL 16 24 + #define X64_READV 19 25 + #define X64_WRITEV 20 26 + /* x32-only system call numbers (without X32_BIT) */ 27 + #define X32_IOCTL 514 28 + #define X32_READV 515 29 + #define X32_WRITEV 516 19 30 20 - #define X32_BIT 0x40000000UL 31 + #define X32_BIT 0x40000000 21 32 22 - static void check_enosys(unsigned long nr, bool *ok) 33 + static unsigned int nerr = 0; /* Cumulative error count */ 34 + static int nullfd = -1; /* File descriptor for /dev/null */ 35 + 36 + /* 37 + * Directly invokes the given syscall with nullfd as the first argument 38 + * and the rest zero. Avoids involving glibc wrappers in case they ever 39 + * end up intercepting some system calls for some reason, or modify 40 + * the system call number itself. 41 + */ 42 + static inline long long probe_syscall(int msb, int lsb) 23 43 { 24 - /* If this fails, a segfault is reasonably likely. */ 25 - fflush(stdout); 44 + register long long arg1 asm("rdi") = nullfd; 45 + register long long arg2 asm("rsi") = 0; 46 + register long long arg3 asm("rdx") = 0; 47 + register long long arg4 asm("r10") = 0; 48 + register long long arg5 asm("r8") = 0; 49 + register long long arg6 asm("r9") = 0; 50 + long long nr = ((long long)msb << 32) | (unsigned int)lsb; 51 + long long ret; 26 52 27 - long ret = syscall(nr, 0, 0, 0, 0, 0, 0); 28 - if (ret == 0) { 29 - printf("[FAIL]\tsyscall %lu succeeded, but it should have failed\n", nr); 30 - *ok = false; 31 - } else if (errno != ENOSYS) { 32 - printf("[FAIL]\tsyscall %lu had error code %d, but it should have reported ENOSYS\n", nr, errno); 33 - *ok = false; 34 - } 53 + asm volatile("syscall" 54 + : "=a" (ret) 55 + : "a" (nr), "r" (arg1), "r" (arg2), "r" (arg3), 56 + "r" (arg4), "r" (arg5), "r" (arg6) 57 + : "rcx", "r11", "memory", "cc"); 58 + 59 + return ret; 35 60 } 36 61 37 - static void test_x32_without_x32_bit(void) 62 + static const char *syscall_str(int msb, int start, int end) 38 63 { 39 - bool ok = true; 64 + static char buf[64]; 65 + const char * const type = (start & X32_BIT) ? "x32" : "x64"; 66 + int lsb = start; 40 67 41 68 /* 42 - * Syscalls 512-547 are "x32" syscalls. They are intended to be 43 - * called with the x32 (0x40000000) bit set. Calling them without 44 - * the x32 bit set is nonsense and should not work. 69 + * Improve readability by stripping the x32 bit, but round 70 + * toward zero so we don't display -1 as -1073741825. 45 71 */ 46 - printf("[RUN]\tChecking syscalls 512-547\n"); 47 - for (int i = 512; i <= 547; i++) 48 - check_enosys(i, &ok); 49 - 50 - /* 51 - * Check that a handful of 64-bit-only syscalls are rejected if the x32 52 - * bit is set. 53 - */ 54 - printf("[RUN]\tChecking some 64-bit syscalls in x32 range\n"); 55 - check_enosys(16 | X32_BIT, &ok); /* ioctl */ 56 - check_enosys(19 | X32_BIT, &ok); /* readv */ 57 - check_enosys(20 | X32_BIT, &ok); /* writev */ 58 - 59 - /* 60 - * Check some syscalls with high bits set. 61 - */ 62 - printf("[RUN]\tChecking numbers above 2^32-1\n"); 63 - check_enosys((1UL << 32), &ok); 64 - check_enosys(X32_BIT | (1UL << 32), &ok); 65 - 66 - if (!ok) 67 - nerrs++; 72 + if (lsb < 0) 73 + lsb |= X32_BIT; 68 74 else 69 - printf("[OK]\tThey all returned -ENOSYS\n"); 75 + lsb &= ~X32_BIT; 76 + 77 + if (start == end) 78 + snprintf(buf, sizeof buf, "%s syscall %d:%d", 79 + type, msb, lsb); 80 + else 81 + snprintf(buf, sizeof buf, "%s syscalls %d:%d..%d", 82 + type, msb, lsb, lsb + (end-start)); 83 + 84 + return buf; 70 85 } 71 86 72 - int main() 87 + static unsigned int _check_for(int msb, int start, int end, long long expect, 88 + const char *expect_str) 73 89 { 74 - /* 75 - * Anyone diagnosing a failure will want to know whether the kernel 76 - * supports x32. Tell them. 77 - */ 78 - printf("\tChecking for x32..."); 79 - fflush(stdout); 80 - if (syscall(39 | X32_BIT, 0, 0, 0, 0, 0, 0) >= 0) { 81 - printf(" supported\n"); 82 - } else if (errno == ENOSYS) { 83 - printf(" not supported\n"); 84 - } else { 85 - printf(" confused\n"); 90 + unsigned int err = 0; 91 + 92 + for (int nr = start; nr <= end; nr++) { 93 + long long ret = probe_syscall(msb, nr); 94 + 95 + if (ret != expect) { 96 + printf("[FAIL]\t %s returned %lld, but it should have returned %s\n", 97 + syscall_str(msb, nr, nr), 98 + ret, expect_str); 99 + err++; 100 + } 86 101 } 87 102 88 - test_x32_without_x32_bit(); 103 + if (err) { 104 + nerr += err; 105 + if (start != end) 106 + printf("[FAIL]\t %s had %u failure%s\n", 107 + syscall_str(msb, start, end), 108 + err, (err == 1) ? "s" : ""); 109 + } else { 110 + printf("[OK]\t %s returned %s as expected\n", 111 + syscall_str(msb, start, end), expect_str); 112 + } 89 113 90 - return nerrs ? 1 : 0; 114 + return err; 115 + } 116 + 117 + #define check_for(msb,start,end,expect) \ 118 + _check_for(msb,start,end,expect,#expect) 119 + 120 + static bool check_zero(int msb, int nr) 121 + { 122 + return check_for(msb, nr, nr, 0); 123 + } 124 + 125 + static bool check_enosys(int msb, int nr) 126 + { 127 + return check_for(msb, nr, nr, -ENOSYS); 128 + } 129 + 130 + /* 131 + * Anyone diagnosing a failure will want to know whether the kernel 132 + * supports x32. Tell them. This can also be used to conditionalize 133 + * tests based on existence or nonexistence of x32. 134 + */ 135 + static bool test_x32(void) 136 + { 137 + long long ret; 138 + long long mypid = getpid(); 139 + 140 + printf("[RUN]\tChecking for x32 by calling x32 getpid()\n"); 141 + ret = probe_syscall(0, SYS_GETPID | X32_BIT); 142 + 143 + if (ret == mypid) { 144 + printf("[INFO]\t x32 is supported\n"); 145 + return true; 146 + } else if (ret == -ENOSYS) { 147 + printf("[INFO]\t x32 is not supported\n"); 148 + return false; 149 + } else { 150 + printf("[FAIL]\t x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid); 151 + nerr++; 152 + return true; /* Proceed as if... */ 153 + } 154 + } 155 + 156 + static void test_syscalls_common(int msb) 157 + { 158 + printf("[RUN]\t Checking some common syscalls as 64 bit\n"); 159 + check_zero(msb, SYS_READ); 160 + check_zero(msb, SYS_WRITE); 161 + 162 + printf("[RUN]\t Checking some 64-bit only syscalls as 64 bit\n"); 163 + check_zero(msb, X64_READV); 164 + check_zero(msb, X64_WRITEV); 165 + 166 + printf("[RUN]\t Checking out of range system calls\n"); 167 + check_for(msb, -64, -1, -ENOSYS); 168 + check_for(msb, X32_BIT-64, X32_BIT-1, -ENOSYS); 169 + check_for(msb, -64-X32_BIT, -1-X32_BIT, -ENOSYS); 170 + check_for(msb, INT_MAX-64, INT_MAX-1, -ENOSYS); 171 + } 172 + 173 + static void test_syscalls_with_x32(int msb) 174 + { 175 + /* 176 + * Syscalls 512-547 are "x32" syscalls. They are 177 + * intended to be called with the x32 (0x40000000) bit 178 + * set. Calling them without the x32 bit set is 179 + * nonsense and should not work. 180 + */ 181 + printf("[RUN]\t Checking x32 syscalls as 64 bit\n"); 182 + check_for(msb, 512, 547, -ENOSYS); 183 + 184 + printf("[RUN]\t Checking some common syscalls as x32\n"); 185 + check_zero(msb, SYS_READ | X32_BIT); 186 + check_zero(msb, SYS_WRITE | X32_BIT); 187 + 188 + printf("[RUN]\t Checking some x32 syscalls as x32\n"); 189 + check_zero(msb, X32_READV | X32_BIT); 190 + check_zero(msb, X32_WRITEV | X32_BIT); 191 + 192 + printf("[RUN]\t Checking some 64-bit syscalls as x32\n"); 193 + check_enosys(msb, X64_IOCTL | X32_BIT); 194 + check_enosys(msb, X64_READV | X32_BIT); 195 + check_enosys(msb, X64_WRITEV | X32_BIT); 196 + } 197 + 198 + static void test_syscalls_without_x32(int msb) 199 + { 200 + printf("[RUN]\t Checking for absence of x32 system calls\n"); 201 + check_for(msb, 0 | X32_BIT, 999 | X32_BIT, -ENOSYS); 202 + } 203 + 204 + static void test_syscall_numbering(void) 205 + { 206 + static const int msbs[] = { 207 + 0, 1, -1, X32_BIT-1, X32_BIT, X32_BIT-1, -X32_BIT, INT_MAX, 208 + INT_MIN, INT_MIN+1 209 + }; 210 + bool with_x32 = test_x32(); 211 + 212 + /* 213 + * The MSB is supposed to be ignored, so we loop over a few 214 + * to test that out. 215 + */ 216 + for (size_t i = 0; i < sizeof(msbs)/sizeof(msbs[0]); i++) { 217 + int msb = msbs[i]; 218 + printf("[RUN]\tChecking system calls with msb = %d (0x%x)\n", 219 + msb, msb); 220 + 221 + test_syscalls_common(msb); 222 + if (with_x32) 223 + test_syscalls_with_x32(msb); 224 + else 225 + test_syscalls_without_x32(msb); 226 + } 227 + } 228 + 229 + int main(void) 230 + { 231 + /* 232 + * It is quite likely to get a segfault on a failure, so make 233 + * sure the message gets out by setting stdout to nonbuffered. 234 + */ 235 + setvbuf(stdout, NULL, _IONBF, 0); 236 + 237 + /* 238 + * Harmless file descriptor to work on... 239 + */ 240 + nullfd = open("/dev/null", O_RDWR); 241 + if (nullfd < 0) { 242 + printf("[FAIL]\tUnable to open /dev/null: %s\n", 243 + strerror(errno)); 244 + printf("[SKIP]\tCannot execute test\n"); 245 + return 71; /* EX_OSERR */ 246 + } 247 + 248 + test_syscall_numbering(); 249 + if (!nerr) { 250 + printf("[OK]\tAll system calls succeeded or failed as expected\n"); 251 + return 0; 252 + } else { 253 + printf("[FAIL]\tA total of %u system call%s had incorrect behavior\n", 254 + nerr, nerr != 1 ? "s" : ""); 255 + return 1; 256 + } 91 257 }