1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * NFS server file handle treatment.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6*4882a593Smuzhiyun * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
7*4882a593Smuzhiyun * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
8*4882a593Smuzhiyun * ... and again Southern-Winter 2001 to support export_operations
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/exportfs.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/sunrpc/svcauth_gss.h>
14*4882a593Smuzhiyun #include "nfsd.h"
15*4882a593Smuzhiyun #include "vfs.h"
16*4882a593Smuzhiyun #include "auth.h"
17*4882a593Smuzhiyun #include "trace.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define NFSDDBG_FACILITY NFSDDBG_FH
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * our acceptability function.
24*4882a593Smuzhiyun * if NOSUBTREECHECK, accept anything
25*4882a593Smuzhiyun * if not, require that we can walk up to exp->ex_dentry
26*4882a593Smuzhiyun * doing some checks on the 'x' bits
27*4882a593Smuzhiyun */
nfsd_acceptable(void * expv,struct dentry * dentry)28*4882a593Smuzhiyun static int nfsd_acceptable(void *expv, struct dentry *dentry)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun struct svc_export *exp = expv;
31*4882a593Smuzhiyun int rv;
32*4882a593Smuzhiyun struct dentry *tdentry;
33*4882a593Smuzhiyun struct dentry *parent;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
36*4882a593Smuzhiyun return 1;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun tdentry = dget(dentry);
39*4882a593Smuzhiyun while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
40*4882a593Smuzhiyun /* make sure parents give x permission to user */
41*4882a593Smuzhiyun int err;
42*4882a593Smuzhiyun parent = dget_parent(tdentry);
43*4882a593Smuzhiyun err = inode_permission(d_inode(parent), MAY_EXEC);
44*4882a593Smuzhiyun if (err < 0) {
45*4882a593Smuzhiyun dput(parent);
46*4882a593Smuzhiyun break;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun dput(tdentry);
49*4882a593Smuzhiyun tdentry = parent;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun if (tdentry != exp->ex_path.dentry)
52*4882a593Smuzhiyun dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
53*4882a593Smuzhiyun rv = (tdentry == exp->ex_path.dentry);
54*4882a593Smuzhiyun dput(tdentry);
55*4882a593Smuzhiyun return rv;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* Type check. The correct error return for type mismatches does not seem to be
59*4882a593Smuzhiyun * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60*4882a593Smuzhiyun * comment in the NFSv3 spec says this is incorrect (implementation notes for
61*4882a593Smuzhiyun * the write call).
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun static inline __be32
nfsd_mode_check(struct svc_rqst * rqstp,struct dentry * dentry,umode_t requested)64*4882a593Smuzhiyun nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
65*4882a593Smuzhiyun umode_t requested)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (requested == 0) /* the caller doesn't care */
70*4882a593Smuzhiyun return nfs_ok;
71*4882a593Smuzhiyun if (mode == requested) {
72*4882a593Smuzhiyun if (mode == S_IFDIR && !d_can_lookup(dentry)) {
73*4882a593Smuzhiyun WARN_ON_ONCE(1);
74*4882a593Smuzhiyun return nfserr_notdir;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun return nfs_ok;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * v4 has an error more specific than err_notdir which we should
80*4882a593Smuzhiyun * return in preference to err_notdir:
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun if (rqstp->rq_vers == 4 && mode == S_IFLNK)
83*4882a593Smuzhiyun return nfserr_symlink;
84*4882a593Smuzhiyun if (requested == S_IFDIR)
85*4882a593Smuzhiyun return nfserr_notdir;
86*4882a593Smuzhiyun if (mode == S_IFDIR)
87*4882a593Smuzhiyun return nfserr_isdir;
88*4882a593Smuzhiyun return nfserr_inval;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
nfsd_originating_port_ok(struct svc_rqst * rqstp,int flags)91*4882a593Smuzhiyun static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun if (flags & NFSEXP_INSECURE_PORT)
94*4882a593Smuzhiyun return true;
95*4882a593Smuzhiyun /* We don't require gss requests to use low ports: */
96*4882a593Smuzhiyun if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
97*4882a593Smuzhiyun return true;
98*4882a593Smuzhiyun return test_bit(RQ_SECURE, &rqstp->rq_flags);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
nfsd_setuser_and_check_port(struct svc_rqst * rqstp,struct svc_export * exp)101*4882a593Smuzhiyun static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
102*4882a593Smuzhiyun struct svc_export *exp)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun int flags = nfsexp_flags(rqstp, exp);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Check if the request originated from a secure port. */
107*4882a593Smuzhiyun if (!nfsd_originating_port_ok(rqstp, flags)) {
108*4882a593Smuzhiyun RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
109*4882a593Smuzhiyun dprintk("nfsd: request from insecure port %s!\n",
110*4882a593Smuzhiyun svc_print_addr(rqstp, buf, sizeof(buf)));
111*4882a593Smuzhiyun return nfserr_perm;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Set user creds for this exportpoint */
115*4882a593Smuzhiyun return nfserrno(nfsd_setuser(rqstp, exp));
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
check_pseudo_root(struct svc_rqst * rqstp,struct dentry * dentry,struct svc_export * exp)118*4882a593Smuzhiyun static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
119*4882a593Smuzhiyun struct dentry *dentry, struct svc_export *exp)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun if (!(exp->ex_flags & NFSEXP_V4ROOT))
122*4882a593Smuzhiyun return nfs_ok;
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun * v2/v3 clients have no need for the V4ROOT export--they use
125*4882a593Smuzhiyun * the mount protocl instead; also, further V4ROOT checks may be
126*4882a593Smuzhiyun * in v4-specific code, in which case v2/v3 clients could bypass
127*4882a593Smuzhiyun * them.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun if (!nfsd_v4client(rqstp))
130*4882a593Smuzhiyun return nfserr_stale;
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * We're exposing only the directories and symlinks that have to be
133*4882a593Smuzhiyun * traversed on the way to real exports:
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun if (unlikely(!d_is_dir(dentry) &&
136*4882a593Smuzhiyun !d_is_symlink(dentry)))
137*4882a593Smuzhiyun return nfserr_stale;
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * A pseudoroot export gives permission to access only one
140*4882a593Smuzhiyun * single directory; the kernel has to make another upcall
141*4882a593Smuzhiyun * before granting access to anything else under it:
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun if (unlikely(dentry != exp->ex_path.dentry))
144*4882a593Smuzhiyun return nfserr_stale;
145*4882a593Smuzhiyun return nfs_ok;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * Use the given filehandle to look up the corresponding export and
150*4882a593Smuzhiyun * dentry. On success, the results are used to set fh_export and
151*4882a593Smuzhiyun * fh_dentry.
152*4882a593Smuzhiyun */
nfsd_set_fh_dentry(struct svc_rqst * rqstp,struct svc_fh * fhp)153*4882a593Smuzhiyun static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun struct knfsd_fh *fh = &fhp->fh_handle;
156*4882a593Smuzhiyun struct fid *fid = NULL, sfid;
157*4882a593Smuzhiyun struct svc_export *exp;
158*4882a593Smuzhiyun struct dentry *dentry;
159*4882a593Smuzhiyun int fileid_type;
160*4882a593Smuzhiyun int data_left = fh->fh_size/4;
161*4882a593Smuzhiyun __be32 error;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun error = nfserr_stale;
164*4882a593Smuzhiyun if (rqstp->rq_vers > 2)
165*4882a593Smuzhiyun error = nfserr_badhandle;
166*4882a593Smuzhiyun if (rqstp->rq_vers == 4 && fh->fh_size == 0)
167*4882a593Smuzhiyun return nfserr_nofilehandle;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (fh->fh_version == 1) {
170*4882a593Smuzhiyun int len;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (--data_left < 0)
173*4882a593Smuzhiyun return error;
174*4882a593Smuzhiyun if (fh->fh_auth_type != 0)
175*4882a593Smuzhiyun return error;
176*4882a593Smuzhiyun len = key_len(fh->fh_fsid_type) / 4;
177*4882a593Smuzhiyun if (len == 0)
178*4882a593Smuzhiyun return error;
179*4882a593Smuzhiyun if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
180*4882a593Smuzhiyun /* deprecated, convert to type 3 */
181*4882a593Smuzhiyun len = key_len(FSID_ENCODE_DEV)/4;
182*4882a593Smuzhiyun fh->fh_fsid_type = FSID_ENCODE_DEV;
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * struct knfsd_fh uses host-endian fields, which are
185*4882a593Smuzhiyun * sometimes used to hold net-endian values. This
186*4882a593Smuzhiyun * confuses sparse, so we must use __force here to
187*4882a593Smuzhiyun * keep it from complaining.
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
190*4882a593Smuzhiyun ntohl((__force __be32)fh->fh_fsid[1])));
191*4882a593Smuzhiyun fh->fh_fsid[1] = fh->fh_fsid[2];
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun data_left -= len;
194*4882a593Smuzhiyun if (data_left < 0)
195*4882a593Smuzhiyun return error;
196*4882a593Smuzhiyun exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
197*4882a593Smuzhiyun fid = (struct fid *)(fh->fh_fsid + len);
198*4882a593Smuzhiyun } else {
199*4882a593Smuzhiyun __u32 tfh[2];
200*4882a593Smuzhiyun dev_t xdev;
201*4882a593Smuzhiyun ino_t xino;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (fh->fh_size != NFS_FHSIZE)
204*4882a593Smuzhiyun return error;
205*4882a593Smuzhiyun /* assume old filehandle format */
206*4882a593Smuzhiyun xdev = old_decode_dev(fh->ofh_xdev);
207*4882a593Smuzhiyun xino = u32_to_ino_t(fh->ofh_xino);
208*4882a593Smuzhiyun mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
209*4882a593Smuzhiyun exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun error = nfserr_stale;
213*4882a593Smuzhiyun if (IS_ERR(exp)) {
214*4882a593Smuzhiyun trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (PTR_ERR(exp) == -ENOENT)
217*4882a593Smuzhiyun return error;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return nfserrno(PTR_ERR(exp));
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
223*4882a593Smuzhiyun /* Elevate privileges so that the lack of 'r' or 'x'
224*4882a593Smuzhiyun * permission on some parent directory will
225*4882a593Smuzhiyun * not stop exportfs_decode_fh from being able
226*4882a593Smuzhiyun * to reconnect a directory into the dentry cache.
227*4882a593Smuzhiyun * The same problem can affect "SUBTREECHECK" exports,
228*4882a593Smuzhiyun * but as nfsd_acceptable depends on correct
229*4882a593Smuzhiyun * access control settings being in effect, we cannot
230*4882a593Smuzhiyun * fix that case easily.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun struct cred *new = prepare_creds();
233*4882a593Smuzhiyun if (!new) {
234*4882a593Smuzhiyun error = nfserrno(-ENOMEM);
235*4882a593Smuzhiyun goto out;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun new->cap_effective =
238*4882a593Smuzhiyun cap_raise_nfsd_set(new->cap_effective,
239*4882a593Smuzhiyun new->cap_permitted);
240*4882a593Smuzhiyun put_cred(override_creds(new));
241*4882a593Smuzhiyun put_cred(new);
242*4882a593Smuzhiyun } else {
243*4882a593Smuzhiyun error = nfsd_setuser_and_check_port(rqstp, exp);
244*4882a593Smuzhiyun if (error)
245*4882a593Smuzhiyun goto out;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * Look up the dentry using the NFS file handle.
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun error = nfserr_stale;
252*4882a593Smuzhiyun if (rqstp->rq_vers > 2)
253*4882a593Smuzhiyun error = nfserr_badhandle;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun if (fh->fh_version != 1) {
256*4882a593Smuzhiyun sfid.i32.ino = fh->ofh_ino;
257*4882a593Smuzhiyun sfid.i32.gen = fh->ofh_generation;
258*4882a593Smuzhiyun sfid.i32.parent_ino = fh->ofh_dirino;
259*4882a593Smuzhiyun fid = &sfid;
260*4882a593Smuzhiyun data_left = 3;
261*4882a593Smuzhiyun if (fh->ofh_dirino == 0)
262*4882a593Smuzhiyun fileid_type = FILEID_INO32_GEN;
263*4882a593Smuzhiyun else
264*4882a593Smuzhiyun fileid_type = FILEID_INO32_GEN_PARENT;
265*4882a593Smuzhiyun } else
266*4882a593Smuzhiyun fileid_type = fh->fh_fileid_type;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun if (fileid_type == FILEID_ROOT)
269*4882a593Smuzhiyun dentry = dget(exp->ex_path.dentry);
270*4882a593Smuzhiyun else {
271*4882a593Smuzhiyun dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
272*4882a593Smuzhiyun data_left, fileid_type,
273*4882a593Smuzhiyun nfsd_acceptable, exp);
274*4882a593Smuzhiyun if (IS_ERR_OR_NULL(dentry))
275*4882a593Smuzhiyun trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
276*4882a593Smuzhiyun dentry ? PTR_ERR(dentry) : -ESTALE);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun if (dentry == NULL)
279*4882a593Smuzhiyun goto out;
280*4882a593Smuzhiyun if (IS_ERR(dentry)) {
281*4882a593Smuzhiyun if (PTR_ERR(dentry) != -EINVAL)
282*4882a593Smuzhiyun error = nfserrno(PTR_ERR(dentry));
283*4882a593Smuzhiyun goto out;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (d_is_dir(dentry) &&
287*4882a593Smuzhiyun (dentry->d_flags & DCACHE_DISCONNECTED)) {
288*4882a593Smuzhiyun printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
289*4882a593Smuzhiyun dentry);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun fhp->fh_dentry = dentry;
293*4882a593Smuzhiyun fhp->fh_export = exp;
294*4882a593Smuzhiyun return 0;
295*4882a593Smuzhiyun out:
296*4882a593Smuzhiyun exp_put(exp);
297*4882a593Smuzhiyun return error;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun * fh_verify - filehandle lookup and access checking
302*4882a593Smuzhiyun * @rqstp: pointer to current rpc request
303*4882a593Smuzhiyun * @fhp: filehandle to be verified
304*4882a593Smuzhiyun * @type: expected type of object pointed to by filehandle
305*4882a593Smuzhiyun * @access: type of access needed to object
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * Look up a dentry from the on-the-wire filehandle, check the client's
308*4882a593Smuzhiyun * access to the export, and set the current task's credentials.
309*4882a593Smuzhiyun *
310*4882a593Smuzhiyun * Regardless of success or failure of fh_verify(), fh_put() should be
311*4882a593Smuzhiyun * called on @fhp when the caller is finished with the filehandle.
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * fh_verify() may be called multiple times on a given filehandle, for
314*4882a593Smuzhiyun * example, when processing an NFSv4 compound. The first call will look
315*4882a593Smuzhiyun * up a dentry using the on-the-wire filehandle. Subsequent calls will
316*4882a593Smuzhiyun * skip the lookup and just perform the other checks and possibly change
317*4882a593Smuzhiyun * the current task's credentials.
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * @type specifies the type of object expected using one of the S_IF*
320*4882a593Smuzhiyun * constants defined in include/linux/stat.h. The caller may use zero
321*4882a593Smuzhiyun * to indicate that it doesn't care, or a negative integer to indicate
322*4882a593Smuzhiyun * that it expects something not of the given type.
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * @access is formed from the NFSD_MAY_* constants defined in
325*4882a593Smuzhiyun * fs/nfsd/vfs.h.
326*4882a593Smuzhiyun */
327*4882a593Smuzhiyun __be32
fh_verify(struct svc_rqst * rqstp,struct svc_fh * fhp,umode_t type,int access)328*4882a593Smuzhiyun fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun struct svc_export *exp;
331*4882a593Smuzhiyun struct dentry *dentry;
332*4882a593Smuzhiyun __be32 error;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if (!fhp->fh_dentry) {
337*4882a593Smuzhiyun error = nfsd_set_fh_dentry(rqstp, fhp);
338*4882a593Smuzhiyun if (error)
339*4882a593Smuzhiyun goto out;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun dentry = fhp->fh_dentry;
342*4882a593Smuzhiyun exp = fhp->fh_export;
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun * We still have to do all these permission checks, even when
345*4882a593Smuzhiyun * fh_dentry is already set:
346*4882a593Smuzhiyun * - fh_verify may be called multiple times with different
347*4882a593Smuzhiyun * "access" arguments (e.g. nfsd_proc_create calls
348*4882a593Smuzhiyun * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
349*4882a593Smuzhiyun * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
350*4882a593Smuzhiyun * - in the NFSv4 case, the filehandle may have been filled
351*4882a593Smuzhiyun * in by fh_compose, and given a dentry, but further
352*4882a593Smuzhiyun * compound operations performed with that filehandle
353*4882a593Smuzhiyun * still need permissions checks. In the worst case, a
354*4882a593Smuzhiyun * mountpoint crossing may have changed the export
355*4882a593Smuzhiyun * options, and we may now need to use a different uid
356*4882a593Smuzhiyun * (for example, if different id-squashing options are in
357*4882a593Smuzhiyun * effect on the new filesystem).
358*4882a593Smuzhiyun */
359*4882a593Smuzhiyun error = check_pseudo_root(rqstp, dentry, exp);
360*4882a593Smuzhiyun if (error)
361*4882a593Smuzhiyun goto out;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun error = nfsd_setuser_and_check_port(rqstp, exp);
364*4882a593Smuzhiyun if (error)
365*4882a593Smuzhiyun goto out;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun error = nfsd_mode_check(rqstp, dentry, type);
368*4882a593Smuzhiyun if (error)
369*4882a593Smuzhiyun goto out;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /*
372*4882a593Smuzhiyun * pseudoflavor restrictions are not enforced on NLM,
373*4882a593Smuzhiyun * which clients virtually always use auth_sys for,
374*4882a593Smuzhiyun * even while using RPCSEC_GSS for NFS.
375*4882a593Smuzhiyun */
376*4882a593Smuzhiyun if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
377*4882a593Smuzhiyun goto skip_pseudoflavor_check;
378*4882a593Smuzhiyun /*
379*4882a593Smuzhiyun * Clients may expect to be able to use auth_sys during mount,
380*4882a593Smuzhiyun * even if they use gss for everything else; see section 2.3.2
381*4882a593Smuzhiyun * of rfc 2623.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
384*4882a593Smuzhiyun && exp->ex_path.dentry == dentry)
385*4882a593Smuzhiyun goto skip_pseudoflavor_check;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun error = check_nfsd_access(exp, rqstp);
388*4882a593Smuzhiyun if (error)
389*4882a593Smuzhiyun goto out;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun skip_pseudoflavor_check:
392*4882a593Smuzhiyun /* Finally, check access permissions. */
393*4882a593Smuzhiyun error = nfsd_permission(rqstp, exp, dentry, access);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (error) {
396*4882a593Smuzhiyun dprintk("fh_verify: %pd2 permission failure, "
397*4882a593Smuzhiyun "acc=%x, error=%d\n",
398*4882a593Smuzhiyun dentry,
399*4882a593Smuzhiyun access, ntohl(error));
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun out:
402*4882a593Smuzhiyun if (error == nfserr_stale)
403*4882a593Smuzhiyun nfsdstats.fh_stale++;
404*4882a593Smuzhiyun return error;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /*
409*4882a593Smuzhiyun * Compose a file handle for an NFS reply.
410*4882a593Smuzhiyun *
411*4882a593Smuzhiyun * Note that when first composed, the dentry may not yet have
412*4882a593Smuzhiyun * an inode. In this case a call to fh_update should be made
413*4882a593Smuzhiyun * before the fh goes out on the wire ...
414*4882a593Smuzhiyun */
_fh_update(struct svc_fh * fhp,struct svc_export * exp,struct dentry * dentry)415*4882a593Smuzhiyun static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
416*4882a593Smuzhiyun struct dentry *dentry)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun if (dentry != exp->ex_path.dentry) {
419*4882a593Smuzhiyun struct fid *fid = (struct fid *)
420*4882a593Smuzhiyun (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
421*4882a593Smuzhiyun int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
422*4882a593Smuzhiyun int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun fhp->fh_handle.fh_fileid_type =
425*4882a593Smuzhiyun exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
426*4882a593Smuzhiyun fhp->fh_handle.fh_size += maxsize * 4;
427*4882a593Smuzhiyun } else {
428*4882a593Smuzhiyun fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun * for composing old style file handles
434*4882a593Smuzhiyun */
_fh_update_old(struct dentry * dentry,struct svc_export * exp,struct knfsd_fh * fh)435*4882a593Smuzhiyun static inline void _fh_update_old(struct dentry *dentry,
436*4882a593Smuzhiyun struct svc_export *exp,
437*4882a593Smuzhiyun struct knfsd_fh *fh)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun fh->ofh_ino = ino_t_to_u32(d_inode(dentry)->i_ino);
440*4882a593Smuzhiyun fh->ofh_generation = d_inode(dentry)->i_generation;
441*4882a593Smuzhiyun if (d_is_dir(dentry) ||
442*4882a593Smuzhiyun (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
443*4882a593Smuzhiyun fh->ofh_dirino = 0;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
is_root_export(struct svc_export * exp)446*4882a593Smuzhiyun static bool is_root_export(struct svc_export *exp)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
exp_sb(struct svc_export * exp)451*4882a593Smuzhiyun static struct super_block *exp_sb(struct svc_export *exp)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun return exp->ex_path.dentry->d_sb;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
fsid_type_ok_for_exp(u8 fsid_type,struct svc_export * exp)456*4882a593Smuzhiyun static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun switch (fsid_type) {
459*4882a593Smuzhiyun case FSID_DEV:
460*4882a593Smuzhiyun if (!old_valid_dev(exp_sb(exp)->s_dev))
461*4882a593Smuzhiyun return false;
462*4882a593Smuzhiyun fallthrough;
463*4882a593Smuzhiyun case FSID_MAJOR_MINOR:
464*4882a593Smuzhiyun case FSID_ENCODE_DEV:
465*4882a593Smuzhiyun return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
466*4882a593Smuzhiyun case FSID_NUM:
467*4882a593Smuzhiyun return exp->ex_flags & NFSEXP_FSID;
468*4882a593Smuzhiyun case FSID_UUID8:
469*4882a593Smuzhiyun case FSID_UUID16:
470*4882a593Smuzhiyun if (!is_root_export(exp))
471*4882a593Smuzhiyun return false;
472*4882a593Smuzhiyun fallthrough;
473*4882a593Smuzhiyun case FSID_UUID4_INUM:
474*4882a593Smuzhiyun case FSID_UUID16_INUM:
475*4882a593Smuzhiyun return exp->ex_uuid != NULL;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun return true;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun
set_version_and_fsid_type(struct svc_fh * fhp,struct svc_export * exp,struct svc_fh * ref_fh)481*4882a593Smuzhiyun static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun u8 version;
484*4882a593Smuzhiyun u8 fsid_type;
485*4882a593Smuzhiyun retry:
486*4882a593Smuzhiyun version = 1;
487*4882a593Smuzhiyun if (ref_fh && ref_fh->fh_export == exp) {
488*4882a593Smuzhiyun version = ref_fh->fh_handle.fh_version;
489*4882a593Smuzhiyun fsid_type = ref_fh->fh_handle.fh_fsid_type;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun ref_fh = NULL;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun switch (version) {
494*4882a593Smuzhiyun case 0xca:
495*4882a593Smuzhiyun fsid_type = FSID_DEV;
496*4882a593Smuzhiyun break;
497*4882a593Smuzhiyun case 1:
498*4882a593Smuzhiyun break;
499*4882a593Smuzhiyun default:
500*4882a593Smuzhiyun goto retry;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun * As the fsid -> filesystem mapping was guided by
505*4882a593Smuzhiyun * user-space, there is no guarantee that the filesystem
506*4882a593Smuzhiyun * actually supports that fsid type. If it doesn't we
507*4882a593Smuzhiyun * loop around again without ref_fh set.
508*4882a593Smuzhiyun */
509*4882a593Smuzhiyun if (!fsid_type_ok_for_exp(fsid_type, exp))
510*4882a593Smuzhiyun goto retry;
511*4882a593Smuzhiyun } else if (exp->ex_flags & NFSEXP_FSID) {
512*4882a593Smuzhiyun fsid_type = FSID_NUM;
513*4882a593Smuzhiyun } else if (exp->ex_uuid) {
514*4882a593Smuzhiyun if (fhp->fh_maxsize >= 64) {
515*4882a593Smuzhiyun if (is_root_export(exp))
516*4882a593Smuzhiyun fsid_type = FSID_UUID16;
517*4882a593Smuzhiyun else
518*4882a593Smuzhiyun fsid_type = FSID_UUID16_INUM;
519*4882a593Smuzhiyun } else {
520*4882a593Smuzhiyun if (is_root_export(exp))
521*4882a593Smuzhiyun fsid_type = FSID_UUID8;
522*4882a593Smuzhiyun else
523*4882a593Smuzhiyun fsid_type = FSID_UUID4_INUM;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun } else if (!old_valid_dev(exp_sb(exp)->s_dev))
526*4882a593Smuzhiyun /* for newer device numbers, we must use a newer fsid format */
527*4882a593Smuzhiyun fsid_type = FSID_ENCODE_DEV;
528*4882a593Smuzhiyun else
529*4882a593Smuzhiyun fsid_type = FSID_DEV;
530*4882a593Smuzhiyun fhp->fh_handle.fh_version = version;
531*4882a593Smuzhiyun if (version)
532*4882a593Smuzhiyun fhp->fh_handle.fh_fsid_type = fsid_type;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun __be32
fh_compose(struct svc_fh * fhp,struct svc_export * exp,struct dentry * dentry,struct svc_fh * ref_fh)536*4882a593Smuzhiyun fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
537*4882a593Smuzhiyun struct svc_fh *ref_fh)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun /* ref_fh is a reference file handle.
540*4882a593Smuzhiyun * if it is non-null and for the same filesystem, then we should compose
541*4882a593Smuzhiyun * a filehandle which is of the same version, where possible.
542*4882a593Smuzhiyun * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
543*4882a593Smuzhiyun * Then create a 32byte filehandle using nfs_fhbase_old
544*4882a593Smuzhiyun *
545*4882a593Smuzhiyun */
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun struct inode * inode = d_inode(dentry);
548*4882a593Smuzhiyun dev_t ex_dev = exp_sb(exp)->s_dev;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
551*4882a593Smuzhiyun MAJOR(ex_dev), MINOR(ex_dev),
552*4882a593Smuzhiyun (long) d_inode(exp->ex_path.dentry)->i_ino,
553*4882a593Smuzhiyun dentry,
554*4882a593Smuzhiyun (inode ? inode->i_ino : 0));
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /* Choose filehandle version and fsid type based on
557*4882a593Smuzhiyun * the reference filehandle (if it is in the same export)
558*4882a593Smuzhiyun * or the export options.
559*4882a593Smuzhiyun */
560*4882a593Smuzhiyun set_version_and_fsid_type(fhp, exp, ref_fh);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (ref_fh == fhp)
563*4882a593Smuzhiyun fh_put(ref_fh);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun if (fhp->fh_locked || fhp->fh_dentry) {
566*4882a593Smuzhiyun printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
567*4882a593Smuzhiyun dentry);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun if (fhp->fh_maxsize < NFS_FHSIZE)
570*4882a593Smuzhiyun printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
571*4882a593Smuzhiyun fhp->fh_maxsize,
572*4882a593Smuzhiyun dentry);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun fhp->fh_dentry = dget(dentry); /* our internal copy */
575*4882a593Smuzhiyun fhp->fh_export = exp_get(exp);
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun if (fhp->fh_handle.fh_version == 0xca) {
578*4882a593Smuzhiyun /* old style filehandle please */
579*4882a593Smuzhiyun memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
580*4882a593Smuzhiyun fhp->fh_handle.fh_size = NFS_FHSIZE;
581*4882a593Smuzhiyun fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
582*4882a593Smuzhiyun fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
583*4882a593Smuzhiyun fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
584*4882a593Smuzhiyun fhp->fh_handle.ofh_xino =
585*4882a593Smuzhiyun ino_t_to_u32(d_inode(exp->ex_path.dentry)->i_ino);
586*4882a593Smuzhiyun fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
587*4882a593Smuzhiyun if (inode)
588*4882a593Smuzhiyun _fh_update_old(dentry, exp, &fhp->fh_handle);
589*4882a593Smuzhiyun } else {
590*4882a593Smuzhiyun fhp->fh_handle.fh_size =
591*4882a593Smuzhiyun key_len(fhp->fh_handle.fh_fsid_type) + 4;
592*4882a593Smuzhiyun fhp->fh_handle.fh_auth_type = 0;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun mk_fsid(fhp->fh_handle.fh_fsid_type,
595*4882a593Smuzhiyun fhp->fh_handle.fh_fsid,
596*4882a593Smuzhiyun ex_dev,
597*4882a593Smuzhiyun d_inode(exp->ex_path.dentry)->i_ino,
598*4882a593Smuzhiyun exp->ex_fsid, exp->ex_uuid);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun if (inode)
601*4882a593Smuzhiyun _fh_update(fhp, exp, dentry);
602*4882a593Smuzhiyun if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
603*4882a593Smuzhiyun fh_put(fhp);
604*4882a593Smuzhiyun return nfserr_opnotsupp;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun return 0;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun * Update file handle information after changing a dentry.
613*4882a593Smuzhiyun * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
614*4882a593Smuzhiyun */
615*4882a593Smuzhiyun __be32
fh_update(struct svc_fh * fhp)616*4882a593Smuzhiyun fh_update(struct svc_fh *fhp)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun struct dentry *dentry;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun if (!fhp->fh_dentry)
621*4882a593Smuzhiyun goto out_bad;
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun dentry = fhp->fh_dentry;
624*4882a593Smuzhiyun if (d_really_is_negative(dentry))
625*4882a593Smuzhiyun goto out_negative;
626*4882a593Smuzhiyun if (fhp->fh_handle.fh_version != 1) {
627*4882a593Smuzhiyun _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
628*4882a593Smuzhiyun } else {
629*4882a593Smuzhiyun if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
630*4882a593Smuzhiyun return 0;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun _fh_update(fhp, fhp->fh_export, dentry);
633*4882a593Smuzhiyun if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
634*4882a593Smuzhiyun return nfserr_opnotsupp;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun return 0;
637*4882a593Smuzhiyun out_bad:
638*4882a593Smuzhiyun printk(KERN_ERR "fh_update: fh not verified!\n");
639*4882a593Smuzhiyun return nfserr_serverfault;
640*4882a593Smuzhiyun out_negative:
641*4882a593Smuzhiyun printk(KERN_ERR "fh_update: %pd2 still negative!\n",
642*4882a593Smuzhiyun dentry);
643*4882a593Smuzhiyun return nfserr_serverfault;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun * Release a file handle.
648*4882a593Smuzhiyun */
649*4882a593Smuzhiyun void
fh_put(struct svc_fh * fhp)650*4882a593Smuzhiyun fh_put(struct svc_fh *fhp)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun struct dentry * dentry = fhp->fh_dentry;
653*4882a593Smuzhiyun struct svc_export * exp = fhp->fh_export;
654*4882a593Smuzhiyun if (dentry) {
655*4882a593Smuzhiyun fh_unlock(fhp);
656*4882a593Smuzhiyun fhp->fh_dentry = NULL;
657*4882a593Smuzhiyun dput(dentry);
658*4882a593Smuzhiyun fh_clear_wcc(fhp);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun fh_drop_write(fhp);
661*4882a593Smuzhiyun if (exp) {
662*4882a593Smuzhiyun exp_put(exp);
663*4882a593Smuzhiyun fhp->fh_export = NULL;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun return;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun /*
669*4882a593Smuzhiyun * Shorthand for dprintk()'s
670*4882a593Smuzhiyun */
SVCFH_fmt(struct svc_fh * fhp)671*4882a593Smuzhiyun char * SVCFH_fmt(struct svc_fh *fhp)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun struct knfsd_fh *fh = &fhp->fh_handle;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun static char buf[80];
676*4882a593Smuzhiyun sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
677*4882a593Smuzhiyun fh->fh_size,
678*4882a593Smuzhiyun fh->fh_base.fh_pad[0],
679*4882a593Smuzhiyun fh->fh_base.fh_pad[1],
680*4882a593Smuzhiyun fh->fh_base.fh_pad[2],
681*4882a593Smuzhiyun fh->fh_base.fh_pad[3],
682*4882a593Smuzhiyun fh->fh_base.fh_pad[4],
683*4882a593Smuzhiyun fh->fh_base.fh_pad[5]);
684*4882a593Smuzhiyun return buf;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun
fsid_source(struct svc_fh * fhp)687*4882a593Smuzhiyun enum fsid_source fsid_source(struct svc_fh *fhp)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun if (fhp->fh_handle.fh_version != 1)
690*4882a593Smuzhiyun return FSIDSOURCE_DEV;
691*4882a593Smuzhiyun switch(fhp->fh_handle.fh_fsid_type) {
692*4882a593Smuzhiyun case FSID_DEV:
693*4882a593Smuzhiyun case FSID_ENCODE_DEV:
694*4882a593Smuzhiyun case FSID_MAJOR_MINOR:
695*4882a593Smuzhiyun if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
696*4882a593Smuzhiyun return FSIDSOURCE_DEV;
697*4882a593Smuzhiyun break;
698*4882a593Smuzhiyun case FSID_NUM:
699*4882a593Smuzhiyun if (fhp->fh_export->ex_flags & NFSEXP_FSID)
700*4882a593Smuzhiyun return FSIDSOURCE_FSID;
701*4882a593Smuzhiyun break;
702*4882a593Smuzhiyun default:
703*4882a593Smuzhiyun break;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun /* either a UUID type filehandle, or the filehandle doesn't
706*4882a593Smuzhiyun * match the export.
707*4882a593Smuzhiyun */
708*4882a593Smuzhiyun if (fhp->fh_export->ex_flags & NFSEXP_FSID)
709*4882a593Smuzhiyun return FSIDSOURCE_FSID;
710*4882a593Smuzhiyun if (fhp->fh_export->ex_uuid)
711*4882a593Smuzhiyun return FSIDSOURCE_UUID;
712*4882a593Smuzhiyun return FSIDSOURCE_DEV;
713*4882a593Smuzhiyun }
714