auth.c 12 KB

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