From 9910d07b2f8670a44edbe17863f961ffbc6ea537 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 15 Dec 2009 10:43:48 -0800 Subject: [PATCH] kernel - use new td_ucred in numerous places * Use curthread->td_ucred in numerous places, primarily system calls, where curproc->p_ucred was used before. * Clean up local variable use related to the above. * Adjust several places where p_ucred is replaced to properly deal with lwp threading races to avoid accessing and freeing a potentially stale ucred. * Adjust static procedures in the ktrace code to generally take lwp pointers instead of proc pointers. --- sys/emulation/43bsd/43bsd_hostinfo.c | 4 +- sys/emulation/dragonfly12/dfbsd12_stat.c | 2 +- sys/emulation/linux/linux_file.c | 15 +++-- sys/emulation/linux/linux_misc.c | 14 ++-- sys/emulation/linux/linux_socket.c | 3 +- sys/emulation/linux/linux_stats.c | 3 +- sys/emulation/linux/linux_uid16.c | 6 +- sys/kern/imgact_resident.c | 14 ++-- sys/kern/init_main.c | 1 + sys/kern/kern_acl.c | 12 ++-- sys/kern/kern_checkpoint.c | 17 ++--- sys/kern/kern_descrip.c | 10 +-- sys/kern/kern_event.c | 2 +- sys/kern/kern_fork.c | 6 +- sys/kern/kern_jail.c | 14 ++-- sys/kern/kern_ktrace.c | 57 ++++++++-------- sys/kern/kern_prot.c | 73 ++++++++++----------- sys/kern/kern_resource.c | 4 +- sys/kern/kern_sysctl.c | 2 +- sys/kern/kern_varsym.c | 59 +++++++++++------ sys/kern/lwkt_caps.c | 58 ++++++++++------- sys/kern/sys_generic.c | 2 +- sys/kern/sys_mqueue.c | 14 ++-- sys/kern/sysv_msg.c | 30 ++++----- sys/kern/sysv_sem.c | 65 +++++++++++-------- sys/kern/sysv_shm.c | 29 +++++---- sys/kern/uipc_syscalls.c | 6 +- sys/kern/vfs_syscalls.c | 83 +++++++++++------------- sys/netproto/ncp/ncp_mod.c | 10 +-- sys/platform/pc32/i386/sys_machdep.c | 2 +- sys/sys/thread.h | 5 ++ sys/vfs/nfs/nfs_syscalls.c | 2 +- sys/vm/vm_mmap.c | 5 +- sys/vm/vm_swap.c | 6 +- 34 files changed, 341 insertions(+), 294 deletions(-) diff --git a/sys/emulation/43bsd/43bsd_hostinfo.c b/sys/emulation/43bsd/43bsd_hostinfo.c index a9e4a572d9..68985c3321 100644 --- a/sys/emulation/43bsd/43bsd_hostinfo.c +++ b/sys/emulation/43bsd/43bsd_hostinfo.c @@ -84,16 +84,14 @@ int sys_osethostname(struct sethostname_args *uap) { struct thread *td = curthread; - struct proc *p = td->td_proc; size_t len; char *hostname; int name[2]; int error; - KKASSERT(p); name[0] = CTL_KERN; name[1] = KERN_HOSTNAME; - error = priv_check_cred(p->p_ucred, PRIV_SETHOSTNAME, 0); + error = priv_check_cred(td->td_ucred, PRIV_SETHOSTNAME, 0); if (error) return (error); len = MIN(uap->len, MAXHOSTNAMELEN); diff --git a/sys/emulation/dragonfly12/dfbsd12_stat.c b/sys/emulation/dragonfly12/dfbsd12_stat.c index 3462fb704e..404b316e41 100644 --- a/sys/emulation/dragonfly12/dfbsd12_stat.c +++ b/sys/emulation/dragonfly12/dfbsd12_stat.c @@ -163,7 +163,7 @@ sys_dfbsd12_fhstat(struct dfbsd12_fhstat_args *uap) } if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp))) goto done; - error = vn_stat(vp, &sb, td->td_proc->p_ucred); + error = vn_stat(vp, &sb, td->td_ucred); vput(vp); if (error) goto done; diff --git a/sys/emulation/linux/linux_file.c b/sys/emulation/linux/linux_file.c index dfe898b7f8..c966d2fd0f 100644 --- a/sys/emulation/linux/linux_file.c +++ b/sys/emulation/linux/linux_file.c @@ -151,8 +151,10 @@ sys_linux_open(struct linux_open_args *args) fp = holdfp(p->p_fd, args->sysmsg_iresult, -1); if (fp) { - if (fp->f_type == DTYPE_VNODE) - fo_ioctl(fp, TIOCSCTTY, NULL, p->p_ucred, NULL); + if (fp->f_type == DTYPE_VNODE) { + fo_ioctl(fp, TIOCSCTTY, NULL, + td->td_ucred, NULL); + } fdrop(fp); } } @@ -1138,7 +1140,7 @@ bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) static int linux_fcntl_common(struct linux_fcntl64_args *args) { - struct proc *p = curproc; + struct thread *td = curthread; struct l_flock linux_flock; struct file *fp; union fcntl_dat dat; @@ -1190,7 +1192,7 @@ linux_fcntl_common(struct linux_fcntl64_args *args) * significant effect for pipes (SIGIO is not delivered for * pipes under Linux-2.2.35 at least). */ - fp = holdfp(p->p_fd, args->fd, -1); + fp = holdfp(td->td_proc->p_fd, args->fd, -1); if (fp == NULL) return (EBADF); if (fp->f_type == DTYPE_PIPE) { @@ -1206,7 +1208,7 @@ linux_fcntl_common(struct linux_fcntl64_args *args) } /* MPSAFE */ - error = kern_fcntl(args->fd, cmd, &dat, p->p_ucred); + error = kern_fcntl(args->fd, cmd, &dat, td->td_ucred); if (error == 0) { switch (args->cmd) { @@ -1284,6 +1286,7 @@ sys_linux_fcntl(struct linux_fcntl_args *args) int sys_linux_fcntl64(struct linux_fcntl64_args *args) { + struct thread *td = curthread; struct l_flock64 linux_flock; union fcntl_dat dat; int error, cmd = 0; @@ -1313,7 +1316,7 @@ sys_linux_fcntl64(struct linux_fcntl64_args *args) linux_to_bsd_flock64(&linux_flock, &dat.fc_flock); /* MPSAFE */ - error = kern_fcntl(args->fd, cmd, &dat, curproc->p_ucred); + error = kern_fcntl(args->fd, cmd, &dat, td->td_ucred); if (error == 0 && args->cmd == LINUX_F_GETLK64) { bsd_to_linux_flock64(&dat.fc_flock, &linux_flock); diff --git a/sys/emulation/linux/linux_misc.c b/sys/emulation/linux/linux_misc.c index 59dcf78209..cf62405ec4 100644 --- a/sys/emulation/linux/linux_misc.c +++ b/sys/emulation/linux/linux_misc.c @@ -316,7 +316,7 @@ sys_linux_uselib(struct linux_uselib_args *args) goto cleanup; } - error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL); + error = VOP_OPEN(vp, FREAD, td->td_ucred, NULL); if (error) goto cleanup; @@ -1028,7 +1028,7 @@ sys_linux_setgroups(struct linux_setgroups_args *args) int ngrp, error; ngrp = args->gidsetsize; - oldcred = p->p_ucred; + oldcred = td->td_ucred; /* * cr_groups[0] holds egid. Setting the whole set from @@ -1066,6 +1066,7 @@ sys_linux_setgroups(struct linux_setgroups_args *args) } setsugid(); + oldcred = p->p_ucred; /* reload, deal with threads race */ p->p_ucred = newcred; crfree(oldcred); rel_mplock(); @@ -1079,13 +1080,12 @@ int sys_linux_getgroups(struct linux_getgroups_args *args) { struct thread *td = curthread; - struct proc *p = td->td_proc; struct ucred *cred; l_gid_t linux_gidset[NGROUPS]; gid_t *bsd_gidset; int bsd_gidsetsz, ngrp, error; - cred = p->p_ucred; + cred = td->td_ucred; bsd_gidset = cred->cr_groups; bsd_gidsetsz = cred->cr_ngroups - 1; @@ -1417,9 +1417,8 @@ int sys_linux_getgid(struct linux_getgid_args *args) { struct thread *td = curthread; - struct proc *p = td->td_proc; - args->sysmsg_result = p->p_ucred->cr_rgid; + args->sysmsg_result = td->td_ucred->cr_rgid; return (0); } @@ -1430,9 +1429,8 @@ int sys_linux_getuid(struct linux_getuid_args *args) { struct thread *td = curthread; - struct proc *p = td->td_proc; - args->sysmsg_result = p->p_ucred->cr_ruid; + args->sysmsg_result = td->td_ucred->cr_ruid; return (0); } diff --git a/sys/emulation/linux/linux_socket.c b/sys/emulation/linux/linux_socket.c index 6edc1403dc..a40609b219 100644 --- a/sys/emulation/linux/linux_socket.c +++ b/sys/emulation/linux/linux_socket.c @@ -415,6 +415,7 @@ struct linux_accept_args { static int linux_accept(struct linux_accept_args *args, int *res) { + struct thread *td = curthread; struct linux_accept_args linux_args; struct sockaddr *sa = NULL; union fcntl_dat dat = { 0 }; @@ -461,7 +462,7 @@ linux_accept(struct linux_accept_args *args, int *res) * accepted one, so we must clear the flags in the new descriptor. * Ignore any errors, because we already have an open fd. */ - kern_fcntl(*res, F_SETFL, &dat, curproc->p_ucred); + kern_fcntl(*res, F_SETFL, &dat, td->td_ucred); return (0); } diff --git a/sys/emulation/linux/linux_stats.c b/sys/emulation/linux/linux_stats.c index b6c2afa6f7..440e38d37f 100644 --- a/sys/emulation/linux/linux_stats.c +++ b/sys/emulation/linux/linux_stats.c @@ -323,6 +323,7 @@ struct l_ustat int sys_linux_ustat(struct linux_ustat_args *args) { + struct thread *td = curthread; struct l_ustat lu; cdev_t dev; struct vnode *vp; @@ -356,7 +357,7 @@ sys_linux_ustat(struct linux_ustat_args *args) goto done; } stat = &(vp->v_mount->mnt_stat); - error = VFS_STATFS(vp->v_mount, stat, curproc->p_ucred); + error = VFS_STATFS(vp->v_mount, stat, td->td_ucred); vrele(vp); if (error == 0) { lu.f_tfree = stat->f_bfree; diff --git a/sys/emulation/linux/linux_uid16.c b/sys/emulation/linux/linux_uid16.c index c01106b5c4..215ff84ed9 100644 --- a/sys/emulation/linux/linux_uid16.c +++ b/sys/emulation/linux/linux_uid16.c @@ -115,7 +115,8 @@ sys_linux_lchown16(struct linux_lchown16_args *args) int sys_linux_setgroups16(struct linux_setgroups16_args *args) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; struct ucred *newcred, *oldcred; l_gid16_t linux_gidset[NGROUPS]; gid_t *bsd_gidset; @@ -127,7 +128,7 @@ sys_linux_setgroups16(struct linux_setgroups16_args *args) #endif ngrp = args->gidsetsize; - oldcred = p->p_ucred; + oldcred = td->td_ucred; /* * cr_groups[0] holds egid. Setting the whole set from @@ -164,6 +165,7 @@ sys_linux_setgroups16(struct linux_setgroups16_args *args) } setsugid(); + oldcred = p->p_ucred; /* deal with threads race */ p->p_ucred = newcred; crfree(oldcred); error = 0; diff --git a/sys/kern/imgact_resident.c b/sys/kern/imgact_resident.c index 4bdc3c6a21..fecaa18bdf 100644 --- a/sys/kern/imgact_resident.c +++ b/sys/kern/imgact_resident.c @@ -109,7 +109,7 @@ fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td) goto done; /* retrieve underlying stat information and release vnode */ - error = vn_stat(vrtmp, &st, td->td_proc->p_ucred); + error = vn_stat(vrtmp, &st, td->td_ucred); vput(vrtmp); if (error) goto done; @@ -195,13 +195,15 @@ exec_resident_imgact(struct image_params *imgp) int sys_exec_sys_register(struct exec_sys_register_args *uap) { + struct thread *td = curthread; struct vmresident *vmres; struct vnode *vp; struct proc *p; int error; - p = curproc; - if ((error = priv_check_cred(p->p_ucred, PRIV_VM_RESIDENT, 0)) != 0) + p = td->td_proc; + error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0); + if (error) return(error); get_mplock(); @@ -244,14 +246,16 @@ sys_exec_sys_register(struct exec_sys_register_args *uap) int sys_exec_sys_unregister(struct exec_sys_unregister_args *uap) { + struct thread *td = curthread; struct vmresident *vmres; struct proc *p; int error; int id; int count; - p = curproc; - if ((error = priv_check_cred(p->p_ucred, PRIV_VM_RESIDENT, 0)) != 0) + p = td->td_proc; + error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0); + if (error) return(error); /* diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 8568923a7f..3e9db5649a 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -378,6 +378,7 @@ proc0_init(void *dummy __unused) p->p_ucred->cr_ruidinfo = uifind(0); p->p_ucred->cr_ngroups = 1; /* group 0 */ p->p_ucred->cr_uidinfo = uifind(0); + thread0.td_ucred = crhold(p->p_ucred); /* bootstrap fork1() */ /* Don't jail it */ p->p_ucred->cr_prison = NULL; diff --git a/sys/kern/kern_acl.c b/sys/kern/kern_acl.c index af23798732..37fa0274ca 100644 --- a/sys/kern/kern_acl.c +++ b/sys/kern/kern_acl.c @@ -74,8 +74,7 @@ vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) error = copyin(aclp, &inkernacl, sizeof(struct acl)); if (error) return(error); - KKASSERT(td->td_proc); - ucred = td->td_proc->p_ucred; + ucred = td->td_ucred; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_SETACL(vp, type, &inkernacl, ucred); @@ -94,8 +93,7 @@ vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) struct ucred *ucred; int error; - KKASSERT(td->td_proc); - ucred = td->td_proc->p_ucred; + ucred = td->td_ucred; error = VOP_GETACL(vp, type, &inkernelacl, ucred); if (error == 0) error = copyout(&inkernelacl, aclp, sizeof(struct acl)); @@ -112,8 +110,7 @@ vacl_delete(struct vnode *vp, acl_type_t type) struct ucred *ucred; int error; - KKASSERT(td->td_proc); - ucred = td->td_proc->p_ucred; + ucred = td->td_ucred; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred); vn_unlock(vp); @@ -131,8 +128,7 @@ vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp) struct acl inkernelacl; int error; - KKASSERT(td->td_proc); - ucred = td->td_proc->p_ucred; + ucred = td->td_ucred; error = copyin(aclp, &inkernelacl, sizeof(struct acl)); if (error) return(error); diff --git a/sys/kern/kern_checkpoint.c b/sys/kern/kern_checkpoint.c index 7efcff160f..e86d326251 100644 --- a/sys/kern/kern_checkpoint.c +++ b/sys/kern/kern_checkpoint.c @@ -711,15 +711,15 @@ int sys_sys_checkpoint(struct sys_checkpoint_args *uap) { int error = 0; - struct lwp *lp = curthread->td_lwp; - struct proc *p = curthread->td_proc; + struct thread *td = curthread; + struct proc *p = td->td_proc; struct file *fp; /* * Only certain groups (to reduce our security exposure). -1 * allows any group. */ - if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0) + if (ckptgroup >= 0 && groupmember(ckptgroup, td->td_ucred) == 0) return (EPERM); /* @@ -734,11 +734,11 @@ sys_sys_checkpoint(struct sys_checkpoint_args *uap) case CKPT_FREEZE: fp = NULL; if (uap->fd == -1 && uap->pid == (pid_t)-1) - error = checkpoint_signal_handler(lp); + error = checkpoint_signal_handler(td->td_lwp); else if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL) error = EBADF; else - error = ckpt_freeze_proc(lp, fp); + error = ckpt_freeze_proc(td->td_lwp, fp); if (fp) fdrop(fp); break; @@ -752,7 +752,7 @@ sys_sys_checkpoint(struct sys_checkpoint_args *uap) break; } uap->sysmsg_result = uap->retval; - error = ckpt_thaw_proc(lp, fp); + error = ckpt_thaw_proc(td->td_lwp, fp); fdrop(fp); break; default: @@ -766,6 +766,7 @@ sys_sys_checkpoint(struct sys_checkpoint_args *uap) int checkpoint_signal_handler(struct lwp *lp) { + struct thread *td = lp->lwp_thread; struct proc *p = lp->lwp_proc; char *buf; struct file *fp; @@ -783,7 +784,7 @@ checkpoint_signal_handler(struct lwp *lp) return (EPERM); } - buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); + buf = ckpt_expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid); if (buf == NULL) { chptinuse--; return (ENOMEM); @@ -791,7 +792,7 @@ checkpoint_signal_handler(struct lwp *lp) log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n", p->p_pid, p->p_comm, - (p->p_ucred ? p->p_ucred->cr_uid : -1), + (td->td_ucred ? td->td_ucred->cr_uid : -1), buf); PRINTF(("ckpt handler called, using '%s'\n", buf)); diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 4053a9f91a..d4909405b2 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -415,7 +415,7 @@ sys_fcntl(struct fcntl_args *uap) break; } - error = kern_fcntl(uap->fd, uap->cmd, &dat, curproc->p_ucred); + error = kern_fcntl(uap->fd, uap->cmd, &dat, curthread->td_ucred); if (error == 0) { switch (uap->cmd) { @@ -712,9 +712,9 @@ fsetown(pid_t pgid, struct sigio **sigiop) sigio->sio_pgrp = pgrp; } sigio->sio_pgid = pgid; - sigio->sio_ucred = crhold(curproc->p_ucred); + sigio->sio_ucred = crhold(curthread->td_ucred); /* It would be convenient if p_ruid was in ucred. */ - sigio->sio_ruid = curproc->p_ucred->cr_ruid; + sigio->sio_ruid = sigio->sio_ucred->cr_ruid; sigio->sio_myref = sigiop; crit_enter(); *sigiop = sigio; @@ -900,7 +900,7 @@ kern_fstat(int fd, struct stat *ub) if ((fp = holdfp(p->p_fd, fd, -1)) == NULL) return (EBADF); - error = fo_stat(fp, ub, p->p_ucred); + error = fo_stat(fp, ub, td->td_ucred); fdrop(fp); return (error); @@ -2617,7 +2617,7 @@ sysctl_kern_file_callback(struct proc *p, void *data) if (p->p_stat == SIDL || p->p_stat == SZOMB) return(0); - if (!PRISON_CHECK(info->req->td->td_proc->p_ucred, p->p_ucred) != 0) + if (!PRISON_CHECK(info->req->td->td_ucred, p->p_ucred) != 0) return(0); /* diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index 74d4efda28..9367dca43b 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -204,7 +204,7 @@ filt_procattach(struct knote *kn) } if (p == NULL) return (ESRCH); - if (! PRISON_CHECK(curproc->p_ucred, p->p_ucred)) + if (!PRISON_CHECK(curthread->td_ucred, p->p_ucred)) return (EACCES); kn->kn_ptr.p_proc = p; diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index daf75f5fcf..b24aa22101 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -315,7 +315,7 @@ fork1(struct lwp *lp1, int flags, struct proc **procp) * exceed the limit. The variable nprocs is the current number of * processes, maxproc is the limit. */ - uid = p1->p_ucred->cr_ruid; + uid = lp1->lwp_thread->td_ucred->cr_ruid; if ((nprocs >= maxproc - 10 && uid != 0) || nprocs >= maxproc) { if (ppsratecheck(&lastfail, &curfail, 1)) kprintf("maxproc limit exceeded by uid %d, please " @@ -334,7 +334,7 @@ fork1(struct lwp *lp1, int flags, struct proc **procp) * Increment the count of procs running with this uid. Don't allow * a nonprivileged user to exceed their current limit. */ - ok = chgproccnt(p1->p_ucred->cr_ruidinfo, 1, + ok = chgproccnt(lp1->lwp_thread->td_ucred->cr_ruidinfo, 1, (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0); if (!ok) { /* @@ -388,7 +388,7 @@ fork1(struct lwp *lp1, int flags, struct proc **procp) */ if (p1->p_flag & P_PROFIL) startprofclock(p2); - p2->p_ucred = crhold(p1->p_ucred); + p2->p_ucred = crhold(lp1->lwp_thread->td_ucred); KKASSERT(p2->p_lock == 0); if (jailed(p2->p_ucred)) diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 733e3aac3d..6b0da590bf 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -366,7 +366,7 @@ prison_replace_wildcards(struct thread *td, struct sockaddr *ip) if (td->td_proc == NULL) return (1); - if ((pr = td->td_proc->p_ucred->cr_prison) == NULL) + if ((pr = td->td_ucred->cr_prison) == NULL) return (1); if ((ip->sa_family == AF_INET && @@ -398,7 +398,7 @@ prison_remote_ip(struct thread *td, struct sockaddr *ip) if (td == NULL || td->td_proc == NULL) return(1); - if ((pr = td->td_proc->p_ucred->cr_prison) == NULL) + if ((pr = td->td_ucred->cr_prison) == NULL) return(1); if ((ip->sa_family == AF_INET && ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) || @@ -548,12 +548,13 @@ prison_find(int prid) static int sysctl_jail_list(SYSCTL_HANDLER_ARGS) { + struct thread *td = curthread; struct jail_ip_storage *jip; #ifdef INET6 struct sockaddr_in6 *jsin6; #endif struct sockaddr_in *jsin; - struct proc *p; + struct lwp *lp; struct prison *pr; unsigned int jlssize, jlsused; int count, error; @@ -562,10 +563,10 @@ sysctl_jail_list(SYSCTL_HANDLER_ARGS) char *fullpath, *freepath; jlsused = 0; - p = curthread->td_proc; - if (jailed(p->p_ucred)) + if (jailed(td->td_ucred)) return (0); + lp = td->td_lwp; retry: count = prisoncount; @@ -581,7 +582,8 @@ retry: count = prisoncount; LIST_FOREACH(pr, &allprison, pr_list) { - error = cache_fullpath(p, &pr->pr_root, &fullpath, &freepath); + error = cache_fullpath(lp->lwp_proc, &pr->pr_root, + &fullpath, &freepath); if (error) continue; if (jlsused && jlsused < jlssize) diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c index 5af53133e5..f21cdf42f8 100644 --- a/sys/kern/kern_ktrace.c +++ b/sys/kern/kern_ktrace.c @@ -56,10 +56,10 @@ static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); #ifdef KTRACE static struct ktr_header *ktrgetheader (int type); -static void ktrwrite (struct proc *, struct ktr_header *, struct uio *); -static int ktrcanset (struct proc *,struct proc *); -static int ktrsetchildren (struct proc *,struct proc *,int,int, ktrace_node_t); -static int ktrops (struct proc *,struct proc *,int,int, ktrace_node_t); +static void ktrwrite (struct lwp *, struct ktr_header *, struct uio *); +static int ktrcanset (struct thread *,struct proc *); +static int ktrsetchildren (struct thread *,struct proc *,int,int, ktrace_node_t); +static int ktrops (struct thread *,struct proc *,int,int, ktrace_node_t); /* * MPSAFE @@ -109,7 +109,7 @@ ktrsyscall(struct lwp *lp, int code, int narg, register_t args[]) *argp++ = args[i]; kth->ktr_buf = (caddr_t)ktp; kth->ktr_len = len; - ktrwrite(lp->lwp_proc, kth, NULL); + ktrwrite(lp, kth, NULL); FREE(ktp, M_KTRACE); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; @@ -130,7 +130,7 @@ ktrsysret(struct lwp *lp, int code, int error, register_t retval) kth->ktr_buf = (caddr_t)&ktp; kth->ktr_len = sizeof(struct ktr_sysret); - ktrwrite(lp->lwp_proc, kth, NULL); + ktrwrite(lp, kth, NULL); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; } @@ -145,7 +145,7 @@ ktrnamei(struct lwp *lp, char *path) kth->ktr_len = strlen(path); kth->ktr_buf = path; - ktrwrite(lp->lwp_proc, kth, NULL); + ktrwrite(lp, kth, NULL); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; } @@ -167,7 +167,7 @@ ktrgenio(struct lwp *lp, int fd, enum uio_rw rw, struct uio *uio, int error) uio->uio_offset = 0; uio->uio_rw = UIO_WRITE; - ktrwrite(lp->lwp_proc, kth, uio); + ktrwrite(lp, kth, uio); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; } @@ -187,7 +187,7 @@ ktrpsig(struct lwp *lp, int sig, sig_t action, sigset_t *mask, int code) kth->ktr_buf = (caddr_t)&kp; kth->ktr_len = sizeof (struct ktr_psig); - ktrwrite(lp->lwp_proc, kth, NULL); + ktrwrite(lp, kth, NULL); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; } @@ -205,7 +205,7 @@ ktrcsw(struct lwp *lp, int out, int user) kth->ktr_buf = (caddr_t)&kc; kth->ktr_len = sizeof (struct ktr_csw); - ktrwrite(lp->lwp_proc, kth, NULL); + ktrwrite(lp, kth, NULL); FREE(kth, M_KTRACE); lp->lwp_traceflag &= ~KTRFAC_ACTIVE; } @@ -305,9 +305,9 @@ sys_ktrace(struct ktrace_args *uap) } LIST_FOREACH(p, &pg->pg_members, p_pglist) { if (descend) - ret |= ktrsetchildren(curp, p, ops, facs, tracenode); + ret |= ktrsetchildren(td, p, ops, facs, tracenode); else - ret |= ktrops(curp, p, ops, facs, tracenode); + ret |= ktrops(td, p, ops, facs, tracenode); } } else { /* @@ -319,9 +319,9 @@ sys_ktrace(struct ktrace_args *uap) goto done; } if (descend) - ret |= ktrsetchildren(curp, p, ops, facs, tracenode); + ret |= ktrsetchildren(td, p, ops, facs, tracenode); else - ret |= ktrops(curp, p, ops, facs, tracenode); + ret |= ktrops(td, p, ops, facs, tracenode); } if (!ret) error = EPERM; @@ -354,7 +354,7 @@ ktrace_clear_callback(struct proc *p, void *data) } } else { if (p->p_tracenode->kn_vp == info->tracenode->kn_vp) { - if (ktrcanset(curproc, p)) { + if (ktrcanset(curthread, p)) { ktrdestroy(&p->p_tracenode); p->p_traceflag = 0; } else { @@ -379,7 +379,6 @@ sys_utrace(struct utrace_args *uap) #ifdef KTRACE struct ktr_header *kth; struct thread *td = curthread; /* XXX */ - struct proc *p = td->td_proc; caddr_t cp; if (!KTRPOINT(td, KTR_USER)) @@ -393,7 +392,7 @@ sys_utrace(struct utrace_args *uap) kth->ktr_buf = cp; kth->ktr_len = uap->len; get_mplock(); - ktrwrite(p, kth, NULL); + ktrwrite(td->td_lwp, kth, NULL); rel_mplock(); } FREE(kth, M_KTRACE); @@ -441,12 +440,12 @@ ktrinherit(ktrace_node_t tracenode) #ifdef KTRACE static int -ktrops(struct proc *curp, struct proc *p, int ops, int facs, +ktrops(struct thread *td, struct proc *p, int ops, int facs, ktrace_node_t tracenode) { ktrace_node_t oldnode; - if (!ktrcanset(curp, p)) + if (!ktrcanset(td, p)) return (0); if (ops == KTROP_SET) { if ((oldnode = p->p_tracenode) != tracenode) { @@ -454,7 +453,7 @@ ktrops(struct proc *curp, struct proc *p, int ops, int facs, ktrdestroy(&oldnode); } p->p_traceflag |= facs; - if (curp->p_ucred->cr_uid == 0) + if (td->td_ucred->cr_uid == 0) p->p_traceflag |= KTRFAC_ROOT; } else { /* KTROP_CLEAR */ @@ -469,7 +468,7 @@ ktrops(struct proc *curp, struct proc *p, int ops, int facs, } static int -ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs, +ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs, ktrace_node_t tracenode) { struct proc *p; @@ -477,7 +476,7 @@ ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs, p = top; for (;;) { - ret |= ktrops(curp, p, ops, facs, tracenode); + ret |= ktrops(td, p, ops, facs, tracenode); /* * If this process has children, descend to them next, * otherwise do any siblings, and if done with this level, @@ -499,7 +498,7 @@ ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs, } static void -ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio) +ktrwrite(struct lwp *lp, struct ktr_header *kth, struct uio *uio) { struct ktrace_clear_info info; struct uio auio; @@ -514,9 +513,9 @@ ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio) * * XXX not MP safe */ - if (p->p_tracenode == NULL) + if (lp->lwp_proc->p_tracenode == NULL) return; - tracenode = ktrinherit(p->p_tracenode); + tracenode = ktrinherit(lp->lwp_proc->p_tracenode); auio.uio_iov = &aiov[0]; auio.uio_offset = 0; auio.uio_segflg = UIO_SYSSPACE; @@ -536,10 +535,10 @@ ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio) } vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_WRITE(tracenode->kn_vp, &auio, - IO_UNIT | IO_APPEND, p->p_ucred); + IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred); if (error == 0 && uio != NULL) { error = VOP_WRITE(tracenode->kn_vp, uio, - IO_UNIT | IO_APPEND, p->p_ucred); + IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred); } vn_unlock(tracenode->kn_vp); if (error) { @@ -568,9 +567,9 @@ ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio) * TODO: check groups. use caller effective gid. */ static int -ktrcanset(struct proc *callp, struct proc *targetp) +ktrcanset(struct thread *calltd, struct proc *targetp) { - struct ucred *caller = callp->p_ucred; + struct ucred *caller = calltd->td_ucred; struct ucred *target = targetp->p_ucred; if (!PRISON_CHECK(caller, target)) diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 0abdbcf61f..55a54ff2cb 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -184,16 +184,16 @@ sys_getsid(struct getsid_args *uap) /* * getuid() * - * MPSAFE XXX ucred + * MPSAFE */ int sys_getuid(struct getuid_args *uap) { - struct proc *p = curproc; + struct ucred *cred = curthread->td_ucred; - uap->sysmsg_fds[0] = p->p_ucred->cr_ruid; + uap->sysmsg_fds[0] = cred->cr_ruid; #if defined(COMPAT_43) || defined(COMPAT_SUNOS) - uap->sysmsg_fds[1] = p->p_ucred->cr_uid; + uap->sysmsg_fds[1] = cred->cr_uid; #endif return (0); } @@ -201,30 +201,30 @@ sys_getuid(struct getuid_args *uap) /* * geteuid() * - * MPSAFE XXX ucred + * MPSAFE */ int sys_geteuid(struct geteuid_args *uap) { - struct proc *p = curproc; + struct ucred *cred = curthread->td_ucred; - uap->sysmsg_result = p->p_ucred->cr_uid; + uap->sysmsg_result = cred->cr_uid; return (0); } /* * getgid() * - * MPSAFE XXX UCRED + * MPSAFE */ int sys_getgid(struct getgid_args *uap) { - struct proc *p = curproc; + struct ucred *cred = curthread->td_ucred; - uap->sysmsg_fds[0] = p->p_ucred->cr_rgid; + uap->sysmsg_fds[0] = cred->cr_rgid; #if defined(COMPAT_43) || defined(COMPAT_SUNOS) - uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0]; + uap->sysmsg_fds[1] = cred->cr_groups[0]; #endif return (0); } @@ -239,9 +239,9 @@ sys_getgid(struct getgid_args *uap) int sys_getegid(struct getegid_args *uap) { - struct proc *p = curproc; + struct ucred *cred = curthread->td_ucred; - uap->sysmsg_result = p->p_ucred->cr_groups[0]; + uap->sysmsg_result = cred->cr_groups[0]; return (0); } @@ -251,12 +251,11 @@ sys_getegid(struct getegid_args *uap) int sys_getgroups(struct getgroups_args *uap) { - struct proc *p = curproc; struct ucred *cr; u_int ngrp; int error; - cr = p->p_ucred; + cr = curthread->td_ucred; if ((ngrp = uap->gidsetsize) == 0) { uap->sysmsg_result = cr->cr_ngroups; return (0); @@ -467,11 +466,13 @@ sys_seteuid(struct seteuid_args *uap) uid_t euid; int error; + get_mplock(); cr = p->p_ucred; euid = uap->euid; if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */ euid != cr->cr_svuid && /* allow seteuid(saved uid) */ (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) { + rel_mplock(); return (error); } @@ -480,11 +481,10 @@ sys_seteuid(struct seteuid_args *uap) * not see our changes. */ if (cr->cr_uid != euid) { - get_mplock(); change_euid(euid); setsugid(); - rel_mplock(); } + rel_mplock(); return (0); } @@ -863,27 +863,24 @@ sys_getresuid(struct getresuid_args *uap) } /* - * MPALMOSTSAFE + * MPSAFE */ int sys_getresgid(struct getresgid_args *uap) { - struct proc *p = curproc; struct ucred *cr; int error1 = 0, error2 = 0, error3 = 0; - get_mplock(); - cr = p->p_ucred; + cr = curthread->td_ucred; if (uap->rgid) - error1 = copyout((caddr_t)&cr->cr_rgid, - (caddr_t)uap->rgid, sizeof(cr->cr_rgid)); + error1 = copyout(&cr->cr_rgid, uap->rgid, + sizeof(cr->cr_rgid)); if (uap->egid) - error2 = copyout((caddr_t)&cr->cr_groups[0], - (caddr_t)uap->egid, sizeof(cr->cr_groups[0])); + error2 = copyout(&cr->cr_groups[0], uap->egid, + sizeof(cr->cr_groups[0])); if (uap->sgid) - error3 = copyout((caddr_t)&cr->cr_svgid, - (caddr_t)uap->sgid, sizeof(cr->cr_svgid)); - rel_mplock(); + error3 = copyout(&cr->cr_svgid, uap->sgid, + sizeof(cr->cr_svgid)); return error1 ? error1 : (error2 ? error2 : error3); } @@ -938,13 +935,9 @@ groupmember(gid_t gid, struct ucred *cred) int priv_check(struct thread *td, int priv) { - struct proc *p = td->td_proc; - - if (p != NULL) { - return priv_check_cred(p->p_ucred, priv, 0); - } else { - return (0); - } + if (td->td_lwp != NULL) + return priv_check_cred(td->td_ucred, priv, 0); + return (0); } /* @@ -1221,12 +1214,16 @@ sys_getlogin(struct getlogin_args *uap) int sys_setlogin(struct setlogin_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p; + struct ucred *cred; char buf[MAXLOGNAME]; int error; - KKASSERT(p != NULL); - if ((error = priv_check_cred(p->p_ucred, PRIV_PROC_SETLOGIN, 0))) + cred = td->td_ucred; + p = td->td_proc; + + if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0))) return (error); bzero(buf, sizeof(buf)); error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL); diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index f0f9b8ee3e..e6c8ce6de0 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -294,7 +294,7 @@ sys_lwp_rtprio(struct lwp_rtprio_args *uap) struct proc *p = curproc; struct lwp *lp; struct rtprio rtp; - struct ucred *cr = p->p_ucred; + struct ucred *cr = curthread->td_ucred; int error; error = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); @@ -403,7 +403,7 @@ sys_rtprio(struct rtprio_args *uap) struct proc *curp = curproc; struct proc *p; struct lwp *lp; - struct ucred *cr = curp->p_ucred; + struct ucred *cr = curthread->td_ucred; struct rtprio rtp; int error; diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 8a25550151..5b5f4524e4 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1180,7 +1180,7 @@ sysctl_root(SYSCTL_HANDLER_ARGS) /* Most likely only root can write */ if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p && - (error = priv_check_cred(p->p_ucred, + (error = priv_check_cred(td->td_ucred, (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL : PRIV_SYSCTL_WRITE, 0))) return (error); diff --git a/sys/kern/kern_varsym.c b/sys/kern/kern_varsym.c index b4d9515ae8..e90a533a22 100644 --- a/sys/kern/kern_varsym.c +++ b/sys/kern/kern_varsym.c @@ -136,10 +136,14 @@ sys_varsym_set(struct varsym_set_args *uap) { char name[MAXVARSYM_NAME]; char *buf; + struct thread *td; struct proc *p; + struct lwp *lp; int error; - p = curproc; + td = curthread; + lp = td->td_lwp; + p = lp ? lp->lwp_proc : NULL; if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0) goto done2; @@ -154,11 +158,11 @@ sys_varsym_set(struct varsym_set_args *uap) switch(uap->level) { case VARSYM_SYS: - if (p != NULL && p->p_ucred->cr_prison != NULL) + if (lp != NULL && td->td_ucred->cr_prison != NULL) uap->level = VARSYM_PRISON; case VARSYM_PRISON: - if (p != NULL && - (error = priv_check_cred(p->p_ucred, PRIV_VARSYM_SYS, 0)) != 0) + if (lp != NULL && + (error = priv_check_cred(td->td_ucred, PRIV_VARSYM_SYS, 0)) != 0) break; /* fall through */ case VARSYM_USER: @@ -228,7 +232,9 @@ sys_varsym_list(struct varsym_list_args *uap) { struct varsymset *vss; struct varsyment *ve; + struct thread *td; struct proc *p; + struct lwp *lp; int i; int error; int bytes; @@ -245,7 +251,10 @@ sys_varsym_list(struct varsym_list_args *uap) /* * Figure out the varsym set. */ - p = curproc; + td = curthread; + lp = td->td_lwp; + p = lp ? lp->lwp_proc : NULL; + vss = NULL; switch (uap->level) { @@ -254,15 +263,15 @@ sys_varsym_list(struct varsym_list_args *uap) vss = &p->p_varsymset; break; case VARSYM_USER: - if (p) - vss = &p->p_ucred->cr_uidinfo->ui_varsymset; + if (lp) + vss = &td->td_ucred->cr_uidinfo->ui_varsymset; break; case VARSYM_SYS: vss = &varsymset_sys; break; case VARSYM_PRISON: - if (p != NULL && p->p_ucred->cr_prison != NULL) - vss = &p->p_ucred->cr_prison->pr_varsymset; + if (lp && td->td_ucred->cr_prison) + vss = &td->td_ucred->cr_prison->pr_varsymset; break; } if (vss == NULL) { @@ -374,24 +383,30 @@ vsslock(struct varsymset **vss, struct varsymset *n) varsym_t varsymfind(int mask, const char *name, int namelen) { - struct proc *p = curproc; struct varsyment *ve = NULL; struct varsymset *vss = NULL; + struct thread *td; + struct lwp *lp; + struct proc *p; varsym_t sym; - if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && p != NULL) { + td = curthread; + lp = td->td_lwp; + p = lp ? lp->lwp_proc : NULL; + + if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && lp != NULL) { if (mask & VARSYM_PROC_MASK) { vsslock(&vss, &p->p_varsymset); ve = varsymlookup(vss, name, namelen); } if (ve == NULL && (mask & VARSYM_USER_MASK)) { - vsslock(&vss, &p->p_ucred->cr_uidinfo->ui_varsymset); + vsslock(&vss, &td->td_ucred->cr_uidinfo->ui_varsymset); ve = varsymlookup(vss, name, namelen); } } if (ve == NULL && (mask & VARSYM_SYS_MASK)) { - if (p != NULL && p->p_ucred->cr_prison) { - vsslock(&vss, &p->p_ucred->cr_prison->pr_varsymset); + if (lp != NULL && td->td_ucred->cr_prison) { + vsslock(&vss, &td->td_ucred->cr_prison->pr_varsymset); ve = varsymlookup(vss, name, namelen); } else { vsslock(&vss, &varsymset_sys); @@ -413,27 +428,33 @@ varsymmake(int level, const char *name, const char *data) { struct varsymset *vss = NULL; struct varsyment *ve; - struct proc *p = curproc; + struct thread *td; + struct proc *p; + struct lwp *lp; varsym_t sym; int namelen = strlen(name); int datalen; int error; + td = curthread; + lp = td->td_lwp; + p = lp ? lp->lwp_proc : NULL; + switch(level) { case VARSYM_PROC: if (p) vss = &p->p_varsymset; break; case VARSYM_USER: - if (p) - vss = &p->p_ucred->cr_uidinfo->ui_varsymset; + if (lp) + vss = &td->td_ucred->cr_uidinfo->ui_varsymset; break; case VARSYM_SYS: vss = &varsymset_sys; break; case VARSYM_PRISON: - if (p != NULL && p->p_ucred->cr_prison != NULL) - vss = &p->p_ucred->cr_prison->pr_varsymset; + if (lp && td->td_ucred->cr_prison) + vss = &td->td_ucred->cr_prison->pr_varsymset; break; } if (vss == NULL) { diff --git a/sys/kern/lwkt_caps.c b/sys/kern/lwkt_caps.c index 57e9c6ce56..78f2c7962f 100644 --- a/sys/kern/lwkt_caps.c +++ b/sys/kern/lwkt_caps.c @@ -236,12 +236,12 @@ caps_find_msg(caps_kinfo_t caps, off_t msgid) static caps_kinfo_t -caps_load_ccr(caps_kinfo_t caps, caps_kmsg_t msg, struct proc *p, - void *udata, int ubytes) +caps_load_ccr(caps_kinfo_t caps, caps_kmsg_t msg, struct lwp *lp, + void *udata, int ubytes) { - int i; - struct ucred *cr = p->p_ucred; + struct ucred *cr = lp ? lp->lwp_thread->td_ucred : proc0.p_ucred; caps_kinfo_t rcaps; + int i; /* * replace km_mcaps with new VM state, return the old km_mcaps. The @@ -253,7 +253,7 @@ caps_load_ccr(caps_kinfo_t caps, caps_kmsg_t msg, struct proc *p, msg->km_mcaps = caps; xio_init_ubuf(&msg->km_xio, udata, ubytes, XIOF_READ); - msg->km_ccr.pid = p ? p->p_pid : -1; + msg->km_ccr.pid = lp ? lp->lwp_proc->p_pid : -1; msg->km_ccr.uid = cr->cr_ruid; msg->km_ccr.euid = cr->cr_uid; msg->km_ccr.gid = cr->cr_rgid; @@ -361,6 +361,7 @@ caps_name_check(const char *name, size_t len) static void caps_term(caps_kinfo_t caps, int flags, caps_kinfo_t cflush) { + struct thread *td = curthread; struct caps_kinfo **scan; caps_kmsg_t msg; @@ -397,7 +398,7 @@ caps_term(caps_kinfo_t caps, int flags, caps_kinfo_t cflush) switch(msg->km_state) { case CAPMS_REQUEST: case CAPMS_REQUEST_RETRY: - rcaps = caps_load_ccr(caps, msg, curproc, NULL, 0); + rcaps = caps_load_ccr(caps, msg, td->td_lwp, NULL, 0); if (rcaps->ci_flags & CAPKF_CLOSED) { /* * can't reply, if we never read the message (its on @@ -428,7 +429,7 @@ caps_term(caps_kinfo_t caps, int flags, caps_kinfo_t cflush) break; case CAPMS_REPLY: case CAPMS_REPLY_RETRY: - rcaps = caps_load_ccr(caps, msg, curproc, NULL, 0); + rcaps = caps_load_ccr(caps, msg, td->td_lwp, NULL, 0); if (caps == rcaps || (rcaps->ci_flags & CAPKF_CLOSED)) { caps_free_msg(msg); /* degenerate disposal case */ caps_drop(rcaps); @@ -572,7 +573,7 @@ caps_exit(struct thread *td) int sys_caps_sys_service(struct caps_sys_service_args *uap) { - struct ucred *cred = curproc->p_ucred; + struct ucred *cred = curthread->td_ucred; char name[CAPS_MAXNAMELEN]; caps_kinfo_t caps; size_t len; @@ -610,7 +611,7 @@ sys_caps_sys_service(struct caps_sys_service_args *uap) int sys_caps_sys_client(struct caps_sys_client_args *uap) { - struct ucred *cred = curproc->p_ucred; + struct ucred *cred = curthread->td_ucred; char name[CAPS_MAXNAMELEN]; caps_kinfo_t caps; size_t len; @@ -640,12 +641,13 @@ sys_caps_sys_client(struct caps_sys_client_args *uap) int sys_caps_sys_close(struct caps_sys_close_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; int error; get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) != NULL) { + if ((caps = caps_find_id(td, uap->portid)) != NULL) { caps_term(caps, CAPKF_TDLIST|CAPKF_HLIST|CAPKF_FLUSH|CAPKF_RCAPS, NULL); caps_drop(caps); @@ -663,12 +665,13 @@ sys_caps_sys_close(struct caps_sys_close_args *uap) int sys_caps_sys_setgen(struct caps_sys_setgen_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; int error; get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) != NULL) { + if ((caps = caps_find_id(td, uap->portid)) != NULL) { if (caps->ci_type == CAPT_FORKED) { error = ENOTCONN; } else { @@ -689,12 +692,13 @@ sys_caps_sys_setgen(struct caps_sys_setgen_args *uap) int sys_caps_sys_getgen(struct caps_sys_getgen_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; int error; get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) != NULL) { + if ((caps = caps_find_id(td, uap->portid)) != NULL) { if (caps->ci_type == CAPT_FORKED) { error = ENOTCONN; } else if (caps->ci_rcaps == NULL) { @@ -722,16 +726,16 @@ sys_caps_sys_getgen(struct caps_sys_getgen_args *uap) int sys_caps_sys_put(struct caps_sys_put_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; caps_kmsg_t msg; - struct proc *p = curproc; int error; if (uap->msgsize < 0) return(EINVAL); get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) == NULL) { + if ((caps = caps_find_id(td, uap->portid)) == NULL) { error = EINVAL; goto done; } @@ -767,7 +771,7 @@ sys_caps_sys_put(struct caps_sys_put_args *uap) * new message, load_ccr returns NULL. hold rcaps for put_msg */ error = 0; - caps_load_ccr(caps, msg, p, uap->msg, uap->msgsize); + caps_load_ccr(caps, msg, td->td_lwp, uap->msg, uap->msgsize); caps_hold(caps->ci_rcaps); ++caps->ci_cmsgcount; caps_put_msg(caps->ci_rcaps, msg, CAPMS_REQUEST); /* drops rcaps */ @@ -790,17 +794,17 @@ done: int sys_caps_sys_reply(struct caps_sys_reply_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; caps_kinfo_t rcaps; caps_kmsg_t msg; - struct proc *p; int error; if (uap->msgsize < 0) return(EINVAL); get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) == NULL) { + if ((caps = caps_find_id(td, uap->portid)) == NULL) { error = EINVAL; goto done; } @@ -829,13 +833,12 @@ sys_caps_sys_reply(struct caps_sys_reply_args *uap) */ error = 0; caps_dequeue_msg(caps, msg); - p = curproc; if (msg->km_mcaps->ci_flags & CAPKF_CLOSED) { - caps_drop(caps_load_ccr(caps, msg, p, NULL, 0)); + caps_drop(caps_load_ccr(caps, msg, td->td_lwp, NULL, 0)); caps_hold(caps); /* ref for message */ caps_put_msg(caps, msg, CAPMS_DISPOSE); } else { - rcaps = caps_load_ccr(caps, msg, p, uap->msg, uap->msgsize); + rcaps = caps_load_ccr(caps, msg, td->td_lwp, uap->msg, uap->msgsize); caps_put_msg(rcaps, msg, CAPMS_REPLY); } } @@ -863,6 +866,7 @@ done: int sys_caps_sys_get(struct caps_sys_get_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; caps_kmsg_t msg; int error; @@ -871,7 +875,7 @@ sys_caps_sys_get(struct caps_sys_get_args *uap) return(EINVAL); get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) != NULL) { + if ((caps = caps_find_id(td, uap->portid)) != NULL) { if (caps->ci_type == CAPT_FORKED) { error = ENOTCONN; } else if ((msg = TAILQ_FIRST(&caps->ci_msgpendq)) == NULL) { @@ -906,6 +910,7 @@ sys_caps_sys_get(struct caps_sys_get_args *uap) int sys_caps_sys_wait(struct caps_sys_wait_args *uap) { + struct thread *td = curthread; caps_kinfo_t caps; caps_kmsg_t msg; int error; @@ -914,7 +919,7 @@ sys_caps_sys_wait(struct caps_sys_wait_args *uap) return(EINVAL); get_mplock(); - if ((caps = caps_find_id(curthread, uap->portid)) != NULL) { + if ((caps = caps_find_id(td, uap->portid)) != NULL) { if (caps->ci_type == CAPT_FORKED) { error = ENOTCONN; } else { @@ -940,6 +945,7 @@ static int caps_process_msg(caps_kinfo_t caps, caps_kmsg_t msg, struct caps_sys_get_args *uap) { + struct thread *td = curthread; int error = 0; int msgsize; caps_kinfo_t rcaps; @@ -984,7 +990,7 @@ caps_process_msg(caps_kinfo_t caps, caps_kmsg_t msg, case CAPMS_REPLY: case CAPMS_REPLY_RETRY: --caps->ci_cmsgcount; - rcaps = caps_load_ccr(caps, msg, curproc, NULL, 0); + rcaps = caps_load_ccr(caps, msg, td->td_lwp, NULL, 0); if (caps == rcaps || (rcaps->ci_flags & CAPKF_CLOSED)) { /* degenerate disposal case */ caps_free_msg(msg); @@ -1027,6 +1033,7 @@ caps_kinfo_t kern_caps_sys_service(const char *name, uid_t uid, gid_t gid, struct ucred *cred, int flags, int *error) { + struct thread *td = curthread; caps_kinfo_t caps; int len; @@ -1060,7 +1067,7 @@ kern_caps_sys_service(const char *name, uid_t uid, gid_t gid, /* * Create the service */ - caps = caps_alloc(curthread, name, len, + caps = caps_alloc(td, name, len, uid, gid, flags & CAPF_UFLAGS, CAPT_SERVICE); wakeup(&caps_waitsvc); return(caps); @@ -1071,6 +1078,7 @@ caps_kinfo_t kern_caps_sys_client(const char *name, uid_t uid, gid_t gid, struct ucred *cred, int flags, int *error) { + struct thread *td = curthread; caps_kinfo_t caps, rcaps; int len; @@ -1120,7 +1128,7 @@ again: /* * Allocate the client side and connect to the server */ - caps = caps_alloc(curthread, name, len, + caps = caps_alloc(td, name, len, uid, gid, flags & CAPF_UFLAGS, CAPT_CLIENT); caps->ci_rcaps = rcaps; caps->ci_flags |= CAPKF_RCAPS; diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index 8a978c2f77..2463f5f672 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -559,7 +559,7 @@ mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map, } ubuf; KKASSERT(p); - cred = p->p_ucred; + cred = td->td_ucred; fp = holdfp(p->p_fd, fd, FREAD|FWRITE); if (fp == NULL) diff --git a/sys/kern/sys_mqueue.c b/sys/kern/sys_mqueue.c index 607ca45bc0..ae72251ac4 100644 --- a/sys/kern/sys_mqueue.c +++ b/sys/kern/sys_mqueue.c @@ -389,7 +389,8 @@ sys_mq_open(struct mq_open_args *uap) syscallarg(mode_t) mode; syscallarg(struct mq_attr) attr; } */ - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; struct mqueue *mq, *mq_new = NULL; file_t *fp; char *name; @@ -467,8 +468,8 @@ sys_mq_open(struct mq_open_args *uap) /* Store mode and effective UID with GID */ mq_new->mq_mode = ((SCARG(uap, mode) & ~p->p_fd->fd_cmask) & ALLPERMS) & ~S_ISTXT; - mq_new->mq_euid = curproc->p_ucred->cr_uid; - mq_new->mq_egid = curproc->p_ucred->cr_svgid; + mq_new->mq_euid = td->td_ucred->cr_uid; + mq_new->mq_egid = td->td_ucred->cr_svgid; } /* Allocate file structure and descriptor */ @@ -514,7 +515,7 @@ sys_mq_open(struct mq_open_args *uap) acc_mode |= VWRITE; } if (vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid, - acc_mode, curproc->p_ucred)) { + acc_mode, td->td_ucred)) { error = EACCES; goto exit; @@ -1028,6 +1029,7 @@ sys_mq_unlink(struct mq_unlink_args *uap) /* { syscallarg(const char *) name; } */ + struct thread *td = curthread; struct mqueue *mq; char *name; int error, refcnt = 0; @@ -1049,8 +1051,8 @@ sys_mq_unlink(struct mq_unlink_args *uap) } /* Check the permissions */ - if (curproc->p_ucred->cr_uid != mq->mq_euid && - priv_check(curthread, PRIV_ROOT) != 0) { + if (td->td_ucred->cr_uid != mq->mq_euid && + priv_check(td, PRIV_ROOT) != 0) { lockmgr(&mq->mq_mtx, LK_RELEASE); error = EACCES; goto error; diff --git a/sys/kern/sysv_msg.c b/sys/kern/sysv_msg.c index 4df2752656..0aff652dd1 100644 --- a/sys/kern/sysv_msg.c +++ b/sys/kern/sysv_msg.c @@ -198,11 +198,11 @@ SYSINIT(sysv_msg, SI_SUB_SYSV_MSG, SI_ORDER_FIRST, msginit, NULL) int sys_msgsys(struct msgsys_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; unsigned int which = (unsigned int)uap->which; int error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); if (which >= sizeof(msgcalls)/sizeof(msgcalls[0])) @@ -257,7 +257,7 @@ sys_msgctl(struct msgctl_args *uap) kprintf("call to msgctl(%d, %d, 0x%x)\n", msqid, cmd, user_msqptr); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -385,17 +385,17 @@ done: int sys_msgget(struct msgget_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int msqid, eval; int key = uap->key; int msgflg = uap->msgflg; - struct ucred *cred = p->p_ucred; + struct ucred *cred = td->td_ucred; struct msqid_ds *msqptr = NULL; #ifdef MSG_DEBUG_OK kprintf("msgget(0x%x, 0%o)\n", key, msgflg); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && cred->cr_prison != NULL) return (ENOSYS); eval = 0; @@ -419,7 +419,7 @@ sys_msgget(struct msgget_args *uap) eval = EEXIST; goto done; } - if ((eval = ipcperm(p, &msqptr->msg_perm, msgflg & 0700 ))) { + if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, msgflg & 0700 ))) { #ifdef MSG_DEBUG_OK kprintf("requester doesn't have 0%o access\n", msgflg & 0700); @@ -495,7 +495,7 @@ done: int sys_msgsnd(struct msgsnd_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int msqid = uap->msqid; void *user_msgp = uap->msgp; size_t msgsz = uap->msgsz; @@ -510,7 +510,7 @@ sys_msgsnd(struct msgsnd_args *uap) msgflg); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -541,7 +541,7 @@ sys_msgsnd(struct msgsnd_args *uap) goto done; } - if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_W))) { + if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, IPC_W))) { #ifdef MSG_DEBUG_OK kprintf("requester doesn't have write access\n"); #endif @@ -808,7 +808,7 @@ sys_msgsnd(struct msgsnd_args *uap) msqptr->msg_cbytes += msghdr->msg_ts; msqptr->msg_qnum++; - msqptr->msg_lspid = p->p_pid; + msqptr->msg_lspid = td->td_proc->p_pid; msqptr->msg_stime = time_second; wakeup((caddr_t)msqptr); @@ -826,7 +826,7 @@ done: int sys_msgrcv(struct msgrcv_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int msqid = uap->msqid; void *user_msgp = uap->msgp; size_t msgsz = uap->msgsz; @@ -843,7 +843,7 @@ sys_msgrcv(struct msgrcv_args *uap) msgsz, msgtyp, msgflg); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -874,7 +874,7 @@ sys_msgrcv(struct msgrcv_args *uap) goto done; } - if ((eval = ipcperm(p, &msqptr->msg_perm, IPC_R))) { + if ((eval = ipcperm(td->td_proc, &msqptr->msg_perm, IPC_R))) { #ifdef MSG_DEBUG_OK kprintf("requester doesn't have read access\n"); #endif @@ -1030,7 +1030,7 @@ sys_msgrcv(struct msgrcv_args *uap) msqptr->msg_cbytes -= msghdr->msg_ts; msqptr->msg_qnum--; - msqptr->msg_lrpid = p->p_pid; + msqptr->msg_lrpid = td->td_proc->p_pid; msqptr->msg_rtime = time_second; /* diff --git a/sys/kern/sysv_sem.c b/sys/kern/sysv_sem.c index 3f75691262..fd800eeacc 100644 --- a/sys/kern/sysv_sem.c +++ b/sys/kern/sysv_sem.c @@ -191,11 +191,11 @@ SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL) int sys_semsys(struct semsys_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; unsigned int which = (unsigned int)uap->which; int error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); if (which >= sizeof(semcalls)/sizeof(semcalls[0])) @@ -381,13 +381,13 @@ semundo_clear(int semid, int semnum) int sys___semctl(struct __semctl_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int semid = uap->semid; int semnum = uap->semnum; int cmd = uap->cmd; union semun *arg = uap->arg; union semun real_arg; - struct ucred *cred = p->p_ucred; + struct ucred *cred = td->td_ucred; int i, rval, eval; struct semid_ds sbuf; struct semid_ds *semaptr; @@ -396,7 +396,7 @@ sys___semctl(struct __semctl_args *uap) kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && cred->cr_prison != NULL) return (ENOSYS); semid = IPCID_TO_IX(semid); @@ -416,7 +416,7 @@ sys___semctl(struct __semctl_args *uap) switch (cmd) { case IPC_RMID: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)) != 0) + if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M)) != 0) break; semaptr->sem_perm.cuid = cred->cr_uid; semaptr->sem_perm.uid = cred->cr_uid; @@ -434,7 +434,8 @@ sys___semctl(struct __semctl_args *uap) break; case IPC_SET: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_M); + if (eval) break; if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) break; @@ -450,7 +451,7 @@ sys___semctl(struct __semctl_args *uap) break; case IPC_STAT: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R))) + if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R))) break; if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) break; @@ -458,7 +459,8 @@ sys___semctl(struct __semctl_args *uap) break; case GETNCNT: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R); + if (eval) break; if (semnum < 0 || semnum >= semaptr->sem_nsems) { eval = EINVAL; @@ -468,7 +470,8 @@ sys___semctl(struct __semctl_args *uap) break; case GETPID: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R); + if (eval) break; if (semnum < 0 || semnum >= semaptr->sem_nsems) { eval = EINVAL; @@ -478,7 +481,8 @@ sys___semctl(struct __semctl_args *uap) break; case GETVAL: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R); + if (eval) break; if (semnum < 0 || semnum >= semaptr->sem_nsems) { eval = EINVAL; @@ -488,7 +492,8 @@ sys___semctl(struct __semctl_args *uap) break; case GETALL: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R); + if (eval) break; if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) break; @@ -502,7 +507,8 @@ sys___semctl(struct __semctl_args *uap) break; case GETZCNT: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_R); + if (eval) break; if (semnum < 0 || semnum >= semaptr->sem_nsems) { eval = EINVAL; @@ -512,7 +518,8 @@ sys___semctl(struct __semctl_args *uap) break; case SETVAL: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W); + if (eval) break; if (semnum < 0 || semnum >= semaptr->sem_nsems) { eval = EINVAL; @@ -526,7 +533,8 @@ sys___semctl(struct __semctl_args *uap) break; case SETALL: - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)) != 0) + eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W); + if (eval) break; if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0) break; @@ -558,18 +566,18 @@ sys___semctl(struct __semctl_args *uap) int sys_semget(struct semget_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int semid, eval; int key = uap->key; int nsems = uap->nsems; int semflg = uap->semflg; - struct ucred *cred = p->p_ucred; + struct ucred *cred = td->td_ucred; #ifdef SEM_DEBUG kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && cred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -585,8 +593,9 @@ sys_semget(struct semget_args *uap) #ifdef SEM_DEBUG kprintf("found public key\n"); #endif - if ((eval = ipcperm(p, &sema[semid].sem_perm, - semflg & 0700))) { + if ((eval = ipcperm(td->td_proc, + &sema[semid].sem_perm, + semflg & 0700))) { goto done; } if (nsems > 0 && sema[semid].sem_nsems < nsems) { @@ -682,7 +691,7 @@ done: int sys_semop(struct semop_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; int semid = uap->semid; u_int nsops = uap->nsops; struct sembuf sops[MAX_SOPS]; @@ -697,7 +706,7 @@ sys_semop(struct semop_args *uap) kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops); #endif - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -718,7 +727,7 @@ sys_semop(struct semop_args *uap) goto done; } - if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W))) { + if ((eval = ipcperm(td->td_proc, &semaptr->sem_perm, IPC_W))) { #ifdef SEM_DEBUG kprintf("eval = %d from ipaccess\n", eval); #endif @@ -893,8 +902,8 @@ donex: adjval = sops[i].sem_op; if (adjval == 0) continue; - eval = semundo_adjust(p, &suptr, semid, - sops[i].sem_num, -adjval); + eval = semundo_adjust(td->td_proc, &suptr, semid, + sops[i].sem_num, -adjval); if (eval == 0) continue; @@ -913,8 +922,8 @@ donex: adjval = sops[j].sem_op; if (adjval == 0) continue; - if (semundo_adjust(p, &suptr, semid, - sops[j].sem_num, adjval) != 0) + if (semundo_adjust(td->td_proc, &suptr, semid, + sops[j].sem_num, adjval) != 0) panic("semop - can't undo undos"); } @@ -933,7 +942,7 @@ donex: for (i = 0; i < nsops; i++) { sopptr = &sops[i]; semptr = &semaptr->sem_base[sopptr->sem_num]; - semptr->sempid = p->p_pid; + semptr->sempid = td->td_proc->p_pid; } /* Do a wakeup if any semaphore was up'd. */ diff --git a/sys/kern/sysv_shm.c b/sys/kern/sysv_shm.c index abca0b076a..4a6eb45663 100644 --- a/sys/kern/sysv_shm.c +++ b/sys/kern/sysv_shm.c @@ -216,12 +216,13 @@ shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s) int sys_shmdt(struct shmdt_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; struct shmmap_state *shmmap_s; int i; int error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -250,7 +251,8 @@ done: int sys_shmat(struct shmat_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; int error, i, flags; struct shmid_ds *shmseg; struct shmmap_state *shmmap_s = NULL; @@ -260,7 +262,7 @@ sys_shmat(struct shmat_args *uap) vm_size_t size; int rv; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -377,11 +379,12 @@ static int sys_oshmctl(struct proc *p, struct oshmctl_args *uap) { #ifdef COMPAT_43 - int error; + struct thread *td = curthread; struct shmid_ds *shmseg; struct oshmid_ds outbuf; + int error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -425,12 +428,13 @@ done: int sys_shmctl(struct shmctl_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; int error; struct shmid_ds inbuf; struct shmid_ds *shmseg; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); get_mplock(); @@ -595,10 +599,11 @@ shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode) int sys_shmget(struct shmget_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; int segnum, mode, error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); mode = uap->shmflg & ACCESSPERMS; @@ -632,11 +637,11 @@ done: int sys_shmsys(struct shmsys_args *uap) { - struct proc *p = curproc; + struct thread *td = curthread; unsigned int which = (unsigned int)uap->which; int error; - if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL) + if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) return (ENOSYS); if (which >= sizeof(shmcalls)/sizeof(shmcalls[0])) diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c index f1d695c0da..af2f7b0bd7 100644 --- a/sys/kern/uipc_syscalls.c +++ b/sys/kern/uipc_syscalls.c @@ -330,9 +330,9 @@ kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res) nfp->f_data = so; /* Sync socket nonblocking/async state with file flags */ tmp = fflag & FNONBLOCK; - fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, p->p_ucred, NULL); + fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td->td_ucred, NULL); tmp = fflag & FASYNC; - fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, p->p_ucred, NULL); + fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td->td_ucred, NULL); sa = NULL; error = soaccept(so, &sa); @@ -1700,7 +1700,7 @@ retry_lookup: vn_lock(vp, LK_SHARED | LK_RETRY); error = VOP_READ(vp, &auio, IO_VMIO | ((MAXBSIZE / bsize) << 16), - p->p_ucred); + td->td_ucred); vn_unlock(vp); vm_page_flag_clear(pg, PG_ZERO); vm_page_io_finish(pg); diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 2ec09d166c..b4ea94e81b 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -115,7 +115,6 @@ int sys_mount(struct mount_args *uap) { struct thread *td = curthread; - struct proc *p = td->td_proc; struct vnode *vp; struct nchandle nch; struct mount *mp, *nullmp; @@ -127,9 +126,8 @@ sys_mount(struct mount_args *uap) char fstypename[MFSNAMELEN]; struct ucred *cred; - KKASSERT(p); get_mplock(); - cred = p->p_ucred; + cred = td->td_ucred; if (jailed(cred)) { error = EPERM; goto done; @@ -579,7 +577,7 @@ sys_unmount(struct unmount_args *uap) KKASSERT(p); get_mplock(); - if (p->p_ucred->cr_prison != NULL) { + if (td->td_ucred->cr_prison != NULL) { error = EPERM; goto done; } @@ -598,7 +596,7 @@ sys_unmount(struct unmount_args *uap) * Only root, or the user that did the original mount is * permitted to unmount this filesystem. */ - if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) && + if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) && (error = priv_check(td, PRIV_ROOT))) goto out; @@ -911,7 +909,7 @@ sys_quotactl(struct quotactl_args *uap) get_mplock(); td = curthread; p = td->td_proc; - if (p->p_ucred->cr_prison && !prison_quotas) { + if (td->td_ucred->cr_prison && !prison_quotas) { error = EPERM; goto done; } @@ -957,7 +955,7 @@ sys_mountctl(struct mountctl_args *uap) * Sanity and permissions checks. We must be root. */ KKASSERT(p); - if (p->p_ucred->cr_prison != NULL) + if (td->td_ucred->cr_prison != NULL) return (EPERM); if ((uap->op != MOUNTCTL_MOUNTFLAGS) && (error = priv_check(td, PRIV_ROOT)) != 0) @@ -1314,7 +1312,7 @@ struct getfsstat_info { long maxcount; int error; int flags; - struct proc *p; + struct thread *td; }; static int getfsstat_callback(struct mount *, void *); @@ -1326,7 +1324,6 @@ int sys_getfsstat(struct getfsstat_args *uap) { struct thread *td = curthread; - struct proc *p = td->td_proc; struct getfsstat_info info; bzero(&info, sizeof(info)); @@ -1335,7 +1332,7 @@ sys_getfsstat(struct getfsstat_args *uap) info.sfsp = uap->buf; info.count = 0; info.flags = uap->flags; - info.p = p; + info.td = td; get_mplock(); mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD); @@ -1357,8 +1354,10 @@ getfsstat_callback(struct mount *mp, void *data) int error; if (info->sfsp && info->count < info->maxcount) { - if (info->p && !chroot_visible_mnt(mp, info->p)) + if (info->td->td_proc && + !chroot_visible_mnt(mp, info->td->td_proc)) { return(0); + } sp = &mp->mnt_stat; /* @@ -1368,12 +1367,12 @@ getfsstat_callback(struct mount *mp, void *data) */ if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && - (error = VFS_STATFS(mp, sp, info->p->p_ucred))) { + (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { return(0); } sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; - error = mount_path(info->p, mp, &fullpath, &freepath); + error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); if (error) { info->error = error; return(-1); @@ -1407,7 +1406,7 @@ struct getvfsstat_info { long maxcount; int error; int flags; - struct proc *p; + struct thread *td; }; static int getvfsstat_callback(struct mount *, void *); @@ -1419,7 +1418,6 @@ int sys_getvfsstat(struct getvfsstat_args *uap) { struct thread *td = curthread; - struct proc *p = td->td_proc; struct getvfsstat_info info; bzero(&info, sizeof(info)); @@ -1429,7 +1427,7 @@ sys_getvfsstat(struct getvfsstat_args *uap) info.vsfsp = uap->vbuf; info.count = 0; info.flags = uap->flags; - info.p = p; + info.td = td; get_mplock(); mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD); @@ -1452,8 +1450,10 @@ getvfsstat_callback(struct mount *mp, void *data) int error; if (info->vsfsp && info->count < info->maxcount) { - if (info->p && !chroot_visible_mnt(mp, info->p)) + if (info->td->td_proc && + !chroot_visible_mnt(mp, info->td->td_proc)) { return(0); + } sp = &mp->mnt_stat; vsp = &mp->mnt_vstat; @@ -1464,14 +1464,14 @@ getvfsstat_callback(struct mount *mp, void *data) */ if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && - (error = VFS_STATFS(mp, sp, info->p->p_ucred))) { + (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { return(0); } sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && - (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) { + (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) { return(0); } vsp->f_flag = 0; @@ -1480,7 +1480,7 @@ getvfsstat_callback(struct mount *mp, void *data) if (mp->mnt_flag & MNT_NOSUID) vsp->f_flag |= ST_NOSUID; - error = mount_path(info->p, mp, &fullpath, &freepath); + error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); if (error) { info->error = error; return(-1); @@ -1688,7 +1688,7 @@ kern_chroot(struct nchandle *nch) /* * Only privileged user can chroot */ - error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0); + error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0); if (error) return (error); @@ -1769,7 +1769,7 @@ checkvp_chdir(struct vnode *vp, struct thread *td) if (vp->v_type != VDIR) error = ENOTDIR; else - error = VOP_EACCESS(vp, VEXEC, td->td_proc->p_ucred); + error = VOP_EACCESS(vp, VEXEC, td->td_ucred); return (error); } @@ -1973,7 +1973,7 @@ kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor) switch (mode & S_IFMT) { case S_IFMT: /* used by badsect to flag bad sectors */ - error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0); + error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0); vattr.va_type = VBAD; break; case S_IFCHR: @@ -1985,11 +1985,11 @@ kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor) vattr.va_type = VBLK; break; case S_IFWHT: - error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0); + error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0); whiteout = 1; break; case S_IFDIR: /* special directories support for HAMMER */ - error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0); + error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0); vattr.va_type = VDIR; break; default: @@ -2203,7 +2203,7 @@ kern_link(struct nlookupdata *nd, struct nlookupdata *linknd) /* * Finally run the new API VOP. */ - error = can_hardlink(vp, td, td->td_proc->p_ucred); + error = can_hardlink(vp, td, td->td_ucred); if (error == 0) { error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp, vp, linknd->nl_cred); @@ -2741,7 +2741,6 @@ int kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) { struct thread *td = curthread; - struct proc *p = td->td_proc; struct vnode *vp; struct iovec aiov; struct uio auio; @@ -2764,7 +2763,7 @@ kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) auio.uio_segflg = UIO_USERSPACE; auio.uio_td = td; auio.uio_resid = count; - error = VOP_READLINK(vp, &auio, p->p_ucred); + error = VOP_READLINK(vp, &auio, td->td_ucred); } vput(vp); *res = count - auio.uio_resid; @@ -2799,7 +2798,6 @@ static int setfflags(struct vnode *vp, int flags) { struct thread *td = curthread; - struct proc *p = td->td_proc; int error; struct vattr vattr; @@ -2810,7 +2808,7 @@ setfflags(struct vnode *vp, int flags) * chown can't fail when done as root. */ if ((vp->v_type == VCHR || vp->v_type == VBLK) && - ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0)) + ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0)) return (error); /* @@ -2820,7 +2818,7 @@ setfflags(struct vnode *vp, int flags) if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { VATTR_NULL(&vattr); vattr.va_flags = flags; - error = VOP_SETATTR(vp, &vattr, p->p_ucred); + error = VOP_SETATTR(vp, &vattr, td->td_ucred); vput(vp); } return (error); @@ -2921,7 +2919,6 @@ static int setfmode(struct vnode *vp, int mode) { struct thread *td = curthread; - struct proc *p = td->td_proc; int error; struct vattr vattr; @@ -2932,7 +2929,7 @@ setfmode(struct vnode *vp, int mode) if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { VATTR_NULL(&vattr); vattr.va_mode = mode & ALLPERMS; - error = VOP_SETATTR(vp, &vattr, p->p_ucred); + error = VOP_SETATTR(vp, &vattr, td->td_ucred); vput(vp); } return error; @@ -3058,7 +3055,6 @@ static int setfown(struct vnode *vp, uid_t uid, gid_t gid) { struct thread *td = curthread; - struct proc *p = td->td_proc; int error; struct vattr vattr; @@ -3070,7 +3066,7 @@ setfown(struct vnode *vp, uid_t uid, gid_t gid) VATTR_NULL(&vattr); vattr.va_uid = uid; vattr.va_gid = gid; - error = VOP_SETATTR(vp, &vattr, p->p_ucred); + error = VOP_SETATTR(vp, &vattr, td->td_ucred); vput(vp); } return error; @@ -3214,7 +3210,6 @@ setutimes(struct vnode *vp, struct vattr *vattr, const struct timespec *ts, int nullflag) { struct thread *td = curthread; - struct proc *p = td->td_proc; int error; VATTR_NULL(vattr); @@ -3222,7 +3217,7 @@ setutimes(struct vnode *vp, struct vattr *vattr, vattr->va_mtime = ts[1]; if (nullflag) vattr->va_vaflags |= VA_UTIMES_NULL; - error = VOP_SETATTR(vp, vattr, p->p_ucred); + error = VOP_SETATTR(vp, vattr, td->td_ucred); return error; } @@ -3763,7 +3758,7 @@ kern_mkdir(struct nlookupdata *nd, int mode) vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask; vp = NULL; - error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr); + error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr); if (error == 0) vput(vp); return (error); @@ -4159,7 +4154,7 @@ sys_fhopen(struct fhopen_args *uap) if (fmode & FREAD) mode |= VREAD; if (mode) { - error = VOP_ACCESS(vp, mode, p->p_ucred); + error = VOP_ACCESS(vp, mode, td->td_ucred); if (error) goto bad; } @@ -4168,7 +4163,7 @@ sys_fhopen(struct fhopen_args *uap) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ VATTR_NULL(vap); vap->va_size = 0; - error = VOP_SETATTR(vp, vap, p->p_ucred); + error = VOP_SETATTR(vp, vap, td->td_ucred); if (error) goto bad; } @@ -4184,7 +4179,7 @@ sys_fhopen(struct fhopen_args *uap) goto bad; fp = nfp; - error = VOP_OPEN(vp, fmode, p->p_ucred, fp); + error = VOP_OPEN(vp, fmode, td->td_ucred, fp); if (error) { /* * setting f_ops this way prevents VOP_CLOSE from being @@ -4287,7 +4282,7 @@ sys_fhstat(struct fhstat_args *uap) error = ESTALE; if (error == 0) { if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) { - error = vn_stat(vp, &sb, td->td_proc->p_ucred); + error = vn_stat(vp, &sb, td->td_ucred); vput(vp); } } @@ -4340,7 +4335,7 @@ sys_fhstatfs(struct fhstatfs_args *uap) mp = vp->v_mount; sp = &mp->mnt_stat; vput(vp); - if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0) + if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0) goto done; error = mount_path(p, mp, &fullpath, &freepath); @@ -4403,7 +4398,7 @@ sys_fhstatvfs(struct fhstatvfs_args *uap) mp = vp->v_mount; sp = &mp->mnt_vstat; vput(vp); - if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0) + if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0) goto done; sp->f_flag = 0; diff --git a/sys/netproto/ncp/ncp_mod.c b/sys/netproto/ncp/ncp_mod.c index 0a68ad3585..f36a45bdcb 100644 --- a/sys/netproto/ncp/ncp_mod.c +++ b/sys/netproto/ncp/ncp_mod.c @@ -85,7 +85,7 @@ sys_sncp_connect(struct sncp_connect_args *uap) struct ucred *cred; KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; checkbad(copyin(uap->li,&li,sizeof(li))); checkbad(copyout(&connHandle,uap->connHandle,sizeof(connHandle))); /* check before */ @@ -134,7 +134,7 @@ sys_sncp_request(struct sncp_request_args *uap) get_mplock(); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; error = ncp_conn_findhandle(uap->connHandle,td,&handle); if (error) @@ -179,7 +179,7 @@ ncp_conn_handler(struct thread *td, struct sncp_request_args *uap, char *pdata; KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; error = copyin(&uap->ncpbuf->rqsize, &rqsize, sizeof(int)); if (error) return(error); @@ -350,7 +350,7 @@ sys_sncp_conn_scan(struct thread *td, struct sncp_conn_scan_args *uap) struct ucred *cred; KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; if (uap->li) { if (copyin(uap->li,&li,sizeof(li))) return EFAULT; @@ -405,7 +405,7 @@ ncp_conn_frag_rq(struct ncp_conn *conn, struct thread *td, struct ncp_conn_frag DECLARE_RQ; KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; ncp_rq_head(rqp,NCP_REQUEST,nfp->fn,td,cred); if (nfp->rqfcnt) { diff --git a/sys/platform/pc32/i386/sys_machdep.c b/sys/platform/pc32/i386/sys_machdep.c index ae463d8fc0..9c7459fbb9 100644 --- a/sys/platform/pc32/i386/sys_machdep.c +++ b/sys/platform/pc32/i386/sys_machdep.c @@ -178,7 +178,7 @@ ki386_set_ioperm(struct lwp *lp, char *args) if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0) return (error); - if ((error = priv_check_cred(lp->lwp_proc->p_ucred, PRIV_ROOT, 0)) != 0) + if ((error = priv_check_cred(lp->lwp_thread->td_ucred, PRIV_ROOT, 0)) != 0) return (error); if (securelevel > 0) return (EPERM); diff --git a/sys/sys/thread.h b/sys/sys/thread.h index 5c29a893b0..4aacce7973 100644 --- a/sys/sys/thread.h +++ b/sys/sys/thread.h @@ -211,6 +211,11 @@ typedef struct lwkt_cpu_msg { * * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section, * but this does not effect how the thread is scheduled by LWKT. + * + * NOTE: td_ucred is synchronized from the p_ucred on user->kernel syscall, + * trap, and AST/signal transitions to provide a stable ucred for + * (primarily) system calls. This field will be NULL for pure kernel + * threads. */ struct md_intr_info; struct caps_kinfo; diff --git a/sys/vfs/nfs/nfs_syscalls.c b/sys/vfs/nfs/nfs_syscalls.c index 5ef86727bb..2058282009 100644 --- a/sys/vfs/nfs/nfs_syscalls.c +++ b/sys/vfs/nfs/nfs_syscalls.c @@ -187,7 +187,7 @@ sys_nfssvc(struct nfssvc_args *uap) goto done; } nmp->nm_state |= NFSSTA_MNTD; - error = nfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag, + error = nfs_clientd(nmp, td->td_ucred, &ncd, uap->flag, uap->argp, td); } else if (uap->flag & NFSSVC_ADDSOCK) { error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg)); diff --git a/sys/vm/vm_mmap.c b/sys/vm/vm_mmap.c index 3cf36ae7a0..d266f27ff3 100644 --- a/sys/vm/vm_mmap.c +++ b/sys/vm/vm_mmap.c @@ -976,7 +976,8 @@ sys_mlock(struct mlock_args *uap) vm_offset_t addr; vm_offset_t tmpaddr; vm_size_t size, pageoff; - struct proc *p = curproc; + struct thread *td = curthread; + struct proc *p = td->td_proc; int error; addr = (vm_offset_t) uap->addr; @@ -1003,7 +1004,7 @@ sys_mlock(struct mlock_args *uap) return (ENOMEM); } #else - error = priv_check_cred(p->p_ucred, PRIV_ROOT, 0); + error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0); if (error) { rel_mplock(); return (error); diff --git a/sys/vm/vm_swap.c b/sys/vm/vm_swap.c index 4acef177d7..765457fe0c 100644 --- a/sys/vm/vm_swap.c +++ b/sys/vm/vm_swap.c @@ -182,8 +182,7 @@ sys_swapon(struct swapon_args *uap) int error; struct ucred *cred; - KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; error = priv_check(td, PRIV_ROOT); if (error) @@ -244,8 +243,7 @@ swaponvp(struct thread *td, struct vnode *vp, u_quad_t nblks) int error; long blk; - KKASSERT(td->td_proc); - cred = td->td_proc->p_ucred; + cred = td->td_ucred; if (!swapdev_vp) { error = getspecialvnode(VT_NON, NULL, &swapdev_vnode_vops_p, -- 2.41.0