auth.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/auth.c
  4. *
  5. * Generic RPC client authentication API.
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/cred.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/errno.h>
  15. #include <linux/hash.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/gss_api.h>
  18. #include <linux/spinlock.h>
  19. #include <trace/events/sunrpc.h>
  20. #define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
  21. struct rpc_cred_cache {
  22. struct hlist_head *hashtable;
  23. unsigned int hashbits;
  24. spinlock_t lock;
  25. };
  26. static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
  27. static const struct rpc_authops __rcu *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
  28. [RPC_AUTH_NULL] = (const struct rpc_authops __force __rcu *)&authnull_ops,
  29. [RPC_AUTH_UNIX] = (const struct rpc_authops __force __rcu *)&authunix_ops,
  30. [RPC_AUTH_TLS] = (const struct rpc_authops __force __rcu *)&authtls_ops,
  31. };
  32. static LIST_HEAD(cred_unused);
  33. static unsigned long number_cred_unused;
  34. static struct cred machine_cred = {
  35. .usage = ATOMIC_INIT(1),
  36. };
  37. /*
  38. * Return the machine_cred pointer to be used whenever
  39. * the a generic machine credential is needed.
  40. */
  41. const struct cred *rpc_machine_cred(void)
  42. {
  43. return &machine_cred;
  44. }
  45. EXPORT_SYMBOL_GPL(rpc_machine_cred);
  46. #define MAX_HASHTABLE_BITS (14)
  47. static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
  48. {
  49. unsigned long num;
  50. unsigned int nbits;
  51. int ret;
  52. if (!val)
  53. goto out_inval;
  54. ret = kstrtoul(val, 0, &num);
  55. if (ret)
  56. goto out_inval;
  57. nbits = fls(num - 1);
  58. if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
  59. goto out_inval;
  60. *(unsigned int *)kp->arg = nbits;
  61. return 0;
  62. out_inval:
  63. return -EINVAL;
  64. }
  65. static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
  66. {
  67. unsigned int nbits;
  68. nbits = *(unsigned int *)kp->arg;
  69. return sprintf(buffer, "%u\n", 1U << nbits);
  70. }
  71. #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
  72. static const struct kernel_param_ops param_ops_hashtbl_sz = {
  73. .set = param_set_hashtbl_sz,
  74. .get = param_get_hashtbl_sz,
  75. };
  76. module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
  77. MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
  78. static unsigned long auth_max_cred_cachesize = ULONG_MAX;
  79. module_param(auth_max_cred_cachesize, ulong, 0644);
  80. MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
  81. static u32
  82. pseudoflavor_to_flavor(u32 flavor) {
  83. if (flavor > RPC_AUTH_MAXFLAVOR)
  84. return RPC_AUTH_GSS;
  85. return flavor;
  86. }
  87. int
  88. rpcauth_register(const struct rpc_authops *ops)
  89. {
  90. const struct rpc_authops *old;
  91. rpc_authflavor_t flavor;
  92. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  93. return -EINVAL;
  94. old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], NULL, ops);
  95. if (old == NULL || old == ops)
  96. return 0;
  97. return -EPERM;
  98. }
  99. EXPORT_SYMBOL_GPL(rpcauth_register);
  100. int
  101. rpcauth_unregister(const struct rpc_authops *ops)
  102. {
  103. const struct rpc_authops *old;
  104. rpc_authflavor_t flavor;
  105. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  106. return -EINVAL;
  107. old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], ops, NULL);
  108. if (old == ops || old == NULL)
  109. return 0;
  110. return -EPERM;
  111. }
  112. EXPORT_SYMBOL_GPL(rpcauth_unregister);
  113. static const struct rpc_authops *
  114. rpcauth_get_authops(rpc_authflavor_t flavor)
  115. {
  116. const struct rpc_authops *ops;
  117. if (flavor >= RPC_AUTH_MAXFLAVOR)
  118. return NULL;
  119. rcu_read_lock();
  120. ops = rcu_dereference(auth_flavors[flavor]);
  121. if (ops == NULL) {
  122. rcu_read_unlock();
  123. request_module("rpc-auth-%u", flavor);
  124. rcu_read_lock();
  125. ops = rcu_dereference(auth_flavors[flavor]);
  126. if (ops == NULL)
  127. goto out;
  128. }
  129. if (!try_module_get(ops->owner))
  130. ops = NULL;
  131. out:
  132. rcu_read_unlock();
  133. return ops;
  134. }
  135. static void
  136. rpcauth_put_authops(const struct rpc_authops *ops)
  137. {
  138. module_put(ops->owner);
  139. }
  140. /**
  141. * rpcauth_get_pseudoflavor - check if security flavor is supported
  142. * @flavor: a security flavor
  143. * @info: a GSS mech OID, quality of protection, and service value
  144. *
  145. * Verifies that an appropriate kernel module is available or already loaded.
  146. * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
  147. * not supported locally.
  148. */
  149. rpc_authflavor_t
  150. rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
  151. {
  152. const struct rpc_authops *ops = rpcauth_get_authops(flavor);
  153. rpc_authflavor_t pseudoflavor;
  154. if (!ops)
  155. return RPC_AUTH_MAXFLAVOR;
  156. pseudoflavor = flavor;
  157. if (ops->info2flavor != NULL)
  158. pseudoflavor = ops->info2flavor(info);
  159. rpcauth_put_authops(ops);
  160. return pseudoflavor;
  161. }
  162. EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
  163. /**
  164. * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
  165. * @pseudoflavor: GSS pseudoflavor to match
  166. * @info: rpcsec_gss_info structure to fill in
  167. *
  168. * Returns zero and fills in "info" if pseudoflavor matches a
  169. * supported mechanism.
  170. */
  171. int
  172. rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
  173. {
  174. rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
  175. const struct rpc_authops *ops;
  176. int result;
  177. ops = rpcauth_get_authops(flavor);
  178. if (ops == NULL)
  179. return -ENOENT;
  180. result = -ENOENT;
  181. if (ops->flavor2info != NULL)
  182. result = ops->flavor2info(pseudoflavor, info);
  183. rpcauth_put_authops(ops);
  184. return result;
  185. }
  186. EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
  187. struct rpc_auth *
  188. rpcauth_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  189. {
  190. struct rpc_auth *auth = ERR_PTR(-EINVAL);
  191. const struct rpc_authops *ops;
  192. u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
  193. ops = rpcauth_get_authops(flavor);
  194. if (ops == NULL)
  195. goto out;
  196. auth = ops->create(args, clnt);
  197. rpcauth_put_authops(ops);
  198. if (IS_ERR(auth))
  199. return auth;
  200. if (clnt->cl_auth)
  201. rpcauth_release(clnt->cl_auth);
  202. clnt->cl_auth = auth;
  203. out:
  204. return auth;
  205. }
  206. EXPORT_SYMBOL_GPL(rpcauth_create);
  207. void
  208. rpcauth_release(struct rpc_auth *auth)
  209. {
  210. if (!refcount_dec_and_test(&auth->au_count))
  211. return;
  212. auth->au_ops->destroy(auth);
  213. }
  214. static DEFINE_SPINLOCK(rpc_credcache_lock);
  215. /*
  216. * On success, the caller is responsible for freeing the reference
  217. * held by the hashtable
  218. */
  219. static bool
  220. rpcauth_unhash_cred_locked(struct rpc_cred *cred)
  221. {
  222. if (!test_and_clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
  223. return false;
  224. hlist_del_rcu(&cred->cr_hash);
  225. return true;
  226. }
  227. static bool
  228. rpcauth_unhash_cred(struct rpc_cred *cred)
  229. {
  230. spinlock_t *cache_lock;
  231. bool ret;
  232. if (!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
  233. return false;
  234. cache_lock = &cred->cr_auth->au_credcache->lock;
  235. spin_lock(cache_lock);
  236. ret = rpcauth_unhash_cred_locked(cred);
  237. spin_unlock(cache_lock);
  238. return ret;
  239. }
  240. /*
  241. * Initialize RPC credential cache
  242. */
  243. int
  244. rpcauth_init_credcache(struct rpc_auth *auth)
  245. {
  246. struct rpc_cred_cache *new;
  247. unsigned int hashsize;
  248. new = kmalloc_obj(*new);
  249. if (!new)
  250. goto out_nocache;
  251. new->hashbits = auth_hashbits;
  252. hashsize = 1U << new->hashbits;
  253. new->hashtable = kzalloc_objs(new->hashtable[0], hashsize);
  254. if (!new->hashtable)
  255. goto out_nohashtbl;
  256. spin_lock_init(&new->lock);
  257. auth->au_credcache = new;
  258. return 0;
  259. out_nohashtbl:
  260. kfree(new);
  261. out_nocache:
  262. return -ENOMEM;
  263. }
  264. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  265. char *
  266. rpcauth_stringify_acceptor(struct rpc_cred *cred)
  267. {
  268. if (!cred->cr_ops->crstringify_acceptor)
  269. return NULL;
  270. return cred->cr_ops->crstringify_acceptor(cred);
  271. }
  272. EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
  273. /*
  274. * Destroy a list of credentials
  275. */
  276. static inline
  277. void rpcauth_destroy_credlist(struct list_head *head)
  278. {
  279. struct rpc_cred *cred;
  280. while (!list_empty(head)) {
  281. cred = list_entry(head->next, struct rpc_cred, cr_lru);
  282. list_del_init(&cred->cr_lru);
  283. put_rpccred(cred);
  284. }
  285. }
  286. static void
  287. rpcauth_lru_add_locked(struct rpc_cred *cred)
  288. {
  289. if (!list_empty(&cred->cr_lru))
  290. return;
  291. number_cred_unused++;
  292. list_add_tail(&cred->cr_lru, &cred_unused);
  293. }
  294. static void
  295. rpcauth_lru_add(struct rpc_cred *cred)
  296. {
  297. if (!list_empty(&cred->cr_lru))
  298. return;
  299. spin_lock(&rpc_credcache_lock);
  300. rpcauth_lru_add_locked(cred);
  301. spin_unlock(&rpc_credcache_lock);
  302. }
  303. static void
  304. rpcauth_lru_remove_locked(struct rpc_cred *cred)
  305. {
  306. if (list_empty(&cred->cr_lru))
  307. return;
  308. number_cred_unused--;
  309. list_del_init(&cred->cr_lru);
  310. }
  311. static void
  312. rpcauth_lru_remove(struct rpc_cred *cred)
  313. {
  314. if (list_empty(&cred->cr_lru))
  315. return;
  316. spin_lock(&rpc_credcache_lock);
  317. rpcauth_lru_remove_locked(cred);
  318. spin_unlock(&rpc_credcache_lock);
  319. }
  320. /*
  321. * Clear the RPC credential cache, and delete those credentials
  322. * that are not referenced.
  323. */
  324. void
  325. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  326. {
  327. LIST_HEAD(free);
  328. struct hlist_head *head;
  329. struct rpc_cred *cred;
  330. unsigned int hashsize = 1U << cache->hashbits;
  331. int i;
  332. spin_lock(&rpc_credcache_lock);
  333. spin_lock(&cache->lock);
  334. for (i = 0; i < hashsize; i++) {
  335. head = &cache->hashtable[i];
  336. while (!hlist_empty(head)) {
  337. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  338. rpcauth_unhash_cred_locked(cred);
  339. /* Note: We now hold a reference to cred */
  340. rpcauth_lru_remove_locked(cred);
  341. list_add_tail(&cred->cr_lru, &free);
  342. }
  343. }
  344. spin_unlock(&cache->lock);
  345. spin_unlock(&rpc_credcache_lock);
  346. rpcauth_destroy_credlist(&free);
  347. }
  348. /*
  349. * Destroy the RPC credential cache
  350. */
  351. void
  352. rpcauth_destroy_credcache(struct rpc_auth *auth)
  353. {
  354. struct rpc_cred_cache *cache = auth->au_credcache;
  355. if (cache) {
  356. auth->au_credcache = NULL;
  357. rpcauth_clear_credcache(cache);
  358. kfree(cache->hashtable);
  359. kfree(cache);
  360. }
  361. }
  362. EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
  363. #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
  364. /*
  365. * Remove stale credentials. Avoid sleeping inside the loop.
  366. */
  367. static long
  368. rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
  369. {
  370. struct rpc_cred *cred, *next;
  371. unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
  372. long freed = 0;
  373. list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
  374. if (nr_to_scan-- == 0)
  375. break;
  376. if (refcount_read(&cred->cr_count) > 1) {
  377. rpcauth_lru_remove_locked(cred);
  378. continue;
  379. }
  380. /*
  381. * Enforce a 60 second garbage collection moratorium
  382. * Note that the cred_unused list must be time-ordered.
  383. */
  384. if (time_in_range(cred->cr_expire, expired, jiffies))
  385. continue;
  386. if (!rpcauth_unhash_cred(cred))
  387. continue;
  388. rpcauth_lru_remove_locked(cred);
  389. freed++;
  390. list_add_tail(&cred->cr_lru, free);
  391. }
  392. return freed ? freed : SHRINK_STOP;
  393. }
  394. static unsigned long
  395. rpcauth_cache_do_shrink(int nr_to_scan)
  396. {
  397. LIST_HEAD(free);
  398. unsigned long freed;
  399. spin_lock(&rpc_credcache_lock);
  400. freed = rpcauth_prune_expired(&free, nr_to_scan);
  401. spin_unlock(&rpc_credcache_lock);
  402. rpcauth_destroy_credlist(&free);
  403. return freed;
  404. }
  405. /*
  406. * Run memory cache shrinker.
  407. */
  408. static unsigned long
  409. rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  410. {
  411. if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  412. return SHRINK_STOP;
  413. /* nothing left, don't come back */
  414. if (list_empty(&cred_unused))
  415. return SHRINK_STOP;
  416. return rpcauth_cache_do_shrink(sc->nr_to_scan);
  417. }
  418. static unsigned long
  419. rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  420. {
  421. return number_cred_unused;
  422. }
  423. static void
  424. rpcauth_cache_enforce_limit(void)
  425. {
  426. unsigned long diff;
  427. unsigned int nr_to_scan;
  428. if (number_cred_unused <= auth_max_cred_cachesize)
  429. return;
  430. diff = number_cred_unused - auth_max_cred_cachesize;
  431. nr_to_scan = 100;
  432. if (diff < nr_to_scan)
  433. nr_to_scan = diff;
  434. rpcauth_cache_do_shrink(nr_to_scan);
  435. }
  436. /*
  437. * Look up a process' credentials in the authentication cache
  438. */
  439. struct rpc_cred *
  440. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  441. int flags, gfp_t gfp)
  442. {
  443. LIST_HEAD(free);
  444. struct rpc_cred_cache *cache = auth->au_credcache;
  445. struct rpc_cred *cred = NULL,
  446. *entry, *new;
  447. unsigned int nr;
  448. nr = auth->au_ops->hash_cred(acred, cache->hashbits);
  449. rcu_read_lock();
  450. hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
  451. if (!entry->cr_ops->crmatch(acred, entry, flags))
  452. continue;
  453. cred = get_rpccred(entry);
  454. if (cred)
  455. break;
  456. }
  457. rcu_read_unlock();
  458. if (cred != NULL)
  459. goto found;
  460. new = auth->au_ops->crcreate(auth, acred, flags, gfp);
  461. if (IS_ERR(new)) {
  462. cred = new;
  463. goto out;
  464. }
  465. spin_lock(&cache->lock);
  466. hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
  467. if (!entry->cr_ops->crmatch(acred, entry, flags))
  468. continue;
  469. cred = get_rpccred(entry);
  470. if (cred)
  471. break;
  472. }
  473. if (cred == NULL) {
  474. cred = new;
  475. set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  476. refcount_inc(&cred->cr_count);
  477. hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
  478. } else
  479. list_add_tail(&new->cr_lru, &free);
  480. spin_unlock(&cache->lock);
  481. rpcauth_cache_enforce_limit();
  482. found:
  483. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  484. cred->cr_ops->cr_init != NULL &&
  485. !(flags & RPCAUTH_LOOKUP_NEW)) {
  486. int res = cred->cr_ops->cr_init(auth, cred);
  487. if (res < 0) {
  488. put_rpccred(cred);
  489. cred = ERR_PTR(res);
  490. }
  491. }
  492. rpcauth_destroy_credlist(&free);
  493. out:
  494. return cred;
  495. }
  496. EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
  497. struct rpc_cred *
  498. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  499. {
  500. struct auth_cred acred;
  501. struct rpc_cred *ret;
  502. const struct cred *cred = current_cred();
  503. memset(&acred, 0, sizeof(acred));
  504. acred.cred = cred;
  505. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
  509. void
  510. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  511. struct rpc_auth *auth, const struct rpc_credops *ops)
  512. {
  513. INIT_HLIST_NODE(&cred->cr_hash);
  514. INIT_LIST_HEAD(&cred->cr_lru);
  515. refcount_set(&cred->cr_count, 1);
  516. cred->cr_auth = auth;
  517. cred->cr_flags = 0;
  518. cred->cr_ops = ops;
  519. cred->cr_expire = jiffies;
  520. cred->cr_cred = get_cred(acred->cred);
  521. }
  522. EXPORT_SYMBOL_GPL(rpcauth_init_cred);
  523. static struct rpc_cred *
  524. rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
  525. {
  526. struct rpc_auth *auth = task->tk_client->cl_auth;
  527. struct auth_cred acred = {
  528. .cred = get_task_cred(&init_task),
  529. };
  530. struct rpc_cred *ret;
  531. if (RPC_IS_ASYNC(task))
  532. lookupflags |= RPCAUTH_LOOKUP_ASYNC;
  533. ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  534. put_cred(acred.cred);
  535. return ret;
  536. }
  537. static struct rpc_cred *
  538. rpcauth_bind_machine_cred(struct rpc_task *task, int lookupflags)
  539. {
  540. struct rpc_auth *auth = task->tk_client->cl_auth;
  541. struct auth_cred acred = {
  542. .principal = task->tk_client->cl_principal,
  543. .cred = init_task.cred,
  544. };
  545. if (!acred.principal)
  546. return NULL;
  547. if (RPC_IS_ASYNC(task))
  548. lookupflags |= RPCAUTH_LOOKUP_ASYNC;
  549. return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  550. }
  551. static struct rpc_cred *
  552. rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
  553. {
  554. struct rpc_auth *auth = task->tk_client->cl_auth;
  555. return rpcauth_lookupcred(auth, lookupflags);
  556. }
  557. static int
  558. rpcauth_bindcred(struct rpc_task *task, const struct cred *cred, int flags)
  559. {
  560. struct rpc_rqst *req = task->tk_rqstp;
  561. struct rpc_cred *new = NULL;
  562. int lookupflags = 0;
  563. struct rpc_auth *auth = task->tk_client->cl_auth;
  564. struct auth_cred acred = {
  565. .cred = cred,
  566. };
  567. if (flags & RPC_TASK_ASYNC)
  568. lookupflags |= RPCAUTH_LOOKUP_NEW | RPCAUTH_LOOKUP_ASYNC;
  569. if (task->tk_op_cred)
  570. /* Task must use exactly this rpc_cred */
  571. new = get_rpccred(task->tk_op_cred);
  572. else if (cred != NULL && cred != &machine_cred)
  573. new = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  574. else if (cred == &machine_cred)
  575. new = rpcauth_bind_machine_cred(task, lookupflags);
  576. /* If machine cred couldn't be bound, try a root cred */
  577. if (new)
  578. ;
  579. else if (cred == &machine_cred)
  580. new = rpcauth_bind_root_cred(task, lookupflags);
  581. else if (flags & RPC_TASK_NULLCREDS)
  582. new = authnull_ops.lookup_cred(NULL, NULL, 0);
  583. else
  584. new = rpcauth_bind_new_cred(task, lookupflags);
  585. if (IS_ERR(new))
  586. return PTR_ERR(new);
  587. put_rpccred(req->rq_cred);
  588. req->rq_cred = new;
  589. return 0;
  590. }
  591. void
  592. put_rpccred(struct rpc_cred *cred)
  593. {
  594. if (cred == NULL)
  595. return;
  596. rcu_read_lock();
  597. if (refcount_dec_and_test(&cred->cr_count))
  598. goto destroy;
  599. if (refcount_read(&cred->cr_count) != 1 ||
  600. !test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
  601. goto out;
  602. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  603. cred->cr_expire = jiffies;
  604. rpcauth_lru_add(cred);
  605. /* Race breaker */
  606. if (unlikely(!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)))
  607. rpcauth_lru_remove(cred);
  608. } else if (rpcauth_unhash_cred(cred)) {
  609. rpcauth_lru_remove(cred);
  610. if (refcount_dec_and_test(&cred->cr_count))
  611. goto destroy;
  612. }
  613. out:
  614. rcu_read_unlock();
  615. return;
  616. destroy:
  617. rcu_read_unlock();
  618. cred->cr_ops->crdestroy(cred);
  619. }
  620. EXPORT_SYMBOL_GPL(put_rpccred);
  621. /**
  622. * rpcauth_marshcred - Append RPC credential to end of @xdr
  623. * @task: controlling RPC task
  624. * @xdr: xdr_stream containing initial portion of RPC Call header
  625. *
  626. * On success, an appropriate verifier is added to @xdr, @xdr is
  627. * updated to point past the verifier, and zero is returned.
  628. * Otherwise, @xdr is in an undefined state and a negative errno
  629. * is returned.
  630. */
  631. int rpcauth_marshcred(struct rpc_task *task, struct xdr_stream *xdr)
  632. {
  633. const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
  634. return ops->crmarshal(task, xdr);
  635. }
  636. /**
  637. * rpcauth_wrap_req_encode - XDR encode the RPC procedure
  638. * @task: controlling RPC task
  639. * @xdr: stream where on-the-wire bytes are to be marshalled
  640. *
  641. * On success, @xdr contains the encoded and wrapped message.
  642. * Otherwise, @xdr is in an undefined state.
  643. */
  644. int rpcauth_wrap_req_encode(struct rpc_task *task, struct xdr_stream *xdr)
  645. {
  646. kxdreproc_t encode = task->tk_msg.rpc_proc->p_encode;
  647. encode(task->tk_rqstp, xdr, task->tk_msg.rpc_argp);
  648. return 0;
  649. }
  650. EXPORT_SYMBOL_GPL(rpcauth_wrap_req_encode);
  651. /**
  652. * rpcauth_wrap_req - XDR encode and wrap the RPC procedure
  653. * @task: controlling RPC task
  654. * @xdr: stream where on-the-wire bytes are to be marshalled
  655. *
  656. * On success, @xdr contains the encoded and wrapped message,
  657. * and zero is returned. Otherwise, @xdr is in an undefined
  658. * state and a negative errno is returned.
  659. */
  660. int rpcauth_wrap_req(struct rpc_task *task, struct xdr_stream *xdr)
  661. {
  662. const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
  663. return ops->crwrap_req(task, xdr);
  664. }
  665. /**
  666. * rpcauth_checkverf - Validate verifier in RPC Reply header
  667. * @task: controlling RPC task
  668. * @xdr: xdr_stream containing RPC Reply header
  669. *
  670. * Return values:
  671. * %0: Verifier is valid. @xdr now points past the verifier.
  672. * %-EIO: Verifier is corrupted or message ended early.
  673. * %-EACCES: Verifier is intact but not valid.
  674. * %-EPROTONOSUPPORT: Server does not support the requested auth type.
  675. *
  676. * When a negative errno is returned, @xdr is left in an undefined
  677. * state.
  678. */
  679. int
  680. rpcauth_checkverf(struct rpc_task *task, struct xdr_stream *xdr)
  681. {
  682. const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
  683. return ops->crvalidate(task, xdr);
  684. }
  685. /**
  686. * rpcauth_unwrap_resp_decode - Invoke XDR decode function
  687. * @task: controlling RPC task
  688. * @xdr: stream where the Reply message resides
  689. *
  690. * Returns zero on success; otherwise a negative errno is returned.
  691. */
  692. int
  693. rpcauth_unwrap_resp_decode(struct rpc_task *task, struct xdr_stream *xdr)
  694. {
  695. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  696. return decode(task->tk_rqstp, xdr, task->tk_msg.rpc_resp);
  697. }
  698. EXPORT_SYMBOL_GPL(rpcauth_unwrap_resp_decode);
  699. /**
  700. * rpcauth_unwrap_resp - Invoke unwrap and decode function for the cred
  701. * @task: controlling RPC task
  702. * @xdr: stream where the Reply message resides
  703. *
  704. * Returns zero on success; otherwise a negative errno is returned.
  705. */
  706. int
  707. rpcauth_unwrap_resp(struct rpc_task *task, struct xdr_stream *xdr)
  708. {
  709. const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
  710. return ops->crunwrap_resp(task, xdr);
  711. }
  712. bool
  713. rpcauth_xmit_need_reencode(struct rpc_task *task)
  714. {
  715. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  716. if (!cred || !cred->cr_ops->crneed_reencode)
  717. return false;
  718. return cred->cr_ops->crneed_reencode(task);
  719. }
  720. int
  721. rpcauth_refreshcred(struct rpc_task *task)
  722. {
  723. struct rpc_cred *cred;
  724. int err;
  725. cred = task->tk_rqstp->rq_cred;
  726. if (cred == NULL) {
  727. err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
  728. if (err < 0)
  729. goto out;
  730. cred = task->tk_rqstp->rq_cred;
  731. }
  732. err = cred->cr_ops->crrefresh(task);
  733. out:
  734. if (err < 0)
  735. task->tk_status = err;
  736. return err;
  737. }
  738. void
  739. rpcauth_invalcred(struct rpc_task *task)
  740. {
  741. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  742. if (cred)
  743. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  744. }
  745. int
  746. rpcauth_uptodatecred(struct rpc_task *task)
  747. {
  748. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  749. return cred == NULL ||
  750. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  751. }
  752. static struct shrinker *rpc_cred_shrinker;
  753. int __init rpcauth_init_module(void)
  754. {
  755. int err;
  756. err = rpc_init_authunix();
  757. if (err < 0)
  758. goto out1;
  759. rpc_cred_shrinker = shrinker_alloc(0, "sunrpc_cred");
  760. if (!rpc_cred_shrinker) {
  761. err = -ENOMEM;
  762. goto out2;
  763. }
  764. rpc_cred_shrinker->count_objects = rpcauth_cache_shrink_count;
  765. rpc_cred_shrinker->scan_objects = rpcauth_cache_shrink_scan;
  766. shrinker_register(rpc_cred_shrinker);
  767. return 0;
  768. out2:
  769. rpc_destroy_authunix();
  770. out1:
  771. return err;
  772. }
  773. void rpcauth_remove_module(void)
  774. {
  775. rpc_destroy_authunix();
  776. shrinker_free(rpc_cred_shrinker);
  777. }