Attach implicit threads to initproc instead of proc0. This way

applications which alter, by purpose or by accident, the uid in an
implicit thread are don't affect kernel threads.

from discussion with njoly
This commit is contained in:
pooka 2010-10-29 15:32:23 +00:00
parent 082af2faa5
commit 41a10084d4
6 changed files with 37 additions and 22 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_descrip.c,v 1.206 2010/09/01 15:15:18 pooka Exp $ */
/* $NetBSD: kern_descrip.c,v 1.207 2010/10/29 15:32:23 pooka Exp $ */
/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@ -70,7 +70,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.206 2010/09/01 15:15:18 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.207 2010/10/29 15:32:23 pooka Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -1339,7 +1339,6 @@ fd_hold(lwp_t *l)
{
filedesc_t *fdp = l->l_fd;
KASSERT(fdp == curlwp->l_fd || fdp == lwp0.l_fd);
atomic_inc_uint(&fdp->fd_refcnt);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lwproc.c,v 1.2 2010/09/01 21:16:56 pooka Exp $ */
/* $NetBSD: lwproc.c,v 1.3 2010/10/29 15:32:24 pooka Exp $ */
/*
* Copyright (c) 2010 Antti Kantee. All Rights Reserved.
@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.2 2010/09/01 21:16:56 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.3 2010/10/29 15:32:24 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
@ -222,16 +222,20 @@ lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
}
struct lwp *
rump__lwproc_allockernlwp(void)
rump__lwproc_alloclwp(struct proc *p)
{
struct proc *p;
struct lwp *l;
bool newproc = false;
if (p == NULL) {
p = lwproc_newproc(&proc0);
newproc = true;
}
l = kmem_zalloc(sizeof(*l), KM_SLEEP);
p = &proc0;
mutex_enter(p->p_lock);
lwproc_makelwp(p, l, false, false);
lwproc_makelwp(p, l, false, newproc);
return l;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 pooka Exp $ */
/* $NetBSD: rump.c,v 1.193 2010/10/29 15:32:24 pooka Exp $ */
/*
* Copyright (c) 2007 Antti Kantee. All Rights Reserved.
@ -28,7 +28,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.193 2010/10/29 15:32:24 pooka Exp $");
#include <sys/systm.h>
#define ELFSIZE ARCH_ELFSIZE
@ -86,8 +86,7 @@ __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 pooka Exp $");
char machine[] = MACHINE;
/* pretend the master rump proc is init */
struct proc *initproc = &proc0;
struct proc *initproc;
struct rumpuser_mtx *rump_giantlock;
@ -322,9 +321,10 @@ rump__init(int rump_version)
lwp_initspecific(&lwp0);
rump_scheduler_init(numcpu);
/* revert temporary context and schedule a real context */
/* revert temporary context and schedule a semireal context */
l->l_cpu = NULL;
rumpuser_set_curlwp(NULL);
initproc = &proc0; /* borrow proc0 before we get initproc started */
rump_schedule();
percpu_init();
@ -407,6 +407,18 @@ rump__init(int rump_version)
if (rump_threads)
vmem_rehash_start();
/*
* Create init, used to attach implicit threads in rump.
* (note: must be done after vfsinit to get cwdi)
*/
(void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
mutex_enter(proc_lock);
initproc = proc_find_raw(1);
mutex_exit(proc_lock);
if (initproc == NULL)
panic("where in the world is initproc?");
/* release cpu */
rump_unschedule();
return 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: rump_private.h,v 1.59 2010/10/27 20:44:49 pooka Exp $ */
/* $NetBSD: rump_private.h,v 1.60 2010/10/29 15:32:24 pooka Exp $ */
/*
* Copyright (c) 2007 Antti Kantee. All Rights Reserved.
@ -105,7 +105,7 @@ extern rump_proc_vfs_release_fn rump_proc_vfs_release;
extern struct cpu_info *rump_cpu;
struct lwp * rump__lwproc_allockernlwp(void);
struct lwp * rump__lwproc_alloclwp(struct proc *);
void rump_cpus_bootstrap(int);
void rump_scheduler_init(int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: scheduler.c,v 1.20 2010/09/07 07:59:48 pooka Exp $ */
/* $NetBSD: scheduler.c,v 1.21 2010/10/29 15:32:24 pooka Exp $ */
/*
* Copyright (c) 2010 Antti Kantee. All Rights Reserved.
@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.20 2010/09/07 07:59:48 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.21 2010/10/29 15:32:24 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
@ -236,7 +236,7 @@ rump_schedule()
rumpuser_set_curlwp(&lwp0);
/* allocate thread, switch to it, and release lwp0 */
l = rump__lwproc_allockernlwp();
l = rump__lwproc_alloclwp(initproc);
rump_lwproc_switch(l);
lwp0rele();

View File

@ -1,4 +1,4 @@
/* $NetBSD: threads.c,v 1.12 2010/09/01 19:37:59 pooka Exp $ */
/* $NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $ */
/*
* Copyright (c) 2007-2009 Antti Kantee. All Rights Reserved.
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.12 2010/09/01 19:37:59 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
@ -139,7 +139,7 @@ kthread_create(pri_t pri, int flags, struct cpu_info *ci,
k = malloc(sizeof(*k), M_TEMP, M_WAITOK);
k->f = func;
k->arg = arg;
k->mylwp = l = rump__lwproc_allockernlwp();
k->mylwp = l = rump__lwproc_alloclwp(&proc0);
l->l_flag |= LW_SYSTEM;
if (flags & KTHREAD_MPSAFE)
l->l_pflag |= LP_MPSAFE;