blob: f650e475d8f0d84af1bb3013b6b35ef77421cde2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
J. Bruce Fields7663dac2009-12-04 19:49:00 -05002/* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/sched.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
David Howellse0e81732009-09-02 09:13:40 +010029 validate_process_creds();
30
David Howells3b11a1d2008-11-14 10:39:26 +110031 /* discard any old override before preparing the new set */
Jeff Laytonae4b8842014-07-15 12:59:36 -040032 revert_creds(get_cred(current_real_cred()));
David Howellsd84f4f92008-11-14 10:39:23 +110033 new = prepare_creds();
34 if (!new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return -ENOMEM;
David Howellsd84f4f92008-11-14 10:39:23 +110036
37 new->fsuid = rqstp->rq_cred.cr_uid;
38 new->fsgid = rqstp->rq_cred.cr_gid;
39
40 rqgi = rqstp->rq_cred.cr_group_info;
41
42 if (flags & NFSEXP_ALLSQUASH) {
43 new->fsuid = exp->ex_anon_uid;
44 new->fsgid = exp->ex_anon_gid;
45 gi = groups_alloc(0);
J. Bruce Fieldsbf935a72009-01-20 19:32:59 -050046 if (!gi)
47 goto oom;
David Howellsd84f4f92008-11-14 10:39:23 +110048 } else if (flags & NFSEXP_ROOTSQUASH) {
Eric W. Biederman6fab8772013-02-02 06:53:11 -080049 if (uid_eq(new->fsuid, GLOBAL_ROOT_UID))
David Howellsd84f4f92008-11-14 10:39:23 +110050 new->fsuid = exp->ex_anon_uid;
Eric W. Biederman6fab8772013-02-02 06:53:11 -080051 if (gid_eq(new->fsgid, GLOBAL_ROOT_GID))
David Howellsd84f4f92008-11-14 10:39:23 +110052 new->fsgid = exp->ex_anon_gid;
53
54 gi = groups_alloc(rqgi->ngroups);
55 if (!gi)
56 goto oom;
57
58 for (i = 0; i < rqgi->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070059 if (gid_eq(GLOBAL_ROOT_GID, rqgi->gid[i]))
60 gi->gid[i] = exp->ex_anon_gid;
David Howellsd84f4f92008-11-14 10:39:23 +110061 else
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070062 gi->gid[i] = rqgi->gid[i];
Thiago Rafael Beckerbdcf0a42017-12-14 15:33:12 -080063
64 /* Each thread allocates its own gi, no race */
65 groups_sort(gi);
David Howellsd84f4f92008-11-14 10:39:23 +110066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 } else {
David Howellsd84f4f92008-11-14 10:39:23 +110068 gi = get_group_info(rqgi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
David Howellsd84f4f92008-11-14 10:39:23 +110070
Eric W. Biederman6fab8772013-02-02 06:53:11 -080071 if (uid_eq(new->fsuid, INVALID_UID))
David Howellsd84f4f92008-11-14 10:39:23 +110072 new->fsuid = exp->ex_anon_uid;
Eric W. Biederman6fab8772013-02-02 06:53:11 -080073 if (gid_eq(new->fsgid, INVALID_GID))
David Howellsd84f4f92008-11-14 10:39:23 +110074 new->fsgid = exp->ex_anon_gid;
75
Wang YanQing8f6c5ff2014-04-03 14:48:26 -070076 set_groups(new, gi);
David Howellsd84f4f92008-11-14 10:39:23 +110077 put_group_info(gi);
David Howellsd84f4f92008-11-14 10:39:23 +110078
Eric W. Biederman6fab8772013-02-02 06:53:11 -080079 if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
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:
David Howellsd84f4f92008-11-14 10:39:23 +110091 abort_creds(new);
Kinglong Mee61a27f02014-05-23 20:53:44 +080092 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
David Howellsb6dff3e2008-11-14 10:39:16 +110094