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