auth.c 13 KB

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