auth.c 21 KB

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