Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/net/sunrpc/auth_unix.c |
| 4 | * |
| 5 | * UNIX-style authentication; no AUTH_SHORT support |
| 6 | * |
| 7 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 8 | */ |
| 9 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/module.h> |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 14 | #include <linux/mempool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sunrpc/clnt.h> |
| 16 | #include <linux/sunrpc/auth.h> |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 17 | #include <linux/user_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 20 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 22 | #endif |
| 23 | |
| 24 | static struct rpc_auth unix_auth; |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 25 | static const struct rpc_credops unix_credops; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 26 | static mempool_t *unix_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | static struct rpc_auth * |
Sargun Dhillon | 82b98ca | 2018-07-05 16:48:50 +0000 | [diff] [blame] | 29 | unx_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 31 | dprintk("RPC: creating UNIX authenticator for client %p\n", |
| 32 | clnt); |
Trond Myklebust | 331bc71 | 2018-10-14 10:40:29 -0400 | [diff] [blame] | 33 | refcount_inc(&unix_auth.au_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | return &unix_auth; |
| 35 | } |
| 36 | |
| 37 | static void |
| 38 | unx_destroy(struct rpc_auth *auth) |
| 39 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 40 | dprintk("RPC: destroying UNIX authenticator %p\n", auth); |
Frank Sorenson | 1e035d0 | 2016-09-29 10:44:39 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Lookup AUTH_UNIX creds for current process |
| 45 | */ |
| 46 | static struct rpc_cred * |
| 47 | unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) |
| 48 | { |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 49 | struct rpc_cred *ret = mempool_alloc(unix_pool, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 51 | dprintk("RPC: allocating UNIX cred for uid %d gid %d\n", |
NeilBrown | 8276c90 | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 52 | from_kuid(&init_user_ns, acred->cred->fsuid), |
| 53 | from_kgid(&init_user_ns, acred->cred->fsgid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 55 | rpcauth_init_cred(ret, acred, auth, &unix_credops); |
| 56 | ret->cr_flags = 1UL << RPCAUTH_CRED_UPTODATE; |
| 57 | return ret; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 58 | } |
Trond Myklebust | 696e38d | 2007-06-25 09:48:25 -0400 | [diff] [blame] | 59 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 60 | static void |
| 61 | unx_free_cred_callback(struct rcu_head *head) |
| 62 | { |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 63 | struct rpc_cred *rpc_cred = container_of(head, struct rpc_cred, cr_rcu); |
| 64 | dprintk("RPC: unx_free_cred %p\n", rpc_cred); |
| 65 | put_cred(rpc_cred->cr_cred); |
| 66 | mempool_free(rpc_cred, unix_pool); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void |
| 70 | unx_destroy_cred(struct rpc_cred *cred) |
| 71 | { |
| 72 | call_rcu(&cred->cr_rcu, unx_free_cred_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 76 | * Match credentials against current the auth_cred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | */ |
| 78 | static int |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 79 | unx_match(struct auth_cred *acred, struct rpc_cred *cred, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 81 | unsigned int groups = 0; |
| 82 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 84 | if (cred->cr_cred == acred->cred) |
| 85 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 87 | if (!uid_eq(cred->cr_cred->fsuid, acred->cred->fsuid) || !gid_eq(cred->cr_cred->fsgid, acred->cred->fsgid)) |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 88 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
NeilBrown | fc0664f | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 90 | if (acred->cred && acred->cred->group_info != NULL) |
| 91 | groups = acred->cred->group_info->ngroups; |
Kinglong Mee | 5786461 | 2017-02-07 21:48:11 +0800 | [diff] [blame] | 92 | if (groups > UNX_NGROUPS) |
| 93 | groups = UNX_NGROUPS; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 94 | if (cred->cr_cred->group_info == NULL) |
| 95 | return groups == 0; |
| 96 | if (groups != cred->cr_cred->group_info->ngroups) |
NeilBrown | dc6f55e | 2011-10-25 10:25:49 +1100 | [diff] [blame] | 97 | return 0; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < groups ; i++) |
| 100 | if (!gid_eq(cred->cr_cred->group_info->gid[i], acred->cred->group_info->gid[i])) |
| 101 | return 0; |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 102 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Marshal credentials. |
| 107 | * Maybe we should keep a cached credential for performance reasons. |
| 108 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 109 | static __be32 * |
| 110 | unx_marshal(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | struct rpc_clnt *clnt = task->tk_client; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 113 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 114 | __be32 *base, *hold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | int i; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 116 | struct group_info *gi = cred->cr_cred->group_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | *p++ = htonl(RPC_AUTH_UNIX); |
| 119 | base = p++; |
| 120 | *p++ = htonl(jiffies/HZ); |
| 121 | |
| 122 | /* |
| 123 | * Copy the UTS nodename captured when the client was created. |
| 124 | */ |
| 125 | p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen); |
| 126 | |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 127 | *p++ = htonl((u32) from_kuid(&init_user_ns, cred->cr_cred->fsuid)); |
| 128 | *p++ = htonl((u32) from_kgid(&init_user_ns, cred->cr_cred->fsgid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | hold = p++; |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 130 | if (gi) |
| 131 | for (i = 0; i < UNX_NGROUPS && i < gi->ngroups; i++) |
| 132 | *p++ = htonl((u32) from_kgid(&init_user_ns, gi->gid[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | *hold = htonl(p - hold - 1); /* gid array length */ |
| 134 | *base = htonl((p - base - 1) << 2); /* cred length */ |
| 135 | |
| 136 | *p++ = htonl(RPC_AUTH_NULL); |
| 137 | *p++ = htonl(0); |
| 138 | |
| 139 | return p; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Refresh credentials. This is a no-op for AUTH_UNIX |
| 144 | */ |
| 145 | static int |
| 146 | unx_refresh(struct rpc_task *task) |
| 147 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 148 | set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_rqstp->rq_cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 152 | static __be32 * |
| 153 | unx_validate(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | rpc_authflavor_t flavor; |
| 156 | u32 size; |
| 157 | |
| 158 | flavor = ntohl(*p++); |
| 159 | if (flavor != RPC_AUTH_NULL && |
| 160 | flavor != RPC_AUTH_UNIX && |
| 161 | flavor != RPC_AUTH_SHORT) { |
| 162 | printk("RPC: bad verf flavor: %u\n", flavor); |
Andy Adamson | 35fa5f7 | 2013-08-14 11:59:17 -0400 | [diff] [blame] | 163 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | size = ntohl(*p++); |
| 167 | if (size > RPC_MAX_AUTH_SIZE) { |
| 168 | printk("RPC: giant verf size: %u\n", size); |
Andy Adamson | 35fa5f7 | 2013-08-14 11:59:17 -0400 | [diff] [blame] | 169 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 171 | task->tk_rqstp->rq_cred->cr_auth->au_rslack = (size >> 2) + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | p += (size >> 2); |
| 173 | |
| 174 | return p; |
| 175 | } |
| 176 | |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 177 | int __init rpc_init_authunix(void) |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 178 | { |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 179 | unix_pool = mempool_create_kmalloc_pool(16, sizeof(struct rpc_cred)); |
| 180 | return unix_pool ? 0 : -ENOMEM; |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void rpc_destroy_authunix(void) |
| 184 | { |
NeilBrown | 2edd8d7 | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 185 | mempool_destroy(unix_pool); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 188 | const struct rpc_authops authunix_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .owner = THIS_MODULE, |
| 190 | .au_flavor = RPC_AUTH_UNIX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | .au_name = "UNIX", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .create = unx_create, |
| 193 | .destroy = unx_destroy, |
| 194 | .lookup_cred = unx_lookup_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct rpc_auth unix_auth = { |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 199 | .au_cslack = UNX_CALLSLACK, |
| 200 | .au_rslack = NUL_REPLYSLACK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .au_ops = &authunix_ops, |
Trond Myklebust | 81039f1 | 2006-06-09 09:34:34 -0400 | [diff] [blame] | 202 | .au_flavor = RPC_AUTH_UNIX, |
Trond Myklebust | 331bc71 | 2018-10-14 10:40:29 -0400 | [diff] [blame] | 203 | .au_count = REFCOUNT_INIT(1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 207 | const struct rpc_credops unix_credops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | .cr_name = "AUTH_UNIX", |
| 209 | .crdestroy = unx_destroy_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | .crmatch = unx_match, |
| 211 | .crmarshal = unx_marshal, |
| 212 | .crrefresh = unx_refresh, |
| 213 | .crvalidate = unx_validate, |
| 214 | }; |