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