auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. unsigned int hashsize;
  130. int i;
  131. new = kmalloc(sizeof(*new), GFP_KERNEL);
  132. if (!new)
  133. return -ENOMEM;
  134. new->hashbits = RPC_CREDCACHE_HASHBITS;
  135. hashsize = 1U << new->hashbits;
  136. for (i = 0; i < hashsize; i++)
  137. INIT_HLIST_HEAD(&new->hashtable[i]);
  138. spin_lock_init(&new->lock);
  139. auth->au_credcache = new;
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  143. /*
  144. * Destroy a list of credentials
  145. */
  146. static inline
  147. void rpcauth_destroy_credlist(struct list_head *head)
  148. {
  149. struct rpc_cred *cred;
  150. while (!list_empty(head)) {
  151. cred = list_entry(head->next, struct rpc_cred, cr_lru);
  152. list_del_init(&cred->cr_lru);
  153. put_rpccred(cred);
  154. }
  155. }
  156. /*
  157. * Clear the RPC credential cache, and delete those credentials
  158. * that are not referenced.
  159. */
  160. void
  161. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  162. {
  163. LIST_HEAD(free);
  164. struct hlist_head *head;
  165. struct rpc_cred *cred;
  166. unsigned int hashsize = 1U << cache->hashbits;
  167. int i;
  168. spin_lock(&rpc_credcache_lock);
  169. spin_lock(&cache->lock);
  170. for (i = 0; i < hashsize; i++) {
  171. head = &cache->hashtable[i];
  172. while (!hlist_empty(head)) {
  173. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  174. get_rpccred(cred);
  175. if (!list_empty(&cred->cr_lru)) {
  176. list_del(&cred->cr_lru);
  177. number_cred_unused--;
  178. }
  179. list_add_tail(&cred->cr_lru, &free);
  180. rpcauth_unhash_cred_locked(cred);
  181. }
  182. }
  183. spin_unlock(&cache->lock);
  184. spin_unlock(&rpc_credcache_lock);
  185. rpcauth_destroy_credlist(&free);
  186. }
  187. /*
  188. * Destroy the RPC credential cache
  189. */
  190. void
  191. rpcauth_destroy_credcache(struct rpc_auth *auth)
  192. {
  193. struct rpc_cred_cache *cache = auth->au_credcache;
  194. if (cache) {
  195. auth->au_credcache = NULL;
  196. rpcauth_clear_credcache(cache);
  197. kfree(cache);
  198. }
  199. }
  200. EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
  201. #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
  202. /*
  203. * Remove stale credentials. Avoid sleeping inside the loop.
  204. */
  205. static int
  206. rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
  207. {
  208. spinlock_t *cache_lock;
  209. struct rpc_cred *cred, *next;
  210. unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
  211. list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
  212. if (nr_to_scan-- == 0)
  213. break;
  214. /*
  215. * Enforce a 60 second garbage collection moratorium
  216. * Note that the cred_unused list must be time-ordered.
  217. */
  218. if (time_in_range(cred->cr_expire, expired, jiffies) &&
  219. test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
  220. return 0;
  221. list_del_init(&cred->cr_lru);
  222. number_cred_unused--;
  223. if (atomic_read(&cred->cr_count) != 0)
  224. continue;
  225. cache_lock = &cred->cr_auth->au_credcache->lock;
  226. spin_lock(cache_lock);
  227. if (atomic_read(&cred->cr_count) == 0) {
  228. get_rpccred(cred);
  229. list_add_tail(&cred->cr_lru, free);
  230. rpcauth_unhash_cred_locked(cred);
  231. }
  232. spin_unlock(cache_lock);
  233. }
  234. return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
  235. }
  236. /*
  237. * Run memory cache shrinker.
  238. */
  239. static int
  240. rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  241. {
  242. LIST_HEAD(free);
  243. int res;
  244. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  245. return (nr_to_scan == 0) ? 0 : -1;
  246. if (list_empty(&cred_unused))
  247. return 0;
  248. spin_lock(&rpc_credcache_lock);
  249. res = rpcauth_prune_expired(&free, nr_to_scan);
  250. spin_unlock(&rpc_credcache_lock);
  251. rpcauth_destroy_credlist(&free);
  252. return res;
  253. }
  254. /*
  255. * Look up a process' credentials in the authentication cache
  256. */
  257. struct rpc_cred *
  258. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  259. int flags)
  260. {
  261. LIST_HEAD(free);
  262. struct rpc_cred_cache *cache = auth->au_credcache;
  263. struct hlist_node *pos;
  264. struct rpc_cred *cred = NULL,
  265. *entry, *new;
  266. unsigned int nr;
  267. nr = hash_long(acred->uid, cache->hashbits);
  268. rcu_read_lock();
  269. hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
  270. if (!entry->cr_ops->crmatch(acred, entry, flags))
  271. continue;
  272. spin_lock(&cache->lock);
  273. if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
  274. spin_unlock(&cache->lock);
  275. continue;
  276. }
  277. cred = get_rpccred(entry);
  278. spin_unlock(&cache->lock);
  279. break;
  280. }
  281. rcu_read_unlock();
  282. if (cred != NULL)
  283. goto found;
  284. new = auth->au_ops->crcreate(auth, acred, flags);
  285. if (IS_ERR(new)) {
  286. cred = new;
  287. goto out;
  288. }
  289. spin_lock(&cache->lock);
  290. hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
  291. if (!entry->cr_ops->crmatch(acred, entry, flags))
  292. continue;
  293. cred = get_rpccred(entry);
  294. break;
  295. }
  296. if (cred == NULL) {
  297. cred = new;
  298. set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  299. hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
  300. } else
  301. list_add_tail(&new->cr_lru, &free);
  302. spin_unlock(&cache->lock);
  303. found:
  304. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  305. cred->cr_ops->cr_init != NULL &&
  306. !(flags & RPCAUTH_LOOKUP_NEW)) {
  307. int res = cred->cr_ops->cr_init(auth, cred);
  308. if (res < 0) {
  309. put_rpccred(cred);
  310. cred = ERR_PTR(res);
  311. }
  312. }
  313. rpcauth_destroy_credlist(&free);
  314. out:
  315. return cred;
  316. }
  317. EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
  318. struct rpc_cred *
  319. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  320. {
  321. struct auth_cred acred;
  322. struct rpc_cred *ret;
  323. const struct cred *cred = current_cred();
  324. dprintk("RPC: looking up %s cred\n",
  325. auth->au_ops->au_name);
  326. memset(&acred, 0, sizeof(acred));
  327. acred.uid = cred->fsuid;
  328. acred.gid = cred->fsgid;
  329. acred.group_info = get_group_info(((struct cred *)cred)->group_info);
  330. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  331. put_group_info(acred.group_info);
  332. return ret;
  333. }
  334. void
  335. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  336. struct rpc_auth *auth, const struct rpc_credops *ops)
  337. {
  338. INIT_HLIST_NODE(&cred->cr_hash);
  339. INIT_LIST_HEAD(&cred->cr_lru);
  340. atomic_set(&cred->cr_count, 1);
  341. cred->cr_auth = auth;
  342. cred->cr_ops = ops;
  343. cred->cr_expire = jiffies;
  344. #ifdef RPC_DEBUG
  345. cred->cr_magic = RPCAUTH_CRED_MAGIC;
  346. #endif
  347. cred->cr_uid = acred->uid;
  348. }
  349. EXPORT_SYMBOL_GPL(rpcauth_init_cred);
  350. void
  351. rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
  352. {
  353. task->tk_msg.rpc_cred = get_rpccred(cred);
  354. dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
  355. cred->cr_auth->au_ops->au_name, cred);
  356. }
  357. EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
  358. static void
  359. rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
  360. {
  361. struct rpc_auth *auth = task->tk_client->cl_auth;
  362. struct auth_cred acred = {
  363. .uid = 0,
  364. .gid = 0,
  365. };
  366. struct rpc_cred *ret;
  367. dprintk("RPC: %5u looking up %s cred\n",
  368. task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
  369. ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  370. if (!IS_ERR(ret))
  371. task->tk_msg.rpc_cred = ret;
  372. else
  373. task->tk_status = PTR_ERR(ret);
  374. }
  375. static void
  376. rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
  377. {
  378. struct rpc_auth *auth = task->tk_client->cl_auth;
  379. struct rpc_cred *ret;
  380. dprintk("RPC: %5u looking up %s cred\n",
  381. task->tk_pid, auth->au_ops->au_name);
  382. ret = rpcauth_lookupcred(auth, lookupflags);
  383. if (!IS_ERR(ret))
  384. task->tk_msg.rpc_cred = ret;
  385. else
  386. task->tk_status = PTR_ERR(ret);
  387. }
  388. void
  389. rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
  390. {
  391. int lookupflags = 0;
  392. if (flags & RPC_TASK_ASYNC)
  393. lookupflags |= RPCAUTH_LOOKUP_NEW;
  394. if (cred != NULL)
  395. cred->cr_ops->crbind(task, cred, lookupflags);
  396. else if (flags & RPC_TASK_ROOTCREDS)
  397. rpcauth_bind_root_cred(task, lookupflags);
  398. else
  399. rpcauth_bind_new_cred(task, lookupflags);
  400. }
  401. void
  402. put_rpccred(struct rpc_cred *cred)
  403. {
  404. /* Fast path for unhashed credentials */
  405. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
  406. if (atomic_dec_and_test(&cred->cr_count))
  407. cred->cr_ops->crdestroy(cred);
  408. return;
  409. }
  410. if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
  411. return;
  412. if (!list_empty(&cred->cr_lru)) {
  413. number_cred_unused--;
  414. list_del_init(&cred->cr_lru);
  415. }
  416. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
  417. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  418. cred->cr_expire = jiffies;
  419. list_add_tail(&cred->cr_lru, &cred_unused);
  420. number_cred_unused++;
  421. goto out_nodestroy;
  422. }
  423. if (!rpcauth_unhash_cred(cred)) {
  424. /* We were hashed and someone looked us up... */
  425. goto out_nodestroy;
  426. }
  427. }
  428. spin_unlock(&rpc_credcache_lock);
  429. cred->cr_ops->crdestroy(cred);
  430. return;
  431. out_nodestroy:
  432. spin_unlock(&rpc_credcache_lock);
  433. }
  434. EXPORT_SYMBOL_GPL(put_rpccred);
  435. void
  436. rpcauth_unbindcred(struct rpc_task *task)
  437. {
  438. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  439. dprintk("RPC: %5u releasing %s cred %p\n",
  440. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  441. put_rpccred(cred);
  442. task->tk_msg.rpc_cred = NULL;
  443. }
  444. __be32 *
  445. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  446. {
  447. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  448. dprintk("RPC: %5u marshaling %s cred %p\n",
  449. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  450. return cred->cr_ops->crmarshal(task, p);
  451. }
  452. __be32 *
  453. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  454. {
  455. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  456. dprintk("RPC: %5u validating %s cred %p\n",
  457. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  458. return cred->cr_ops->crvalidate(task, p);
  459. }
  460. int
  461. rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
  462. __be32 *data, void *obj)
  463. {
  464. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  465. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  466. task->tk_pid, cred->cr_ops->cr_name, cred);
  467. if (cred->cr_ops->crwrap_req)
  468. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  469. /* By default, we encode the arguments normally. */
  470. return encode(rqstp, data, obj);
  471. }
  472. int
  473. rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
  474. __be32 *data, void *obj)
  475. {
  476. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  477. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  478. task->tk_pid, cred->cr_ops->cr_name, cred);
  479. if (cred->cr_ops->crunwrap_resp)
  480. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  481. data, obj);
  482. /* By default, we decode the arguments normally. */
  483. return decode(rqstp, data, obj);
  484. }
  485. int
  486. rpcauth_refreshcred(struct rpc_task *task)
  487. {
  488. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  489. int err;
  490. dprintk("RPC: %5u refreshing %s cred %p\n",
  491. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  492. err = cred->cr_ops->crrefresh(task);
  493. if (err < 0)
  494. task->tk_status = err;
  495. return err;
  496. }
  497. void
  498. rpcauth_invalcred(struct rpc_task *task)
  499. {
  500. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  501. dprintk("RPC: %5u invalidating %s cred %p\n",
  502. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  503. if (cred)
  504. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  505. }
  506. int
  507. rpcauth_uptodatecred(struct rpc_task *task)
  508. {
  509. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  510. return cred == NULL ||
  511. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  512. }
  513. static struct shrinker rpc_cred_shrinker = {
  514. .shrink = rpcauth_cache_shrinker,
  515. .seeks = DEFAULT_SEEKS,
  516. };
  517. int __init rpcauth_init_module(void)
  518. {
  519. int err;
  520. err = rpc_init_authunix();
  521. if (err < 0)
  522. goto out1;
  523. err = rpc_init_generic_auth();
  524. if (err < 0)
  525. goto out2;
  526. register_shrinker(&rpc_cred_shrinker);
  527. return 0;
  528. out2:
  529. rpc_destroy_authunix();
  530. out1:
  531. return err;
  532. }
  533. void __exit rpcauth_remove_module(void)
  534. {
  535. rpc_destroy_authunix();
  536. rpc_destroy_generic_auth();
  537. unregister_shrinker(&rpc_cred_shrinker);
  538. }