blob: 62469c60be23263f21903fadc2217488c7160ed6 [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +02004#include "nfsd.h"
Harvey Harrisona254b242008-02-20 12:49:00 -08005#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07007int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fields1269bc62007-07-17 04:04:52 -07008{
9 struct exp_flavor_info *f;
10 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
11
12 for (f = exp->ex_flavors; f < end; f++) {
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -040013 if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
J. Bruce Fields1269bc62007-07-17 04:04:52 -070014 return f->flags;
15 }
16 return exp->ex_flags;
17
18}
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
21{
David Howellsd84f4f92008-11-14 10:39:23 +110022 struct group_info *rqgi;
23 struct group_info *gi;
24 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 int i;
J. Bruce Fields1269bc62007-07-17 04:04:52 -070026 int flags = nfsexp_flags(rqstp, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howellse0e81732009-09-02 09:13:40 +010028 validate_process_creds();
29
David Howells3b11a1d2008-11-14 10:39:26 +110030 /* discard any old override before preparing the new set */
Jeff Laytonae4b8842014-07-15 12:59:36 -040031 revert_creds(get_cred(current_real_cred()));
David Howellsd84f4f92008-11-14 10:39:23 +110032 new = prepare_creds();
33 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +110035
36 new->fsuid = rqstp->rq_cred.cr_uid;
37 new->fsgid = rqstp->rq_cred.cr_gid;
38
39 rqgi = rqstp->rq_cred.cr_group_info;
40
41 if (flags & NFSEXP_ALLSQUASH) {
42 new->fsuid = exp->ex_anon_uid;
43 new->fsgid = exp->ex_anon_gid;
44 gi = groups_alloc(0);
J. Bruce Fieldsbf935a72009-01-20 19:32:59 -050045 if (!gi)
46 goto oom;
David Howellsd84f4f92008-11-14 10:39:23 +110047 } else if (flags & NFSEXP_ROOTSQUASH) {
Eric W. Biederman6fab8772013-02-02 06:53:11 -080048 if (uid_eq(new->fsuid, GLOBAL_ROOT_UID))
David Howellsd84f4f92008-11-14 10:39:23 +110049 new->fsuid = exp->ex_anon_uid;
Eric W. Biederman6fab8772013-02-02 06:53:11 -080050 if (gid_eq(new->fsgid, GLOBAL_ROOT_GID))
David Howellsd84f4f92008-11-14 10:39:23 +110051 new->fsgid = exp->ex_anon_gid;
52
53 gi = groups_alloc(rqgi->ngroups);
54 if (!gi)
55 goto oom;
56
57 for (i = 0; i < rqgi->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070058 if (gid_eq(GLOBAL_ROOT_GID, rqgi->gid[i]))
59 gi->gid[i] = exp->ex_anon_gid;
David Howellsd84f4f92008-11-14 10:39:23 +110060 else
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070061 gi->gid[i] = rqgi->gid[i];
David Howellsd84f4f92008-11-14 10:39:23 +110062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 } else {
David Howellsd84f4f92008-11-14 10:39:23 +110064 gi = get_group_info(rqgi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
David Howellsd84f4f92008-11-14 10:39:23 +110066
Eric W. Biederman6fab8772013-02-02 06:53:11 -080067 if (uid_eq(new->fsuid, INVALID_UID))
David Howellsd84f4f92008-11-14 10:39:23 +110068 new->fsuid = exp->ex_anon_uid;
Eric W. Biederman6fab8772013-02-02 06:53:11 -080069 if (gid_eq(new->fsgid, INVALID_GID))
David Howellsd84f4f92008-11-14 10:39:23 +110070 new->fsgid = exp->ex_anon_gid;
71
Wang YanQing8f6c5ff2014-04-03 14:48:26 -070072 set_groups(new, gi);
David Howellsd84f4f92008-11-14 10:39:23 +110073 put_group_info(gi);
David Howellsd84f4f92008-11-14 10:39:23 +110074
Eric W. Biederman6fab8772013-02-02 06:53:11 -080075 if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
David Howellsd84f4f92008-11-14 10:39:23 +110076 new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
77 else
78 new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
79 new->cap_permitted);
David Howellse0e81732009-09-02 09:13:40 +010080 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110081 put_cred(override_creds(new));
J. Bruce Fieldsb9141522009-01-20 19:34:22 -050082 put_cred(new);
David Howellse0e81732009-09-02 09:13:40 +010083 validate_process_creds();
David Howells3b11a1d2008-11-14 10:39:26 +110084 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +110085
86oom:
David Howellsd84f4f92008-11-14 10:39:23 +110087 abort_creds(new);
Kinglong Mee61a27f02014-05-23 20:53:44 +080088 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
David Howellsb6dff3e2008-11-14 10:39:16 +110090