2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
20 #include <asm/unistd.h>
22 #include <sys/types.h>
23 #include "user_util.h"
24 #include "kern_util.h"
26 #include "signal_kern.h"
27 #include "sysdep/ptrace.h"
28 #include "sysdep/sigcontext.h"
30 #include "ptrace_user.h"
32 #include "time_user.h"
35 #include "uml-config.h"
36 #include "choose-mode.h"
39 #include "kern_constants.h"
41 #ifdef UML_CONFIG_MODE_SKAS
43 #include "skas_ptrace.h"
44 #include "registers.h"
47 static int ptrace_child(void *arg
)
50 int pid
= os_getpid(), ppid
= getppid();
53 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
55 os_kill_process(pid
, 0);
59 /*This syscall will be intercepted by the parent. Don't call more than
61 sc_result
= os_getpid();
64 ret
= 1; /*Nothing modified by the parent, we are running
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*/
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.*/
76 static int start_ptraced_child(void **stack_out
)
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
);
89 panic("start_ptraced_child : clone failed, errno = %d", errno
);
90 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
92 panic("check_ptrace : clone failed, errno = %d", errno
);
93 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
94 panic("check_ptrace : expected SIGSTOP, got status = %d",
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
104 * So only for SYSEMU features we test mustpanic, while normal host features
107 static int stop_ptraced_child(int pid
, void *stack
, int exitcode
,
110 int status
, n
, ret
= 0;
112 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
113 panic("check_ptrace : ptrace failed, errno = %d", errno
);
114 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
115 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
116 int exit_with
= WEXITSTATUS(status
);
118 printf("check_ptrace : child exited with status 2. "
119 "Serious trouble happening! Try updating your "
120 "host skas patch!\nDisabling SYSEMU support.");
121 printf("check_ptrace : child exited with exitcode %d, while "
122 "expecting %d; status 0x%x", exit_with
,
131 if(munmap(stack
, PAGE_SIZE
) < 0)
132 panic("check_ptrace : munmap failed, errno = %d", errno
);
136 int ptrace_faultinfo
= 1;
139 int skas_needs_stub
= 0;
141 static int __init
skas0_cmd_param(char *str
, int* add
)
143 ptrace_faultinfo
= proc_mm
= 0;
147 /* The two __uml_setup would conflict, without this stupid alias. */
149 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
150 __attribute__((alias("skas0_cmd_param")));
152 __uml_setup("skas0", skas0_cmd_param
,
154 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
155 " you specify mode=tt.\n\n");
157 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
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");
163 static int force_sysemu_disabled
= 0;
165 static int __init
nosysemu_cmd_param(char *str
, int* add
)
167 force_sysemu_disabled
= 1;
171 __uml_setup("nosysemu", nosysemu_cmd_param
,
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");
180 static void __init
check_sysemu(void)
183 int pid
, n
, status
, count
=0;
185 printf("Checking syscall emulation patch for ptrace...");
186 sysemu_supported
= 0;
187 pid
= start_ptraced_child(&stack
);
189 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
192 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
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
);
199 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
202 panic("check_sysemu : failed to modify system "
203 "call return, errno = %d", errno
);
205 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
208 sysemu_supported
= 1;
210 set_using_sysemu(!force_sysemu_disabled
);
212 printf("Checking advanced syscall emulation patch for ptrace...");
213 pid
= start_ptraced_child(&stack
);
215 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
216 (void *) PTRACE_O_TRACESYSGOOD
) < 0)
217 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
222 if(ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
224 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
226 panic("check_ptrace : wait failed, errno = %d", errno
);
227 if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == (SIGTRAP
|0x80))){
229 panic("check_ptrace : SYSEMU_SINGLESTEP "
230 "doesn't singlestep");
231 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
234 panic("check_sysemu : failed to modify system "
235 "call return, errno = %d", errno
);
238 else if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
241 panic("check_ptrace : expected SIGTRAP or "
242 "(SIGTRAP|0x80), got status = %d", status
);
244 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
247 sysemu_supported
= 2;
250 if ( !force_sysemu_disabled
)
251 set_using_sysemu(sysemu_supported
);
255 stop_ptraced_child(pid
, stack
, 1, 0);
260 static void __init
check_ptrace(void)
263 int pid
, syscall
, n
, status
;
265 printf("Checking that ptrace can change system call numbers...");
266 pid
= start_ptraced_child(&stack
);
268 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
269 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno
);
272 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
273 panic("check_ptrace : ptrace failed, errno = %d",
275 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
277 panic("check_ptrace : wait failed, errno = %d", errno
);
278 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != (SIGTRAP
|0x80)))
279 panic("check_ptrace : expected (SIGTRAP|0x80), "
280 "got status = %d", status
);
282 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
284 if(syscall
== __NR_getpid
){
285 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
288 panic("check_ptrace : failed to modify system "
289 "call, errno = %d", errno
);
293 stop_ptraced_child(pid
, stack
, 0, 1);
298 extern int create_tmp_file(unsigned long long len
);
300 static void check_tmpexec(void)
303 int err
, fd
= create_tmp_file(UM_KERN_PAGE_SIZE
);
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...");
309 if(addr
== MAP_FAILED
){
313 printf("/tmp must be not mounted noexec\n");
317 munmap(addr
, UM_KERN_PAGE_SIZE
);
322 void os_early_checks(void)
326 /* Need to check this early because mmapping happens before the
332 static int __init
noprocmm_cmd_param(char *str
, int* add
)
338 __uml_setup("noprocmm", noprocmm_cmd_param
,
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");
344 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
346 ptrace_faultinfo
= 0;
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");
356 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
362 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
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");
368 #ifdef UML_CONFIG_MODE_SKAS
369 static inline void check_skas3_ptrace_faultinfo(void)
371 struct ptrace_faultinfo fi
;
375 printf(" - PTRACE_FAULTINFO...");
376 pid
= start_ptraced_child(&stack
);
378 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
380 ptrace_faultinfo
= 0;
382 printf("not found\n");
387 if (!ptrace_faultinfo
)
388 printf("found but disabled on command line\n");
394 stop_ptraced_child(pid
, stack
, 1, 1);
397 static inline void check_skas3_ptrace_ldt(void)
402 unsigned char ldtbuf
[40];
403 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
404 .func
= 2, /* read default ldt */
406 .bytecount
= sizeof(ldtbuf
)};
408 printf(" - PTRACE_LDT...");
409 pid
= start_ptraced_child(&stack
);
411 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
414 printf("not found\n");
424 printf("found, but use is disabled\n");
427 stop_ptraced_child(pid
, stack
, 1, 1);
429 /* PTRACE_LDT might be disabled via cmdline option.
430 * We want to override this, else we might use the stub
437 static inline void check_skas3_proc_mm(void)
439 printf(" - /proc/mm...");
440 if (os_access("/proc/mm", OS_ACC_W_OK
) < 0) {
442 printf("not found\n");
446 printf("found but disabled on command line\n");
452 int can_do_skas(void)
454 printf("Checking for the skas3 patch in the host:\n");
456 check_skas3_proc_mm();
457 check_skas3_ptrace_faultinfo();
458 check_skas3_ptrace_ldt();
460 if(!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
466 int can_do_skas(void)
472 int have_devanon
= 0;
474 /* Runs on boot kernel stack - already safe to use printk. */
476 void check_devanon(void)
480 printk("Checking for /dev/anon on the host...");
481 fd
= open("/dev/anon", O_RDWR
);
483 printk("Not available (open failed with errno %d)\n", errno
);
491 int __init
parse_iomem(char *str
, int *add
)
493 struct iomem_region
*new;
499 file
= strchr(str
,',');
501 printf("parse_iomem : failed to parse iomem\n");
506 fd
= os_open_file(file
, of_rdwr(OPENFLAGS()), 0);
508 os_print_error(fd
, "parse_iomem - Couldn't open io file");
512 err
= os_stat_fd(fd
, &buf
);
514 os_print_error(err
, "parse_iomem - cannot stat_fd file");
518 new = malloc(sizeof(*new));
520 perror("Couldn't allocate iomem_region struct");
524 size
= (buf
.ust_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
526 *new = ((struct iomem_region
) { .next
= iomem_regions
,
533 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;