auth.c 21 KB

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