1 // SPDX-License-Identifier: GPL-2.0-only 2 #define __SANE_USERSPACE_TYPES__ 3 #include <sys/syscall.h> 4 #include <unistd.h> 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <string.h> 8 9 #include "pkey-helpers.h" 10 11 int iteration_nr = 1; 12 int test_nr; 13 int dprint_in_signal; 14 15 #if CONTROL_TRACING > 0 16 static void cat_into_file(char *str, char *file) 17 { 18 int fd = open(file, O_RDWR); 19 int ret; 20 21 dprintf2("%s(): writing '%s' to '%s'\n", __func__, str, file); 22 /* 23 * these need to be raw because they are called under 24 * pkey_assert() 25 */ 26 if (fd < 0) { 27 fprintf(stderr, "error opening '%s'\n", file); 28 perror("error: "); 29 exit(__LINE__); 30 } 31 32 ret = write(fd, str, strlen(str)); 33 if (ret != strlen(str)) { 34 perror("write to file failed"); 35 fprintf(stderr, "filename: '%s' str: '%s'\n", file, str); 36 exit(__LINE__); 37 } 38 close(fd); 39 } 40 41 static int warned_tracing; 42 static int tracing_root_ok(void) 43 { 44 if (geteuid() != 0) { 45 if (!warned_tracing) 46 fprintf(stderr, "WARNING: not run as root, " 47 "can not do tracing control\n"); 48 warned_tracing = 1; 49 return 0; 50 } 51 return 1; 52 } 53 #endif 54 55 void tracing_on(void) 56 { 57 #if CONTROL_TRACING > 0 58 #define TRACEDIR "/sys/kernel/tracing" 59 char pidstr[32]; 60 61 if (!tracing_root_ok()) 62 return; 63 64 sprintf(pidstr, "%d", getpid()); 65 cat_into_file("0", TRACEDIR "/tracing_on"); 66 cat_into_file("\n", TRACEDIR "/trace"); 67 if (1) { 68 cat_into_file("function_graph", TRACEDIR "/current_tracer"); 69 cat_into_file("1", TRACEDIR "/options/funcgraph-proc"); 70 } else { 71 cat_into_file("nop", TRACEDIR "/current_tracer"); 72 } 73 cat_into_file(pidstr, TRACEDIR "/set_ftrace_pid"); 74 cat_into_file("1", TRACEDIR "/tracing_on"); 75 dprintf1("enabled tracing\n"); 76 #endif 77 } 78 79 void tracing_off(void) 80 { 81 #if CONTROL_TRACING > 0 82 if (!tracing_root_ok()) 83 return; 84 cat_into_file("0", "/sys/kernel/tracing/tracing_on"); 85 #endif 86 } 87 88 void abort_hooks(void) 89 { 90 fflush(stdout); 91 fprintf(stderr, "running %s()...\n", __func__); 92 tracing_off(); 93 #ifdef SLEEP_ON_ABORT 94 sleep(SLEEP_ON_ABORT); 95 #endif 96 } 97 98 int sys_pkey_alloc(unsigned long flags, unsigned long init_val) 99 { 100 int ret = syscall(SYS_pkey_alloc, flags, init_val); 101 dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n", 102 __func__, flags, init_val, ret, errno); 103 return ret; 104 } 105 106 int sys_pkey_free(unsigned long pkey) 107 { 108 int ret = syscall(SYS_pkey_free, pkey); 109 dprintf1("%s(pkey=%ld) syscall ret: %d\n", __func__, pkey, ret); 110 return ret; 111 } 112 113 int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, 114 unsigned long pkey) 115 { 116 int sret; 117 118 dprintf2("%s(0x%p, %zx, prot=%lx, pkey=%lx)\n", __func__, 119 ptr, size, orig_prot, pkey); 120 121 errno = 0; 122 sret = syscall(__NR_pkey_mprotect, ptr, size, orig_prot, pkey); 123 if (errno) { 124 dprintf2("SYS_mprotect_key sret: %d\n", sret); 125 dprintf2("SYS_mprotect_key prot: 0x%lx\n", orig_prot); 126 dprintf2("SYS_mprotect_key failed, errno: %d\n", errno); 127 if (DEBUG_LEVEL >= 2) 128 perror("SYS_mprotect_pkey"); 129 } 130 return sret; 131 } 132