J. Bruce Fields | 7663dac | 2009-12-04 19:49:00 -0500 | [diff] [blame] | 1 | /* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/sched.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 4 | #include "nfsd.h" |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 7 | int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 8 | { |
| 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 Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 13 | if (f->pseudoflavor == rqstp->rq_cred.cr_flavor) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 14 | return f->flags; |
| 15 | } |
| 16 | return exp->ex_flags; |
| 17 | |
| 18 | } |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) |
| 21 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 22 | struct group_info *rqgi; |
| 23 | struct group_info *gi; |
| 24 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | int i; |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 26 | int flags = nfsexp_flags(rqstp, exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 28 | validate_process_creds(); |
| 29 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 30 | /* discard any old override before preparing the new set */ |
Jeff Layton | ae4b884 | 2014-07-15 12:59:36 -0400 | [diff] [blame] | 31 | revert_creds(get_cred(current_real_cred())); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 32 | new = prepare_creds(); |
| 33 | if (!new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | return -ENOMEM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 35 | |
| 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 Fields | bf935a7 | 2009-01-20 19:32:59 -0500 | [diff] [blame] | 45 | if (!gi) |
| 46 | goto oom; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 47 | } else if (flags & NFSEXP_ROOTSQUASH) { |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 48 | if (uid_eq(new->fsuid, GLOBAL_ROOT_UID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 49 | new->fsuid = exp->ex_anon_uid; |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 50 | if (gid_eq(new->fsgid, GLOBAL_ROOT_GID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 51 | 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 Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 58 | if (gid_eq(GLOBAL_ROOT_GID, rqgi->gid[i])) |
| 59 | gi->gid[i] = exp->ex_anon_gid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 60 | else |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 61 | gi->gid[i] = rqgi->gid[i]; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } else { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 64 | gi = get_group_info(rqgi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 66 | |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 67 | if (uid_eq(new->fsuid, INVALID_UID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 68 | new->fsuid = exp->ex_anon_uid; |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 69 | if (gid_eq(new->fsgid, INVALID_GID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 70 | new->fsgid = exp->ex_anon_gid; |
| 71 | |
Wang YanQing | 8f6c5ff | 2014-04-03 14:48:26 -0700 | [diff] [blame] | 72 | set_groups(new, gi); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 73 | put_group_info(gi); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 74 | |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 75 | if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 76 | 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 Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 80 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 81 | put_cred(override_creds(new)); |
J. Bruce Fields | b914152 | 2009-01-20 19:34:22 -0500 | [diff] [blame] | 82 | put_cred(new); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 83 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 84 | return 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 85 | |
| 86 | oom: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 87 | abort_creds(new); |
Kinglong Mee | 61a27f0 | 2014-05-23 20:53:44 +0800 | [diff] [blame] | 88 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 90 | |