Skip to content

Commit 15c82d9

Browse files
H. Peter Anvin (Intel)KAGA-KOKO
authored andcommitted
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
1 parent 3cba325 commit 15c82d9

File tree

1 file changed

+222
-52
lines changed

1 file changed

+222
-52
lines changed
Lines changed: 222 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
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+
*
46
* Copyright (c) 2018 Andrew Lutomirski
57
*/
68

@@ -11,79 +13,247 @@
1113
#include <stdbool.h>
1214
#include <errno.h>
1315
#include <unistd.h>
14-
#include <syscall.h>
16+
#include <string.h>
17+
#include <fcntl.h>
18+
#include <limits.h>
1519

16-
static int nerrs;
20+
/* Common system call numbers */
21+
#define SYS_READ 0
22+
#define SYS_WRITE 1
23+
#define SYS_GETPID 39
24+
/* x64-only system call numbers */
25+
#define X64_IOCTL 16
26+
#define X64_READV 19
27+
#define X64_WRITEV 20
28+
/* x32-only system call numbers (without X32_BIT) */
29+
#define X32_IOCTL 514
30+
#define X32_READV 515
31+
#define X32_WRITEV 516
1732

18-
#define X32_BIT 0x40000000UL
33+
#define X32_BIT 0x40000000
1934

20-
static void check_enosys(unsigned long nr, bool *ok)
35+
static unsigned int nerr = 0; /* Cumulative error count */
36+
static int nullfd = -1; /* File descriptor for /dev/null */
37+
38+
/*
39+
* Directly invokes the given syscall with nullfd as the first argument
40+
* and the rest zero. Avoids involving glibc wrappers in case they ever
41+
* end up intercepting some system calls for some reason, or modify
42+
* the system call number itself.
43+
*/
44+
static inline long long probe_syscall(int msb, int lsb)
2145
{
22-
/* If this fails, a segfault is reasonably likely. */
23-
fflush(stdout);
24-
25-
long ret = syscall(nr, 0, 0, 0, 0, 0, 0);
26-
if (ret == 0) {
27-
printf("[FAIL]\tsyscall %lu succeeded, but it should have failed\n", nr);
28-
*ok = false;
29-
} else if (errno != ENOSYS) {
30-
printf("[FAIL]\tsyscall %lu had error code %d, but it should have reported ENOSYS\n", nr, errno);
31-
*ok = false;
32-
}
46+
register long long arg1 asm("rdi") = nullfd;
47+
register long long arg2 asm("rsi") = 0;
48+
register long long arg3 asm("rdx") = 0;
49+
register long long arg4 asm("r10") = 0;
50+
register long long arg5 asm("r8") = 0;
51+
register long long arg6 asm("r9") = 0;
52+
long long nr = ((long long)msb << 32) | (unsigned int)lsb;
53+
long long ret;
54+
55+
asm volatile("syscall"
56+
: "=a" (ret)
57+
: "a" (nr), "r" (arg1), "r" (arg2), "r" (arg3),
58+
"r" (arg4), "r" (arg5), "r" (arg6)
59+
: "rcx", "r11", "memory", "cc");
60+
61+
return ret;
3362
}
3463

35-
static void test_x32_without_x32_bit(void)
64+
static const char *syscall_str(int msb, int start, int end)
3665
{
37-
bool ok = true;
66+
static char buf[64];
67+
const char * const type = (start & X32_BIT) ? "x32" : "x64";
68+
int lsb = start;
3869

3970
/*
40-
* Syscalls 512-547 are "x32" syscalls. They are intended to be
41-
* called with the x32 (0x40000000) bit set. Calling them without
42-
* the x32 bit set is nonsense and should not work.
71+
* Improve readability by stripping the x32 bit, but round
72+
* toward zero so we don't display -1 as -1073741825.
4373
*/
44-
printf("[RUN]\tChecking syscalls 512-547\n");
45-
for (int i = 512; i <= 547; i++)
46-
check_enosys(i, &ok);
74+
if (lsb < 0)
75+
lsb |= X32_BIT;
76+
else
77+
lsb &= ~X32_BIT;
78+
79+
if (start == end)
80+
snprintf(buf, sizeof buf, "%s syscall %d:%d",
81+
type, msb, lsb);
82+
else
83+
snprintf(buf, sizeof buf, "%s syscalls %d:%d..%d",
84+
type, msb, lsb, lsb + (end-start));
85+
86+
return buf;
87+
}
88+
89+
static unsigned int _check_for(int msb, int start, int end, long long expect,
90+
const char *expect_str)
91+
{
92+
unsigned int err = 0;
93+
94+
for (int nr = start; nr <= end; nr++) {
95+
long long ret = probe_syscall(msb, nr);
96+
97+
if (ret != expect) {
98+
printf("[FAIL]\t %s returned %lld, but it should have returned %s\n",
99+
syscall_str(msb, nr, nr),
100+
ret, expect_str);
101+
err++;
102+
}
103+
}
47104

105+
if (err) {
106+
nerr += err;
107+
if (start != end)
108+
printf("[FAIL]\t %s had %u failure%s\n",
109+
syscall_str(msb, start, end),
110+
err, (err == 1) ? "s" : "");
111+
} else {
112+
printf("[OK]\t %s returned %s as expected\n",
113+
syscall_str(msb, start, end), expect_str);
114+
}
115+
116+
return err;
117+
}
118+
119+
#define check_for(msb,start,end,expect) \
120+
_check_for(msb,start,end,expect,#expect)
121+
122+
static bool check_zero(int msb, int nr)
123+
{
124+
return check_for(msb, nr, nr, 0);
125+
}
126+
127+
static bool check_enosys(int msb, int nr)
128+
{
129+
return check_for(msb, nr, nr, -ENOSYS);
130+
}
131+
132+
/*
133+
* Anyone diagnosing a failure will want to know whether the kernel
134+
* supports x32. Tell them. This can also be used to conditionalize
135+
* tests based on existence or nonexistence of x32.
136+
*/
137+
static bool test_x32(void)
138+
{
139+
long long ret;
140+
long long mypid = getpid();
141+
142+
printf("[RUN]\tChecking for x32 by calling x32 getpid()\n");
143+
ret = probe_syscall(0, SYS_GETPID | X32_BIT);
144+
145+
if (ret == mypid) {
146+
printf("[INFO]\t x32 is supported\n");
147+
return true;
148+
} else if (ret == -ENOSYS) {
149+
printf("[INFO]\t x32 is not supported\n");
150+
return false;
151+
} else {
152+
printf("[FAIL]\t x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid);
153+
nerr++;
154+
return true; /* Proceed as if... */
155+
}
156+
}
157+
158+
static void test_syscalls_common(int msb)
159+
{
160+
printf("[RUN]\t Checking some common syscalls as 64 bit\n");
161+
check_zero(msb, SYS_READ);
162+
check_zero(msb, SYS_WRITE);
163+
164+
printf("[RUN]\t Checking some 64-bit only syscalls as 64 bit\n");
165+
check_zero(msb, X64_READV);
166+
check_zero(msb, X64_WRITEV);
167+
168+
printf("[RUN]\t Checking out of range system calls\n");
169+
check_for(msb, -64, -1, -ENOSYS);
170+
check_for(msb, X32_BIT-64, X32_BIT-1, -ENOSYS);
171+
check_for(msb, -64-X32_BIT, -1-X32_BIT, -ENOSYS);
172+
check_for(msb, INT_MAX-64, INT_MAX-1, -ENOSYS);
173+
}
174+
175+
static void test_syscalls_with_x32(int msb)
176+
{
48177
/*
49-
* Check that a handful of 64-bit-only syscalls are rejected if the x32
50-
* bit is set.
178+
* Syscalls 512-547 are "x32" syscalls. They are
179+
* intended to be called with the x32 (0x40000000) bit
180+
* set. Calling them without the x32 bit set is
181+
* nonsense and should not work.
51182
*/
52-
printf("[RUN]\tChecking some 64-bit syscalls in x32 range\n");
53-
check_enosys(16 | X32_BIT, &ok); /* ioctl */
54-
check_enosys(19 | X32_BIT, &ok); /* readv */
55-
check_enosys(20 | X32_BIT, &ok); /* writev */
183+
printf("[RUN]\t Checking x32 syscalls as 64 bit\n");
184+
check_for(msb, 512, 547, -ENOSYS);
185+
186+
printf("[RUN]\t Checking some common syscalls as x32\n");
187+
check_zero(msb, SYS_READ | X32_BIT);
188+
check_zero(msb, SYS_WRITE | X32_BIT);
189+
190+
printf("[RUN]\t Checking some x32 syscalls as x32\n");
191+
check_zero(msb, X32_READV | X32_BIT);
192+
check_zero(msb, X32_WRITEV | X32_BIT);
193+
194+
printf("[RUN]\t Checking some 64-bit syscalls as x32\n");
195+
check_enosys(msb, X64_IOCTL | X32_BIT);
196+
check_enosys(msb, X64_READV | X32_BIT);
197+
check_enosys(msb, X64_WRITEV | X32_BIT);
198+
}
199+
200+
static void test_syscalls_without_x32(int msb)
201+
{
202+
printf("[RUN]\t Checking for absence of x32 system calls\n");
203+
check_for(msb, 0 | X32_BIT, 999 | X32_BIT, -ENOSYS);
204+
}
205+
206+
static void test_syscall_numbering(void)
207+
{
208+
static const int msbs[] = {
209+
0, 1, -1, X32_BIT-1, X32_BIT, X32_BIT-1, -X32_BIT, INT_MAX,
210+
INT_MIN, INT_MIN+1
211+
};
212+
bool with_x32 = test_x32();
56213

57214
/*
58-
* Check some syscalls with high bits set.
215+
* The MSB is supposed to be ignored, so we loop over a few
216+
* to test that out.
59217
*/
60-
printf("[RUN]\tChecking numbers above 2^32-1\n");
61-
check_enosys((1UL << 32), &ok);
62-
check_enosys(X32_BIT | (1UL << 32), &ok);
218+
for (size_t i = 0; i < sizeof(msbs)/sizeof(msbs[0]); i++) {
219+
int msb = msbs[i];
220+
printf("[RUN]\tChecking system calls with msb = %d (0x%x)\n",
221+
msb, msb);
63222

64-
if (!ok)
65-
nerrs++;
66-
else
67-
printf("[OK]\tThey all returned -ENOSYS\n");
223+
test_syscalls_common(msb);
224+
if (with_x32)
225+
test_syscalls_with_x32(msb);
226+
else
227+
test_syscalls_without_x32(msb);
228+
}
68229
}
69230

70-
int main()
231+
int main(void)
71232
{
72233
/*
73-
* Anyone diagnosing a failure will want to know whether the kernel
74-
* supports x32. Tell them.
234+
* It is quite likely to get a segfault on a failure, so make
235+
* sure the message gets out by setting stdout to nonbuffered.
75236
*/
76-
printf("\tChecking for x32...");
77-
fflush(stdout);
78-
if (syscall(39 | X32_BIT, 0, 0, 0, 0, 0, 0) >= 0) {
79-
printf(" supported\n");
80-
} else if (errno == ENOSYS) {
81-
printf(" not supported\n");
82-
} else {
83-
printf(" confused\n");
84-
}
237+
setvbuf(stdout, NULL, _IONBF, 0);
85238

86-
test_x32_without_x32_bit();
239+
/*
240+
* Harmless file descriptor to work on...
241+
*/
242+
nullfd = open("/dev/null", O_RDWR);
243+
if (nullfd < 0) {
244+
printf("[FAIL]\tUnable to open /dev/null: %s\n",
245+
strerror(errno));
246+
printf("[SKIP]\tCannot execute test\n");
247+
return 71; /* EX_OSERR */
248+
}
87249

88-
return nerrs ? 1 : 0;
250+
test_syscall_numbering();
251+
if (!nerr) {
252+
printf("[OK]\tAll system calls succeeded or failed as expected\n");
253+
return 0;
254+
} else {
255+
printf("[FAIL]\tA total of %u system call%s had incorrect behavior\n",
256+
nerr, nerr != 1 ? "s" : "");
257+
return 1;
258+
}
89259
}

0 commit comments

Comments
 (0)