blob: 9b81be8d9946460f2576b8b9c8d6926ac7d456fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/errno.h>
Trond Myklebust25337fd2008-03-12 14:40:14 -040014#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Chuck Lever6a1a1e32012-07-11 16:31:08 -040016#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18
19#ifdef RPC_DEBUG
20# define RPCDBG_FACILITY RPCDBG_AUTH
21#endif
22
Trond Myklebust241269b2010-07-31 14:29:08 -040023#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
24struct rpc_cred_cache {
25 struct hlist_head *hashtable;
26 unsigned int hashbits;
27 spinlock_t lock;
28};
29
30static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31
Trond Myklebustfc1b3562007-06-09 16:15:46 -040032static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040033static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 &authnull_ops, /* AUTH_NULL */
35 &authunix_ops, /* AUTH_UNIX */
36 NULL, /* others can be loadable modules */
37};
38
Trond Myklebuste092bdc2007-06-23 19:45:36 -040039static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040040static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040041
Miquel van Smoorenburgdb5fe262010-09-12 19:55:26 -040042#define MAX_HASHTABLE_BITS (14)
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100043static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040044{
45 unsigned long num;
46 unsigned int nbits;
47 int ret;
48
49 if (!val)
50 goto out_inval;
51 ret = strict_strtoul(val, 0, &num);
52 if (ret == -EINVAL)
53 goto out_inval;
54 nbits = fls(num);
55 if (num > (1U << nbits))
56 nbits++;
57 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
58 goto out_inval;
59 *(unsigned int *)kp->arg = nbits;
60 return 0;
61out_inval:
62 return -EINVAL;
63}
64
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100065static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040066{
67 unsigned int nbits;
68
69 nbits = *(unsigned int *)kp->arg;
70 return sprintf(buffer, "%u", 1U << nbits);
71}
72
73#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100075static struct kernel_param_ops param_ops_hashtbl_sz = {
76 .set = param_set_hashtbl_sz,
77 .get = param_get_hashtbl_sz,
78};
79
Trond Myklebust241269b2010-07-31 14:29:08 -040080module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static u32
84pseudoflavor_to_flavor(u32 flavor) {
85 if (flavor >= RPC_AUTH_MAXFLAVOR)
86 return RPC_AUTH_GSS;
87 return flavor;
88}
89
90int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040091rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040094 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
97 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040098 spin_lock(&rpc_authflavor_lock);
99 if (auth_flavors[flavor] == NULL) {
100 auth_flavors[flavor] = ops;
101 ret = 0;
102 }
103 spin_unlock(&rpc_authflavor_lock);
104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400106EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400109rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400112 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
115 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400116 spin_lock(&rpc_authflavor_lock);
117 if (auth_flavors[flavor] == ops) {
118 auth_flavors[flavor] = NULL;
119 ret = 0;
120 }
121 spin_unlock(&rpc_authflavor_lock);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400124EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400126/**
Chuck Lever9568c5e2013-03-16 15:54:43 -0400127 * rpcauth_get_pseudoflavor - check if security flavor is supported
128 * @flavor: a security flavor
129 * @info: a GSS mech OID, quality of protection, and service value
130 *
131 * Verifies that an appropriate kernel module is available or already loaded.
132 * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
133 * not supported locally.
134 */
135rpc_authflavor_t
136rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
137{
138 const struct rpc_authops *ops;
139 rpc_authflavor_t pseudoflavor;
140
141 ops = auth_flavors[flavor];
142 if (ops == NULL)
143 request_module("rpc-auth-%u", flavor);
144 spin_lock(&rpc_authflavor_lock);
145 ops = auth_flavors[flavor];
146 if (ops == NULL || !try_module_get(ops->owner)) {
147 spin_unlock(&rpc_authflavor_lock);
148 return RPC_AUTH_MAXFLAVOR;
149 }
150 spin_unlock(&rpc_authflavor_lock);
151
152 pseudoflavor = flavor;
153 if (ops->info2flavor != NULL)
154 pseudoflavor = ops->info2flavor(info);
155
156 module_put(ops->owner);
157 return pseudoflavor;
158}
159EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
160
161/**
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400162 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
163 * @array: array to fill in
164 * @size: size of "array"
165 *
166 * Returns the number of array items filled in, or a negative errno.
167 *
168 * The returned array is not sorted by any policy. Callers should not
169 * rely on the order of the items in the returned array.
170 */
171int
172rpcauth_list_flavors(rpc_authflavor_t *array, int size)
173{
174 rpc_authflavor_t flavor;
175 int result = 0;
176
177 spin_lock(&rpc_authflavor_lock);
178 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
179 const struct rpc_authops *ops = auth_flavors[flavor];
180 rpc_authflavor_t pseudos[4];
181 int i, len;
182
183 if (result >= size) {
184 result = -ENOMEM;
185 break;
186 }
187
188 if (ops == NULL)
189 continue;
190 if (ops->list_pseudoflavors == NULL) {
191 array[result++] = ops->au_flavor;
192 continue;
193 }
194 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
195 if (len < 0) {
196 result = len;
197 break;
198 }
199 for (i = 0; i < len; i++) {
200 if (result >= size) {
201 result = -ENOMEM;
202 break;
203 }
204 array[result++] = pseudos[i];
205 }
206 }
207 spin_unlock(&rpc_authflavor_lock);
208
209 dprintk("RPC: %s returns %d\n", __func__, result);
210 return result;
211}
212EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214struct rpc_auth *
215rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
216{
217 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400218 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
220
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500221 auth = ERR_PTR(-EINVAL);
222 if (flavor >= RPC_AUTH_MAXFLAVOR)
223 goto out;
224
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500225 if ((ops = auth_flavors[flavor]) == NULL)
226 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400227 spin_lock(&rpc_authflavor_lock);
228 ops = auth_flavors[flavor];
229 if (ops == NULL || !try_module_get(ops->owner)) {
230 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500231 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400232 }
233 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400235 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000236 if (IS_ERR(auth))
237 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400239 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500241
242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return auth;
244}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400245EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400248rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 if (!atomic_dec_and_test(&auth->au_count))
251 return;
252 auth->au_ops->destroy(auth);
253}
254
255static DEFINE_SPINLOCK(rpc_credcache_lock);
256
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400257static void
258rpcauth_unhash_cred_locked(struct rpc_cred *cred)
259{
260 hlist_del_rcu(&cred->cr_hash);
261 smp_mb__before_clear_bit();
262 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
263}
264
Trond Myklebustf0380f32009-12-03 08:10:17 -0500265static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400266rpcauth_unhash_cred(struct rpc_cred *cred)
267{
268 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500269 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400270
271 cache_lock = &cred->cr_auth->au_credcache->lock;
272 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500273 ret = atomic_read(&cred->cr_count) == 0;
274 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400275 rpcauth_unhash_cred_locked(cred);
276 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500277 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * Initialize RPC credential cache
282 */
283int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400284rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400287 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800289 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400291 goto out_nocache;
292 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400293 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400294 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
295 if (!new->hashtable)
296 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400297 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 auth->au_credcache = new;
299 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400300out_nohashtbl:
301 kfree(new);
302out_nocache:
303 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400305EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*
308 * Destroy a list of credentials
309 */
310static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400311void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct rpc_cred *cred;
314
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400315 while (!list_empty(head)) {
316 cred = list_entry(head->next, struct rpc_cred, cr_lru);
317 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 put_rpccred(cred);
319 }
320}
321
322/*
323 * Clear the RPC credential cache, and delete those credentials
324 * that are not referenced.
325 */
326void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400327rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400329 LIST_HEAD(free);
330 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400332 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int i;
334
335 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400336 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400337 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400338 head = &cache->hashtable[i];
339 while (!hlist_empty(head)) {
340 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
341 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400342 if (!list_empty(&cred->cr_lru)) {
343 list_del(&cred->cr_lru);
344 number_cred_unused--;
345 }
346 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400347 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400350 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 spin_unlock(&rpc_credcache_lock);
352 rpcauth_destroy_credlist(&free);
353}
354
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400355/*
356 * Destroy the RPC credential cache
357 */
358void
359rpcauth_destroy_credcache(struct rpc_auth *auth)
360{
361 struct rpc_cred_cache *cache = auth->au_credcache;
362
363 if (cache) {
364 auth->au_credcache = NULL;
365 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400366 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400367 kfree(cache);
368 }
369}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400370EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400371
Trond Myklebustd2b83142008-04-14 18:13:37 -0400372
373#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Remove stale credentials. Avoid sleeping inside the loop.
377 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400378static int
379rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Trond Myklebust9499b432007-06-24 15:57:57 -0400381 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400382 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400383 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Trond Myklebusteac0d182008-10-28 15:21:41 -0400385 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
386
Trond Myklebust20673402010-05-13 12:51:06 -0400387 if (nr_to_scan-- == 0)
388 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400389 /*
390 * Enforce a 60 second garbage collection moratorium
391 * Note that the cred_unused list must be time-ordered.
392 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400393 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400394 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400395 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400396
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400397 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400398 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400399 if (atomic_read(&cred->cr_count) != 0)
400 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400401
Trond Myklebust9499b432007-06-24 15:57:57 -0400402 cache_lock = &cred->cr_auth->au_credcache->lock;
403 spin_lock(cache_lock);
404 if (atomic_read(&cred->cr_count) == 0) {
405 get_rpccred(cred);
406 list_add_tail(&cred->cr_lru, free);
407 rpcauth_unhash_cred_locked(cred);
408 }
409 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400411 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400412}
413
414/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400415 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400416 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400417static int
Ying Han1495f232011-05-24 17:12:27 -0700418rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400419{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400420 LIST_HEAD(free);
421 int res;
Ying Han1495f232011-05-24 17:12:27 -0700422 int nr_to_scan = sc->nr_to_scan;
423 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400424
Trond Myklebustd300a412010-05-13 12:51:03 -0400425 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
426 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400427 if (list_empty(&cred_unused))
428 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400429 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400430 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400431 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400432 rpcauth_destroy_credlist(&free);
433 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * Look up a process' credentials in the authentication cache
438 */
439struct rpc_cred *
440rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500441 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400443 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400445 struct rpc_cred *cred = NULL,
446 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400447 unsigned int nr;
448
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800449 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400451 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800452 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400453 if (!entry->cr_ops->crmatch(acred, entry, flags))
454 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400455 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400456 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400457 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400458 continue;
459 }
460 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400461 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400462 break;
463 }
464 rcu_read_unlock();
465
Trond Myklebust9499b432007-06-24 15:57:57 -0400466 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400467 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400468
469 new = auth->au_ops->crcreate(auth, acred, flags);
470 if (IS_ERR(new)) {
471 cred = new;
472 goto out;
473 }
474
Trond Myklebust9499b432007-06-24 15:57:57 -0400475 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800476 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400477 if (!entry->cr_ops->crmatch(acred, entry, flags))
478 continue;
479 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400482 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400483 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400484 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
485 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
486 } else
487 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400488 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400489found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800490 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
491 cred->cr_ops->cr_init != NULL &&
492 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500493 int res = cred->cr_ops->cr_init(auth, cred);
494 if (res < 0) {
495 put_rpccred(cred);
496 cred = ERR_PTR(res);
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400499 rpcauth_destroy_credlist(&free);
500out:
501 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400503EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500506rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
David Howells86a264a2008-11-14 10:39:18 +1100508 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100510 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Chuck Lever46121cf2007-01-31 12:14:08 -0500512 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100514
515 memset(&acred, 0, sizeof(acred));
516 acred.uid = cred->fsuid;
517 acred.gid = cred->fsgid;
518 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
519
Trond Myklebust8a317762006-02-01 12:18:36 -0500520 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 put_group_info(acred.group_info);
522 return ret;
523}
524
Trond Myklebust5fe47552007-06-23 19:55:31 -0400525void
526rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
527 struct rpc_auth *auth, const struct rpc_credops *ops)
528{
529 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400530 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400531 atomic_set(&cred->cr_count, 1);
532 cred->cr_auth = auth;
533 cred->cr_ops = ops;
534 cred->cr_expire = jiffies;
535#ifdef RPC_DEBUG
536 cred->cr_magic = RPCAUTH_CRED_MAGIC;
537#endif
538 cred->cr_uid = acred->uid;
539}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400540EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400541
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400542struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400543rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400544{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400545 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
546 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400547 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400548}
Trond Myklebust5c691042008-03-12 16:21:07 -0400549EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400550
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400551static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400552rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400554 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800556 .uid = GLOBAL_ROOT_UID,
557 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Chuck Lever46121cf2007-01-31 12:14:08 -0500560 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400561 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400562 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400563}
564
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400565static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400566rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400567{
568 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400569
570 dprintk("RPC: %5u looking up %s cred\n",
571 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400572 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Trond Myklebusta17c2152010-07-31 14:29:08 -0400575static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400576rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400578 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400579 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400580 int lookupflags = 0;
581
582 if (flags & RPC_TASK_ASYNC)
583 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400584 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400585 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400586 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400587 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400588 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400589 new = rpcauth_bind_new_cred(task, lookupflags);
590 if (IS_ERR(new))
591 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400592 if (req->rq_cred != NULL)
593 put_rpccred(req->rq_cred);
594 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598void
599put_rpccred(struct rpc_cred *cred)
600{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400601 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500602 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
603 if (atomic_dec_and_test(&cred->cr_count))
604 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500606 }
607
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400608 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
609 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400610 if (!list_empty(&cred->cr_lru)) {
611 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400612 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400613 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400614 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500615 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
616 cred->cr_expire = jiffies;
617 list_add_tail(&cred->cr_lru, &cred_unused);
618 number_cred_unused++;
619 goto out_nodestroy;
620 }
621 if (!rpcauth_unhash_cred(cred)) {
622 /* We were hashed and someone looked us up... */
623 goto out_nodestroy;
624 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400625 }
626 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500628 return;
629out_nodestroy:
630 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400632EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700634__be32 *
635rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400637 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Chuck Lever46121cf2007-01-31 12:14:08 -0500639 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400640 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return cred->cr_ops->crmarshal(task, p);
643}
644
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700645__be32 *
646rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400648 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Chuck Lever46121cf2007-01-31 12:14:08 -0500650 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400651 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return cred->cr_ops->crvalidate(task, p);
654}
655
Chuck Lever9f06c712010-12-14 14:59:18 +0000656static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
657 __be32 *data, void *obj)
658{
659 struct xdr_stream xdr;
660
661 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
662 encode(rqstp, &xdr, obj);
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int
Chuck Lever9f06c712010-12-14 14:59:18 +0000666rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700667 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400669 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Chuck Lever46121cf2007-01-31 12:14:08 -0500671 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 task->tk_pid, cred->cr_ops->cr_name, cred);
673 if (cred->cr_ops->crwrap_req)
674 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
675 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000676 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Chuck Leverbf269552010-12-14 14:59:29 +0000680static int
681rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
682 __be32 *data, void *obj)
683{
684 struct xdr_stream xdr;
685
686 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
687 return decode(rqstp, &xdr, obj);
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690int
Chuck Leverbf269552010-12-14 14:59:29 +0000691rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700692 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400694 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Chuck Lever46121cf2007-01-31 12:14:08 -0500696 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 task->tk_pid, cred->cr_ops->cr_name, cred);
698 if (cred->cr_ops->crunwrap_resp)
699 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
700 data, obj);
701 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000702 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705int
706rpcauth_refreshcred(struct rpc_task *task)
707{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400708 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int err;
710
Trond Myklebusta17c2152010-07-31 14:29:08 -0400711 cred = task->tk_rqstp->rq_cred;
712 if (cred == NULL) {
713 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
714 if (err < 0)
715 goto out;
716 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000717 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500718 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400719 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400722out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (err < 0)
724 task->tk_status = err;
725 return err;
726}
727
728void
729rpcauth_invalcred(struct rpc_task *task)
730{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400731 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400732
Chuck Lever46121cf2007-01-31 12:14:08 -0500733 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400734 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400735 if (cred)
736 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739int
740rpcauth_uptodatecred(struct rpc_task *task)
741{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400742 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400743
744 return cred == NULL ||
745 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400747
Rusty Russell8e1f9362007-07-17 04:03:17 -0700748static struct shrinker rpc_cred_shrinker = {
749 .shrink = rpcauth_cache_shrinker,
750 .seeks = DEFAULT_SEEKS,
751};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400752
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400753int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400754{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400755 int err;
756
757 err = rpc_init_authunix();
758 if (err < 0)
759 goto out1;
760 err = rpc_init_generic_auth();
761 if (err < 0)
762 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700763 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400764 return 0;
765out2:
766 rpc_destroy_authunix();
767out1:
768 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400769}
770
Stephen Rothwellc135e842010-09-29 14:16:57 +1000771void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400772{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400773 rpc_destroy_authunix();
774 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700775 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400776}