Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | #include <signal.h> |
| 13 | #include <sched.h> |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 14 | #include <fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <setjmp.h> |
| 17 | #include <sys/time.h> |
| 18 | #include <sys/wait.h> |
| 19 | #include <sys/mman.h> |
| 20 | #include <asm/unistd.h> |
| 21 | #include <asm/page.h> |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "user_util.h" |
| 24 | #include "kern_util.h" |
| 25 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "signal_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "sysdep/ptrace.h" |
| 28 | #include "sysdep/sigcontext.h" |
| 29 | #include "irq_user.h" |
| 30 | #include "ptrace_user.h" |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 31 | #include "mem_user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "init.h" |
| 33 | #include "os.h" |
| 34 | #include "uml-config.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "choose-mode.h" |
| 36 | #include "mode.h" |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 37 | #include "tempfile.h" |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 38 | #include "kern_constants.h" |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #ifdef UML_CONFIG_MODE_SKAS |
| 41 | #include "skas.h" |
| 42 | #include "skas_ptrace.h" |
| 43 | #include "registers.h" |
| 44 | #endif |
| 45 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 46 | static int ptrace_child(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | int ret; |
| 49 | int pid = os_getpid(), ppid = getppid(); |
| 50 | int sc_result; |
| 51 | |
Jeff Dike | 43b00fd | 2006-02-07 12:58:42 -0800 | [diff] [blame] | 52 | change_sig(SIGWINCH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){ |
| 54 | perror("ptrace"); |
| 55 | os_kill_process(pid, 0); |
| 56 | } |
| 57 | os_stop_process(pid); |
| 58 | |
| 59 | /*This syscall will be intercepted by the parent. Don't call more than |
| 60 | * once, please.*/ |
| 61 | sc_result = os_getpid(); |
| 62 | |
| 63 | if (sc_result == pid) |
| 64 | ret = 1; /*Nothing modified by the parent, we are running |
| 65 | normally.*/ |
| 66 | else if (sc_result == ppid) |
| 67 | ret = 0; /*Expected in check_ptrace and check_sysemu when they |
| 68 | succeed in modifying the stack frame*/ |
| 69 | else |
| 70 | ret = 2; /*Serious trouble! This could be caused by a bug in |
| 71 | host 2.6 SKAS3/2.6 patch before release -V6, together |
| 72 | with a bug in the UML code itself.*/ |
| 73 | _exit(ret); |
| 74 | } |
| 75 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 76 | static int start_ptraced_child(void **stack_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 78 | void *stack; |
| 79 | unsigned long sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int pid, n, status; |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 81 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 82 | stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 83 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 84 | if(stack == MAP_FAILED) |
| 85 | panic("check_ptrace : mmap failed, errno = %d", errno); |
| 86 | sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); |
| 87 | pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if(pid < 0) |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 89 | panic("start_ptraced_child : clone failed, errno = %d", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 91 | if(n < 0) |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 92 | panic("check_ptrace : clone failed, errno = %d", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) |
| 94 | panic("check_ptrace : expected SIGSTOP, got status = %d", |
| 95 | status); |
| 96 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 97 | *stack_out = stack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return(pid); |
| 99 | } |
| 100 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 101 | /* When testing for SYSEMU support, if it is one of the broken versions, we |
| 102 | * must just avoid using sysemu, not panic, but only if SYSEMU features are |
| 103 | * broken. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * So only for SYSEMU features we test mustpanic, while normal host features |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 105 | * must work anyway! |
| 106 | */ |
| 107 | static int stop_ptraced_child(int pid, void *stack, int exitcode, |
| 108 | int mustpanic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | int status, n, ret = 0; |
| 111 | |
| 112 | if(ptrace(PTRACE_CONT, pid, 0, 0) < 0) |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 113 | panic("check_ptrace : ptrace failed, errno = %d", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | CATCH_EINTR(n = waitpid(pid, &status, 0)); |
| 115 | if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) { |
| 116 | int exit_with = WEXITSTATUS(status); |
| 117 | if (exit_with == 2) |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 118 | printf("check_ptrace : child exited with status 2. " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | "Serious trouble happening! Try updating your " |
| 120 | "host skas patch!\nDisabling SYSEMU support."); |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 121 | printf("check_ptrace : child exited with exitcode %d, while " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | "expecting %d; status 0x%x", exit_with, |
| 123 | exitcode, status); |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 124 | if (mustpanic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | panic("\n"); |
| 126 | else |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 127 | printf("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | ret = -1; |
| 129 | } |
| 130 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 131 | if(munmap(stack, PAGE_SIZE) < 0) |
| 132 | panic("check_ptrace : munmap failed, errno = %d", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return ret; |
| 134 | } |
| 135 | |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 136 | int ptrace_faultinfo = 1; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 137 | int ptrace_ldt = 1; |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 138 | int proc_mm = 1; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 139 | int skas_needs_stub = 0; |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 140 | |
| 141 | static int __init skas0_cmd_param(char *str, int* add) |
| 142 | { |
| 143 | ptrace_faultinfo = proc_mm = 0; |
| 144 | return 0; |
| 145 | } |
| 146 | |
Paolo 'Blaisorblade' Giarrusso | 9e3d862 | 2005-10-09 21:37:18 +0200 | [diff] [blame] | 147 | /* The two __uml_setup would conflict, without this stupid alias. */ |
| 148 | |
| 149 | static int __init mode_skas0_cmd_param(char *str, int* add) |
| 150 | __attribute__((alias("skas0_cmd_param"))); |
| 151 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 152 | __uml_setup("skas0", skas0_cmd_param, |
| 153 | "skas0\n" |
| 154 | " Disables SKAS3 usage, so that SKAS0 is used, unless \n" |
| 155 | " you specify mode=tt.\n\n"); |
| 156 | |
Paolo 'Blaisorblade' Giarrusso | 9e3d862 | 2005-10-09 21:37:18 +0200 | [diff] [blame] | 157 | __uml_setup("mode=skas0", mode_skas0_cmd_param, |
| 158 | "mode=skas0\n" |
| 159 | " Disables SKAS3 usage, so that SKAS0 is used, unless you \n" |
| 160 | " specify mode=tt. Note that this was recently added - on \n" |
| 161 | " older kernels you must use simply \"skas0\".\n\n"); |
| 162 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 163 | static int force_sysemu_disabled = 0; |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static int __init nosysemu_cmd_param(char *str, int* add) |
| 166 | { |
| 167 | force_sysemu_disabled = 1; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | __uml_setup("nosysemu", nosysemu_cmd_param, |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 172 | "nosysemu\n" |
| 173 | " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n" |
| 174 | " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n" |
| 175 | " behaviour of ptrace() and helps reducing host context switch rate.\n" |
| 176 | " To make it working, you need a kernel patch for your host, too.\n" |
| 177 | " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n" |
| 178 | " information.\n\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | static void __init check_sysemu(void) |
| 181 | { |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 182 | void *stack; |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 183 | int pid, n, status, count=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 185 | printf("Checking syscall emulation patch for ptrace..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | sysemu_supported = 0; |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 187 | pid = start_ptraced_child(&stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0) |
| 190 | goto fail; |
| 191 | |
| 192 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 193 | if (n < 0) |
| 194 | panic("check_sysemu : wait failed, errno = %d", errno); |
| 195 | if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP)) |
| 196 | panic("check_sysemu : expected SIGTRAP, " |
| 197 | "got status = %d", status); |
| 198 | |
| 199 | n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, |
| 200 | os_getpid()); |
| 201 | if(n < 0) |
| 202 | panic("check_sysemu : failed to modify system " |
| 203 | "call return, errno = %d", errno); |
| 204 | |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 205 | if (stop_ptraced_child(pid, stack, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | goto fail_stopped; |
| 207 | |
| 208 | sysemu_supported = 1; |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 209 | printf("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | set_using_sysemu(!force_sysemu_disabled); |
| 211 | |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 212 | printf("Checking advanced syscall emulation patch for ptrace..."); |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 213 | pid = start_ptraced_child(&stack); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 214 | |
| 215 | if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, |
| 216 | (void *) PTRACE_O_TRACESYSGOOD) < 0) |
| 217 | panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", |
| 218 | errno); |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | while(1){ |
| 221 | count++; |
| 222 | if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0) |
| 223 | goto fail; |
| 224 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 225 | if(n < 0) |
| 226 | panic("check_ptrace : wait failed, errno = %d", errno); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 227 | if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (!count) |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 229 | panic("check_ptrace : SYSEMU_SINGLESTEP " |
| 230 | "doesn't singlestep"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, |
| 232 | os_getpid()); |
| 233 | if(n < 0) |
| 234 | panic("check_sysemu : failed to modify system " |
| 235 | "call return, errno = %d", errno); |
| 236 | break; |
| 237 | } |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 238 | else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP)) |
| 239 | count++; |
| 240 | else |
| 241 | panic("check_ptrace : expected SIGTRAP or " |
| 242 | "(SIGTRAP|0x80), got status = %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 244 | if (stop_ptraced_child(pid, stack, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | goto fail_stopped; |
| 246 | |
| 247 | sysemu_supported = 2; |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 248 | printf("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | if ( !force_sysemu_disabled ) |
| 251 | set_using_sysemu(sysemu_supported); |
| 252 | return; |
| 253 | |
| 254 | fail: |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 255 | stop_ptraced_child(pid, stack, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | fail_stopped: |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 257 | printf("missing\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 260 | static void __init check_ptrace(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 262 | void *stack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int pid, syscall, n, status; |
| 264 | |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 265 | printf("Checking that ptrace can change system call numbers..."); |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 266 | pid = start_ptraced_child(&stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 268 | if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0) |
| 269 | panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | while(1){ |
| 272 | if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 273 | panic("check_ptrace : ptrace failed, errno = %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | errno); |
| 275 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 276 | if(n < 0) |
| 277 | panic("check_ptrace : wait failed, errno = %d", errno); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 278 | if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80))) |
| 279 | panic("check_ptrace : expected (SIGTRAP|0x80), " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | "got status = %d", status); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET, |
| 283 | 0); |
| 284 | if(syscall == __NR_getpid){ |
| 285 | n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, |
| 286 | __NR_getppid); |
| 287 | if(n < 0) |
| 288 | panic("check_ptrace : failed to modify system " |
| 289 | "call, errno = %d", errno); |
| 290 | break; |
| 291 | } |
| 292 | } |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 293 | stop_ptraced_child(pid, stack, 0, 1); |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 294 | printf("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | check_sysemu(); |
| 296 | } |
| 297 | |
Jeff Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 298 | extern int create_tmp_file(unsigned long long len); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 299 | |
| 300 | static void check_tmpexec(void) |
| 301 | { |
| 302 | void *addr; |
| 303 | int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE); |
| 304 | |
| 305 | addr = mmap(NULL, UM_KERN_PAGE_SIZE, |
| 306 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); |
| 307 | printf("Checking PROT_EXEC mmap in /tmp..."); |
| 308 | fflush(stdout); |
| 309 | if(addr == MAP_FAILED){ |
| 310 | err = errno; |
| 311 | perror("failed"); |
| 312 | if(err == EPERM) |
| 313 | printf("/tmp must be not mounted noexec\n"); |
| 314 | exit(1); |
| 315 | } |
| 316 | printf("OK\n"); |
| 317 | munmap(addr, UM_KERN_PAGE_SIZE); |
| 318 | |
| 319 | close(fd); |
| 320 | } |
| 321 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 322 | void os_early_checks(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 324 | check_ptrace(); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 325 | |
| 326 | /* Need to check this early because mmapping happens before the |
| 327 | * kernel is running. |
| 328 | */ |
| 329 | check_tmpexec(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Bodo Stroesser | d9838d8 | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 332 | static int __init noprocmm_cmd_param(char *str, int* add) |
| 333 | { |
| 334 | proc_mm = 0; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | __uml_setup("noprocmm", noprocmm_cmd_param, |
| 339 | "noprocmm\n" |
| 340 | " Turns off usage of /proc/mm, even if host supports it.\n" |
| 341 | " To support /proc/mm, the host needs to be patched using\n" |
| 342 | " the current skas3 patch.\n\n"); |
| 343 | |
| 344 | static int __init noptracefaultinfo_cmd_param(char *str, int* add) |
| 345 | { |
| 346 | ptrace_faultinfo = 0; |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param, |
| 351 | "noptracefaultinfo\n" |
| 352 | " Turns off usage of PTRACE_FAULTINFO, even if host supports\n" |
| 353 | " it. To support PTRACE_FAULTINFO, the host needs to be patched\n" |
| 354 | " using the current skas3 patch.\n\n"); |
| 355 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 356 | static int __init noptraceldt_cmd_param(char *str, int* add) |
| 357 | { |
| 358 | ptrace_ldt = 0; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | __uml_setup("noptraceldt", noptraceldt_cmd_param, |
| 363 | "noptraceldt\n" |
| 364 | " Turns off usage of PTRACE_LDT, even if host supports it.\n" |
| 365 | " To support PTRACE_LDT, the host needs to be patched using\n" |
| 366 | " the current skas3 patch.\n\n"); |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | #ifdef UML_CONFIG_MODE_SKAS |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 369 | static inline void check_skas3_ptrace_faultinfo(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct ptrace_faultinfo fi; |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 372 | void *stack; |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 373 | int pid, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 375 | printf(" - PTRACE_FAULTINFO..."); |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 376 | pid = start_ptraced_child(&stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi); |
| 379 | if (n < 0) { |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 380 | ptrace_faultinfo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if(errno == EIO) |
| 382 | printf("not found\n"); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 383 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | perror("not found"); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 385 | } |
| 386 | else { |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 387 | if (!ptrace_faultinfo) |
| 388 | printf("found but disabled on command line\n"); |
| 389 | else |
| 390 | printf("found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | init_registers(pid); |
Jeff Dike | b85e968 | 2005-07-28 21:16:01 -0700 | [diff] [blame] | 394 | stop_ptraced_child(pid, stack, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 397 | static inline void check_skas3_ptrace_ldt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 399 | #ifdef PTRACE_LDT |
| 400 | void *stack; |
| 401 | int pid, n; |
| 402 | unsigned char ldtbuf[40]; |
| 403 | struct ptrace_ldt ldt_op = (struct ptrace_ldt) { |
| 404 | .func = 2, /* read default ldt */ |
| 405 | .ptr = ldtbuf, |
| 406 | .bytecount = sizeof(ldtbuf)}; |
| 407 | |
| 408 | printf(" - PTRACE_LDT..."); |
| 409 | pid = start_ptraced_child(&stack); |
| 410 | |
| 411 | n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op); |
| 412 | if (n < 0) { |
| 413 | if(errno == EIO) |
| 414 | printf("not found\n"); |
| 415 | else { |
| 416 | perror("not found"); |
| 417 | } |
| 418 | ptrace_ldt = 0; |
| 419 | } |
| 420 | else { |
| 421 | if(ptrace_ldt) |
| 422 | printf("found\n"); |
| 423 | else |
| 424 | printf("found, but use is disabled\n"); |
| 425 | } |
| 426 | |
| 427 | stop_ptraced_child(pid, stack, 1, 1); |
| 428 | #else |
| 429 | /* PTRACE_LDT might be disabled via cmdline option. |
| 430 | * We want to override this, else we might use the stub |
| 431 | * without real need |
| 432 | */ |
| 433 | ptrace_ldt = 1; |
| 434 | #endif |
| 435 | } |
| 436 | |
| 437 | static inline void check_skas3_proc_mm(void) |
| 438 | { |
| 439 | printf(" - /proc/mm..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (os_access("/proc/mm", OS_ACC_W_OK) < 0) { |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 441 | proc_mm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | printf("not found\n"); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 443 | } |
| 444 | else { |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 445 | if (!proc_mm) |
| 446 | printf("found but disabled on command line\n"); |
| 447 | else |
| 448 | printf("found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 452 | int can_do_skas(void) |
| 453 | { |
| 454 | printf("Checking for the skas3 patch in the host:\n"); |
| 455 | |
| 456 | check_skas3_proc_mm(); |
| 457 | check_skas3_ptrace_faultinfo(); |
| 458 | check_skas3_ptrace_ldt(); |
| 459 | |
| 460 | if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt) |
| 461 | skas_needs_stub = 1; |
| 462 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 463 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | #else |
| 466 | int can_do_skas(void) |
| 467 | { |
| 468 | return(0); |
| 469 | } |
| 470 | #endif |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 471 | |
| 472 | int have_devanon = 0; |
| 473 | |
Paolo 'Blaisorblade' Giarrusso | 5169494 | 2005-12-29 17:39:51 +0100 | [diff] [blame] | 474 | /* Runs on boot kernel stack - already safe to use printk. */ |
| 475 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 476 | void check_devanon(void) |
| 477 | { |
| 478 | int fd; |
| 479 | |
| 480 | printk("Checking for /dev/anon on the host..."); |
| 481 | fd = open("/dev/anon", O_RDWR); |
| 482 | if(fd < 0){ |
| 483 | printk("Not available (open failed with errno %d)\n", errno); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | printk("OK\n"); |
| 488 | have_devanon = 1; |
| 489 | } |
| 490 | |
| 491 | int __init parse_iomem(char *str, int *add) |
| 492 | { |
| 493 | struct iomem_region *new; |
| 494 | struct uml_stat buf; |
| 495 | char *file, *driver; |
| 496 | int fd, err, size; |
| 497 | |
| 498 | driver = str; |
| 499 | file = strchr(str,','); |
| 500 | if(file == NULL){ |
| 501 | printf("parse_iomem : failed to parse iomem\n"); |
| 502 | goto out; |
| 503 | } |
| 504 | *file = '\0'; |
| 505 | file++; |
| 506 | fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0); |
| 507 | if(fd < 0){ |
| 508 | os_print_error(fd, "parse_iomem - Couldn't open io file"); |
| 509 | goto out; |
| 510 | } |
| 511 | |
| 512 | err = os_stat_fd(fd, &buf); |
| 513 | if(err < 0){ |
| 514 | os_print_error(err, "parse_iomem - cannot stat_fd file"); |
| 515 | goto out_close; |
| 516 | } |
| 517 | |
| 518 | new = malloc(sizeof(*new)); |
| 519 | if(new == NULL){ |
| 520 | perror("Couldn't allocate iomem_region struct"); |
| 521 | goto out_close; |
| 522 | } |
| 523 | |
| 524 | size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1); |
| 525 | |
| 526 | *new = ((struct iomem_region) { .next = iomem_regions, |
| 527 | .driver = driver, |
| 528 | .fd = fd, |
| 529 | .size = size, |
| 530 | .phys = 0, |
| 531 | .virt = 0 }); |
| 532 | iomem_regions = new; |
| 533 | iomem_size += new->size + UM_KERN_PAGE_SIZE; |
| 534 | |
| 535 | return(0); |
| 536 | out_close: |
| 537 | os_close_file(fd); |
| 538 | out: |
| 539 | return(1); |
| 540 | } |
| 541 | |