18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ioperm.c - Test case for ioperm(2) 48c2ecf20Sopenharmony_ci * Copyright (c) 2015 Andrew Lutomirski 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#define _GNU_SOURCE 88c2ecf20Sopenharmony_ci#include <err.h> 98c2ecf20Sopenharmony_ci#include <stdio.h> 108c2ecf20Sopenharmony_ci#include <stdint.h> 118c2ecf20Sopenharmony_ci#include <signal.h> 128c2ecf20Sopenharmony_ci#include <setjmp.h> 138c2ecf20Sopenharmony_ci#include <stdlib.h> 148c2ecf20Sopenharmony_ci#include <string.h> 158c2ecf20Sopenharmony_ci#include <errno.h> 168c2ecf20Sopenharmony_ci#include <unistd.h> 178c2ecf20Sopenharmony_ci#include <sys/types.h> 188c2ecf20Sopenharmony_ci#include <sys/wait.h> 198c2ecf20Sopenharmony_ci#include <stdbool.h> 208c2ecf20Sopenharmony_ci#include <sched.h> 218c2ecf20Sopenharmony_ci#include <sys/io.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic int nerrs = 0; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), 268c2ecf20Sopenharmony_ci int flags) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct sigaction sa; 298c2ecf20Sopenharmony_ci memset(&sa, 0, sizeof(sa)); 308c2ecf20Sopenharmony_ci sa.sa_sigaction = handler; 318c2ecf20Sopenharmony_ci sa.sa_flags = SA_SIGINFO | flags; 328c2ecf20Sopenharmony_ci sigemptyset(&sa.sa_mask); 338c2ecf20Sopenharmony_ci if (sigaction(sig, &sa, 0)) 348c2ecf20Sopenharmony_ci err(1, "sigaction"); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic void clearhandler(int sig) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci struct sigaction sa; 418c2ecf20Sopenharmony_ci memset(&sa, 0, sizeof(sa)); 428c2ecf20Sopenharmony_ci sa.sa_handler = SIG_DFL; 438c2ecf20Sopenharmony_ci sigemptyset(&sa.sa_mask); 448c2ecf20Sopenharmony_ci if (sigaction(sig, &sa, 0)) 458c2ecf20Sopenharmony_ci err(1, "sigaction"); 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic jmp_buf jmpbuf; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void sigsegv(int sig, siginfo_t *si, void *ctx_void) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci siglongjmp(jmpbuf, 1); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic bool try_outb(unsigned short port) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci sethandler(SIGSEGV, sigsegv, SA_RESETHAND); 588c2ecf20Sopenharmony_ci if (sigsetjmp(jmpbuf, 1) != 0) { 598c2ecf20Sopenharmony_ci return false; 608c2ecf20Sopenharmony_ci } else { 618c2ecf20Sopenharmony_ci asm volatile ("outb %%al, %w[port]" 628c2ecf20Sopenharmony_ci : : [port] "Nd" (port), "a" (0)); 638c2ecf20Sopenharmony_ci return true; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci clearhandler(SIGSEGV); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic void expect_ok(unsigned short port) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci if (!try_outb(port)) { 718c2ecf20Sopenharmony_ci printf("[FAIL]\toutb to 0x%02hx failed\n", port); 728c2ecf20Sopenharmony_ci exit(1); 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci printf("[OK]\toutb to 0x%02hx worked\n", port); 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic void expect_gp(unsigned short port) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci if (try_outb(port)) { 818c2ecf20Sopenharmony_ci printf("[FAIL]\toutb to 0x%02hx worked\n", port); 828c2ecf20Sopenharmony_ci exit(1); 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci printf("[OK]\toutb to 0x%02hx failed\n", port); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ciint main(void) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci cpu_set_t cpuset; 918c2ecf20Sopenharmony_ci CPU_ZERO(&cpuset); 928c2ecf20Sopenharmony_ci CPU_SET(0, &cpuset); 938c2ecf20Sopenharmony_ci if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) 948c2ecf20Sopenharmony_ci err(1, "sched_setaffinity to CPU 0"); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci expect_gp(0x80); 978c2ecf20Sopenharmony_ci expect_gp(0xed); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* 1008c2ecf20Sopenharmony_ci * Probe for ioperm support. Note that clearing ioperm bits 1018c2ecf20Sopenharmony_ci * works even as nonroot. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci printf("[RUN]\tenable 0x80\n"); 1048c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 1) != 0) { 1058c2ecf20Sopenharmony_ci printf("[OK]\tioperm(0x80, 1, 1) failed (%d) -- try running as root\n", 1068c2ecf20Sopenharmony_ci errno); 1078c2ecf20Sopenharmony_ci return 0; 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci expect_ok(0x80); 1108c2ecf20Sopenharmony_ci expect_gp(0xed); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci printf("[RUN]\tdisable 0x80\n"); 1138c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 0) != 0) { 1148c2ecf20Sopenharmony_ci printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno); 1158c2ecf20Sopenharmony_ci return 1; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci expect_gp(0x80); 1188c2ecf20Sopenharmony_ci expect_gp(0xed); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* Make sure that fork() preserves ioperm. */ 1218c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 1) != 0) { 1228c2ecf20Sopenharmony_ci printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno); 1238c2ecf20Sopenharmony_ci return 1; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci pid_t child = fork(); 1278c2ecf20Sopenharmony_ci if (child == -1) 1288c2ecf20Sopenharmony_ci err(1, "fork"); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci if (child == 0) { 1318c2ecf20Sopenharmony_ci printf("[RUN]\tchild: check that we inherited permissions\n"); 1328c2ecf20Sopenharmony_ci expect_ok(0x80); 1338c2ecf20Sopenharmony_ci expect_gp(0xed); 1348c2ecf20Sopenharmony_ci printf("[RUN]\tchild: Extend permissions to 0x81\n"); 1358c2ecf20Sopenharmony_ci if (ioperm(0x81, 1, 1) != 0) { 1368c2ecf20Sopenharmony_ci printf("[FAIL]\tioperm(0x81, 1, 1) failed (%d)", errno); 1378c2ecf20Sopenharmony_ci return 1; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci printf("[RUN]\tchild: Drop permissions to 0x80\n"); 1408c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 0) != 0) { 1418c2ecf20Sopenharmony_ci printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno); 1428c2ecf20Sopenharmony_ci return 1; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci expect_gp(0x80); 1458c2ecf20Sopenharmony_ci return 0; 1468c2ecf20Sopenharmony_ci } else { 1478c2ecf20Sopenharmony_ci int status; 1488c2ecf20Sopenharmony_ci if (waitpid(child, &status, 0) != child || 1498c2ecf20Sopenharmony_ci !WIFEXITED(status)) { 1508c2ecf20Sopenharmony_ci printf("[FAIL]\tChild died\n"); 1518c2ecf20Sopenharmony_ci nerrs++; 1528c2ecf20Sopenharmony_ci } else if (WEXITSTATUS(status) != 0) { 1538c2ecf20Sopenharmony_ci printf("[FAIL]\tChild failed\n"); 1548c2ecf20Sopenharmony_ci nerrs++; 1558c2ecf20Sopenharmony_ci } else { 1568c2ecf20Sopenharmony_ci printf("[OK]\tChild succeeded\n"); 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* Verify that the child dropping 0x80 did not affect the parent */ 1618c2ecf20Sopenharmony_ci printf("\tVerify that unsharing the bitmap worked\n"); 1628c2ecf20Sopenharmony_ci expect_ok(0x80); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Test the capability checks. */ 1658c2ecf20Sopenharmony_ci printf("\tDrop privileges\n"); 1668c2ecf20Sopenharmony_ci if (setresuid(1, 1, 1) != 0) { 1678c2ecf20Sopenharmony_ci printf("[WARN]\tDropping privileges failed\n"); 1688c2ecf20Sopenharmony_ci return 0; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci printf("[RUN]\tdisable 0x80\n"); 1728c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 0) != 0) { 1738c2ecf20Sopenharmony_ci printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno); 1748c2ecf20Sopenharmony_ci return 1; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci printf("[OK]\tit worked\n"); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci printf("[RUN]\tenable 0x80 again\n"); 1798c2ecf20Sopenharmony_ci if (ioperm(0x80, 1, 1) == 0) { 1808c2ecf20Sopenharmony_ci printf("[FAIL]\tit succeeded but should have failed.\n"); 1818c2ecf20Sopenharmony_ci return 1; 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci printf("[OK]\tit failed\n"); 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci} 186