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> |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 4 | #include <linux/user_namespace.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 5 | #include "nfsd.h" |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 6 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 8 | int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 9 | { |
| 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 Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 14 | if (f->pseudoflavor == rqstp->rq_cred.cr_flavor) |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 15 | return f->flags; |
| 16 | } |
| 17 | return exp->ex_flags; |
| 18 | |
| 19 | } |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp) |
| 22 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 23 | struct group_info *rqgi; |
| 24 | struct group_info *gi; |
| 25 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | int i; |
J. Bruce Fields | 1269bc6 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 27 | int flags = nfsexp_flags(rqstp, exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | int ret; |
| 29 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 30 | validate_process_creds(); |
| 31 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 32 | /* discard any old override before preparing the new set */ |
| 33 | revert_creds(get_cred(current->real_cred)); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 34 | new = prepare_creds(); |
| 35 | if (!new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | return -ENOMEM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 37 | |
| 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 Fields | bf935a7 | 2009-01-20 19:32:59 -0500 | [diff] [blame] | 47 | if (!gi) |
| 48 | goto oom; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 49 | } 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. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 60 | 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 Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 62 | else |
| 63 | GROUP_AT(gi, i) = GROUP_AT(rqgi, i); |
| 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } else { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 66 | gi = get_group_info(rqgi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 68 | |
| 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 Howells | f05ef8d | 2009-01-05 17:19:37 +0000 | [diff] [blame] | 76 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 77 | goto error; |
| 78 | |
David Howells | f05ef8d | 2009-01-05 17:19:37 +0000 | [diff] [blame] | 79 | if (new->fsuid) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 80 | 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 Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 84 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 85 | put_cred(override_creds(new)); |
J. Bruce Fields | b914152 | 2009-01-20 19:34:22 -0500 | [diff] [blame] | 86 | put_cred(new); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 87 | validate_process_creds(); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 88 | return 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 89 | |
| 90 | oom: |
| 91 | ret = -ENOMEM; |
| 92 | error: |
| 93 | abort_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return ret; |
| 95 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 96 | |