blob: 34a10d78b839f4c73b3d851e19820bc712129f36 [file] [log] [blame]
J. Bruce Fields7663dac2009-12-04 19:49:00 -05001/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/sched.h>
Eric W. Biedermanae2975b2011-11-14 15:56:38 -08004#include <linux/user_namespace.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +02005#include "nfsd.h"
Harvey Harrisona254b242008-02-20 12:49:00 -08006#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07008int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -07009{
10 struct exp_flavor_info *f;
11 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
12
13 for (f = exp->ex_flavors; f < end; f++) {
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -040014 if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070015 return f->flags;
16 }
17 return exp->ex_flags;
18
19}
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
22{
David Howellsd84f4f92008-11-14 10:39:23 +110023 struct group_info *rqgi;
24 struct group_info *gi;
25 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070027 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 int ret;
29
David Howellse0e81732009-09-02 09:13:40 +010030 validate_process_creds();
31
David Howells3b11a1d2008-11-14 10:39:26 +110032 /* discard any old override before preparing the new set */
33 revert_creds(get_cred(current->real_cred));
David Howellsd84f4f92008-11-14 10:39:23 +110034 new = prepare_creds();
35 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +110037
38 new->fsuid = rqstp->rq_cred.cr_uid;
39 new->fsgid = rqstp->rq_cred.cr_gid;
40
41 rqgi = rqstp->rq_cred.cr_group_info;
42
43 if (flags & NFSEXP_ALLSQUASH) {
44 new->fsuid = exp->ex_anon_uid;
45 new->fsgid = exp->ex_anon_gid;
46 gi = groups_alloc(0);
J. Bruce Fieldsbf935a72009-01-20 19:32:59 -050047 if (!gi)
48 goto oom;
David Howellsd84f4f92008-11-14 10:39:23 +110049 } else if (flags & NFSEXP_ROOTSQUASH) {
50 if (!new->fsuid)
51 new->fsuid = exp->ex_anon_uid;
52 if (!new->fsgid)
53 new->fsgid = exp->ex_anon_gid;
54
55 gi = groups_alloc(rqgi->ngroups);
56 if (!gi)
57 goto oom;
58
59 for (i = 0; i < rqgi->ngroups; i++) {
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080060 if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
61 GROUP_AT(gi, i) = make_kgid(&init_user_ns, exp->ex_anon_gid);
David Howellsd84f4f92008-11-14 10:39:23 +110062 else
63 GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 } else {
David Howellsd84f4f92008-11-14 10:39:23 +110066 gi = get_group_info(rqgi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
David Howellsd84f4f92008-11-14 10:39:23 +110068
69 if (new->fsuid == (uid_t) -1)
70 new->fsuid = exp->ex_anon_uid;
71 if (new->fsgid == (gid_t) -1)
72 new->fsgid = exp->ex_anon_gid;
73
74 ret = set_groups(new, gi);
75 put_group_info(gi);
David Howellsf05ef8d2009-01-05 17:19:37 +000076 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +110077 goto error;
78
David Howellsf05ef8d2009-01-05 17:19:37 +000079 if (new->fsuid)
David Howellsd84f4f92008-11-14 10:39:23 +110080 new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
81 else
82 new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
83 new->cap_permitted);
David Howellse0e81732009-09-02 09:13:40 +010084 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110085 put_cred(override_creds(new));
J. Bruce Fieldsb9141522009-01-20 19:34:22 -050086 put_cred(new);
David Howellse0e81732009-09-02 09:13:40 +010087 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110088 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +110089
90oom:
91 ret = -ENOMEM;
92error:
93 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return ret;
95}
David Howellsb6dff3e2008-11-14 10:39:16 +110096