2001-04-17 20:45:23 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Berkeley Software Design Inc's name may not be used to endorse or
|
|
|
|
* promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
|
|
|
|
*/
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/fcntl.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/kernel.h> /* for hz */
|
|
|
|
#include <sys/lock.h>
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <sys/malloc.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/proc.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/socket.h>
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
2001-08-23 13:27:58 +00:00
|
|
|
#include <machine/limits.h>
|
|
|
|
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <nfs/rpcv2.h>
|
|
|
|
#include <nfs/nfsproto.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <nfsclient/nfs.h>
|
|
|
|
#include <nfsclient/nfsmount.h>
|
|
|
|
#include <nfsclient/nfsnode.h>
|
|
|
|
#include <nfsclient/nfs_lock.h>
|
|
|
|
#include <nfsclient/nlminfo.h>
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
#define NFSOWNER_1ST_LEVEL_START 1 /* initial entries */
|
|
|
|
#define NFSOWNER_2ND_LEVEL 256 /* some power of 2 */
|
|
|
|
|
|
|
|
#define NFSOWNER(tbl, i) \
|
|
|
|
(tbl)[(i) / NFSOWNER_2ND_LEVEL][(i) % NFSOWNER_2ND_LEVEL]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* We have to let the process know if the call succeeded. I'm using an extra
|
2001-09-18 23:32:09 +00:00
|
|
|
* field in the p_nlminfo field in the proc structure, as it is already for
|
2001-04-17 20:45:23 +00:00
|
|
|
* lockd stuff.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nfs_advlock --
|
|
|
|
* NFS advisory byte-level locks.
|
|
|
|
*/
|
|
|
|
int
|
2001-09-18 23:32:09 +00:00
|
|
|
nfs_dolock(struct vop_advlock_args *ap)
|
2001-04-17 20:45:23 +00:00
|
|
|
{
|
|
|
|
LOCKD_MSG msg;
|
|
|
|
struct nameidata nd;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
2001-04-17 20:45:23 +00:00
|
|
|
struct vnode *vp, *wvp;
|
|
|
|
int error, error1;
|
|
|
|
struct flock *fl;
|
|
|
|
int fmode, ioflg;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
vp = ap->a_vp;
|
|
|
|
fl = ap->a_fl;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the NLM protocol doesn't allow the server to return an error
|
2001-08-23 08:20:21 +00:00
|
|
|
* on ranges, so we do it.
|
2001-04-17 20:45:23 +00:00
|
|
|
*/
|
2001-08-23 14:21:26 +00:00
|
|
|
if (fl->l_whence != SEEK_END) {
|
2001-08-23 18:02:29 +00:00
|
|
|
if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
|
2001-08-23 16:13:59 +00:00
|
|
|
fl->l_start < 0 ||
|
|
|
|
(fl->l_len < 0 &&
|
|
|
|
(fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
|
2001-08-23 14:21:26 +00:00
|
|
|
return (EINVAL);
|
2001-08-23 16:13:59 +00:00
|
|
|
if (fl->l_len > 0 &&
|
|
|
|
(fl->l_len - 1 > OFF_MAX - fl->l_start))
|
2001-08-23 14:21:26 +00:00
|
|
|
return (EOVERFLOW);
|
|
|
|
}
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the information structure.
|
|
|
|
*/
|
|
|
|
msg.lm_version = LOCKD_MSG_VERSION;
|
|
|
|
msg.lm_msg_ident.pid = p->p_pid;
|
|
|
|
/*
|
|
|
|
* if there is no nfsowner table yet, allocate one.
|
|
|
|
*/
|
|
|
|
if (p->p_nlminfo == NULL) {
|
|
|
|
MALLOC(p->p_nlminfo, struct nlminfo *,
|
|
|
|
sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO);
|
|
|
|
p->p_nlminfo->pid_start = p->p_stats->p_start;
|
|
|
|
}
|
|
|
|
msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
|
|
|
|
msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
|
|
|
|
|
|
|
|
msg.lm_fl = *fl;
|
|
|
|
msg.lm_wait = ap->a_flags & F_WAIT;
|
|
|
|
msg.lm_getlk = ap->a_op == F_GETLK;
|
|
|
|
/*
|
2001-10-24 02:48:38 +00:00
|
|
|
* XXX: the lm_cred assignment below directly exports a ucred
|
|
|
|
* structure to userland. This is probably wrong, and should at
|
|
|
|
* least be xucred.
|
2001-04-17 20:45:23 +00:00
|
|
|
*/
|
2002-07-15 19:40:23 +00:00
|
|
|
bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
|
|
|
|
min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
|
2001-04-17 20:45:23 +00:00
|
|
|
msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
|
|
|
|
bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
|
|
|
|
msg.lm_nfsv3 = NFS_ISV3(vp);
|
2002-02-27 18:32:23 +00:00
|
|
|
msg.lm_cred = *(td->td_ucred);
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the lock fifo. If for any reason we don't find the fifo, it
|
|
|
|
* means that the lock daemon isn't running. Translate any missing
|
|
|
|
* file error message for the user, otherwise the application will
|
|
|
|
* complain that the user's file is missing, which isn't the case.
|
|
|
|
* Note that we use proc0's cred, so the fifo is opened as root.
|
2001-10-23 19:11:31 +00:00
|
|
|
*
|
|
|
|
* XXX: Note that this behavior is relative to the root directory
|
|
|
|
* of the current process, and this may result in a variety of
|
|
|
|
* {functional, security} problems in chroot() environments.
|
2001-04-17 20:45:23 +00:00
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td);
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
fmode = FFLAGS(O_WRONLY);
|
2002-02-27 19:18:10 +00:00
|
|
|
error = vn_open_cred(&nd, &fmode, 0, thread0.td_ucred);
|
2001-04-17 20:45:23 +00:00
|
|
|
if (error != 0) {
|
|
|
|
return (error == ENOENT ? EOPNOTSUPP : error);
|
|
|
|
}
|
|
|
|
wvp = nd.ni_vp;
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(wvp, 0, td); /* vn_open leaves it locked */
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
ioflg = IO_UNIT;
|
|
|
|
for (;;) {
|
2002-02-27 19:18:10 +00:00
|
|
|
VOP_LEASE(wvp, td, thread0.td_ucred, LEASE_WRITE);
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0,
|
2002-02-27 19:18:10 +00:00
|
|
|
UIO_SYSSPACE, ioflg, thread0.td_ucred, NULL, td);
|
2001-04-17 20:45:23 +00:00
|
|
|
|
|
|
|
if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we're locking a file, wait for an answer. Unlocks succeed
|
|
|
|
* immediately.
|
|
|
|
*/
|
|
|
|
if (fl->l_type == F_UNLCK)
|
|
|
|
/*
|
|
|
|
* XXX this isn't exactly correct. The client side
|
|
|
|
* needs to continue sending it's unlock until
|
|
|
|
* it gets a responce back.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* retry after 20 seconds if we haven't gotten a responce yet.
|
|
|
|
* This number was picked out of thin air... but is longer
|
|
|
|
* then even a reasonably loaded system should take (at least
|
2001-09-18 23:32:09 +00:00
|
|
|
* on a local network). XXX Probably should use a back-off
|
2001-04-17 20:45:23 +00:00
|
|
|
* scheme.
|
|
|
|
*/
|
2001-09-18 23:32:09 +00:00
|
|
|
if ((error = tsleep((void *)p->p_nlminfo,
|
2001-04-17 20:45:23 +00:00
|
|
|
PCATCH | PUSER, "lockd", 20*hz)) != 0) {
|
|
|
|
if (error == EWOULDBLOCK) {
|
|
|
|
/*
|
|
|
|
* We timed out, so we rewrite the request
|
|
|
|
* to the fifo, but only if it isn't already
|
|
|
|
* full.
|
|
|
|
*/
|
|
|
|
ioflg |= IO_NDELAY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
|
|
|
|
if (p->p_nlminfo->set_getlk_pid) {
|
|
|
|
fl->l_pid = p->p_nlminfo->getlk_pid;
|
|
|
|
} else {
|
|
|
|
fl->l_type = F_UNLCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = p->p_nlminfo->retcode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-02-27 19:18:10 +00:00
|
|
|
if ((error1 = vn_close(wvp, FWRITE, thread0.td_ucred, td)) && error == 0)
|
2001-04-17 20:45:23 +00:00
|
|
|
return (error1);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nfslockdans --
|
|
|
|
* NFS advisory byte-level locks answer from the lock daemon.
|
|
|
|
*/
|
|
|
|
int
|
2001-11-14 18:20:45 +00:00
|
|
|
nfslockdans(struct thread *td, struct lockd_ans *ansp)
|
2001-04-17 20:45:23 +00:00
|
|
|
{
|
2001-11-14 18:20:45 +00:00
|
|
|
struct proc *targetp;
|
2001-04-17 20:45:23 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Let root, or someone who once was root (lockd generally
|
2001-09-18 23:32:09 +00:00
|
|
|
* switches to the daemon uid once it is done setting up) make
|
o Merge contents of struct pcred into struct ucred. Specifically, add the
real uid, saved uid, real gid, and saved gid to ucred, as well as the
pcred->pc_uidinfo, which was associated with the real uid, only rename
it to cr_ruidinfo so as not to conflict with cr_uidinfo, which
corresponds to the effective uid.
o Remove p_cred from struct proc; add p_ucred to struct proc, replacing
original macro that pointed.
p->p_ucred to p->p_cred->pc_ucred.
o Universally update code so that it makes use of ucred instead of pcred,
p->p_ucred instead of p->p_pcred, cr_ruidinfo instead of p_uidinfo,
cr_{r,sv}{u,g}id instead of p_*, etc.
o Remove pcred0 and its initialization from init_main.c; initialize
cr_ruidinfo there.
o Restruction many credential modification chunks to always crdup while
we figure out locking and optimizations; generally speaking, this
means moving to a structure like this:
newcred = crdup(oldcred);
...
p->p_ucred = newcred;
crfree(oldcred);
It's not race-free, but better than nothing. There are also races
in sys_process.c, all inter-process authorization, fork, exec, and
exit.
o Remove sigio->sio_ruid since sigio->sio_ucred now contains the ruid;
remove comments indicating that the old arrangement was a problem.
o Restructure exec1() a little to use newcred/oldcred arrangement, and
use improved uid management primitives.
o Clean up exit1() so as to do less work in credential cleanup due to
pcred removal.
o Clean up fork1() so as to do less work in credential cleanup and
allocation.
o Clean up ktrcanset() to take into account changes, and move to using
suser_xxx() instead of performing a direct uid==0 comparision.
o Improve commenting in various kern_prot.c credential modification
calls to better document current behavior. In a couple of places,
current behavior is a little questionable and we need to check
POSIX.1 to make sure it's "right". More commenting work still
remains to be done.
o Update credential management calls, such as crfree(), to take into
account new ruidinfo reference.
o Modify or add the following uid and gid helper routines:
change_euid()
change_egid()
change_ruid()
change_rgid()
change_svuid()
change_svgid()
In each case, the call now acts on a credential not a process, and as
such no longer requires more complicated process locking/etc. They
now assume the caller will do any necessary allocation of an
exclusive credential reference. Each is commented to document its
reference requirements.
o CANSIGIO() is simplified to require only credentials, not processes
and pcreds.
o Remove lots of (p_pcred==NULL) checks.
o Add an XXX to authorization code in nfs_lock.c, since it's
questionable, and needs to be considered carefully.
o Simplify posix4 authorization code to require only credentials, not
processes and pcreds. Note that this authorization, as well as
CANSIGIO(), needs to be updated to use the p_cansignal() and
p_cansched() centralized authorization routines, as they currently
do not take into account some desirable restrictions that are handled
by the centralized routines, as well as being inconsistent with other
similar authorization instances.
o Update libkvm to take these changes into account.
Obtained from: TrustedBSD Project
Reviewed by: green, bde, jhb, freebsd-arch, freebsd-audit
2001-05-25 16:59:11 +00:00
|
|
|
* this call.
|
|
|
|
*
|
|
|
|
* XXX This authorization check is probably not right.
|
2001-04-17 20:45:23 +00:00
|
|
|
*/
|
2002-04-01 21:31:13 +00:00
|
|
|
if ((error = suser(td)) != 0 &&
|
2002-02-27 18:32:23 +00:00
|
|
|
td->td_ucred->cr_svuid != 0)
|
2001-04-17 20:45:23 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* the version should match, or we're out of sync */
|
|
|
|
if (ansp->la_vers != LOCKD_ANS_VERSION)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Find the process, set its return errno and wake it up. */
|
2001-11-14 18:20:45 +00:00
|
|
|
if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
|
2001-04-17 20:45:23 +00:00
|
|
|
return (ESRCH);
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
/* verify the pid hasn't been reused (if we can), and it isn't waiting
|
2001-04-17 20:45:23 +00:00
|
|
|
* for an answer from a more recent request. We return an EPIPE if
|
|
|
|
* the match fails, because we've already used ESRCH above, and this
|
|
|
|
* is sort of like writing on a pipe after the reader has closed it.
|
|
|
|
*/
|
2001-11-14 18:20:45 +00:00
|
|
|
if (targetp->p_nlminfo == NULL ||
|
2001-04-17 20:45:23 +00:00
|
|
|
((ansp->la_msg_ident.msg_seq != -1) &&
|
2001-11-14 18:20:45 +00:00
|
|
|
(timevalcmp(&targetp->p_nlminfo->pid_start,
|
2001-04-17 20:45:23 +00:00
|
|
|
&ansp->la_msg_ident.pid_start, !=) ||
|
2001-11-14 18:20:45 +00:00
|
|
|
targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
|
|
|
|
PROC_UNLOCK(targetp);
|
2001-04-17 20:45:23 +00:00
|
|
|
return (EPIPE);
|
2001-06-01 01:30:51 +00:00
|
|
|
}
|
2001-04-17 20:45:23 +00:00
|
|
|
|
2001-11-14 18:20:45 +00:00
|
|
|
targetp->p_nlminfo->retcode = ansp->la_errno;
|
|
|
|
targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
|
|
|
|
targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
|
2001-04-17 20:45:23 +00:00
|
|
|
|
2001-11-14 18:20:45 +00:00
|
|
|
(void)wakeup((void *)targetp->p_nlminfo);
|
2001-04-17 20:45:23 +00:00
|
|
|
|
2001-11-14 18:20:45 +00:00
|
|
|
PROC_UNLOCK(targetp);
|
2001-04-17 20:45:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|