Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/auth.c |
| 3 | * |
| 4 | * Generic RPC client authentication API. |
| 5 | * |
| 6 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/sched.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 11 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/errno.h> |
Trond Myklebust | 25337fd | 2008-03-12 14:40:14 -0400 | [diff] [blame] | 15 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 17 | #include <linux/sunrpc/gss_api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 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 | |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 24 | #define RPC_CREDCACHE_DEFAULT_HASHBITS (4) |
| 25 | struct rpc_cred_cache { |
| 26 | struct hlist_head *hashtable; |
| 27 | unsigned int hashbits; |
| 28 | spinlock_t lock; |
| 29 | }; |
| 30 | |
| 31 | static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS; |
| 32 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 33 | static const struct rpc_authops __rcu *auth_flavors[RPC_AUTH_MAXFLAVOR] = { |
| 34 | [RPC_AUTH_NULL] = (const struct rpc_authops __force __rcu *)&authnull_ops, |
| 35 | [RPC_AUTH_UNIX] = (const struct rpc_authops __force __rcu *)&authunix_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | NULL, /* others can be loadable modules */ |
| 37 | }; |
| 38 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 39 | static LIST_HEAD(cred_unused); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 40 | static unsigned long number_cred_unused; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 41 | |
Miquel van Smoorenburg | db5fe26 | 2010-09-12 19:55:26 -0400 | [diff] [blame] | 42 | #define MAX_HASHTABLE_BITS (14) |
Stephen Rothwell | 8e4e15d | 2010-08-04 12:11:22 +1000 | [diff] [blame] | 43 | static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp) |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 44 | { |
| 45 | unsigned long num; |
| 46 | unsigned int nbits; |
| 47 | int ret; |
| 48 | |
| 49 | if (!val) |
| 50 | goto out_inval; |
Daniel Walter | 00cfaa9 | 2014-06-21 13:06:38 +0100 | [diff] [blame] | 51 | ret = kstrtoul(val, 0, &num); |
Dan Carpenter | 1a54c0c | 2018-07-12 15:30:08 +0300 | [diff] [blame] | 52 | if (ret) |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 53 | goto out_inval; |
Frank Sorenson | 34ae685 | 2016-06-27 15:17:19 -0400 | [diff] [blame] | 54 | nbits = fls(num - 1); |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 55 | if (nbits > MAX_HASHTABLE_BITS || nbits < 2) |
| 56 | goto out_inval; |
| 57 | *(unsigned int *)kp->arg = nbits; |
| 58 | return 0; |
| 59 | out_inval: |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | |
Stephen Rothwell | 8e4e15d | 2010-08-04 12:11:22 +1000 | [diff] [blame] | 63 | static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp) |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 64 | { |
| 65 | unsigned int nbits; |
| 66 | |
| 67 | nbits = *(unsigned int *)kp->arg; |
| 68 | return sprintf(buffer, "%u", 1U << nbits); |
| 69 | } |
| 70 | |
| 71 | #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int); |
| 72 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 73 | static const struct kernel_param_ops param_ops_hashtbl_sz = { |
Stephen Rothwell | 8e4e15d | 2010-08-04 12:11:22 +1000 | [diff] [blame] | 74 | .set = param_set_hashtbl_sz, |
| 75 | .get = param_get_hashtbl_sz, |
| 76 | }; |
| 77 | |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 78 | module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644); |
| 79 | MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size"); |
| 80 | |
Trond Myklebust | bae6746 | 2014-07-21 13:32:42 -0400 | [diff] [blame] | 81 | static unsigned long auth_max_cred_cachesize = ULONG_MAX; |
| 82 | module_param(auth_max_cred_cachesize, ulong, 0644); |
| 83 | MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size"); |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static u32 |
| 86 | pseudoflavor_to_flavor(u32 flavor) { |
Chuck Lever | 1c74a24 | 2013-03-22 12:53:08 -0400 | [diff] [blame] | 87 | if (flavor > RPC_AUTH_MAXFLAVOR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return RPC_AUTH_GSS; |
| 89 | return flavor; |
| 90 | } |
| 91 | |
| 92 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 93 | rpcauth_register(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 95 | const struct rpc_authops *old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | rpc_authflavor_t flavor; |
| 97 | |
| 98 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 99 | return -EINVAL; |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 100 | old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], NULL, ops); |
| 101 | if (old == NULL || old == ops) |
| 102 | return 0; |
| 103 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(rpcauth_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 108 | rpcauth_unregister(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 110 | const struct rpc_authops *old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | rpc_authflavor_t flavor; |
| 112 | |
| 113 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 114 | return -EINVAL; |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 115 | |
| 116 | old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], ops, NULL); |
| 117 | if (old == ops || old == NULL) |
| 118 | return 0; |
| 119 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(rpcauth_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 123 | static const struct rpc_authops * |
| 124 | rpcauth_get_authops(rpc_authflavor_t flavor) |
| 125 | { |
| 126 | const struct rpc_authops *ops; |
| 127 | |
| 128 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
| 129 | return NULL; |
| 130 | |
| 131 | rcu_read_lock(); |
| 132 | ops = rcu_dereference(auth_flavors[flavor]); |
| 133 | if (ops == NULL) { |
| 134 | rcu_read_unlock(); |
| 135 | request_module("rpc-auth-%u", flavor); |
| 136 | rcu_read_lock(); |
| 137 | ops = rcu_dereference(auth_flavors[flavor]); |
| 138 | if (ops == NULL) |
| 139 | goto out; |
| 140 | } |
| 141 | if (!try_module_get(ops->owner)) |
| 142 | ops = NULL; |
| 143 | out: |
| 144 | rcu_read_unlock(); |
| 145 | return ops; |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | rpcauth_put_authops(const struct rpc_authops *ops) |
| 150 | { |
| 151 | module_put(ops->owner); |
| 152 | } |
| 153 | |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 154 | /** |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 155 | * rpcauth_get_pseudoflavor - check if security flavor is supported |
| 156 | * @flavor: a security flavor |
| 157 | * @info: a GSS mech OID, quality of protection, and service value |
| 158 | * |
| 159 | * Verifies that an appropriate kernel module is available or already loaded. |
| 160 | * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is |
| 161 | * not supported locally. |
| 162 | */ |
| 163 | rpc_authflavor_t |
| 164 | rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info) |
| 165 | { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 166 | const struct rpc_authops *ops = rpcauth_get_authops(flavor); |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 167 | rpc_authflavor_t pseudoflavor; |
| 168 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 169 | if (!ops) |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 170 | return RPC_AUTH_MAXFLAVOR; |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 171 | pseudoflavor = flavor; |
| 172 | if (ops->info2flavor != NULL) |
| 173 | pseudoflavor = ops->info2flavor(info); |
| 174 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 175 | rpcauth_put_authops(ops); |
Chuck Lever | 9568c5e | 2013-03-16 15:54:43 -0400 | [diff] [blame] | 176 | return pseudoflavor; |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor); |
| 179 | |
| 180 | /** |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 181 | * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor |
| 182 | * @pseudoflavor: GSS pseudoflavor to match |
| 183 | * @info: rpcsec_gss_info structure to fill in |
| 184 | * |
| 185 | * Returns zero and fills in "info" if pseudoflavor matches a |
| 186 | * supported mechanism. |
| 187 | */ |
| 188 | int |
| 189 | rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info) |
| 190 | { |
| 191 | rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor); |
| 192 | const struct rpc_authops *ops; |
| 193 | int result; |
| 194 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 195 | ops = rpcauth_get_authops(flavor); |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 196 | if (ops == NULL) |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 197 | return -ENOENT; |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 198 | |
| 199 | result = -ENOENT; |
| 200 | if (ops->flavor2info != NULL) |
| 201 | result = ops->flavor2info(pseudoflavor, info); |
| 202 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 203 | rpcauth_put_authops(ops); |
Chuck Lever | a77c806 | 2013-03-16 15:55:10 -0400 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo); |
| 207 | |
| 208 | /** |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 209 | * rpcauth_list_flavors - discover registered flavors and pseudoflavors |
| 210 | * @array: array to fill in |
| 211 | * @size: size of "array" |
| 212 | * |
| 213 | * Returns the number of array items filled in, or a negative errno. |
| 214 | * |
| 215 | * The returned array is not sorted by any policy. Callers should not |
| 216 | * rely on the order of the items in the returned array. |
| 217 | */ |
| 218 | int |
| 219 | rpcauth_list_flavors(rpc_authflavor_t *array, int size) |
| 220 | { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 221 | const struct rpc_authops *ops; |
| 222 | rpc_authflavor_t flavor, pseudos[4]; |
| 223 | int i, len, result = 0; |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 224 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 225 | rcu_read_lock(); |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 226 | for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 227 | ops = rcu_dereference(auth_flavors[flavor]); |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 228 | if (result >= size) { |
| 229 | result = -ENOMEM; |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | if (ops == NULL) |
| 234 | continue; |
| 235 | if (ops->list_pseudoflavors == NULL) { |
| 236 | array[result++] = ops->au_flavor; |
| 237 | continue; |
| 238 | } |
| 239 | len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos)); |
| 240 | if (len < 0) { |
| 241 | result = len; |
| 242 | break; |
| 243 | } |
| 244 | for (i = 0; i < len; i++) { |
| 245 | if (result >= size) { |
| 246 | result = -ENOMEM; |
| 247 | break; |
| 248 | } |
| 249 | array[result++] = pseudos[i]; |
| 250 | } |
| 251 | } |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 252 | rcu_read_unlock(); |
Chuck Lever | 6a1a1e3 | 2012-07-11 16:31:08 -0400 | [diff] [blame] | 253 | |
| 254 | dprintk("RPC: %s returns %d\n", __func__, result); |
| 255 | return result; |
| 256 | } |
| 257 | EXPORT_SYMBOL_GPL(rpcauth_list_flavors); |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | struct rpc_auth * |
Sargun Dhillon | 82b98ca | 2018-07-05 16:48:50 +0000 | [diff] [blame] | 260 | rpcauth_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 262 | struct rpc_auth *auth = ERR_PTR(-EINVAL); |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 263 | const struct rpc_authops *ops; |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 264 | u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 266 | ops = rpcauth_get_authops(flavor); |
| 267 | if (ops == NULL) |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 268 | goto out; |
| 269 | |
Trond Myklebust | c219066 | 2013-08-26 19:23:04 -0400 | [diff] [blame] | 270 | auth = ops->create(args, clnt); |
Trond Myklebust | 4e4c3be | 2018-09-27 13:12:44 -0400 | [diff] [blame] | 271 | |
| 272 | rpcauth_put_authops(ops); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 273 | if (IS_ERR(auth)) |
| 274 | return auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (clnt->cl_auth) |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 276 | rpcauth_release(clnt->cl_auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | clnt->cl_auth = auth; |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 278 | |
| 279 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return auth; |
| 281 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 282 | EXPORT_SYMBOL_GPL(rpcauth_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | void |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 285 | rpcauth_release(struct rpc_auth *auth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Trond Myklebust | 331bc71 | 2018-10-14 10:40:29 -0400 | [diff] [blame] | 287 | if (!refcount_dec_and_test(&auth->au_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return; |
| 289 | auth->au_ops->destroy(auth); |
| 290 | } |
| 291 | |
| 292 | static DEFINE_SPINLOCK(rpc_credcache_lock); |
| 293 | |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 294 | /* |
| 295 | * On success, the caller is responsible for freeing the reference |
| 296 | * held by the hashtable |
| 297 | */ |
| 298 | static bool |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 299 | rpcauth_unhash_cred_locked(struct rpc_cred *cred) |
| 300 | { |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 301 | if (!test_and_clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)) |
| 302 | return false; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 303 | hlist_del_rcu(&cred->cr_hash); |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 304 | return true; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 307 | static bool |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 308 | rpcauth_unhash_cred(struct rpc_cred *cred) |
| 309 | { |
| 310 | spinlock_t *cache_lock; |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 311 | bool ret; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 312 | |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 313 | if (!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)) |
| 314 | return false; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 315 | cache_lock = &cred->cr_auth->au_credcache->lock; |
| 316 | spin_lock(cache_lock); |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 317 | ret = rpcauth_unhash_cred_locked(cred); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 318 | spin_unlock(cache_lock); |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 319 | return ret; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Initialize RPC credential cache |
| 324 | */ |
| 325 | int |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 326 | rpcauth_init_credcache(struct rpc_auth *auth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
| 328 | struct rpc_cred_cache *new; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 329 | unsigned int hashsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 331 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (!new) |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 333 | goto out_nocache; |
| 334 | new->hashbits = auth_hashbits; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 335 | hashsize = 1U << new->hashbits; |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 336 | new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL); |
| 337 | if (!new->hashtable) |
| 338 | goto out_nohashtbl; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 339 | spin_lock_init(&new->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | auth->au_credcache = new; |
| 341 | return 0; |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 342 | out_nohashtbl: |
| 343 | kfree(new); |
| 344 | out_nocache: |
| 345 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(rpcauth_init_credcache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
Andy Adamson | 4de6caa | 2013-08-14 11:59:15 -0400 | [diff] [blame] | 350 | * Setup a credential key lifetime timeout notification |
| 351 | */ |
| 352 | int |
| 353 | rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred) |
| 354 | { |
| 355 | if (!cred->cr_auth->au_ops->key_timeout) |
| 356 | return 0; |
| 357 | return cred->cr_auth->au_ops->key_timeout(auth, cred); |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify); |
| 360 | |
| 361 | bool |
Scott Mayhew | ce52914e | 2016-06-07 15:14:48 -0400 | [diff] [blame] | 362 | rpcauth_cred_key_to_expire(struct rpc_auth *auth, struct rpc_cred *cred) |
Andy Adamson | 4de6caa | 2013-08-14 11:59:15 -0400 | [diff] [blame] | 363 | { |
Scott Mayhew | ce52914e | 2016-06-07 15:14:48 -0400 | [diff] [blame] | 364 | if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT) |
| 365 | return false; |
Andy Adamson | 4de6caa | 2013-08-14 11:59:15 -0400 | [diff] [blame] | 366 | if (!cred->cr_ops->crkey_to_expire) |
| 367 | return false; |
| 368 | return cred->cr_ops->crkey_to_expire(cred); |
| 369 | } |
| 370 | EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire); |
| 371 | |
Jeff Layton | a0337d1 | 2014-06-21 20:52:16 -0400 | [diff] [blame] | 372 | char * |
| 373 | rpcauth_stringify_acceptor(struct rpc_cred *cred) |
| 374 | { |
| 375 | if (!cred->cr_ops->crstringify_acceptor) |
| 376 | return NULL; |
| 377 | return cred->cr_ops->crstringify_acceptor(cred); |
| 378 | } |
| 379 | EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor); |
| 380 | |
Andy Adamson | 4de6caa | 2013-08-14 11:59:15 -0400 | [diff] [blame] | 381 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * Destroy a list of credentials |
| 383 | */ |
| 384 | static inline |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 385 | void rpcauth_destroy_credlist(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct rpc_cred *cred; |
| 388 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 389 | while (!list_empty(head)) { |
| 390 | cred = list_entry(head->next, struct rpc_cred, cr_lru); |
| 391 | list_del_init(&cred->cr_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | put_rpccred(cred); |
| 393 | } |
| 394 | } |
| 395 | |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 396 | static void |
| 397 | rpcauth_lru_add_locked(struct rpc_cred *cred) |
| 398 | { |
| 399 | if (!list_empty(&cred->cr_lru)) |
| 400 | return; |
| 401 | number_cred_unused++; |
| 402 | list_add_tail(&cred->cr_lru, &cred_unused); |
| 403 | } |
| 404 | |
| 405 | static void |
| 406 | rpcauth_lru_add(struct rpc_cred *cred) |
| 407 | { |
| 408 | if (!list_empty(&cred->cr_lru)) |
| 409 | return; |
| 410 | spin_lock(&rpc_credcache_lock); |
| 411 | rpcauth_lru_add_locked(cred); |
| 412 | spin_unlock(&rpc_credcache_lock); |
| 413 | } |
| 414 | |
| 415 | static void |
| 416 | rpcauth_lru_remove_locked(struct rpc_cred *cred) |
| 417 | { |
| 418 | if (list_empty(&cred->cr_lru)) |
| 419 | return; |
| 420 | number_cred_unused--; |
| 421 | list_del_init(&cred->cr_lru); |
| 422 | } |
| 423 | |
| 424 | static void |
| 425 | rpcauth_lru_remove(struct rpc_cred *cred) |
| 426 | { |
| 427 | if (list_empty(&cred->cr_lru)) |
| 428 | return; |
| 429 | spin_lock(&rpc_credcache_lock); |
| 430 | rpcauth_lru_remove_locked(cred); |
| 431 | spin_unlock(&rpc_credcache_lock); |
| 432 | } |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Clear the RPC credential cache, and delete those credentials |
| 436 | * that are not referenced. |
| 437 | */ |
| 438 | void |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 439 | rpcauth_clear_credcache(struct rpc_cred_cache *cache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 441 | LIST_HEAD(free); |
| 442 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | struct rpc_cred *cred; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 444 | unsigned int hashsize = 1U << cache->hashbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | int i; |
| 446 | |
| 447 | spin_lock(&rpc_credcache_lock); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 448 | spin_lock(&cache->lock); |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 449 | for (i = 0; i < hashsize; i++) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 450 | head = &cache->hashtable[i]; |
| 451 | while (!hlist_empty(head)) { |
| 452 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 453 | rpcauth_unhash_cred_locked(cred); |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 454 | /* Note: We now hold a reference to cred */ |
| 455 | rpcauth_lru_remove_locked(cred); |
| 456 | list_add_tail(&cred->cr_lru, &free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 459 | spin_unlock(&cache->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | spin_unlock(&rpc_credcache_lock); |
| 461 | rpcauth_destroy_credlist(&free); |
| 462 | } |
| 463 | |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 464 | /* |
| 465 | * Destroy the RPC credential cache |
| 466 | */ |
| 467 | void |
| 468 | rpcauth_destroy_credcache(struct rpc_auth *auth) |
| 469 | { |
| 470 | struct rpc_cred_cache *cache = auth->au_credcache; |
| 471 | |
| 472 | if (cache) { |
| 473 | auth->au_credcache = NULL; |
| 474 | rpcauth_clear_credcache(cache); |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 475 | kfree(cache->hashtable); |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 476 | kfree(cache); |
| 477 | } |
| 478 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 479 | EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache); |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 480 | |
Trond Myklebust | d2b8314 | 2008-04-14 18:13:37 -0400 | [diff] [blame] | 481 | |
| 482 | #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ) |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
| 485 | * Remove stale credentials. Avoid sleeping inside the loop. |
| 486 | */ |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 487 | static long |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 488 | rpcauth_prune_expired(struct list_head *free, int nr_to_scan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 490 | struct rpc_cred *cred, *next; |
Trond Myklebust | d2b8314 | 2008-04-14 18:13:37 -0400 | [diff] [blame] | 491 | unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM; |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 492 | long freed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 494 | list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { |
| 495 | |
Trond Myklebust | 2067340 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 496 | if (nr_to_scan-- == 0) |
| 497 | break; |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 498 | if (refcount_read(&cred->cr_count) > 1) { |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 499 | rpcauth_lru_remove_locked(cred); |
| 500 | continue; |
| 501 | } |
Trond Myklebust | 93a05e6 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 502 | /* |
| 503 | * Enforce a 60 second garbage collection moratorium |
| 504 | * Note that the cred_unused list must be time-ordered. |
| 505 | */ |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 506 | if (!time_in_range(cred->cr_expire, expired, jiffies)) |
| 507 | continue; |
| 508 | if (!rpcauth_unhash_cred(cred)) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 509 | continue; |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 510 | |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 511 | rpcauth_lru_remove_locked(cred); |
| 512 | freed++; |
| 513 | list_add_tail(&cred->cr_lru, free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 515 | return freed ? freed : SHRINK_STOP; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Trond Myklebust | bae6746 | 2014-07-21 13:32:42 -0400 | [diff] [blame] | 518 | static unsigned long |
| 519 | rpcauth_cache_do_shrink(int nr_to_scan) |
| 520 | { |
| 521 | LIST_HEAD(free); |
| 522 | unsigned long freed; |
| 523 | |
| 524 | spin_lock(&rpc_credcache_lock); |
| 525 | freed = rpcauth_prune_expired(&free, nr_to_scan); |
| 526 | spin_unlock(&rpc_credcache_lock); |
| 527 | rpcauth_destroy_credlist(&free); |
| 528 | |
| 529 | return freed; |
| 530 | } |
| 531 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 532 | /* |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 533 | * Run memory cache shrinker. |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 534 | */ |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 535 | static unsigned long |
| 536 | rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) |
| 537 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 538 | { |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 539 | if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL) |
| 540 | return SHRINK_STOP; |
| 541 | |
| 542 | /* nothing left, don't come back */ |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 543 | if (list_empty(&cred_unused)) |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 544 | return SHRINK_STOP; |
| 545 | |
Trond Myklebust | bae6746 | 2014-07-21 13:32:42 -0400 | [diff] [blame] | 546 | return rpcauth_cache_do_shrink(sc->nr_to_scan); |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static unsigned long |
| 550 | rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc) |
| 551 | |
| 552 | { |
NeilBrown | 4c3ffd0 | 2017-01-06 14:19:58 +1100 | [diff] [blame] | 553 | return number_cred_unused * sysctl_vfs_cache_pressure / 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Trond Myklebust | bae6746 | 2014-07-21 13:32:42 -0400 | [diff] [blame] | 556 | static void |
| 557 | rpcauth_cache_enforce_limit(void) |
| 558 | { |
| 559 | unsigned long diff; |
| 560 | unsigned int nr_to_scan; |
| 561 | |
| 562 | if (number_cred_unused <= auth_max_cred_cachesize) |
| 563 | return; |
| 564 | diff = number_cred_unused - auth_max_cred_cachesize; |
| 565 | nr_to_scan = 100; |
| 566 | if (diff < nr_to_scan) |
| 567 | nr_to_scan = diff; |
| 568 | rpcauth_cache_do_shrink(nr_to_scan); |
| 569 | } |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | /* |
| 572 | * Look up a process' credentials in the authentication cache |
| 573 | */ |
| 574 | struct rpc_cred * |
| 575 | rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, |
Jeff Layton | 3c6e0bc | 2016-04-21 20:51:54 -0400 | [diff] [blame] | 576 | int flags, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 578 | LIST_HEAD(free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | struct rpc_cred_cache *cache = auth->au_credcache; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 580 | struct rpc_cred *cred = NULL, |
| 581 | *entry, *new; |
Trond Myklebust | 25337fd | 2008-03-12 14:40:14 -0400 | [diff] [blame] | 582 | unsigned int nr; |
| 583 | |
Frank Sorenson | 66cbd4b | 2016-09-29 10:44:41 -0500 | [diff] [blame] | 584 | nr = auth->au_ops->hash_cred(acred, cache->hashbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 586 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 587 | hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) { |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 588 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 589 | continue; |
NeilBrown | bd95608 | 2014-07-14 11:28:20 +1000 | [diff] [blame] | 590 | if (flags & RPCAUTH_LOOKUP_RCU) { |
Trond Myklebust | 07d02a6 | 2018-10-12 13:28:26 -0400 | [diff] [blame] | 591 | if (test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags) || |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 592 | refcount_read(&entry->cr_count) == 0) |
Trond Myklebust | 07d02a6 | 2018-10-12 13:28:26 -0400 | [diff] [blame] | 593 | continue; |
| 594 | cred = entry; |
NeilBrown | bd95608 | 2014-07-14 11:28:20 +1000 | [diff] [blame] | 595 | break; |
| 596 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 597 | cred = get_rpccred(entry); |
Trond Myklebust | 07d02a6 | 2018-10-12 13:28:26 -0400 | [diff] [blame] | 598 | if (cred) |
| 599 | break; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 600 | } |
| 601 | rcu_read_unlock(); |
| 602 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 603 | if (cred != NULL) |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 604 | goto found; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 605 | |
NeilBrown | bd95608 | 2014-07-14 11:28:20 +1000 | [diff] [blame] | 606 | if (flags & RPCAUTH_LOOKUP_RCU) |
| 607 | return ERR_PTR(-ECHILD); |
| 608 | |
Jeff Layton | 3c6e0bc | 2016-04-21 20:51:54 -0400 | [diff] [blame] | 609 | new = auth->au_ops->crcreate(auth, acred, flags, gfp); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 610 | if (IS_ERR(new)) { |
| 611 | cred = new; |
| 612 | goto out; |
| 613 | } |
| 614 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 615 | spin_lock(&cache->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 616 | hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 617 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 618 | continue; |
| 619 | cred = get_rpccred(entry); |
Trond Myklebust | 07d02a6 | 2018-10-12 13:28:26 -0400 | [diff] [blame] | 620 | if (cred) |
| 621 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 623 | if (cred == NULL) { |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 624 | cred = new; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 625 | set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 626 | refcount_inc(&cred->cr_count); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 627 | hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); |
| 628 | } else |
| 629 | list_add_tail(&new->cr_lru, &free); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 630 | spin_unlock(&cache->lock); |
Trond Myklebust | bae6746 | 2014-07-21 13:32:42 -0400 | [diff] [blame] | 631 | rpcauth_cache_enforce_limit(); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 632 | found: |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 633 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) && |
| 634 | cred->cr_ops->cr_init != NULL && |
| 635 | !(flags & RPCAUTH_LOOKUP_NEW)) { |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 636 | int res = cred->cr_ops->cr_init(auth, cred); |
| 637 | if (res < 0) { |
| 638 | put_rpccred(cred); |
| 639 | cred = ERR_PTR(res); |
| 640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 642 | rpcauth_destroy_credlist(&free); |
| 643 | out: |
| 644 | return cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 646 | EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 649 | rpcauth_lookupcred(struct rpc_auth *auth, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 651 | struct auth_cred acred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | struct rpc_cred *ret; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 653 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 655 | dprintk("RPC: looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | auth->au_ops->au_name); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 657 | |
| 658 | memset(&acred, 0, sizeof(acred)); |
NeilBrown | 97f68c6b | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 659 | acred.cred = cred; |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 660 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return ret; |
| 662 | } |
Andy Adamson | 66b0686 | 2014-06-12 15:02:32 -0400 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(rpcauth_lookupcred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 665 | void |
| 666 | rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, |
| 667 | struct rpc_auth *auth, const struct rpc_credops *ops) |
| 668 | { |
| 669 | INIT_HLIST_NODE(&cred->cr_hash); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 670 | INIT_LIST_HEAD(&cred->cr_lru); |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 671 | refcount_set(&cred->cr_count, 1); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 672 | cred->cr_auth = auth; |
| 673 | cred->cr_ops = ops; |
| 674 | cred->cr_expire = jiffies; |
NeilBrown | 97f68c6b | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 675 | cred->cr_cred = get_cred(acred->cred); |
NeilBrown | 8276c90 | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 676 | cred->cr_uid = acred->cred->fsuid; |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 677 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 678 | EXPORT_SYMBOL_GPL(rpcauth_init_cred); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 679 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 680 | struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 681 | rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 682 | { |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 683 | dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, |
| 684 | cred->cr_auth->au_ops->au_name, cred); |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 685 | return get_rpccred(cred); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 686 | } |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 687 | EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 688 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 689 | static struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 690 | rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 692 | struct rpc_auth *auth = task->tk_client->cl_auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | struct auth_cred acred = { |
NeilBrown | 97f68c6b | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 694 | .cred = get_task_cred(&init_task), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | }; |
NeilBrown | 97f68c6b | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 696 | struct rpc_cred *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 698 | dprintk("RPC: %5u looking up %s cred\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 699 | task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); |
NeilBrown | 97f68c6b | 2018-12-03 11:30:30 +1100 | [diff] [blame] | 700 | ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags); |
| 701 | put_cred(acred.cred); |
| 702 | return ret; |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 703 | } |
| 704 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 705 | static struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 706 | rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 707 | { |
| 708 | struct rpc_auth *auth = task->tk_client->cl_auth; |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 709 | |
| 710 | dprintk("RPC: %5u looking up %s cred\n", |
| 711 | task->tk_pid, auth->au_ops->au_name); |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 712 | return rpcauth_lookupcred(auth, lookupflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 715 | static int |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 716 | rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 718 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 719 | struct rpc_cred *new; |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 720 | int lookupflags = 0; |
| 721 | |
| 722 | if (flags & RPC_TASK_ASYNC) |
| 723 | lookupflags |= RPCAUTH_LOOKUP_NEW; |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 724 | if (cred != NULL) |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 725 | new = cred->cr_ops->crbind(task, cred, lookupflags); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 726 | else if (flags & RPC_TASK_ROOTCREDS) |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 727 | new = rpcauth_bind_root_cred(task, lookupflags); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 728 | else |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 729 | new = rpcauth_bind_new_cred(task, lookupflags); |
| 730 | if (IS_ERR(new)) |
| 731 | return PTR_ERR(new); |
Trond Myklebust | 9a8f6b5 | 2016-05-16 17:42:42 -0400 | [diff] [blame] | 732 | put_rpccred(req->rq_cred); |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 733 | req->rq_cred = new; |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 734 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void |
| 738 | put_rpccred(struct rpc_cred *cred) |
| 739 | { |
Trond Myklebust | 9a8f6b5 | 2016-05-16 17:42:42 -0400 | [diff] [blame] | 740 | if (cred == NULL) |
| 741 | return; |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 742 | rcu_read_lock(); |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 743 | if (refcount_dec_and_test(&cred->cr_count)) |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 744 | goto destroy; |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 745 | if (refcount_read(&cred->cr_count) != 1 || |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 746 | !test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)) |
| 747 | goto out; |
| 748 | if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) { |
| 749 | cred->cr_expire = jiffies; |
| 750 | rpcauth_lru_add(cred); |
| 751 | /* Race breaker */ |
| 752 | if (unlikely(!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))) |
| 753 | rpcauth_lru_remove(cred); |
| 754 | } else if (rpcauth_unhash_cred(cred)) { |
| 755 | rpcauth_lru_remove(cred); |
Trond Myklebust | 79b1818 | 2018-10-14 10:34:31 -0400 | [diff] [blame] | 756 | if (refcount_dec_and_test(&cred->cr_count)) |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 757 | goto destroy; |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 758 | } |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 759 | out: |
| 760 | rcu_read_unlock(); |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 761 | return; |
Trond Myklebust | 95cd623 | 2018-10-11 16:11:09 -0400 | [diff] [blame] | 762 | destroy: |
| 763 | rcu_read_unlock(); |
| 764 | cred->cr_ops->crdestroy(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 766 | EXPORT_SYMBOL_GPL(put_rpccred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 768 | __be32 * |
| 769 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 771 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 773 | dprintk("RPC: %5u marshaling %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 774 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return cred->cr_ops->crmarshal(task, p); |
| 777 | } |
| 778 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 779 | __be32 * |
| 780 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 782 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 784 | dprintk("RPC: %5u validating %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 785 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return cred->cr_ops->crvalidate(task, p); |
| 788 | } |
| 789 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 790 | static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp, |
| 791 | __be32 *data, void *obj) |
| 792 | { |
| 793 | struct xdr_stream xdr; |
| 794 | |
| 795 | xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data); |
| 796 | encode(rqstp, &xdr, obj); |
| 797 | } |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | int |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 800 | rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 801 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 803 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 805 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 807 | if (cred->cr_ops->crwrap_req) |
| 808 | return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); |
| 809 | /* By default, we encode the arguments normally. */ |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 810 | rpcauth_wrap_req_encode(encode, rqstp, data, obj); |
| 811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 814 | static int |
| 815 | rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp, |
| 816 | __be32 *data, void *obj) |
| 817 | { |
| 818 | struct xdr_stream xdr; |
| 819 | |
| 820 | xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data); |
| 821 | return decode(rqstp, &xdr, obj); |
| 822 | } |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | int |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 825 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 826 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 828 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 830 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 832 | if (cred->cr_ops->crunwrap_resp) |
| 833 | return cred->cr_ops->crunwrap_resp(task, decode, rqstp, |
| 834 | data, obj); |
| 835 | /* By default, we decode the arguments normally. */ |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 836 | return rpcauth_unwrap_req_decode(decode, rqstp, data, obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Trond Myklebust | 3021a5bb | 2018-08-14 13:50:21 -0400 | [diff] [blame] | 839 | bool |
| 840 | rpcauth_xmit_need_reencode(struct rpc_task *task) |
| 841 | { |
| 842 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
| 843 | |
| 844 | if (!cred || !cred->cr_ops->crneed_reencode) |
| 845 | return false; |
| 846 | return cred->cr_ops->crneed_reencode(task); |
| 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | int |
| 850 | rpcauth_refreshcred(struct rpc_task *task) |
| 851 | { |
Trond Myklebust | 9a84d38 | 2010-10-24 18:00:46 -0400 | [diff] [blame] | 852 | struct rpc_cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | int err; |
| 854 | |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 855 | cred = task->tk_rqstp->rq_cred; |
| 856 | if (cred == NULL) { |
| 857 | err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags); |
| 858 | if (err < 0) |
| 859 | goto out; |
| 860 | cred = task->tk_rqstp->rq_cred; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 861 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 862 | dprintk("RPC: %5u refreshing %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 863 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | err = cred->cr_ops->crrefresh(task); |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 866 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | if (err < 0) |
| 868 | task->tk_status = err; |
| 869 | return err; |
| 870 | } |
| 871 | |
| 872 | void |
| 873 | rpcauth_invalcred(struct rpc_task *task) |
| 874 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 875 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 876 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 877 | dprintk("RPC: %5u invalidating %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 878 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 879 | if (cred) |
| 880 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | int |
| 884 | rpcauth_uptodatecred(struct rpc_task *task) |
| 885 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 886 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 887 | |
| 888 | return cred == NULL || |
| 889 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 891 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 892 | static struct shrinker rpc_cred_shrinker = { |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 893 | .count_objects = rpcauth_cache_shrink_count, |
| 894 | .scan_objects = rpcauth_cache_shrink_scan, |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 895 | .seeks = DEFAULT_SEEKS, |
| 896 | }; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 897 | |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 898 | int __init rpcauth_init_module(void) |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 899 | { |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 900 | int err; |
| 901 | |
| 902 | err = rpc_init_authunix(); |
| 903 | if (err < 0) |
| 904 | goto out1; |
| 905 | err = rpc_init_generic_auth(); |
| 906 | if (err < 0) |
| 907 | goto out2; |
Kinglong Mee | 2864486 | 2017-02-07 21:46:39 +0800 | [diff] [blame] | 908 | err = register_shrinker(&rpc_cred_shrinker); |
| 909 | if (err < 0) |
| 910 | goto out3; |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 911 | return 0; |
Kinglong Mee | 2864486 | 2017-02-07 21:46:39 +0800 | [diff] [blame] | 912 | out3: |
| 913 | rpc_destroy_generic_auth(); |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 914 | out2: |
| 915 | rpc_destroy_authunix(); |
| 916 | out1: |
| 917 | return err; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 918 | } |
| 919 | |
Stephen Rothwell | c135e84 | 2010-09-29 14:16:57 +1000 | [diff] [blame] | 920 | void rpcauth_remove_module(void) |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 921 | { |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 922 | rpc_destroy_authunix(); |
| 923 | rpc_destroy_generic_auth(); |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 924 | unregister_shrinker(&rpc_cred_shrinker); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 925 | } |