0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/exportfs.h>
0012
0013 #include <linux/sunrpc/svcauth_gss.h>
0014 #include "nfsd.h"
0015 #include "vfs.h"
0016 #include "auth.h"
0017 #include "trace.h"
0018
0019 #define NFSDDBG_FACILITY NFSDDBG_FH
0020
0021
0022
0023
0024
0025
0026
0027
0028 static int nfsd_acceptable(void *expv, struct dentry *dentry)
0029 {
0030 struct svc_export *exp = expv;
0031 int rv;
0032 struct dentry *tdentry;
0033 struct dentry *parent;
0034
0035 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
0036 return 1;
0037
0038 tdentry = dget(dentry);
0039 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
0040
0041 int err;
0042 parent = dget_parent(tdentry);
0043 err = inode_permission(&init_user_ns,
0044 d_inode(parent), MAY_EXEC);
0045 if (err < 0) {
0046 dput(parent);
0047 break;
0048 }
0049 dput(tdentry);
0050 tdentry = parent;
0051 }
0052 if (tdentry != exp->ex_path.dentry)
0053 dprintk("nfsd_acceptable failed at %p %pd\n", tdentry, tdentry);
0054 rv = (tdentry == exp->ex_path.dentry);
0055 dput(tdentry);
0056 return rv;
0057 }
0058
0059
0060
0061
0062
0063
0064 static inline __be32
0065 nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
0066 umode_t requested)
0067 {
0068 umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
0069
0070 if (requested == 0)
0071 return nfs_ok;
0072 if (mode == requested) {
0073 if (mode == S_IFDIR && !d_can_lookup(dentry)) {
0074 WARN_ON_ONCE(1);
0075 return nfserr_notdir;
0076 }
0077 return nfs_ok;
0078 }
0079
0080
0081
0082
0083 if (rqstp->rq_vers == 4 && mode == S_IFLNK)
0084 return nfserr_symlink;
0085 if (requested == S_IFDIR)
0086 return nfserr_notdir;
0087 if (mode == S_IFDIR)
0088 return nfserr_isdir;
0089 return nfserr_inval;
0090 }
0091
0092 static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
0093 {
0094 if (flags & NFSEXP_INSECURE_PORT)
0095 return true;
0096
0097 if (rqstp->rq_cred.cr_flavor >= RPC_AUTH_GSS)
0098 return true;
0099 return test_bit(RQ_SECURE, &rqstp->rq_flags);
0100 }
0101
0102 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
0103 struct svc_export *exp)
0104 {
0105 int flags = nfsexp_flags(rqstp, exp);
0106
0107
0108 if (!nfsd_originating_port_ok(rqstp, flags)) {
0109 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
0110 dprintk("nfsd: request from insecure port %s!\n",
0111 svc_print_addr(rqstp, buf, sizeof(buf)));
0112 return nfserr_perm;
0113 }
0114
0115
0116 return nfserrno(nfsd_setuser(rqstp, exp));
0117 }
0118
0119 static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
0120 struct dentry *dentry, struct svc_export *exp)
0121 {
0122 if (!(exp->ex_flags & NFSEXP_V4ROOT))
0123 return nfs_ok;
0124
0125
0126
0127
0128
0129
0130 if (!nfsd_v4client(rqstp))
0131 return nfserr_stale;
0132
0133
0134
0135
0136 if (unlikely(!d_is_dir(dentry) &&
0137 !d_is_symlink(dentry)))
0138 return nfserr_stale;
0139
0140
0141
0142
0143
0144 if (unlikely(dentry != exp->ex_path.dentry))
0145 return nfserr_stale;
0146 return nfs_ok;
0147 }
0148
0149
0150
0151
0152
0153
0154 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
0155 {
0156 struct knfsd_fh *fh = &fhp->fh_handle;
0157 struct fid *fid = NULL;
0158 struct svc_export *exp;
0159 struct dentry *dentry;
0160 int fileid_type;
0161 int data_left = fh->fh_size/4;
0162 int len;
0163 __be32 error;
0164
0165 error = nfserr_stale;
0166 if (rqstp->rq_vers > 2)
0167 error = nfserr_badhandle;
0168 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
0169 return nfserr_nofilehandle;
0170
0171 if (fh->fh_version != 1)
0172 return error;
0173
0174 if (--data_left < 0)
0175 return error;
0176 if (fh->fh_auth_type != 0)
0177 return error;
0178 len = key_len(fh->fh_fsid_type) / 4;
0179 if (len == 0)
0180 return error;
0181 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
0182
0183 len = key_len(FSID_ENCODE_DEV)/4;
0184 fh->fh_fsid_type = FSID_ENCODE_DEV;
0185
0186
0187
0188
0189
0190
0191 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
0192 ntohl((__force __be32)fh->fh_fsid[1])));
0193 fh->fh_fsid[1] = fh->fh_fsid[2];
0194 }
0195 data_left -= len;
0196 if (data_left < 0)
0197 return error;
0198 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_fsid);
0199 fid = (struct fid *)(fh->fh_fsid + len);
0200
0201 error = nfserr_stale;
0202 if (IS_ERR(exp)) {
0203 trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
0204
0205 if (PTR_ERR(exp) == -ENOENT)
0206 return error;
0207
0208 return nfserrno(PTR_ERR(exp));
0209 }
0210
0211 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221 struct cred *new = prepare_creds();
0222 if (!new) {
0223 error = nfserrno(-ENOMEM);
0224 goto out;
0225 }
0226 new->cap_effective =
0227 cap_raise_nfsd_set(new->cap_effective,
0228 new->cap_permitted);
0229 put_cred(override_creds(new));
0230 put_cred(new);
0231 } else {
0232 error = nfsd_setuser_and_check_port(rqstp, exp);
0233 if (error)
0234 goto out;
0235 }
0236
0237
0238
0239
0240 error = nfserr_stale;
0241 if (rqstp->rq_vers > 2)
0242 error = nfserr_badhandle;
0243
0244 fileid_type = fh->fh_fileid_type;
0245
0246 if (fileid_type == FILEID_ROOT)
0247 dentry = dget(exp->ex_path.dentry);
0248 else {
0249 dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
0250 data_left, fileid_type,
0251 nfsd_acceptable, exp);
0252 if (IS_ERR_OR_NULL(dentry)) {
0253 trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
0254 dentry ? PTR_ERR(dentry) : -ESTALE);
0255 switch (PTR_ERR(dentry)) {
0256 case -ENOMEM:
0257 case -ETIMEDOUT:
0258 break;
0259 default:
0260 dentry = ERR_PTR(-ESTALE);
0261 }
0262 }
0263 }
0264 if (dentry == NULL)
0265 goto out;
0266 if (IS_ERR(dentry)) {
0267 if (PTR_ERR(dentry) != -EINVAL)
0268 error = nfserrno(PTR_ERR(dentry));
0269 goto out;
0270 }
0271
0272 if (d_is_dir(dentry) &&
0273 (dentry->d_flags & DCACHE_DISCONNECTED)) {
0274 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
0275 dentry);
0276 }
0277
0278 fhp->fh_dentry = dentry;
0279 fhp->fh_export = exp;
0280
0281 switch (rqstp->rq_vers) {
0282 case 4:
0283 if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
0284 fhp->fh_no_atomic_attr = true;
0285 break;
0286 case 3:
0287 if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
0288 fhp->fh_no_wcc = true;
0289 break;
0290 case 2:
0291 fhp->fh_no_wcc = true;
0292 }
0293
0294 return 0;
0295 out:
0296 exp_put(exp);
0297 return error;
0298 }
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 __be32
0328 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
0329 {
0330 struct svc_export *exp = NULL;
0331 struct dentry *dentry;
0332 __be32 error;
0333
0334 if (!fhp->fh_dentry) {
0335 error = nfsd_set_fh_dentry(rqstp, fhp);
0336 if (error)
0337 goto out;
0338 }
0339 dentry = fhp->fh_dentry;
0340 exp = fhp->fh_export;
0341
0342 trace_nfsd_fh_verify(rqstp, fhp, type, access);
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 error = check_pseudo_root(rqstp, dentry, exp);
0361 if (error)
0362 goto out;
0363
0364 error = nfsd_setuser_and_check_port(rqstp, exp);
0365 if (error)
0366 goto out;
0367
0368 error = nfsd_mode_check(rqstp, dentry, type);
0369 if (error)
0370 goto out;
0371
0372
0373
0374
0375
0376
0377 if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
0378 goto skip_pseudoflavor_check;
0379
0380
0381
0382
0383
0384 if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
0385 && exp->ex_path.dentry == dentry)
0386 goto skip_pseudoflavor_check;
0387
0388 error = check_nfsd_access(exp, rqstp);
0389 if (error)
0390 goto out;
0391
0392 skip_pseudoflavor_check:
0393
0394 error = nfsd_permission(rqstp, exp, dentry, access);
0395
0396 if (error) {
0397 dprintk("fh_verify: %pd2 permission failure, "
0398 "acc=%x, error=%d\n",
0399 dentry,
0400 access, ntohl(error));
0401 }
0402 out:
0403 if (error == nfserr_stale)
0404 nfsd_stats_fh_stale_inc(exp);
0405 return error;
0406 }
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
0417 struct dentry *dentry)
0418 {
0419 if (dentry != exp->ex_path.dentry) {
0420 struct fid *fid = (struct fid *)
0421 (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
0422 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
0423 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
0424
0425 fhp->fh_handle.fh_fileid_type =
0426 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
0427 fhp->fh_handle.fh_size += maxsize * 4;
0428 } else {
0429 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
0430 }
0431 }
0432
0433 static bool is_root_export(struct svc_export *exp)
0434 {
0435 return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
0436 }
0437
0438 static struct super_block *exp_sb(struct svc_export *exp)
0439 {
0440 return exp->ex_path.dentry->d_sb;
0441 }
0442
0443 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
0444 {
0445 switch (fsid_type) {
0446 case FSID_DEV:
0447 if (!old_valid_dev(exp_sb(exp)->s_dev))
0448 return false;
0449 fallthrough;
0450 case FSID_MAJOR_MINOR:
0451 case FSID_ENCODE_DEV:
0452 return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
0453 case FSID_NUM:
0454 return exp->ex_flags & NFSEXP_FSID;
0455 case FSID_UUID8:
0456 case FSID_UUID16:
0457 if (!is_root_export(exp))
0458 return false;
0459 fallthrough;
0460 case FSID_UUID4_INUM:
0461 case FSID_UUID16_INUM:
0462 return exp->ex_uuid != NULL;
0463 }
0464 return true;
0465 }
0466
0467
0468 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
0469 {
0470 u8 version;
0471 u8 fsid_type;
0472 retry:
0473 version = 1;
0474 if (ref_fh && ref_fh->fh_export == exp) {
0475 version = ref_fh->fh_handle.fh_version;
0476 fsid_type = ref_fh->fh_handle.fh_fsid_type;
0477
0478 ref_fh = NULL;
0479
0480 switch (version) {
0481 case 0xca:
0482 fsid_type = FSID_DEV;
0483 break;
0484 case 1:
0485 break;
0486 default:
0487 goto retry;
0488 }
0489
0490
0491
0492
0493
0494
0495
0496 if (!fsid_type_ok_for_exp(fsid_type, exp))
0497 goto retry;
0498 } else if (exp->ex_flags & NFSEXP_FSID) {
0499 fsid_type = FSID_NUM;
0500 } else if (exp->ex_uuid) {
0501 if (fhp->fh_maxsize >= 64) {
0502 if (is_root_export(exp))
0503 fsid_type = FSID_UUID16;
0504 else
0505 fsid_type = FSID_UUID16_INUM;
0506 } else {
0507 if (is_root_export(exp))
0508 fsid_type = FSID_UUID8;
0509 else
0510 fsid_type = FSID_UUID4_INUM;
0511 }
0512 } else if (!old_valid_dev(exp_sb(exp)->s_dev))
0513
0514 fsid_type = FSID_ENCODE_DEV;
0515 else
0516 fsid_type = FSID_DEV;
0517 fhp->fh_handle.fh_version = version;
0518 if (version)
0519 fhp->fh_handle.fh_fsid_type = fsid_type;
0520 }
0521
0522 __be32
0523 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
0524 struct svc_fh *ref_fh)
0525 {
0526
0527
0528
0529
0530
0531 struct inode * inode = d_inode(dentry);
0532 dev_t ex_dev = exp_sb(exp)->s_dev;
0533
0534 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
0535 MAJOR(ex_dev), MINOR(ex_dev),
0536 (long) d_inode(exp->ex_path.dentry)->i_ino,
0537 dentry,
0538 (inode ? inode->i_ino : 0));
0539
0540
0541
0542
0543
0544 set_version_and_fsid_type(fhp, exp, ref_fh);
0545
0546
0547 fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
0548
0549 if (ref_fh == fhp)
0550 fh_put(ref_fh);
0551
0552 if (fhp->fh_dentry) {
0553 printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
0554 dentry);
0555 }
0556 if (fhp->fh_maxsize < NFS_FHSIZE)
0557 printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
0558 fhp->fh_maxsize,
0559 dentry);
0560
0561 fhp->fh_dentry = dget(dentry);
0562 fhp->fh_export = exp_get(exp);
0563
0564 fhp->fh_handle.fh_size =
0565 key_len(fhp->fh_handle.fh_fsid_type) + 4;
0566 fhp->fh_handle.fh_auth_type = 0;
0567
0568 mk_fsid(fhp->fh_handle.fh_fsid_type,
0569 fhp->fh_handle.fh_fsid,
0570 ex_dev,
0571 d_inode(exp->ex_path.dentry)->i_ino,
0572 exp->ex_fsid, exp->ex_uuid);
0573
0574 if (inode)
0575 _fh_update(fhp, exp, dentry);
0576 if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
0577 fh_put(fhp);
0578 return nfserr_opnotsupp;
0579 }
0580
0581 return 0;
0582 }
0583
0584
0585
0586
0587
0588 __be32
0589 fh_update(struct svc_fh *fhp)
0590 {
0591 struct dentry *dentry;
0592
0593 if (!fhp->fh_dentry)
0594 goto out_bad;
0595
0596 dentry = fhp->fh_dentry;
0597 if (d_really_is_negative(dentry))
0598 goto out_negative;
0599 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
0600 return 0;
0601
0602 _fh_update(fhp, fhp->fh_export, dentry);
0603 if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
0604 return nfserr_opnotsupp;
0605 return 0;
0606 out_bad:
0607 printk(KERN_ERR "fh_update: fh not verified!\n");
0608 return nfserr_serverfault;
0609 out_negative:
0610 printk(KERN_ERR "fh_update: %pd2 still negative!\n",
0611 dentry);
0612 return nfserr_serverfault;
0613 }
0614
0615
0616
0617
0618
0619
0620 void fh_fill_pre_attrs(struct svc_fh *fhp)
0621 {
0622 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
0623 struct inode *inode;
0624 struct kstat stat;
0625 __be32 err;
0626
0627 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
0628 return;
0629
0630 inode = d_inode(fhp->fh_dentry);
0631 err = fh_getattr(fhp, &stat);
0632 if (err) {
0633
0634 stat.mtime = inode->i_mtime;
0635 stat.ctime = inode->i_ctime;
0636 stat.size = inode->i_size;
0637 }
0638 if (v4)
0639 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
0640
0641 fhp->fh_pre_mtime = stat.mtime;
0642 fhp->fh_pre_ctime = stat.ctime;
0643 fhp->fh_pre_size = stat.size;
0644 fhp->fh_pre_saved = true;
0645 }
0646
0647
0648
0649
0650
0651
0652 void fh_fill_post_attrs(struct svc_fh *fhp)
0653 {
0654 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
0655 struct inode *inode = d_inode(fhp->fh_dentry);
0656 __be32 err;
0657
0658 if (fhp->fh_no_wcc)
0659 return;
0660
0661 if (fhp->fh_post_saved)
0662 printk("nfsd: inode locked twice during operation.\n");
0663
0664 err = fh_getattr(fhp, &fhp->fh_post_attr);
0665 if (err) {
0666 fhp->fh_post_saved = false;
0667 fhp->fh_post_attr.ctime = inode->i_ctime;
0668 } else
0669 fhp->fh_post_saved = true;
0670 if (v4)
0671 fhp->fh_post_change =
0672 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
0673 }
0674
0675
0676
0677
0678
0679
0680
0681
0682 void fh_fill_both_attrs(struct svc_fh *fhp)
0683 {
0684 fh_fill_post_attrs(fhp);
0685 if (!fhp->fh_post_saved)
0686 return;
0687 fhp->fh_pre_change = fhp->fh_post_change;
0688 fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
0689 fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
0690 fhp->fh_pre_size = fhp->fh_post_attr.size;
0691 fhp->fh_pre_saved = true;
0692 }
0693
0694
0695
0696
0697 void
0698 fh_put(struct svc_fh *fhp)
0699 {
0700 struct dentry * dentry = fhp->fh_dentry;
0701 struct svc_export * exp = fhp->fh_export;
0702 if (dentry) {
0703 fhp->fh_dentry = NULL;
0704 dput(dentry);
0705 fh_clear_pre_post_attrs(fhp);
0706 }
0707 fh_drop_write(fhp);
0708 if (exp) {
0709 exp_put(exp);
0710 fhp->fh_export = NULL;
0711 }
0712 fhp->fh_no_wcc = false;
0713 return;
0714 }
0715
0716
0717
0718
0719 char * SVCFH_fmt(struct svc_fh *fhp)
0720 {
0721 struct knfsd_fh *fh = &fhp->fh_handle;
0722 static char buf[2+1+1+64*3+1];
0723
0724 if (fh->fh_size < 0 || fh->fh_size> 64)
0725 return "bad-fh";
0726 sprintf(buf, "%d: %*ph", fh->fh_size, fh->fh_size, fh->fh_raw);
0727 return buf;
0728 }
0729
0730 enum fsid_source fsid_source(const struct svc_fh *fhp)
0731 {
0732 if (fhp->fh_handle.fh_version != 1)
0733 return FSIDSOURCE_DEV;
0734 switch(fhp->fh_handle.fh_fsid_type) {
0735 case FSID_DEV:
0736 case FSID_ENCODE_DEV:
0737 case FSID_MAJOR_MINOR:
0738 if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
0739 return FSIDSOURCE_DEV;
0740 break;
0741 case FSID_NUM:
0742 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
0743 return FSIDSOURCE_FSID;
0744 break;
0745 default:
0746 break;
0747 }
0748
0749
0750
0751 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
0752 return FSIDSOURCE_FSID;
0753 if (fhp->fh_export->ex_uuid)
0754 return FSIDSOURCE_UUID;
0755 return FSIDSOURCE_DEV;
0756 }