auth.c 13 KB

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