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