auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. #include <linux/types.h>
  9. #include <linux/sched.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/errno.h>
  13. #include <linux/hash.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/spinlock.h>
  16. #ifdef RPC_DEBUG
  17. # define RPCDBG_FACILITY RPCDBG_AUTH
  18. #endif
  19. static DEFINE_SPINLOCK(rpc_authflavor_lock);
  20. static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
  21. &authnull_ops, /* AUTH_NULL */
  22. &authunix_ops, /* AUTH_UNIX */
  23. NULL, /* others can be loadable modules */
  24. };
  25. static LIST_HEAD(cred_unused);
  26. static unsigned long number_cred_unused;
  27. static u32
  28. pseudoflavor_to_flavor(u32 flavor) {
  29. if (flavor >= RPC_AUTH_MAXFLAVOR)
  30. return RPC_AUTH_GSS;
  31. return flavor;
  32. }
  33. int
  34. rpcauth_register(const struct rpc_authops *ops)
  35. {
  36. rpc_authflavor_t flavor;
  37. int ret = -EPERM;
  38. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  39. return -EINVAL;
  40. spin_lock(&rpc_authflavor_lock);
  41. if (auth_flavors[flavor] == NULL) {
  42. auth_flavors[flavor] = ops;
  43. ret = 0;
  44. }
  45. spin_unlock(&rpc_authflavor_lock);
  46. return ret;
  47. }
  48. EXPORT_SYMBOL_GPL(rpcauth_register);
  49. int
  50. rpcauth_unregister(const struct rpc_authops *ops)
  51. {
  52. rpc_authflavor_t flavor;
  53. int ret = -EPERM;
  54. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  55. return -EINVAL;
  56. spin_lock(&rpc_authflavor_lock);
  57. if (auth_flavors[flavor] == ops) {
  58. auth_flavors[flavor] = NULL;
  59. ret = 0;
  60. }
  61. spin_unlock(&rpc_authflavor_lock);
  62. return ret;
  63. }
  64. EXPORT_SYMBOL_GPL(rpcauth_unregister);
  65. struct rpc_auth *
  66. rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
  67. {
  68. struct rpc_auth *auth;
  69. const struct rpc_authops *ops;
  70. u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
  71. auth = ERR_PTR(-EINVAL);
  72. if (flavor >= RPC_AUTH_MAXFLAVOR)
  73. goto out;
  74. if ((ops = auth_flavors[flavor]) == NULL)
  75. request_module("rpc-auth-%u", flavor);
  76. spin_lock(&rpc_authflavor_lock);
  77. ops = auth_flavors[flavor];
  78. if (ops == NULL || !try_module_get(ops->owner)) {
  79. spin_unlock(&rpc_authflavor_lock);
  80. goto out;
  81. }
  82. spin_unlock(&rpc_authflavor_lock);
  83. auth = ops->create(clnt, pseudoflavor);
  84. module_put(ops->owner);
  85. if (IS_ERR(auth))
  86. return auth;
  87. if (clnt->cl_auth)
  88. rpcauth_release(clnt->cl_auth);
  89. clnt->cl_auth = auth;
  90. out:
  91. return auth;
  92. }
  93. EXPORT_SYMBOL_GPL(rpcauth_create);
  94. void
  95. rpcauth_release(struct rpc_auth *auth)
  96. {
  97. if (!atomic_dec_and_test(&auth->au_count))
  98. return;
  99. auth->au_ops->destroy(auth);
  100. }
  101. static DEFINE_SPINLOCK(rpc_credcache_lock);
  102. static void
  103. rpcauth_unhash_cred_locked(struct rpc_cred *cred)
  104. {
  105. hlist_del_rcu(&cred->cr_hash);
  106. smp_mb__before_clear_bit();
  107. clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  108. }
  109. static int
  110. rpcauth_unhash_cred(struct rpc_cred *cred)
  111. {
  112. spinlock_t *cache_lock;
  113. int ret;
  114. cache_lock = &cred->cr_auth->au_credcache->lock;
  115. spin_lock(cache_lock);
  116. ret = atomic_read(&cred->cr_count) == 0;
  117. if (ret)
  118. rpcauth_unhash_cred_locked(cred);
  119. spin_unlock(cache_lock);
  120. return ret;
  121. }
  122. /*
  123. * Initialize RPC credential cache
  124. */
  125. int
  126. rpcauth_init_credcache(struct rpc_auth *auth)
  127. {
  128. struct rpc_cred_cache *new;
  129. int i;
  130. new = kmalloc(sizeof(*new), GFP_KERNEL);
  131. if (!new)
  132. return -ENOMEM;
  133. for (i = 0; i < RPC_CREDCACHE_NR; i++)
  134. INIT_HLIST_HEAD(&new->hashtable[i]);
  135. spin_lock_init(&new->lock);
  136. auth->au_credcache = new;
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  140. /*
  141. * Destroy a list of credentials
  142. */
  143. static inline
  144. void rpcauth_destroy_credlist(struct list_head *head)
  145. {
  146. struct rpc_cred *cred;
  147. while (!list_empty(head)) {
  148. cred = list_entry(head->next, struct rpc_cred, cr_lru);
  149. list_del_init(&cred->cr_lru);
  150. put_rpccred(cred);
  151. }
  152. }
  153. /*
  154. * Clear the RPC credential cache, and delete those credentials
  155. * that are not referenced.
  156. */
  157. void
  158. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  159. {
  160. LIST_HEAD(free);
  161. struct hlist_head *head;
  162. struct rpc_cred *cred;
  163. int i;
  164. spin_lock(&rpc_credcache_lock);
  165. spin_lock(&cache->lock);
  166. for (i = 0; i < RPC_CREDCACHE_NR; i++) {
  167. head = &cache->hashtable[i];
  168. while (!hlist_empty(head)) {
  169. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  170. get_rpccred(cred);
  171. if (!list_empty(&cred->cr_lru)) {
  172. list_del(&cred->cr_lru);
  173. number_cred_unused--;
  174. }
  175. list_add_tail(&cred->cr_lru, &free);
  176. rpcauth_unhash_cred_locked(cred);
  177. }
  178. }
  179. spin_unlock(&cache->lock);
  180. spin_unlock(&rpc_credcache_lock);
  181. rpcauth_destroy_credlist(&free);
  182. }
  183. /*
  184. * Destroy the RPC credential cache
  185. */
  186. void
  187. rpcauth_destroy_credcache(struct rpc_auth *auth)
  188. {
  189. struct rpc_cred_cache *cache = auth->au_credcache;
  190. if (cache) {
  191. auth->au_credcache = NULL;
  192. rpcauth_clear_credcache(cache);
  193. kfree(cache);
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
  197. #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
  198. /*
  199. * Remove stale credentials. Avoid sleeping inside the loop.
  200. */
  201. static int
  202. rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
  203. {
  204. spinlock_t *cache_lock;
  205. struct rpc_cred *cred, *next;
  206. unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
  207. list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
  208. /*
  209. * Enforce a 60 second garbage collection moratorium
  210. * Note that the cred_unused list must be time-ordered.
  211. */
  212. if (time_in_range(cred->cr_expire, expired, jiffies) &&
  213. test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
  214. return 0;
  215. list_del_init(&cred->cr_lru);
  216. number_cred_unused--;
  217. if (atomic_read(&cred->cr_count) != 0)
  218. continue;
  219. cache_lock = &cred->cr_auth->au_credcache->lock;
  220. spin_lock(cache_lock);
  221. if (atomic_read(&cred->cr_count) == 0) {
  222. get_rpccred(cred);
  223. list_add_tail(&cred->cr_lru, free);
  224. rpcauth_unhash_cred_locked(cred);
  225. nr_to_scan--;
  226. }
  227. spin_unlock(cache_lock);
  228. if (nr_to_scan == 0)
  229. break;
  230. }
  231. return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
  232. }
  233. /*
  234. * Run memory cache shrinker.
  235. */
  236. static int
  237. rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  238. {
  239. LIST_HEAD(free);
  240. int res;
  241. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  242. return (nr_to_scan == 0) ? 0 : -1;
  243. if (list_empty(&cred_unused))
  244. return 0;
  245. spin_lock(&rpc_credcache_lock);
  246. res = rpcauth_prune_expired(&free, nr_to_scan);
  247. spin_unlock(&rpc_credcache_lock);
  248. rpcauth_destroy_credlist(&free);
  249. return res;
  250. }
  251. /*
  252. * Look up a process' credentials in the authentication cache
  253. */
  254. struct rpc_cred *
  255. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  256. int flags)
  257. {
  258. LIST_HEAD(free);
  259. struct rpc_cred_cache *cache = auth->au_credcache;
  260. struct hlist_node *pos;
  261. struct rpc_cred *cred = NULL,
  262. *entry, *new;
  263. unsigned int nr;
  264. nr = hash_long(acred->uid, RPC_CREDCACHE_HASHBITS);
  265. rcu_read_lock();
  266. hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
  267. if (!entry->cr_ops->crmatch(acred, entry, flags))
  268. continue;
  269. spin_lock(&cache->lock);
  270. if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
  271. spin_unlock(&cache->lock);
  272. continue;
  273. }
  274. cred = get_rpccred(entry);
  275. spin_unlock(&cache->lock);
  276. break;
  277. }
  278. rcu_read_unlock();
  279. if (cred != NULL)
  280. goto found;
  281. new = auth->au_ops->crcreate(auth, acred, flags);
  282. if (IS_ERR(new)) {
  283. cred = new;
  284. goto out;
  285. }
  286. spin_lock(&cache->lock);
  287. hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
  288. if (!entry->cr_ops->crmatch(acred, entry, flags))
  289. continue;
  290. cred = get_rpccred(entry);
  291. break;
  292. }
  293. if (cred == NULL) {
  294. cred = new;
  295. set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  296. hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
  297. } else
  298. list_add_tail(&new->cr_lru, &free);
  299. spin_unlock(&cache->lock);
  300. found:
  301. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  302. cred->cr_ops->cr_init != NULL &&
  303. !(flags & RPCAUTH_LOOKUP_NEW)) {
  304. int res = cred->cr_ops->cr_init(auth, cred);
  305. if (res < 0) {
  306. put_rpccred(cred);
  307. cred = ERR_PTR(res);
  308. }
  309. }
  310. rpcauth_destroy_credlist(&free);
  311. out:
  312. return cred;
  313. }
  314. EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
  315. struct rpc_cred *
  316. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  317. {
  318. struct auth_cred acred;
  319. struct rpc_cred *ret;
  320. const struct cred *cred = current_cred();
  321. dprintk("RPC: looking up %s cred\n",
  322. auth->au_ops->au_name);
  323. memset(&acred, 0, sizeof(acred));
  324. acred.uid = cred->fsuid;
  325. acred.gid = cred->fsgid;
  326. acred.group_info = get_group_info(((struct cred *)cred)->group_info);
  327. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  328. put_group_info(acred.group_info);
  329. return ret;
  330. }
  331. void
  332. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  333. struct rpc_auth *auth, const struct rpc_credops *ops)
  334. {
  335. INIT_HLIST_NODE(&cred->cr_hash);
  336. INIT_LIST_HEAD(&cred->cr_lru);
  337. atomic_set(&cred->cr_count, 1);
  338. cred->cr_auth = auth;
  339. cred->cr_ops = ops;
  340. cred->cr_expire = jiffies;
  341. #ifdef RPC_DEBUG
  342. cred->cr_magic = RPCAUTH_CRED_MAGIC;
  343. #endif
  344. cred->cr_uid = acred->uid;
  345. }
  346. EXPORT_SYMBOL_GPL(rpcauth_init_cred);
  347. void
  348. rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
  349. {
  350. task->tk_msg.rpc_cred = get_rpccred(cred);
  351. dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
  352. cred->cr_auth->au_ops->au_name, cred);
  353. }
  354. EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
  355. static void
  356. rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
  357. {
  358. struct rpc_auth *auth = task->tk_client->cl_auth;
  359. struct auth_cred acred = {
  360. .uid = 0,
  361. .gid = 0,
  362. };
  363. struct rpc_cred *ret;
  364. dprintk("RPC: %5u looking up %s cred\n",
  365. task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
  366. ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  367. if (!IS_ERR(ret))
  368. task->tk_msg.rpc_cred = ret;
  369. else
  370. task->tk_status = PTR_ERR(ret);
  371. }
  372. static void
  373. rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
  374. {
  375. struct rpc_auth *auth = task->tk_client->cl_auth;
  376. struct rpc_cred *ret;
  377. dprintk("RPC: %5u looking up %s cred\n",
  378. task->tk_pid, auth->au_ops->au_name);
  379. ret = rpcauth_lookupcred(auth, lookupflags);
  380. if (!IS_ERR(ret))
  381. task->tk_msg.rpc_cred = ret;
  382. else
  383. task->tk_status = PTR_ERR(ret);
  384. }
  385. void
  386. rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
  387. {
  388. int lookupflags = 0;
  389. if (flags & RPC_TASK_ASYNC)
  390. lookupflags |= RPCAUTH_LOOKUP_NEW;
  391. if (cred != NULL)
  392. cred->cr_ops->crbind(task, cred, lookupflags);
  393. else if (flags & RPC_TASK_ROOTCREDS)
  394. rpcauth_bind_root_cred(task, lookupflags);
  395. else
  396. rpcauth_bind_new_cred(task, lookupflags);
  397. }
  398. void
  399. put_rpccred(struct rpc_cred *cred)
  400. {
  401. /* Fast path for unhashed credentials */
  402. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
  403. if (atomic_dec_and_test(&cred->cr_count))
  404. cred->cr_ops->crdestroy(cred);
  405. return;
  406. }
  407. if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
  408. return;
  409. if (!list_empty(&cred->cr_lru)) {
  410. number_cred_unused--;
  411. list_del_init(&cred->cr_lru);
  412. }
  413. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
  414. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  415. cred->cr_expire = jiffies;
  416. list_add_tail(&cred->cr_lru, &cred_unused);
  417. number_cred_unused++;
  418. goto out_nodestroy;
  419. }
  420. if (!rpcauth_unhash_cred(cred)) {
  421. /* We were hashed and someone looked us up... */
  422. goto out_nodestroy;
  423. }
  424. }
  425. spin_unlock(&rpc_credcache_lock);
  426. cred->cr_ops->crdestroy(cred);
  427. return;
  428. out_nodestroy:
  429. spin_unlock(&rpc_credcache_lock);
  430. }
  431. EXPORT_SYMBOL_GPL(put_rpccred);
  432. void
  433. rpcauth_unbindcred(struct rpc_task *task)
  434. {
  435. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  436. dprintk("RPC: %5u releasing %s cred %p\n",
  437. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  438. put_rpccred(cred);
  439. task->tk_msg.rpc_cred = NULL;
  440. }
  441. __be32 *
  442. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  443. {
  444. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  445. dprintk("RPC: %5u marshaling %s cred %p\n",
  446. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  447. return cred->cr_ops->crmarshal(task, p);
  448. }
  449. __be32 *
  450. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  451. {
  452. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  453. dprintk("RPC: %5u validating %s cred %p\n",
  454. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  455. return cred->cr_ops->crvalidate(task, p);
  456. }
  457. int
  458. rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
  459. __be32 *data, void *obj)
  460. {
  461. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  462. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  463. task->tk_pid, cred->cr_ops->cr_name, cred);
  464. if (cred->cr_ops->crwrap_req)
  465. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  466. /* By default, we encode the arguments normally. */
  467. return encode(rqstp, data, obj);
  468. }
  469. int
  470. rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
  471. __be32 *data, void *obj)
  472. {
  473. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  474. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  475. task->tk_pid, cred->cr_ops->cr_name, cred);
  476. if (cred->cr_ops->crunwrap_resp)
  477. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  478. data, obj);
  479. /* By default, we decode the arguments normally. */
  480. return decode(rqstp, data, obj);
  481. }
  482. int
  483. rpcauth_refreshcred(struct rpc_task *task)
  484. {
  485. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  486. int err;
  487. dprintk("RPC: %5u refreshing %s cred %p\n",
  488. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  489. err = cred->cr_ops->crrefresh(task);
  490. if (err < 0)
  491. task->tk_status = err;
  492. return err;
  493. }
  494. void
  495. rpcauth_invalcred(struct rpc_task *task)
  496. {
  497. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  498. dprintk("RPC: %5u invalidating %s cred %p\n",
  499. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  500. if (cred)
  501. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  502. }
  503. int
  504. rpcauth_uptodatecred(struct rpc_task *task)
  505. {
  506. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  507. return cred == NULL ||
  508. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  509. }
  510. static struct shrinker rpc_cred_shrinker = {
  511. .shrink = rpcauth_cache_shrinker,
  512. .seeks = DEFAULT_SEEKS,
  513. };
  514. void __init rpcauth_init_module(void)
  515. {
  516. rpc_init_authunix();
  517. rpc_init_generic_auth();
  518. register_shrinker(&rpc_cred_shrinker);
  519. }
  520. void __exit rpcauth_remove_module(void)
  521. {
  522. unregister_shrinker(&rpc_cred_shrinker);
  523. }