]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/testing/selftests/x86/ioperm.c
2 * ioperm.c - Test case for ioperm(2)
3 * Copyright (c) 2015 Andrew Lutomirski
16 #include <sys/types.h>
24 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
28 memset(&sa
, 0, sizeof(sa
));
29 sa
.sa_sigaction
= handler
;
30 sa
.sa_flags
= SA_SIGINFO
| flags
;
31 sigemptyset(&sa
.sa_mask
);
32 if (sigaction(sig
, &sa
, 0))
37 static void clearhandler(int sig
)
40 memset(&sa
, 0, sizeof(sa
));
41 sa
.sa_handler
= SIG_DFL
;
42 sigemptyset(&sa
.sa_mask
);
43 if (sigaction(sig
, &sa
, 0))
47 static jmp_buf jmpbuf
;
49 static void sigsegv(int sig
, siginfo_t
*si
, void *ctx_void
)
51 siglongjmp(jmpbuf
, 1);
54 static bool try_outb(unsigned short port
)
56 sethandler(SIGSEGV
, sigsegv
, SA_RESETHAND
);
57 if (sigsetjmp(jmpbuf
, 1) != 0) {
60 asm volatile ("outb %%al, %w[port]"
61 : : [port
] "Nd" (port
), "a" (0));
64 clearhandler(SIGSEGV
);
67 static void expect_ok(unsigned short port
)
69 if (!try_outb(port
)) {
70 printf("[FAIL]\toutb to 0x%02hx failed\n", port
);
74 printf("[OK]\toutb to 0x%02hx worked\n", port
);
77 static void expect_gp(unsigned short port
)
80 printf("[FAIL]\toutb to 0x%02hx worked\n", port
);
84 printf("[OK]\toutb to 0x%02hx failed\n", port
);
92 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
93 err(1, "sched_setaffinity to CPU 0");
99 * Probe for ioperm support. Note that clearing ioperm bits
100 * works even as nonroot.
102 printf("[RUN]\tenable 0x80\n");
103 if (ioperm(0x80, 1, 1) != 0) {
104 printf("[OK]\tioperm(0x80, 1, 1) failed (%d) -- try running as root\n",
111 printf("[RUN]\tdisable 0x80\n");
112 if (ioperm(0x80, 1, 0) != 0) {
113 printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno
);
119 /* Make sure that fork() preserves ioperm. */
120 if (ioperm(0x80, 1, 1) != 0) {
121 printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno
);
125 pid_t child
= fork();
130 printf("[RUN]\tchild: check that we inherited permissions\n");
136 if (waitpid(child
, &status
, 0) != child
||
137 !WIFEXITED(status
)) {
138 printf("[FAIL]\tChild died\n");
140 } else if (WEXITSTATUS(status
) != 0) {
141 printf("[FAIL]\tChild failed\n");
144 printf("[OK]\tChild succeeded\n");
148 /* Test the capability checks. */
150 printf("\tDrop privileges\n");
151 if (setresuid(1, 1, 1) != 0) {
152 printf("[WARN]\tDropping privileges failed\n");
156 printf("[RUN]\tdisable 0x80\n");
157 if (ioperm(0x80, 1, 0) != 0) {
158 printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno
);
161 printf("[OK]\tit worked\n");
163 printf("[RUN]\tenable 0x80 again\n");
164 if (ioperm(0x80, 1, 1) == 0) {
165 printf("[FAIL]\tit succeeded but should have failed.\n");
168 printf("[OK]\tit failed\n");