2005-09-03 18:57:47 -04:00
|
|
|
/*
|
2005-04-16 18:20:36 -04:00
|
|
|
* Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
|
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2006-03-27 04:14:32 -05:00
|
|
|
#include <pty.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <stdio.h>
|
2005-09-16 22:27:50 -04:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sched.h>
|
2005-09-16 22:27:50 -04:00
|
|
|
#include <fcntl.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mman.h>
|
2007-05-06 17:51:00 -04:00
|
|
|
#include <sys/resource.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <asm/unistd.h>
|
2005-09-16 22:27:50 -04:00
|
|
|
#include <sys/types.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include "kern_util.h"
|
|
|
|
#include "user.h"
|
|
|
|
#include "signal_kern.h"
|
|
|
|
#include "sysdep/ptrace.h"
|
|
|
|
#include "sysdep/sigcontext.h"
|
|
|
|
#include "irq_user.h"
|
|
|
|
#include "ptrace_user.h"
|
2005-09-16 22:27:50 -04:00
|
|
|
#include "mem_user.h"
|
2005-04-16 18:20:36 -04:00
|
|
|
#include "init.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "uml-config.h"
|
|
|
|
#include "choose-mode.h"
|
|
|
|
#include "mode.h"
|
[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-07 20:56:49 -04:00
|
|
|
#include "tempfile.h"
|
2005-09-16 22:27:50 -04:00
|
|
|
#include "kern_constants.h"
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
#ifdef UML_CONFIG_MODE_SKAS
|
|
|
|
#include "skas.h"
|
|
|
|
#include "skas_ptrace.h"
|
|
|
|
#include "registers.h"
|
|
|
|
#endif
|
|
|
|
|
2005-07-29 00:16:01 -04:00
|
|
|
static int ptrace_child(void *arg)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int pid = os_getpid(), ppid = getppid();
|
|
|
|
int sc_result;
|
|
|
|
|
2006-02-07 15:58:42 -05:00
|
|
|
change_sig(SIGWINCH, 0);
|
2005-04-16 18:20:36 -04:00
|
|
|
if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
|
|
|
|
perror("ptrace");
|
|
|
|
os_kill_process(pid, 0);
|
|
|
|
}
|
2007-02-10 04:44:20 -05:00
|
|
|
kill(pid, SIGSTOP);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/*This syscall will be intercepted by the parent. Don't call more than
|
|
|
|
* once, please.*/
|
|
|
|
sc_result = os_getpid();
|
|
|
|
|
|
|
|
if (sc_result == pid)
|
|
|
|
ret = 1; /*Nothing modified by the parent, we are running
|
|
|
|
normally.*/
|
|
|
|
else if (sc_result == ppid)
|
|
|
|
ret = 0; /*Expected in check_ptrace and check_sysemu when they
|
|
|
|
succeed in modifying the stack frame*/
|
|
|
|
else
|
|
|
|
ret = 2; /*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.*/
|
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
static void fatal_perror(char *str)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2005-07-29 00:16:01 -04:00
|
|
|
static int start_ptraced_child(void **stack_out)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2005-07-29 00:16:01 -04:00
|
|
|
void *stack;
|
|
|
|
unsigned long sp;
|
2005-04-16 18:20:36 -04:00
|
|
|
int pid, n, status;
|
2005-09-03 18:57:47 -04:00
|
|
|
|
2007-06-16 13:16:09 -04:00
|
|
|
stack = mmap(NULL, UM_KERN_PAGE_SIZE,
|
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC,
|
2005-07-29 00:16:01 -04:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if(stack == MAP_FAILED)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : mmap failed");
|
2007-06-16 13:16:09 -04:00
|
|
|
sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
|
2005-07-29 00:16:01 -04:00
|
|
|
pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
|
2005-04-16 18:20:36 -04:00
|
|
|
if(pid < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("start_ptraced_child : clone failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
|
|
|
if(n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : clone failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal("check_ptrace : expected SIGSTOP, got status = %d",
|
2005-04-16 18:20:36 -04:00
|
|
|
status);
|
|
|
|
|
2005-07-29 00:16:01 -04:00
|
|
|
*stack_out = stack;
|
2007-02-10 04:44:20 -05:00
|
|
|
return pid;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2005-09-03 18:57:47 -04: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-16 18:20:36 -04:00
|
|
|
* So only for SYSEMU features we test mustpanic, while normal host features
|
2005-09-03 18:57:47 -04:00
|
|
|
* must work anyway!
|
|
|
|
*/
|
|
|
|
static int stop_ptraced_child(int pid, void *stack, int exitcode,
|
2007-02-10 04:44:28 -05:00
|
|
|
int mustexit)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
int status, n, ret = 0;
|
|
|
|
|
|
|
|
if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("stop_ptraced_child : ptrace failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, 0));
|
|
|
|
if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
|
|
|
|
int exit_with = WEXITSTATUS(status);
|
|
|
|
if (exit_with == 2)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("check_ptrace : child exited with status 2. "
|
2007-05-23 16:57:40 -04:00
|
|
|
"\nDisabling SYSEMU support.\n");
|
2007-02-10 04:44:28 -05: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-16 18:20:36 -04:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2007-06-16 13:16:09 -04:00
|
|
|
if(munmap(stack, UM_KERN_PAGE_SIZE) < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : munmap failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:44:19 -05:00
|
|
|
/* Changed only during early boot */
|
2005-07-27 14:43:31 -04:00
|
|
|
int ptrace_faultinfo = 1;
|
2005-11-07 03:58:55 -05:00
|
|
|
int ptrace_ldt = 1;
|
2005-07-27 14:43:31 -04:00
|
|
|
int proc_mm = 1;
|
2005-11-07 03:58:55 -05:00
|
|
|
int skas_needs_stub = 0;
|
2005-07-27 14:43:31 -04:00
|
|
|
|
|
|
|
static int __init skas0_cmd_param(char *str, int* add)
|
|
|
|
{
|
|
|
|
ptrace_faultinfo = proc_mm = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-09 15:37:18 -04: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-03 18:57:47 -04: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 15:37:18 -04: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 04:44:19 -05:00
|
|
|
/* Changed only during early boot */
|
2005-09-03 18:57:47 -04:00
|
|
|
static int force_sysemu_disabled = 0;
|
|
|
|
|
2005-04-16 18:20:36 -04: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-03 18:57:47 -04: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-16 18:20:36 -04:00
|
|
|
|
|
|
|
static void __init check_sysemu(void)
|
|
|
|
{
|
2005-07-29 00:16:01 -04:00
|
|
|
void *stack;
|
2007-05-23 16:57:40 -04:00
|
|
|
unsigned long regs[MAX_REG_NR];
|
2007-02-10 04:44:20 -05:00
|
|
|
int pid, n, status, count=0;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("Checking syscall emulation patch for ptrace...");
|
2005-04-16 18:20:36 -04:00
|
|
|
sysemu_supported = 0;
|
2005-07-29 00:16:01 -04:00
|
|
|
pid = start_ptraced_child(&stack);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
|
|
|
if (n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_sysemu : wait failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal("check_sysemu : expected SIGTRAP, got status = %d",
|
|
|
|
status);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-05-23 16:57:40 -04:00
|
|
|
if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
|
|
|
|
fatal_perror("check_sysemu : PTRACE_GETREGS failed");
|
|
|
|
if(PT_SYSCALL_NR(regs) != __NR_getpid){
|
|
|
|
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());
|
|
|
|
if(n < 0){
|
|
|
|
non_fatal("check_sysemu : failed to modify system call "
|
|
|
|
"return");
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2005-07-29 00:16:01 -04:00
|
|
|
if (stop_ptraced_child(pid, stack, 0, 0) < 0)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto fail_stopped;
|
|
|
|
|
|
|
|
sysemu_supported = 1;
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("OK\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
set_using_sysemu(!force_sysemu_disabled);
|
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("Checking advanced syscall emulation patch for ptrace...");
|
2005-07-29 00:16:01 -04:00
|
|
|
pid = start_ptraced_child(&stack);
|
2005-09-03 18:57:51 -04:00
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
|
|
|
|
(void *) PTRACE_O_TRACESYSGOOD) < 0))
|
|
|
|
fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
|
2005-09-03 18:57:51 -04:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
while(1){
|
|
|
|
count++;
|
|
|
|
if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
|
|
|
|
goto fail;
|
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
|
|
|
if(n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : wait failed");
|
|
|
|
|
2005-09-03 18:57:51 -04:00
|
|
|
if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!count)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal("check_ptrace : SYSEMU_SINGLESTEP "
|
2005-09-03 18:57:51 -04:00
|
|
|
"doesn't singlestep");
|
2005-04-16 18:20:36 -04:00
|
|
|
n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
|
|
|
|
os_getpid());
|
|
|
|
if(n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_sysemu : failed to modify "
|
|
|
|
"system call return");
|
2005-04-16 18:20:36 -04:00
|
|
|
break;
|
|
|
|
}
|
2005-09-03 18:57:51 -04:00
|
|
|
else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
|
|
|
|
count++;
|
|
|
|
else
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal("check_ptrace : expected SIGTRAP or "
|
|
|
|
"(SIGTRAP | 0x80), got status = %d", status);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
2005-07-29 00:16:01 -04:00
|
|
|
if (stop_ptraced_child(pid, stack, 0, 0) < 0)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto fail_stopped;
|
|
|
|
|
|
|
|
sysemu_supported = 2;
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("OK\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
if ( !force_sysemu_disabled )
|
|
|
|
set_using_sysemu(sysemu_supported);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2005-07-29 00:16:01 -04:00
|
|
|
stop_ptraced_child(pid, stack, 1, 0);
|
2005-04-16 18:20:36 -04:00
|
|
|
fail_stopped:
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("missing\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2005-09-03 18:57:47 -04:00
|
|
|
static void __init check_ptrace(void)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2005-07-29 00:16:01 -04:00
|
|
|
void *stack;
|
2005-04-16 18:20:36 -04:00
|
|
|
int pid, syscall, n, status;
|
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("Checking that ptrace can change system call numbers...");
|
2005-07-29 00:16:01 -04:00
|
|
|
pid = start_ptraced_child(&stack);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
|
|
|
|
(void *) PTRACE_O_TRACESYSGOOD) < 0))
|
|
|
|
fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
while(1){
|
|
|
|
if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : ptrace failed");
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
|
|
|
|
if(n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : wait failed");
|
|
|
|
|
|
|
|
if(!WIFSTOPPED(status) ||
|
|
|
|
(WSTOPSIG(status) != (SIGTRAP | 0x80)))
|
|
|
|
fatal("check_ptrace : expected (SIGTRAP|0x80), "
|
|
|
|
"got status = %d", status);
|
2005-09-03 18:57:47 -04:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
|
|
|
|
0);
|
|
|
|
if(syscall == __NR_getpid){
|
|
|
|
n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
|
|
|
|
__NR_getppid);
|
|
|
|
if(n < 0)
|
2007-02-10 04:44:28 -05:00
|
|
|
fatal_perror("check_ptrace : failed to modify "
|
|
|
|
"system call");
|
2005-04-16 18:20:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-07-29 00:16:01 -04:00
|
|
|
stop_ptraced_child(pid, stack, 0, 1);
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("OK\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
check_sysemu();
|
|
|
|
}
|
|
|
|
|
2006-04-19 01:21:43 -04:00
|
|
|
extern void check_tmpexec(void);
|
2005-09-16 22:27:50 -04:00
|
|
|
|
2007-05-06 17:51:11 -04:00
|
|
|
static void __init check_coredump_limit(void)
|
2007-05-06 17:51:00 -04:00
|
|
|
{
|
|
|
|
struct rlimit lim;
|
|
|
|
int err = getrlimit(RLIMIT_CORE, &lim);
|
|
|
|
|
|
|
|
if(err){
|
|
|
|
perror("Getting core dump limit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Core dump limits :\n\tsoft - ");
|
|
|
|
if(lim.rlim_cur == RLIM_INFINITY)
|
|
|
|
printf("NONE\n");
|
|
|
|
else printf("%lu\n", lim.rlim_cur);
|
|
|
|
|
|
|
|
printf("\thard - ");
|
|
|
|
if(lim.rlim_max == RLIM_INFINITY)
|
|
|
|
printf("NONE\n");
|
|
|
|
else printf("%lu\n", lim.rlim_max);
|
|
|
|
}
|
|
|
|
|
2007-05-06 17:51:11 -04:00
|
|
|
void __init os_early_checks(void)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2007-05-06 17:51:00 -04:00
|
|
|
/* Print out the core dump limits early */
|
|
|
|
check_coredump_limit();
|
|
|
|
|
2005-09-03 18:57:47 -04:00
|
|
|
check_ptrace();
|
2005-09-16 22:27:50 -04:00
|
|
|
|
|
|
|
/* Need to check this early because mmapping happens before the
|
|
|
|
* kernel is running.
|
|
|
|
*/
|
|
|
|
check_tmpexec();
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2005-09-03 18:57:51 -04: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 03:58:55 -05: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");
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
#ifdef UML_CONFIG_MODE_SKAS
|
2005-11-07 03:58:55 -05:00
|
|
|
static inline void check_skas3_ptrace_faultinfo(void)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct ptrace_faultinfo fi;
|
2005-07-29 00:16:01 -04:00
|
|
|
void *stack;
|
[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-07 20:56:49 -04:00
|
|
|
int pid, n;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal(" - PTRACE_FAULTINFO...");
|
2005-07-29 00:16:01 -04:00
|
|
|
pid = start_ptraced_child(&stack);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
|
|
|
|
if (n < 0) {
|
2005-07-27 14:43:31 -04:00
|
|
|
ptrace_faultinfo = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
if(errno == EIO)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("not found\n");
|
2005-09-03 18:57:47 -04:00
|
|
|
else
|
2005-04-16 18:20:36 -04: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-07 20:56:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2005-07-27 14:43:31 -04:00
|
|
|
if (!ptrace_faultinfo)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found but disabled on command line\n");
|
2005-07-27 14:43:31 -04:00
|
|
|
else
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
init_registers(pid);
|
2005-07-29 00:16:01 -04:00
|
|
|
stop_ptraced_child(pid, stack, 1, 1);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2005-11-07 03:58:55 -05:00
|
|
|
static inline void check_skas3_ptrace_ldt(void)
|
|
|
|
{
|
|
|
|
#ifdef PTRACE_LDT
|
|
|
|
void *stack;
|
|
|
|
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 04:44:28 -05:00
|
|
|
non_fatal(" - PTRACE_LDT...");
|
2005-11-07 03:58:55 -05:00
|
|
|
pid = start_ptraced_child(&stack);
|
|
|
|
|
|
|
|
n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
|
|
|
|
if (n < 0) {
|
|
|
|
if(errno == EIO)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("not found\n");
|
2005-11-07 03:58:55 -05:00
|
|
|
else {
|
|
|
|
perror("not found");
|
|
|
|
}
|
|
|
|
ptrace_ldt = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(ptrace_ldt)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found\n");
|
2005-11-07 03:58:55 -05:00
|
|
|
else
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found, but use is disabled\n");
|
2005-11-07 03:58:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stop_ptraced_child(pid, stack, 1, 1);
|
|
|
|
#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-16 18:20:36 -04:00
|
|
|
{
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal(" - /proc/mm...");
|
2007-02-10 04:44:20 -05:00
|
|
|
if (access("/proc/mm", W_OK) < 0) {
|
2007-02-10 04:44:20 -05:00
|
|
|
proc_mm = 0;
|
2007-02-10 04:44:28 -05:00
|
|
|
perror("not found");
|
2005-09-03 18:57:47 -04:00
|
|
|
}
|
|
|
|
else {
|
2005-07-27 14:43:31 -04:00
|
|
|
if (!proc_mm)
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found but disabled on command line\n");
|
2005-07-27 14:43:31 -04:00
|
|
|
else
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("found\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
2005-11-07 03:58:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int can_do_skas(void)
|
|
|
|
{
|
2007-02-10 04:44:28 -05:00
|
|
|
non_fatal("Checking for the skas3 patch in the host:\n");
|
2005-11-07 03:58:55 -05:00
|
|
|
|
|
|
|
check_skas3_proc_mm();
|
|
|
|
check_skas3_ptrace_faultinfo();
|
|
|
|
check_skas3_ptrace_ldt();
|
|
|
|
|
|
|
|
if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
|
|
|
|
skas_needs_stub = 1;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
[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-07 20:56:49 -04:00
|
|
|
return 1;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int can_do_skas(void)
|
|
|
|
{
|
2007-02-10 04:44:20 -05:00
|
|
|
return 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
#endif
|
2005-09-16 22:27:50 -04:00
|
|
|
|
|
|
|
int __init parse_iomem(char *str, int *add)
|
|
|
|
{
|
|
|
|
struct iomem_region *new;
|
2007-02-10 04:44:20 -05:00
|
|
|
struct stat64 buf;
|
2005-09-16 22:27:50 -04:00
|
|
|
char *file, *driver;
|
2007-02-10 04:44:20 -05:00
|
|
|
int fd, size;
|
2005-09-16 22:27:50 -04:00
|
|
|
|
|
|
|
driver = str;
|
|
|
|
file = strchr(str,',');
|
|
|
|
if(file == NULL){
|
|
|
|
printf("parse_iomem : failed to parse iomem\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*file = '\0';
|
|
|
|
file++;
|
2007-02-10 04:44:20 -05:00
|
|
|
fd = open(file, O_RDWR, 0);
|
2005-09-16 22:27:50 -04:00
|
|
|
if(fd < 0){
|
|
|
|
os_print_error(fd, "parse_iomem - Couldn't open io file");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:44:20 -05:00
|
|
|
if(fstat64(fd, &buf) < 0){
|
|
|
|
perror("parse_iomem - cannot stat_fd file");
|
2005-09-16 22:27:50 -04:00
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
new = malloc(sizeof(*new));
|
|
|
|
if(new == NULL){
|
|
|
|
perror("Couldn't allocate iomem_region struct");
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:44:20 -05:00
|
|
|
size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
|
2005-09-16 22:27:50 -04: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 04:44:20 -05:00
|
|
|
return 0;
|
2005-09-16 22:27:50 -04:00
|
|
|
out_close:
|
2007-02-10 04:44:20 -05:00
|
|
|
close(fd);
|
2005-09-16 22:27:50 -04:00
|
|
|
out:
|
2007-02-10 04:44:20 -05:00
|
|
|
return 1;
|
2005-09-16 22:27:50 -04:00
|
|
|
}
|