2005-09-04 00:57:47 +02:00
|
|
|
/*
|
2007-10-16 10:27:00 +02:00
|
|
|
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-17 00:20:36 +02:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-09-17 04:27:50 +02:00
|
|
|
#include <stdlib.h>
|
2007-10-16 10:27:00 +02:00
|
|
|
#include <stdarg.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2007-10-16 10:27:00 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <sys/mman.h>
|
2007-10-16 10:27:00 +02:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <asm/unistd.h>
|
|
|
|
#include "init.h"
|
2005-09-17 04:27:50 +02:00
|
|
|
#include "kern_constants.h"
|
2007-10-16 10:27:00 +02:00
|
|
|
#include "os.h"
|
|
|
|
#include "mem_user.h"
|
|
|
|
#include "ptrace_user.h"
|
2005-04-17 00:20:36 +02:00
|
|
|
#include "registers.h"
|
2007-10-16 10:27:00 +02:00
|
|
|
#include "skas_ptrace.h"
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
static int ptrace_child(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2007-10-16 10:27:11 +02:00
|
|
|
/* Calling os_getpid because some libcs cached getpid incorrectly */
|
2005-04-17 00:20:36 +02:00
|
|
|
int pid = os_getpid(), ppid = getppid();
|
|
|
|
int sc_result;
|
|
|
|
|
2006-02-07 21:58:42 +01:00
|
|
|
change_sig(SIGWINCH, 0);
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
|
2005-04-17 00:20:36 +02:00
|
|
|
perror("ptrace");
|
2007-10-16 10:27:11 +02:00
|
|
|
kill(pid, SIGKILL);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-02-10 10:44:20 +01:00
|
|
|
kill(pid, SIGSTOP);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
/*
|
|
|
|
* This syscall will be intercepted by the parent. Don't call more than
|
|
|
|
* once, please.
|
|
|
|
*/
|
2005-04-17 00:20:36 +02:00
|
|
|
sc_result = os_getpid();
|
|
|
|
|
|
|
|
if (sc_result == pid)
|
2007-10-16 10:27:00 +02:00
|
|
|
/* Nothing modified by the parent, we are running normally. */
|
|
|
|
ret = 1;
|
2005-04-17 00:20:36 +02:00
|
|
|
else if (sc_result == ppid)
|
2007-10-16 10:27:00 +02:00
|
|
|
/*
|
|
|
|
* Expected in check_ptrace and check_sysemu when they succeed
|
|
|
|
* in modifying the stack frame
|
|
|
|
*/
|
|
|
|
ret = 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
2007-10-16 10:27:00 +02:00
|
|
|
/* Serious trouble! This could be caused by a bug in host 2.6
|
|
|
|
* SKAS3/2.6 patch before release -V6, together with a bug in
|
|
|
|
* the UML code itself.
|
|
|
|
*/
|
|
|
|
ret = 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
2008-02-05 07:30:35 +01:00
|
|
|
static void fatal_perror(const char *str)
|
2007-02-10 10:44:28 +01:00
|
|
|
{
|
|
|
|
perror(str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fatal(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
|
|
|
|
va_start(list, fmt);
|
|
|
|
vprintf(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void non_fatal(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
|
|
|
|
va_start(list, fmt);
|
|
|
|
vprintf(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
static int start_ptraced_child(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int pid, n, status;
|
2005-09-04 00:57:47 +02:00
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
ptrace_child();
|
|
|
|
else if (pid < 0)
|
|
|
|
fatal_perror("start_ptraced_child : fork failed");
|
2007-10-16 10:27:00 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0)
|
2007-10-16 10:27:09 +02:00
|
|
|
fatal_perror("check_ptrace : waitpid failed");
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal("check_ptrace : expected SIGSTOP, got status = %d",
|
2005-04-17 00:20:36 +02:00
|
|
|
status);
|
|
|
|
|
2007-02-10 10:44:20 +01:00
|
|
|
return pid;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-09-04 00:57:47 +02:00
|
|
|
/* When testing for SYSEMU support, if it is one of the broken versions, we
|
|
|
|
* must just avoid using sysemu, not panic, but only if SYSEMU features are
|
|
|
|
* broken.
|
2005-04-17 00:20:36 +02:00
|
|
|
* So only for SYSEMU features we test mustpanic, while normal host features
|
2005-09-04 00:57:47 +02:00
|
|
|
* must work anyway!
|
|
|
|
*/
|
2007-10-16 10:27:09 +02:00
|
|
|
static int stop_ptraced_child(int pid, int exitcode, int mustexit)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int status, n, ret = 0;
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("stop_ptraced_child : ptrace failed");
|
2005-04-17 00:20:36 +02:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, 0));
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
int exit_with = WEXITSTATUS(status);
|
|
|
|
if (exit_with == 2)
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("check_ptrace : child exited with status 2. "
|
2007-05-23 22:57:40 +02:00
|
|
|
"\nDisabling SYSEMU support.\n");
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("check_ptrace : child exited with exitcode %d, while "
|
|
|
|
"expecting %d; status 0x%x\n", exit_with,
|
|
|
|
exitcode, status);
|
|
|
|
if (mustexit)
|
|
|
|
exit(1);
|
2005-04-17 00:20:36 +02:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-10 10:44:19 +01:00
|
|
|
/* Changed only during early boot */
|
2005-07-27 20:43:31 +02:00
|
|
|
int ptrace_faultinfo = 1;
|
2005-11-07 09:58:55 +01:00
|
|
|
int ptrace_ldt = 1;
|
2005-07-27 20:43:31 +02:00
|
|
|
int proc_mm = 1;
|
2005-11-07 09:58:55 +01:00
|
|
|
int skas_needs_stub = 0;
|
2005-07-27 20:43:31 +02:00
|
|
|
|
|
|
|
static int __init skas0_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
ptrace_faultinfo = proc_mm = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-09 21:37:18 +02:00
|
|
|
/* The two __uml_setup would conflict, without this stupid alias. */
|
|
|
|
|
|
|
|
static int __init mode_skas0_cmd_param(char *str, int* add)
|
|
|
|
__attribute__((alias("skas0_cmd_param")));
|
|
|
|
|
2005-09-04 00:57:47 +02:00
|
|
|
__uml_setup("skas0", skas0_cmd_param,
|
|
|
|
"skas0\n"
|
|
|
|
" Disables SKAS3 usage, so that SKAS0 is used, unless \n"
|
|
|
|
" you specify mode=tt.\n\n");
|
|
|
|
|
2005-10-09 21:37:18 +02:00
|
|
|
__uml_setup("mode=skas0", mode_skas0_cmd_param,
|
|
|
|
"mode=skas0\n"
|
|
|
|
" Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
|
|
|
|
" specify mode=tt. Note that this was recently added - on \n"
|
|
|
|
" older kernels you must use simply \"skas0\".\n\n");
|
|
|
|
|
2007-02-10 10:44:19 +01:00
|
|
|
/* Changed only during early boot */
|
2005-09-04 00:57:47 +02:00
|
|
|
static int force_sysemu_disabled = 0;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static int __init nosysemu_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
force_sysemu_disabled = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("nosysemu", nosysemu_cmd_param,
|
2005-09-04 00:57:47 +02:00
|
|
|
"nosysemu\n"
|
|
|
|
" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
|
|
|
|
" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
|
|
|
|
" behaviour of ptrace() and helps reducing host context switch rate.\n"
|
|
|
|
" To make it working, you need a kernel patch for your host, too.\n"
|
|
|
|
" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
|
|
|
|
" information.\n\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
static void __init check_sysemu(void)
|
|
|
|
{
|
2007-05-23 22:57:40 +02:00
|
|
|
unsigned long regs[MAX_REG_NR];
|
2007-02-10 10:44:20 +01:00
|
|
|
int pid, n, status, count=0;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("Checking syscall emulation patch for ptrace...");
|
2005-04-17 00:20:36 +02:00
|
|
|
sysemu_supported = 0;
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = start_ptraced_child();
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
|
|
|
if (n < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_sysemu : wait failed");
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal("check_sysemu : expected SIGTRAP, got status = %d",
|
|
|
|
status);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
|
2007-05-23 22:57:40 +02:00
|
|
|
fatal_perror("check_sysemu : PTRACE_GETREGS failed");
|
2007-10-16 10:27:00 +02:00
|
|
|
if (PT_SYSCALL_NR(regs) != __NR_getpid) {
|
2007-05-23 22:57:40 +02:00
|
|
|
non_fatal("check_sysemu got system call number %d, "
|
|
|
|
"expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0) {
|
2007-05-23 22:57:40 +02:00
|
|
|
non_fatal("check_sysemu : failed to modify system call "
|
|
|
|
"return");
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
if (stop_ptraced_child(pid, 0, 0) < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto fail_stopped;
|
|
|
|
|
|
|
|
sysemu_supported = 1;
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("OK\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
set_using_sysemu(!force_sysemu_disabled);
|
|
|
|
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("Checking advanced syscall emulation patch for ptrace...");
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = start_ptraced_child();
|
2005-09-04 00:57:51 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
|
2007-02-10 10:44:28 +01:00
|
|
|
(void *) PTRACE_O_TRACESYSGOOD) < 0))
|
|
|
|
fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
|
2005-09-04 00:57:51 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
while (1) {
|
2005-04-17 00:20:36 +02:00
|
|
|
count++;
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto fail;
|
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_ptrace : wait failed");
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (WIFSTOPPED(status) &&
|
|
|
|
(WSTOPSIG(status) == (SIGTRAP|0x80))) {
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!count)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal("check_ptrace : SYSEMU_SINGLESTEP "
|
2005-09-04 00:57:51 +02:00
|
|
|
"doesn't singlestep");
|
2005-04-17 00:20:36 +02:00
|
|
|
n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
|
|
|
|
os_getpid());
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_sysemu : failed to modify "
|
|
|
|
"system call return");
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-10-16 10:27:00 +02:00
|
|
|
else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
|
2005-09-04 00:57:51 +02:00
|
|
|
count++;
|
|
|
|
else
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal("check_ptrace : expected SIGTRAP or "
|
|
|
|
"(SIGTRAP | 0x80), got status = %d", status);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-10-16 10:27:09 +02:00
|
|
|
if (stop_ptraced_child(pid, 0, 0) < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto fail_stopped;
|
|
|
|
|
|
|
|
sysemu_supported = 2;
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("OK\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!force_sysemu_disabled)
|
2005-04-17 00:20:36 +02:00
|
|
|
set_using_sysemu(sysemu_supported);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2007-10-16 10:27:09 +02:00
|
|
|
stop_ptraced_child(pid, 1, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
fail_stopped:
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("missing\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-09-04 00:57:47 +02:00
|
|
|
static void __init check_ptrace(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int pid, syscall, n, status;
|
|
|
|
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("Checking that ptrace can change system call numbers...");
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = start_ptraced_child();
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
|
2007-02-10 10:44:28 +01:00
|
|
|
(void *) PTRACE_O_TRACESYSGOOD) < 0))
|
|
|
|
fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
while (1) {
|
|
|
|
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_ptrace : ptrace failed");
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_ptrace : wait failed");
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!WIFSTOPPED(status) ||
|
2007-02-10 10:44:28 +01:00
|
|
|
(WSTOPSIG(status) != (SIGTRAP | 0x80)))
|
|
|
|
fatal("check_ptrace : expected (SIGTRAP|0x80), "
|
|
|
|
"got status = %d", status);
|
2005-09-04 00:57:47 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
|
|
|
|
0);
|
2007-10-16 10:27:00 +02:00
|
|
|
if (syscall == __NR_getpid) {
|
2005-04-17 00:20:36 +02:00
|
|
|
n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
|
|
|
|
__NR_getppid);
|
2007-10-16 10:27:00 +02:00
|
|
|
if (n < 0)
|
2007-02-10 10:44:28 +01:00
|
|
|
fatal_perror("check_ptrace : failed to modify "
|
|
|
|
"system call");
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-16 10:27:09 +02:00
|
|
|
stop_ptraced_child(pid, 0, 1);
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("OK\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
check_sysemu();
|
|
|
|
}
|
|
|
|
|
2006-04-19 07:21:43 +02:00
|
|
|
extern void check_tmpexec(void);
|
2005-09-17 04:27:50 +02:00
|
|
|
|
2007-05-06 23:51:11 +02:00
|
|
|
static void __init check_coredump_limit(void)
|
2007-05-06 23:51:00 +02:00
|
|
|
{
|
|
|
|
struct rlimit lim;
|
|
|
|
int err = getrlimit(RLIMIT_CORE, &lim);
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (err) {
|
2007-05-06 23:51:00 +02:00
|
|
|
perror("Getting core dump limit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Core dump limits :\n\tsoft - ");
|
2007-10-16 10:27:00 +02:00
|
|
|
if (lim.rlim_cur == RLIM_INFINITY)
|
2007-05-06 23:51:00 +02:00
|
|
|
printf("NONE\n");
|
|
|
|
else printf("%lu\n", lim.rlim_cur);
|
|
|
|
|
|
|
|
printf("\thard - ");
|
2007-10-16 10:27:00 +02:00
|
|
|
if (lim.rlim_max == RLIM_INFINITY)
|
2007-05-06 23:51:00 +02:00
|
|
|
printf("NONE\n");
|
|
|
|
else printf("%lu\n", lim.rlim_max);
|
|
|
|
}
|
|
|
|
|
2007-05-06 23:51:11 +02:00
|
|
|
void __init os_early_checks(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-05-06 23:51:00 +02:00
|
|
|
/* Print out the core dump limits early */
|
|
|
|
check_coredump_limit();
|
|
|
|
|
2005-09-04 00:57:47 +02:00
|
|
|
check_ptrace();
|
2005-09-17 04:27:50 +02:00
|
|
|
|
|
|
|
/* Need to check this early because mmapping happens before the
|
|
|
|
* kernel is running.
|
|
|
|
*/
|
|
|
|
check_tmpexec();
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-09-04 00:57:51 +02:00
|
|
|
static int __init noprocmm_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
proc_mm = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("noprocmm", noprocmm_cmd_param,
|
|
|
|
"noprocmm\n"
|
|
|
|
" Turns off usage of /proc/mm, even if host supports it.\n"
|
|
|
|
" To support /proc/mm, the host needs to be patched using\n"
|
|
|
|
" the current skas3 patch.\n\n");
|
|
|
|
|
|
|
|
static int __init noptracefaultinfo_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
ptrace_faultinfo = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
|
|
|
|
"noptracefaultinfo\n"
|
|
|
|
" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
|
|
|
|
" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
|
|
|
|
" using the current skas3 patch.\n\n");
|
|
|
|
|
2005-11-07 09:58:55 +01:00
|
|
|
static int __init noptraceldt_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
ptrace_ldt = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("noptraceldt", noptraceldt_cmd_param,
|
|
|
|
"noptraceldt\n"
|
|
|
|
" Turns off usage of PTRACE_LDT, even if host supports it.\n"
|
|
|
|
" To support PTRACE_LDT, the host needs to be patched using\n"
|
|
|
|
" the current skas3 patch.\n\n");
|
|
|
|
|
|
|
|
static inline void check_skas3_ptrace_faultinfo(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct ptrace_faultinfo fi;
|
[PATCH] uml: skas0 - separate kernel address space on stock hosts
UML has had two modes of operation - an insecure, slow mode (tt mode) in
which the kernel is mapped into every process address space which requires
no host kernel modifications, and a secure, faster mode (skas mode) in
which the UML kernel is in a separate host address space, which requires a
patch to the host kernel.
This patch implements something very close to skas mode for hosts which
don't support skas - I'm calling this skas0. It provides the security of
the skas host patch, and some of the performance gains.
The two main things that are provided by the skas patch, /proc/mm and
PTRACE_FAULTINFO, are implemented in a way that require no host patch.
For the remote address space changing stuff (mmap, munmap, and mprotect),
we set aside two pages in the process above its stack, one of which
contains a little bit of code which can call mmap et al.
To update the address space, the system call information (system call
number and arguments) are written to the stub page above the code. The
%esp is set to the beginning of the data, the %eip is set the the start of
the stub, and it repeatedly pops the information into its registers and
makes the system call until it sees a system call number of zero. This is
to amortize the cost of the context switch across multiple address space
updates.
When the updates are done, it SIGSTOPs itself, and the kernel process
continues what it was doing.
For a PTRACE_FAULTINFO replacement, we set up a SIGSEGV handler in the
child, and let it handle segfaults rather than nullifying them. The
handler is in the same page as the mmap stub. The second page is used as
the stack. The handler reads cr2 and err from the sigcontext, sticks them
at the base of the stack in a faultinfo struct, and SIGSTOPs itself. The
kernel then reads the faultinfo and handles the fault.
A complication on x86_64 is that this involves resetting the registers to
the segfault values when the process is inside the kill system call. This
breaks on x86_64 because %rcx will contain %rip because you tell SYSRET
where to return to by putting the value in %rcx. So, this corrupts $rcx on
return from the segfault. To work around this, I added an
arch_finish_segv, which on x86 does nothing, but which on x86_64 ptraces
the child back through the sigreturn. This causes %rcx to be restored by
sigreturn and avoids the corruption. Ultimately, I think I will replace
this with the trick of having it send itself a blocked signal which will be
unblocked by the sigreturn. This will allow it to be stopped just after
the sigreturn, and PTRACE_SYSCALLed without all the back-and-forth of
PTRACE_SYSCALLing it through sigreturn.
This runs on a stock host, so theoretically (and hopefully), tt mode isn't
needed any more. We need to make sure that this is better in every way
than tt mode, though. I'm concerned about the speed of address space
updates and page fault handling, since they involve extra round-trips to
the child. We can amortize the round-trip cost for large address space
updates by writing all of the operations to the data page and having the
child execute them all at the same time. This will help fork and exec, but
not page faults, since they involve only one page.
I can't think of any way to help page faults, except to add something like
PTRACE_FAULTINFO to the host. There is PTRACE_SIGINFO, but UML doesn't use
siginfo for SIGSEGV (or anything else) because there isn't enough
information in the siginfo struct to handle page faults (the faulting
operation type is missing). Adding that would make PTRACE_SIGINFO a usable
equivalent to PTRACE_FAULTINFO.
As for the code itself:
- The system call stub is in arch/um/kernel/sys-$(SUBARCH)/stub.S. It is
put in its own section of the binary along with stub_segv_handler in
arch/um/kernel/skas/process.c. This is manipulated with run_syscall_stub
in arch/um/kernel/skas/mem_user.c. syscall_stub will execute any system
call at all, but it's only used for mmap, munmap, and mprotect.
- The x86_64 stub calls sigreturn by hand rather than allowing the normal
sigreturn to happen, because the normal sigreturn is a SA_RESTORER in
UML's address space provided by libc. Needless to say, this is not
available in the child's address space. Also, it does a couple of odd
pops before that which restore the stack to the state it was in at the
time the signal handler was called.
- There is a new field in the arch mmu_context, which is now a union.
This is the pid to be manipulated rather than the /proc/mm file
descriptor. Code which deals with this now checks proc_mm to see whether
it should use the usual skas code or the new code.
- userspace_tramp is now used to create a new host process for every UML
process, rather than one per UML processor. It checks proc_mm and
ptrace_faultinfo to decide whether to map in the pages above its stack.
- start_userspace now makes CLONE_VM conditional on proc_mm since we need
separate address spaces now.
- switch_mm_skas now just sets userspace_pid[0] to the new pid rather
than PTRACE_SWITCH_MM. There is an addition to userspace which updates
its idea of the pid being manipulated each time around the loop. This is
important on exec, when the pid will change underneath userspace().
- The stub page has a pte, but it can't be mapped in using tlb_flush
because it is part of tlb_flush. This is why it's required for it to be
mapped in by userspace_tramp.
Other random things:
- The stub section in uml.lds.S is page aligned. This page is written
out to the backing vm file in setup_physmem because it is mapped from
there into user processes.
- There's some confusion with TASK_SIZE now that there are a couple of
extra pages that the process can't use. TASK_SIZE is considered by the
elf code to be the usable process memory, which is reasonable, so it is
decreased by two pages. This confuses the definition of
USER_PGDS_IN_LAST_PML4, making it too small because of the rounding down
of the uneven division. So we round it to the nearest PGDIR_SIZE rather
than the lower one.
- I added a missing PT_SYSCALL_ARG6_OFFSET macro.
- um_mmu.h was made into a userspace-usable file.
- proc_mm and ptrace_faultinfo are globals which say whether the host
supports these features.
- There is a bad interaction between the mm.nr_ptes check at the end of
exit_mmap, stack randomization, and skas0. exit_mmap will stop freeing
pages at the PGDIR_SIZE boundary after the last vma. If the stack isn't
on the last page table page, the last pte page won't be freed, as it
should be since the stub ptes are there, and exit_mmap will BUG because
there is an unfreed page. To get around this, TASK_SIZE is set to the
next lowest PGDIR_SIZE boundary and mm->nr_ptes is decremented after the
calls to init_stub_pte. This ensures that we know the process stack (and
all other process mappings) will be below the top page table page, and
thus we know that mm->nr_ptes will be one too many, and can be
decremented.
Things that need fixing:
- We may need better assurrences that the stub code is PIC.
- The stub pte is set up in init_new_context_skas.
- alloc_pgdir is probably the right place.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-08 02:56:49 +02:00
|
|
|
int pid, n;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal(" - PTRACE_FAULTINFO...");
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = start_ptraced_child();
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
|
|
|
|
if (n < 0) {
|
2005-07-27 20:43:31 +02:00
|
|
|
ptrace_faultinfo = 0;
|
2007-10-16 10:27:00 +02:00
|
|
|
if (errno == EIO)
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("not found\n");
|
2005-09-04 00:57:47 +02:00
|
|
|
else
|
2005-04-17 00:20:36 +02:00
|
|
|
perror("not found");
|
[PATCH] uml: skas0 - separate kernel address space on stock hosts
UML has had two modes of operation - an insecure, slow mode (tt mode) in
which the kernel is mapped into every process address space which requires
no host kernel modifications, and a secure, faster mode (skas mode) in
which the UML kernel is in a separate host address space, which requires a
patch to the host kernel.
This patch implements something very close to skas mode for hosts which
don't support skas - I'm calling this skas0. It provides the security of
the skas host patch, and some of the performance gains.
The two main things that are provided by the skas patch, /proc/mm and
PTRACE_FAULTINFO, are implemented in a way that require no host patch.
For the remote address space changing stuff (mmap, munmap, and mprotect),
we set aside two pages in the process above its stack, one of which
contains a little bit of code which can call mmap et al.
To update the address space, the system call information (system call
number and arguments) are written to the stub page above the code. The
%esp is set to the beginning of the data, the %eip is set the the start of
the stub, and it repeatedly pops the information into its registers and
makes the system call until it sees a system call number of zero. This is
to amortize the cost of the context switch across multiple address space
updates.
When the updates are done, it SIGSTOPs itself, and the kernel process
continues what it was doing.
For a PTRACE_FAULTINFO replacement, we set up a SIGSEGV handler in the
child, and let it handle segfaults rather than nullifying them. The
handler is in the same page as the mmap stub. The second page is used as
the stack. The handler reads cr2 and err from the sigcontext, sticks them
at the base of the stack in a faultinfo struct, and SIGSTOPs itself. The
kernel then reads the faultinfo and handles the fault.
A complication on x86_64 is that this involves resetting the registers to
the segfault values when the process is inside the kill system call. This
breaks on x86_64 because %rcx will contain %rip because you tell SYSRET
where to return to by putting the value in %rcx. So, this corrupts $rcx on
return from the segfault. To work around this, I added an
arch_finish_segv, which on x86 does nothing, but which on x86_64 ptraces
the child back through the sigreturn. This causes %rcx to be restored by
sigreturn and avoids the corruption. Ultimately, I think I will replace
this with the trick of having it send itself a blocked signal which will be
unblocked by the sigreturn. This will allow it to be stopped just after
the sigreturn, and PTRACE_SYSCALLed without all the back-and-forth of
PTRACE_SYSCALLing it through sigreturn.
This runs on a stock host, so theoretically (and hopefully), tt mode isn't
needed any more. We need to make sure that this is better in every way
than tt mode, though. I'm concerned about the speed of address space
updates and page fault handling, since they involve extra round-trips to
the child. We can amortize the round-trip cost for large address space
updates by writing all of the operations to the data page and having the
child execute them all at the same time. This will help fork and exec, but
not page faults, since they involve only one page.
I can't think of any way to help page faults, except to add something like
PTRACE_FAULTINFO to the host. There is PTRACE_SIGINFO, but UML doesn't use
siginfo for SIGSEGV (or anything else) because there isn't enough
information in the siginfo struct to handle page faults (the faulting
operation type is missing). Adding that would make PTRACE_SIGINFO a usable
equivalent to PTRACE_FAULTINFO.
As for the code itself:
- The system call stub is in arch/um/kernel/sys-$(SUBARCH)/stub.S. It is
put in its own section of the binary along with stub_segv_handler in
arch/um/kernel/skas/process.c. This is manipulated with run_syscall_stub
in arch/um/kernel/skas/mem_user.c. syscall_stub will execute any system
call at all, but it's only used for mmap, munmap, and mprotect.
- The x86_64 stub calls sigreturn by hand rather than allowing the normal
sigreturn to happen, because the normal sigreturn is a SA_RESTORER in
UML's address space provided by libc. Needless to say, this is not
available in the child's address space. Also, it does a couple of odd
pops before that which restore the stack to the state it was in at the
time the signal handler was called.
- There is a new field in the arch mmu_context, which is now a union.
This is the pid to be manipulated rather than the /proc/mm file
descriptor. Code which deals with this now checks proc_mm to see whether
it should use the usual skas code or the new code.
- userspace_tramp is now used to create a new host process for every UML
process, rather than one per UML processor. It checks proc_mm and
ptrace_faultinfo to decide whether to map in the pages above its stack.
- start_userspace now makes CLONE_VM conditional on proc_mm since we need
separate address spaces now.
- switch_mm_skas now just sets userspace_pid[0] to the new pid rather
than PTRACE_SWITCH_MM. There is an addition to userspace which updates
its idea of the pid being manipulated each time around the loop. This is
important on exec, when the pid will change underneath userspace().
- The stub page has a pte, but it can't be mapped in using tlb_flush
because it is part of tlb_flush. This is why it's required for it to be
mapped in by userspace_tramp.
Other random things:
- The stub section in uml.lds.S is page aligned. This page is written
out to the backing vm file in setup_physmem because it is mapped from
there into user processes.
- There's some confusion with TASK_SIZE now that there are a couple of
extra pages that the process can't use. TASK_SIZE is considered by the
elf code to be the usable process memory, which is reasonable, so it is
decreased by two pages. This confuses the definition of
USER_PGDS_IN_LAST_PML4, making it too small because of the rounding down
of the uneven division. So we round it to the nearest PGDIR_SIZE rather
than the lower one.
- I added a missing PT_SYSCALL_ARG6_OFFSET macro.
- um_mmu.h was made into a userspace-usable file.
- proc_mm and ptrace_faultinfo are globals which say whether the host
supports these features.
- There is a bad interaction between the mm.nr_ptes check at the end of
exit_mmap, stack randomization, and skas0. exit_mmap will stop freeing
pages at the PGDIR_SIZE boundary after the last vma. If the stack isn't
on the last page table page, the last pte page won't be freed, as it
should be since the stub ptes are there, and exit_mmap will BUG because
there is an unfreed page. To get around this, TASK_SIZE is set to the
next lowest PGDIR_SIZE boundary and mm->nr_ptes is decremented after the
calls to init_stub_pte. This ensures that we know the process stack (and
all other process mappings) will be below the top page table page, and
thus we know that mm->nr_ptes will be one too many, and can be
decremented.
Things that need fixing:
- We may need better assurrences that the stub code is PIC.
- The stub pte is set up in init_new_context_skas.
- alloc_pgdir is probably the right place.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-08 02:56:49 +02:00
|
|
|
}
|
|
|
|
else {
|
2005-07-27 20:43:31 +02:00
|
|
|
if (!ptrace_faultinfo)
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("found but disabled on command line\n");
|
2005-07-27 20:43:31 +02:00
|
|
|
else
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("found\n");
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 07:30:58 +01:00
|
|
|
if (init_registers(pid))
|
|
|
|
fatal("Failed to initialize default registers");
|
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
stop_ptraced_child(pid, 1, 1);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:55 +01:00
|
|
|
static inline void check_skas3_ptrace_ldt(void)
|
|
|
|
{
|
|
|
|
#ifdef PTRACE_LDT
|
|
|
|
int pid, n;
|
|
|
|
unsigned char ldtbuf[40];
|
|
|
|
struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
|
|
|
|
.func = 2, /* read default ldt */
|
|
|
|
.ptr = ldtbuf,
|
|
|
|
.bytecount = sizeof(ldtbuf)};
|
|
|
|
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal(" - PTRACE_LDT...");
|
2007-10-16 10:27:09 +02:00
|
|
|
pid = start_ptraced_child();
|
2005-11-07 09:58:55 +01:00
|
|
|
|
|
|
|
n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
|
|
|
|
if (n < 0) {
|
2007-10-16 10:27:00 +02:00
|
|
|
if (errno == EIO)
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("not found\n");
|
2005-11-07 09:58:55 +01:00
|
|
|
else {
|
|
|
|
perror("not found");
|
|
|
|
}
|
|
|
|
ptrace_ldt = 0;
|
|
|
|
}
|
|
|
|
else {
|
2007-10-16 10:27:00 +02:00
|
|
|
if (ptrace_ldt)
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("found\n");
|
2005-11-07 09:58:55 +01:00
|
|
|
else
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("found, but use is disabled\n");
|
2005-11-07 09:58:55 +01:00
|
|
|
}
|
|
|
|
|
2007-10-16 10:27:09 +02:00
|
|
|
stop_ptraced_child(pid, 1, 1);
|
2005-11-07 09:58:55 +01:00
|
|
|
#else
|
|
|
|
/* PTRACE_LDT might be disabled via cmdline option.
|
|
|
|
* We want to override this, else we might use the stub
|
|
|
|
* without real need
|
|
|
|
*/
|
|
|
|
ptrace_ldt = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void check_skas3_proc_mm(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal(" - /proc/mm...");
|
2007-02-10 10:44:20 +01:00
|
|
|
if (access("/proc/mm", W_OK) < 0) {
|
2007-02-10 10:44:20 +01:00
|
|
|
proc_mm = 0;
|
2007-02-10 10:44:28 +01:00
|
|
|
perror("not found");
|
2005-09-04 00:57:47 +02:00
|
|
|
}
|
2007-10-16 10:27:00 +02:00
|
|
|
else if (!proc_mm)
|
|
|
|
non_fatal("found but disabled on command line\n");
|
|
|
|
else non_fatal("found\n");
|
2005-11-07 09:58:55 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 07:30:50 +01:00
|
|
|
void can_do_skas(void)
|
2005-11-07 09:58:55 +01:00
|
|
|
{
|
2007-02-10 10:44:28 +01:00
|
|
|
non_fatal("Checking for the skas3 patch in the host:\n");
|
2005-11-07 09:58:55 +01:00
|
|
|
|
|
|
|
check_skas3_proc_mm();
|
|
|
|
check_skas3_ptrace_faultinfo();
|
|
|
|
check_skas3_ptrace_ldt();
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
|
2005-11-07 09:58:55 +01:00
|
|
|
skas_needs_stub = 1;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2005-09-17 04:27:50 +02:00
|
|
|
|
|
|
|
int __init parse_iomem(char *str, int *add)
|
|
|
|
{
|
|
|
|
struct iomem_region *new;
|
2007-02-10 10:44:20 +01:00
|
|
|
struct stat64 buf;
|
2005-09-17 04:27:50 +02:00
|
|
|
char *file, *driver;
|
2007-02-10 10:44:20 +01:00
|
|
|
int fd, size;
|
2005-09-17 04:27:50 +02:00
|
|
|
|
|
|
|
driver = str;
|
|
|
|
file = strchr(str,',');
|
2007-10-16 10:27:00 +02:00
|
|
|
if (file == NULL) {
|
2005-09-17 04:27:50 +02:00
|
|
|
printf("parse_iomem : failed to parse iomem\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*file = '\0';
|
|
|
|
file++;
|
2007-02-10 10:44:20 +01:00
|
|
|
fd = open(file, O_RDWR, 0);
|
2007-10-16 10:27:00 +02:00
|
|
|
if (fd < 0) {
|
2007-10-16 10:27:11 +02:00
|
|
|
perror("parse_iomem - Couldn't open io file");
|
2005-09-17 04:27:50 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-10-16 10:27:00 +02:00
|
|
|
if (fstat64(fd, &buf) < 0) {
|
2007-02-10 10:44:20 +01:00
|
|
|
perror("parse_iomem - cannot stat_fd file");
|
2005-09-17 04:27:50 +02:00
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
new = malloc(sizeof(*new));
|
2007-10-16 10:27:00 +02:00
|
|
|
if (new == NULL) {
|
2005-09-17 04:27:50 +02:00
|
|
|
perror("Couldn't allocate iomem_region struct");
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2007-02-10 10:44:20 +01:00
|
|
|
size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
|
2005-09-17 04:27:50 +02:00
|
|
|
|
|
|
|
*new = ((struct iomem_region) { .next = iomem_regions,
|
|
|
|
.driver = driver,
|
|
|
|
.fd = fd,
|
|
|
|
.size = size,
|
|
|
|
.phys = 0,
|
|
|
|
.virt = 0 });
|
|
|
|
iomem_regions = new;
|
|
|
|
iomem_size += new->size + UM_KERN_PAGE_SIZE;
|
|
|
|
|
2007-02-10 10:44:20 +01:00
|
|
|
return 0;
|
2005-09-17 04:27:50 +02:00
|
|
|
out_close:
|
2007-02-10 10:44:20 +01:00
|
|
|
close(fd);
|
2005-09-17 04:27:50 +02:00
|
|
|
out:
|
2007-02-10 10:44:20 +01:00
|
|
|
return 1;
|
2005-09-17 04:27:50 +02:00
|
|
|
}
|