1 // SPDX-License-Identifier: GPL-2.0
3 * NFS server file handle treatment.
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6 * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
7 * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
8 * ... and again Southern-Winter 2001 to support export_operations
11 #include <linux/exportfs.h>
13 #include <linux/sunrpc/svcauth_gss.h>
19 #define NFSDDBG_FACILITY NFSDDBG_FH
23 * our acceptability function.
24 * if NOSUBTREECHECK, accept anything
25 * if not, require that we can walk up to exp->ex_dentry
26 * doing some checks on the 'x' bits
28 static int nfsd_acceptable(void *expv
, struct dentry
*dentry
)
30 struct svc_export
*exp
= expv
;
32 struct dentry
*tdentry
;
33 struct dentry
*parent
;
35 if (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
)
38 tdentry
= dget(dentry
);
39 while (tdentry
!= exp
->ex_path
.dentry
&& !IS_ROOT(tdentry
)) {
40 /* make sure parents give x permission to user */
42 parent
= dget_parent(tdentry
);
43 err
= inode_permission(d_inode(parent
), MAY_EXEC
);
51 if (tdentry
!= exp
->ex_path
.dentry
)
52 dprintk("nfsd_acceptable failed at %p %pd\n", tdentry
, tdentry
);
53 rv
= (tdentry
== exp
->ex_path
.dentry
);
58 /* Type check. The correct error return for type mismatches does not seem to be
59 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60 * comment in the NFSv3 spec says this is incorrect (implementation notes for
64 nfsd_mode_check(struct svc_rqst
*rqstp
, struct dentry
*dentry
,
67 umode_t mode
= d_inode(dentry
)->i_mode
& S_IFMT
;
69 if (requested
== 0) /* the caller doesn't care */
71 if (mode
== requested
) {
72 if (mode
== S_IFDIR
&& !d_can_lookup(dentry
)) {
79 * v4 has an error more specific than err_notdir which we should
80 * return in preference to err_notdir:
82 if (rqstp
->rq_vers
== 4 && mode
== S_IFLNK
)
83 return nfserr_symlink
;
84 if (requested
== S_IFDIR
)
91 static bool nfsd_originating_port_ok(struct svc_rqst
*rqstp
, int flags
)
93 if (flags
& NFSEXP_INSECURE_PORT
)
95 /* We don't require gss requests to use low ports: */
96 if (rqstp
->rq_cred
.cr_flavor
>= RPC_AUTH_GSS
)
98 return test_bit(RQ_SECURE
, &rqstp
->rq_flags
);
101 static __be32
nfsd_setuser_and_check_port(struct svc_rqst
*rqstp
,
102 struct svc_export
*exp
)
104 int flags
= nfsexp_flags(rqstp
, exp
);
106 /* Check if the request originated from a secure port. */
107 if (!nfsd_originating_port_ok(rqstp
, flags
)) {
108 RPC_IFDEBUG(char buf
[RPC_MAX_ADDRBUFLEN
]);
109 dprintk("nfsd: request from insecure port %s!\n",
110 svc_print_addr(rqstp
, buf
, sizeof(buf
)));
114 /* Set user creds for this exportpoint */
115 return nfserrno(nfsd_setuser(rqstp
, exp
));
118 static inline __be32
check_pseudo_root(struct svc_rqst
*rqstp
,
119 struct dentry
*dentry
, struct svc_export
*exp
)
121 if (!(exp
->ex_flags
& NFSEXP_V4ROOT
))
124 * v2/v3 clients have no need for the V4ROOT export--they use
125 * the mount protocl instead; also, further V4ROOT checks may be
126 * in v4-specific code, in which case v2/v3 clients could bypass
129 if (!nfsd_v4client(rqstp
))
132 * We're exposing only the directories and symlinks that have to be
133 * traversed on the way to real exports:
135 if (unlikely(!d_is_dir(dentry
) &&
136 !d_is_symlink(dentry
)))
139 * A pseudoroot export gives permission to access only one
140 * single directory; the kernel has to make another upcall
141 * before granting access to anything else under it:
143 if (unlikely(dentry
!= exp
->ex_path
.dentry
))
149 * Use the given filehandle to look up the corresponding export and
150 * dentry. On success, the results are used to set fh_export and
153 static __be32
nfsd_set_fh_dentry(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
)
155 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
156 struct fid
*fid
= NULL
, sfid
;
157 struct svc_export
*exp
;
158 struct dentry
*dentry
;
160 int data_left
= fh
->fh_size
/4;
163 error
= nfserr_stale
;
164 if (rqstp
->rq_vers
> 2)
165 error
= nfserr_badhandle
;
166 if (rqstp
->rq_vers
== 4 && fh
->fh_size
== 0)
167 return nfserr_nofilehandle
;
169 if (fh
->fh_version
== 1) {
174 if (fh
->fh_auth_type
!= 0)
176 len
= key_len(fh
->fh_fsid_type
) / 4;
179 if (fh
->fh_fsid_type
== FSID_MAJOR_MINOR
) {
180 /* deprecated, convert to type 3 */
181 len
= key_len(FSID_ENCODE_DEV
)/4;
182 fh
->fh_fsid_type
= FSID_ENCODE_DEV
;
184 * struct knfsd_fh uses host-endian fields, which are
185 * sometimes used to hold net-endian values. This
186 * confuses sparse, so we must use __force here to
187 * keep it from complaining.
189 fh
->fh_fsid
[0] = new_encode_dev(MKDEV(ntohl((__force __be32
)fh
->fh_fsid
[0]),
190 ntohl((__force __be32
)fh
->fh_fsid
[1])));
191 fh
->fh_fsid
[1] = fh
->fh_fsid
[2];
196 exp
= rqst_exp_find(rqstp
, fh
->fh_fsid_type
, fh
->fh_fsid
);
197 fid
= (struct fid
*)(fh
->fh_fsid
+ len
);
203 if (fh
->fh_size
!= NFS_FHSIZE
)
205 /* assume old filehandle format */
206 xdev
= old_decode_dev(fh
->ofh_xdev
);
207 xino
= u32_to_ino_t(fh
->ofh_xino
);
208 mk_fsid(FSID_DEV
, tfh
, xdev
, xino
, 0, NULL
);
209 exp
= rqst_exp_find(rqstp
, FSID_DEV
, tfh
);
212 error
= nfserr_stale
;
214 trace_nfsd_set_fh_dentry_badexport(rqstp
, fhp
, PTR_ERR(exp
));
216 if (PTR_ERR(exp
) == -ENOENT
)
219 return nfserrno(PTR_ERR(exp
));
222 if (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
) {
223 /* Elevate privileges so that the lack of 'r' or 'x'
224 * permission on some parent directory will
225 * not stop exportfs_decode_fh from being able
226 * to reconnect a directory into the dentry cache.
227 * The same problem can affect "SUBTREECHECK" exports,
228 * but as nfsd_acceptable depends on correct
229 * access control settings being in effect, we cannot
230 * fix that case easily.
232 struct cred
*new = prepare_creds();
234 error
= nfserrno(-ENOMEM
);
238 cap_raise_nfsd_set(new->cap_effective
,
240 put_cred(override_creds(new));
243 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
249 * Look up the dentry using the NFS file handle.
251 error
= nfserr_stale
;
252 if (rqstp
->rq_vers
> 2)
253 error
= nfserr_badhandle
;
255 if (fh
->fh_version
!= 1) {
256 sfid
.i32
.ino
= fh
->ofh_ino
;
257 sfid
.i32
.gen
= fh
->ofh_generation
;
258 sfid
.i32
.parent_ino
= fh
->ofh_dirino
;
261 if (fh
->ofh_dirino
== 0)
262 fileid_type
= FILEID_INO32_GEN
;
264 fileid_type
= FILEID_INO32_GEN_PARENT
;
266 fileid_type
= fh
->fh_fileid_type
;
268 if (fileid_type
== FILEID_ROOT
)
269 dentry
= dget(exp
->ex_path
.dentry
);
271 dentry
= exportfs_decode_fh(exp
->ex_path
.mnt
, fid
,
272 data_left
, fileid_type
,
273 nfsd_acceptable
, exp
);
274 if (IS_ERR_OR_NULL(dentry
))
275 trace_nfsd_set_fh_dentry_badhandle(rqstp
, fhp
,
276 dentry
? PTR_ERR(dentry
) : -ESTALE
);
280 if (IS_ERR(dentry
)) {
281 if (PTR_ERR(dentry
) != -EINVAL
)
282 error
= nfserrno(PTR_ERR(dentry
));
286 if (d_is_dir(dentry
) &&
287 (dentry
->d_flags
& DCACHE_DISCONNECTED
)) {
288 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
292 fhp
->fh_dentry
= dentry
;
293 fhp
->fh_export
= exp
;
301 * fh_verify - filehandle lookup and access checking
302 * @rqstp: pointer to current rpc request
303 * @fhp: filehandle to be verified
304 * @type: expected type of object pointed to by filehandle
305 * @access: type of access needed to object
307 * Look up a dentry from the on-the-wire filehandle, check the client's
308 * access to the export, and set the current task's credentials.
310 * Regardless of success or failure of fh_verify(), fh_put() should be
311 * called on @fhp when the caller is finished with the filehandle.
313 * fh_verify() may be called multiple times on a given filehandle, for
314 * example, when processing an NFSv4 compound. The first call will look
315 * up a dentry using the on-the-wire filehandle. Subsequent calls will
316 * skip the lookup and just perform the other checks and possibly change
317 * the current task's credentials.
319 * @type specifies the type of object expected using one of the S_IF*
320 * constants defined in include/linux/stat.h. The caller may use zero
321 * to indicate that it doesn't care, or a negative integer to indicate
322 * that it expects something not of the given type.
324 * @access is formed from the NFSD_MAY_* constants defined in
328 fh_verify(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, umode_t type
, int access
)
330 struct svc_export
*exp
;
331 struct dentry
*dentry
;
334 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp
));
336 if (!fhp
->fh_dentry
) {
337 error
= nfsd_set_fh_dentry(rqstp
, fhp
);
341 dentry
= fhp
->fh_dentry
;
342 exp
= fhp
->fh_export
;
344 * We still have to do all these permission checks, even when
345 * fh_dentry is already set:
346 * - fh_verify may be called multiple times with different
347 * "access" arguments (e.g. nfsd_proc_create calls
348 * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
349 * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
350 * - in the NFSv4 case, the filehandle may have been filled
351 * in by fh_compose, and given a dentry, but further
352 * compound operations performed with that filehandle
353 * still need permissions checks. In the worst case, a
354 * mountpoint crossing may have changed the export
355 * options, and we may now need to use a different uid
356 * (for example, if different id-squashing options are in
357 * effect on the new filesystem).
359 error
= check_pseudo_root(rqstp
, dentry
, exp
);
363 error
= nfsd_setuser_and_check_port(rqstp
, exp
);
367 error
= nfsd_mode_check(rqstp
, dentry
, type
);
372 * pseudoflavor restrictions are not enforced on NLM,
373 * which clients virtually always use auth_sys for,
374 * even while using RPCSEC_GSS for NFS.
376 if (access
& NFSD_MAY_LOCK
|| access
& NFSD_MAY_BYPASS_GSS
)
377 goto skip_pseudoflavor_check
;
379 * Clients may expect to be able to use auth_sys during mount,
380 * even if they use gss for everything else; see section 2.3.2
383 if (access
& NFSD_MAY_BYPASS_GSS_ON_ROOT
384 && exp
->ex_path
.dentry
== dentry
)
385 goto skip_pseudoflavor_check
;
387 error
= check_nfsd_access(exp
, rqstp
);
391 skip_pseudoflavor_check
:
392 /* Finally, check access permissions. */
393 error
= nfsd_permission(rqstp
, exp
, dentry
, access
);
396 dprintk("fh_verify: %pd2 permission failure, "
397 "acc=%x, error=%d\n",
399 access
, ntohl(error
));
402 if (error
== nfserr_stale
)
403 nfsdstats
.fh_stale
++;
409 * Compose a file handle for an NFS reply.
411 * Note that when first composed, the dentry may not yet have
412 * an inode. In this case a call to fh_update should be made
413 * before the fh goes out on the wire ...
415 static void _fh_update(struct svc_fh
*fhp
, struct svc_export
*exp
,
416 struct dentry
*dentry
)
418 if (dentry
!= exp
->ex_path
.dentry
) {
419 struct fid
*fid
= (struct fid
*)
420 (fhp
->fh_handle
.fh_fsid
+ fhp
->fh_handle
.fh_size
/4 - 1);
421 int maxsize
= (fhp
->fh_maxsize
- fhp
->fh_handle
.fh_size
)/4;
422 int subtreecheck
= !(exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
);
424 fhp
->fh_handle
.fh_fileid_type
=
425 exportfs_encode_fh(dentry
, fid
, &maxsize
, subtreecheck
);
426 fhp
->fh_handle
.fh_size
+= maxsize
* 4;
428 fhp
->fh_handle
.fh_fileid_type
= FILEID_ROOT
;
433 * for composing old style file handles
435 static inline void _fh_update_old(struct dentry
*dentry
,
436 struct svc_export
*exp
,
439 fh
->ofh_ino
= ino_t_to_u32(d_inode(dentry
)->i_ino
);
440 fh
->ofh_generation
= d_inode(dentry
)->i_generation
;
441 if (d_is_dir(dentry
) ||
442 (exp
->ex_flags
& NFSEXP_NOSUBTREECHECK
))
446 static bool is_root_export(struct svc_export
*exp
)
448 return exp
->ex_path
.dentry
== exp
->ex_path
.dentry
->d_sb
->s_root
;
451 static struct super_block
*exp_sb(struct svc_export
*exp
)
453 return exp
->ex_path
.dentry
->d_sb
;
456 static bool fsid_type_ok_for_exp(u8 fsid_type
, struct svc_export
*exp
)
460 if (!old_valid_dev(exp_sb(exp
)->s_dev
))
463 case FSID_MAJOR_MINOR
:
464 case FSID_ENCODE_DEV
:
465 return exp_sb(exp
)->s_type
->fs_flags
& FS_REQUIRES_DEV
;
467 return exp
->ex_flags
& NFSEXP_FSID
;
470 if (!is_root_export(exp
))
473 case FSID_UUID4_INUM
:
474 case FSID_UUID16_INUM
:
475 return exp
->ex_uuid
!= NULL
;
481 static void set_version_and_fsid_type(struct svc_fh
*fhp
, struct svc_export
*exp
, struct svc_fh
*ref_fh
)
487 if (ref_fh
&& ref_fh
->fh_export
== exp
) {
488 version
= ref_fh
->fh_handle
.fh_version
;
489 fsid_type
= ref_fh
->fh_handle
.fh_fsid_type
;
495 fsid_type
= FSID_DEV
;
504 * As the fsid -> filesystem mapping was guided by
505 * user-space, there is no guarantee that the filesystem
506 * actually supports that fsid type. If it doesn't we
507 * loop around again without ref_fh set.
509 if (!fsid_type_ok_for_exp(fsid_type
, exp
))
511 } else if (exp
->ex_flags
& NFSEXP_FSID
) {
512 fsid_type
= FSID_NUM
;
513 } else if (exp
->ex_uuid
) {
514 if (fhp
->fh_maxsize
>= 64) {
515 if (is_root_export(exp
))
516 fsid_type
= FSID_UUID16
;
518 fsid_type
= FSID_UUID16_INUM
;
520 if (is_root_export(exp
))
521 fsid_type
= FSID_UUID8
;
523 fsid_type
= FSID_UUID4_INUM
;
525 } else if (!old_valid_dev(exp_sb(exp
)->s_dev
))
526 /* for newer device numbers, we must use a newer fsid format */
527 fsid_type
= FSID_ENCODE_DEV
;
529 fsid_type
= FSID_DEV
;
530 fhp
->fh_handle
.fh_version
= version
;
532 fhp
->fh_handle
.fh_fsid_type
= fsid_type
;
536 fh_compose(struct svc_fh
*fhp
, struct svc_export
*exp
, struct dentry
*dentry
,
537 struct svc_fh
*ref_fh
)
539 /* ref_fh is a reference file handle.
540 * if it is non-null and for the same filesystem, then we should compose
541 * a filehandle which is of the same version, where possible.
542 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
543 * Then create a 32byte filehandle using nfs_fhbase_old
547 struct inode
* inode
= d_inode(dentry
);
548 dev_t ex_dev
= exp_sb(exp
)->s_dev
;
550 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
551 MAJOR(ex_dev
), MINOR(ex_dev
),
552 (long) d_inode(exp
->ex_path
.dentry
)->i_ino
,
554 (inode
? inode
->i_ino
: 0));
556 /* Choose filehandle version and fsid type based on
557 * the reference filehandle (if it is in the same export)
558 * or the export options.
560 set_version_and_fsid_type(fhp
, exp
, ref_fh
);
565 if (fhp
->fh_locked
|| fhp
->fh_dentry
) {
566 printk(KERN_ERR
"fh_compose: fh %pd2 not initialized!\n",
569 if (fhp
->fh_maxsize
< NFS_FHSIZE
)
570 printk(KERN_ERR
"fh_compose: called with maxsize %d! %pd2\n",
574 fhp
->fh_dentry
= dget(dentry
); /* our internal copy */
575 fhp
->fh_export
= exp_get(exp
);
577 if (fhp
->fh_handle
.fh_version
== 0xca) {
578 /* old style filehandle please */
579 memset(&fhp
->fh_handle
.fh_base
, 0, NFS_FHSIZE
);
580 fhp
->fh_handle
.fh_size
= NFS_FHSIZE
;
581 fhp
->fh_handle
.ofh_dcookie
= 0xfeebbaca;
582 fhp
->fh_handle
.ofh_dev
= old_encode_dev(ex_dev
);
583 fhp
->fh_handle
.ofh_xdev
= fhp
->fh_handle
.ofh_dev
;
584 fhp
->fh_handle
.ofh_xino
=
585 ino_t_to_u32(d_inode(exp
->ex_path
.dentry
)->i_ino
);
586 fhp
->fh_handle
.ofh_dirino
= ino_t_to_u32(parent_ino(dentry
));
588 _fh_update_old(dentry
, exp
, &fhp
->fh_handle
);
590 fhp
->fh_handle
.fh_size
=
591 key_len(fhp
->fh_handle
.fh_fsid_type
) + 4;
592 fhp
->fh_handle
.fh_auth_type
= 0;
594 mk_fsid(fhp
->fh_handle
.fh_fsid_type
,
595 fhp
->fh_handle
.fh_fsid
,
597 d_inode(exp
->ex_path
.dentry
)->i_ino
,
598 exp
->ex_fsid
, exp
->ex_uuid
);
601 _fh_update(fhp
, exp
, dentry
);
602 if (fhp
->fh_handle
.fh_fileid_type
== FILEID_INVALID
) {
604 return nfserr_opnotsupp
;
612 * Update file handle information after changing a dentry.
613 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
616 fh_update(struct svc_fh
*fhp
)
618 struct dentry
*dentry
;
623 dentry
= fhp
->fh_dentry
;
624 if (d_really_is_negative(dentry
))
626 if (fhp
->fh_handle
.fh_version
!= 1) {
627 _fh_update_old(dentry
, fhp
->fh_export
, &fhp
->fh_handle
);
629 if (fhp
->fh_handle
.fh_fileid_type
!= FILEID_ROOT
)
632 _fh_update(fhp
, fhp
->fh_export
, dentry
);
633 if (fhp
->fh_handle
.fh_fileid_type
== FILEID_INVALID
)
634 return nfserr_opnotsupp
;
638 printk(KERN_ERR
"fh_update: fh not verified!\n");
639 return nfserr_serverfault
;
641 printk(KERN_ERR
"fh_update: %pd2 still negative!\n",
643 return nfserr_serverfault
;
647 * Release a file handle.
650 fh_put(struct svc_fh
*fhp
)
652 struct dentry
* dentry
= fhp
->fh_dentry
;
653 struct svc_export
* exp
= fhp
->fh_export
;
656 fhp
->fh_dentry
= NULL
;
663 fhp
->fh_export
= NULL
;
669 * Shorthand for dprintk()'s
671 char * SVCFH_fmt(struct svc_fh
*fhp
)
673 struct knfsd_fh
*fh
= &fhp
->fh_handle
;
676 sprintf(buf
, "%d: %08x %08x %08x %08x %08x %08x",
678 fh
->fh_base
.fh_pad
[0],
679 fh
->fh_base
.fh_pad
[1],
680 fh
->fh_base
.fh_pad
[2],
681 fh
->fh_base
.fh_pad
[3],
682 fh
->fh_base
.fh_pad
[4],
683 fh
->fh_base
.fh_pad
[5]);
687 enum fsid_source
fsid_source(struct svc_fh
*fhp
)
689 if (fhp
->fh_handle
.fh_version
!= 1)
690 return FSIDSOURCE_DEV
;
691 switch(fhp
->fh_handle
.fh_fsid_type
) {
693 case FSID_ENCODE_DEV
:
694 case FSID_MAJOR_MINOR
:
695 if (exp_sb(fhp
->fh_export
)->s_type
->fs_flags
& FS_REQUIRES_DEV
)
696 return FSIDSOURCE_DEV
;
699 if (fhp
->fh_export
->ex_flags
& NFSEXP_FSID
)
700 return FSIDSOURCE_FSID
;
705 /* either a UUID type filehandle, or the filehandle doesn't
708 if (fhp
->fh_export
->ex_flags
& NFSEXP_FSID
)
709 return FSIDSOURCE_FSID
;
710 if (fhp
->fh_export
->ex_uuid
)
711 return FSIDSOURCE_UUID
;
712 return FSIDSOURCE_DEV
;