auth.c 13 KB

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