Date: Tue, 20 Dec 2011 23:31:40 +0400 From: Gleb Smirnoff <glebius@FreeBSD.org> To: John Baldwin <jhb@FreeBSD.org> Cc: freebsd-current@FreeBSD.org, Rick Macklem <rmacklem@uoguelph.ca> Subject: Re: making crdup()/crcopy() safe?? Message-ID: <20111220193139.GD70684@FreeBSD.org> In-Reply-To: <201112200900.39087.jhb@freebsd.org> References: <261812530.427572.1324344105125.JavaMail.root@erie.cs.uoguelph.ca> <201112200900.39087.jhb@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--FCuugMFkClbJLl1L Content-Type: text/plain; charset=koi8-r Content-Disposition: inline John, On Tue, Dec 20, 2011 at 09:00:39AM -0500, John Baldwin wrote: J> In general the caller of crdup is expected to hold a reference on cred or some J> other lock to ensure that cred remains valid and cannot be free'd while it is J> being duplicated. There is no global lock that crdup can hold for that, J> instead the caller is required to guarantee that. I already noticed to Rick in a private email, that there is suspisious place in new NFS, where newly allocated (via crdup) cred is temporarily placed on td_ucred, and then removed at the end of function. The function may sleep in malloc() and also block on mutexes. I suspect, that it may happen, that some other kernel facility performs crfree(td->td_ucred), and later on we are using already freed cred. However, I can't imagine which facility may do this. What if process gets SIGKILL while its thread is blocked on mutex or sleeping? Would it be reaped after it yields or before? Attached patch demonstrates what I am trying to address, but I'm not sure that this temporary placing on td_ucred is really unsafe. Can you please look at this? -- Totus tuus, Glebius. --FCuugMFkClbJLl1L Content-Type: text/x-diff; charset=koi8-r Content-Disposition: attachment; filename="nfs_cred.diff" Index: nfs_commonkrpc.c =================================================================== --- nfs_commonkrpc.c (revision 228700) +++ nfs_commonkrpc.c (working copy) @@ -186,9 +186,9 @@ * Use the credential in nr_cred, if not NULL. */ if (nrp->nr_cred != NULL) - td->td_ucred = nrp->nr_cred; + td->td_ucred = NFSHOLDCRED(nrp->nr_cred); else - td->td_ucred = cred; + td->td_ucred = NFSHOLDCRED(cred); saddr = nrp->nr_nam; if (saddr->sa_family == AF_INET) @@ -220,10 +220,8 @@ saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *); error = socreate(saddr->sa_family, &so, nrp->nr_sotype, nrp->nr_soproto, td->td_ucred, td); - if (error) { - td->td_ucred = origcred; + if (error) goto out; - } do { if (error != 0 && pktscale > 2) pktscale--; @@ -251,10 +249,8 @@ error = soreserve(so, sndreserve, rcvreserve); } while (error != 0 && pktscale > 2); soclose(so); - if (error) { - td->td_ucred = origcred; + if (error) goto out; - } client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog, nrp->nr_vers, sndreserve, rcvreserve); @@ -305,10 +301,11 @@ mtx_unlock(&nrp->nr_mtx); } +out: /* Restore current thread's credentials. */ + NFSFREECRED(td->td_ucred); td->td_ucred = origcred; -out: NFSEXITCODE(error); return (error); } Index: nfsport.h =================================================================== --- nfsport.h (revision 228700) +++ nfsport.h (working copy) @@ -515,6 +515,7 @@ #define NFSNEWCRED(c) (crdup(c)) #define NFSPROCCRED(p) ((p)->td_ucred) #define NFSFREECRED(c) (crfree(c)) +#define NFSHOLDCRED(c) (crhold(c)) #define NFSUIOPROC(u, p) ((u)->uio_td = NULL) #define NFSPROCP(p) ((p)->td_proc) --FCuugMFkClbJLl1L--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111220193139.GD70684>