auth.c 13 KB

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