Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ioperm.c - Test case for ioperm(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2015 Andrew Lutomirski
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <setjmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <sys/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int nerrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		       int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct sigaction sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	memset(&sa, 0, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	sa.sa_sigaction = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	sa.sa_flags = SA_SIGINFO | flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	sigemptyset(&sa.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (sigaction(sig, &sa, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		err(1, "sigaction");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static void clearhandler(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct sigaction sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	memset(&sa, 0, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	sa.sa_handler = SIG_DFL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	sigemptyset(&sa.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (sigaction(sig, &sa, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		err(1, "sigaction");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static jmp_buf jmpbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	siglongjmp(jmpbuf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static bool try_outb(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (sigsetjmp(jmpbuf, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		asm volatile ("outb %%al, %w[port]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			      : : [port] "Nd" (port), "a" (0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	clearhandler(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void expect_ok(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!try_outb(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		printf("[FAIL]\toutb to 0x%02hx failed\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	printf("[OK]\toutb to 0x%02hx worked\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void expect_gp(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (try_outb(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		printf("[FAIL]\toutb to 0x%02hx worked\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	printf("[OK]\toutb to 0x%02hx failed\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	CPU_SET(0, &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		err(1, "sched_setaffinity to CPU 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	expect_gp(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	expect_gp(0xed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * Probe for ioperm support.  Note that clearing ioperm bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * works even as nonroot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	printf("[RUN]\tenable 0x80\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (ioperm(0x80, 1, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		printf("[OK]\tioperm(0x80, 1, 1) failed (%d) -- try running as root\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		       errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	expect_ok(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	expect_gp(0xed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	printf("[RUN]\tdisable 0x80\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ioperm(0x80, 1, 0) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	expect_gp(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	expect_gp(0xed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/* Make sure that fork() preserves ioperm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ioperm(0x80, 1, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	pid_t child = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (child == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		err(1, "fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (child == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		printf("[RUN]\tchild: check that we inherited permissions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		expect_ok(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		expect_gp(0xed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		printf("[RUN]\tchild: Extend permissions to 0x81\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (ioperm(0x81, 1, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			printf("[FAIL]\tioperm(0x81, 1, 1) failed (%d)", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		printf("[RUN]\tchild: Drop permissions to 0x80\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (ioperm(0x80, 1, 0) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		expect_gp(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (waitpid(child, &status, 0) != child ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		    !WIFEXITED(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			printf("[FAIL]\tChild died\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		} else if (WEXITSTATUS(status) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			printf("[FAIL]\tChild failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			printf("[OK]\tChild succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* Verify that the child dropping 0x80 did not affect the parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	printf("\tVerify that unsharing the bitmap worked\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	expect_ok(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/* Test the capability checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	printf("\tDrop privileges\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (setresuid(1, 1, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		printf("[WARN]\tDropping privileges failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	printf("[RUN]\tdisable 0x80\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (ioperm(0x80, 1, 0) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	printf("[OK]\tit worked\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	printf("[RUN]\tenable 0x80 again\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (ioperm(0x80, 1, 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		printf("[FAIL]\tit succeeded but should have failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	printf("[OK]\tit failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }