auth.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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);
  47. if (num > (1U << nbits))
  48. nbits++;
  49. if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
  50. goto out_inval;
  51. *(unsigned int *)kp->arg = nbits;
  52. return 0;
  53. out_inval:
  54. return -EINVAL;
  55. }
  56. static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
  57. {
  58. unsigned int nbits;
  59. nbits = *(unsigned int *)kp->arg;
  60. return sprintf(buffer, "%u", 1U << nbits);
  61. }
  62. #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
  63. static const struct kernel_param_ops param_ops_hashtbl_sz = {
  64. .set = param_set_hashtbl_sz,
  65. .get = param_get_hashtbl_sz,
  66. };
  67. module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
  68. MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
  69. static unsigned long auth_max_cred_cachesize = ULONG_MAX;
  70. module_param(auth_max_cred_cachesize, ulong, 0644);
  71. MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
  72. static u32
  73. pseudoflavor_to_flavor(u32 flavor) {
  74. if (flavor > RPC_AUTH_MAXFLAVOR)
  75. return RPC_AUTH_GSS;
  76. return flavor;
  77. }
  78. int
  79. rpcauth_register(const struct rpc_authops *ops)
  80. {
  81. rpc_authflavor_t flavor;
  82. int ret = -EPERM;
  83. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  84. return -EINVAL;
  85. spin_lock(&rpc_authflavor_lock);
  86. if (auth_flavors[flavor] == NULL) {
  87. auth_flavors[flavor] = ops;
  88. ret = 0;
  89. }
  90. spin_unlock(&rpc_authflavor_lock);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(rpcauth_register);
  94. int
  95. rpcauth_unregister(const struct rpc_authops *ops)
  96. {
  97. rpc_authflavor_t flavor;
  98. int ret = -EPERM;
  99. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  100. return -EINVAL;
  101. spin_lock(&rpc_authflavor_lock);
  102. if (auth_flavors[flavor] == ops) {
  103. auth_flavors[flavor] = NULL;
  104. ret = 0;
  105. }
  106. spin_unlock(&rpc_authflavor_lock);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL_GPL(rpcauth_unregister);
  110. /**
  111. * rpcauth_get_pseudoflavor - check if security flavor is supported
  112. * @flavor: a security flavor
  113. * @info: a GSS mech OID, quality of protection, and service value
  114. *
  115. * Verifies that an appropriate kernel module is available or already loaded.
  116. * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
  117. * not supported locally.
  118. */
  119. rpc_authflavor_t
  120. rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
  121. {
  122. const struct rpc_authops *ops;
  123. rpc_authflavor_t pseudoflavor;
  124. ops = auth_flavors[flavor];
  125. if (ops == NULL)
  126. request_module("rpc-auth-%u", flavor);
  127. spin_lock(&rpc_authflavor_lock);
  128. ops = auth_flavors[flavor];
  129. if (ops == NULL || !try_module_get(ops->owner)) {
  130. spin_unlock(&rpc_authflavor_lock);
  131. return RPC_AUTH_MAXFLAVOR;
  132. }
  133. spin_unlock(&rpc_authflavor_lock);
  134. pseudoflavor = flavor;
  135. if (ops->info2flavor != NULL)
  136. pseudoflavor = ops->info2flavor(info);
  137. module_put(ops->owner);
  138. return pseudoflavor;
  139. }
  140. EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
  141. /**
  142. * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
  143. * @pseudoflavor: GSS pseudoflavor to match
  144. * @info: rpcsec_gss_info structure to fill in
  145. *
  146. * Returns zero and fills in "info" if pseudoflavor matches a
  147. * supported mechanism.
  148. */
  149. int
  150. rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
  151. {
  152. rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
  153. const struct rpc_authops *ops;
  154. int result;
  155. if (flavor >= RPC_AUTH_MAXFLAVOR)
  156. return -EINVAL;
  157. ops = auth_flavors[flavor];
  158. if (ops == NULL)
  159. request_module("rpc-auth-%u", flavor);
  160. spin_lock(&rpc_authflavor_lock);
  161. ops = auth_flavors[flavor];
  162. if (ops == NULL || !try_module_get(ops->owner)) {
  163. spin_unlock(&rpc_authflavor_lock);
  164. return -ENOENT;
  165. }
  166. spin_unlock(&rpc_authflavor_lock);
  167. result = -ENOENT;
  168. if (ops->flavor2info != NULL)
  169. result = ops->flavor2info(pseudoflavor, info);
  170. module_put(ops->owner);
  171. return result;
  172. }
  173. EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
  174. /**
  175. * rpcauth_list_flavors - discover registered flavors and pseudoflavors
  176. * @array: array to fill in
  177. * @size: size of "array"
  178. *
  179. * Returns the number of array items filled in, or a negative errno.
  180. *
  181. * The returned array is not sorted by any policy. Callers should not
  182. * rely on the order of the items in the returned array.
  183. */
  184. int
  185. rpcauth_list_flavors(rpc_authflavor_t *array, int size)
  186. {
  187. rpc_authflavor_t flavor;
  188. int result = 0;
  189. spin_lock(&rpc_authflavor_lock);
  190. for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
  191. const struct rpc_authops *ops = auth_flavors[flavor];
  192. rpc_authflavor_t pseudos[4];
  193. int i, len;
  194. if (result >= size) {
  195. result = -ENOMEM;
  196. break;
  197. }
  198. if (ops == NULL)
  199. continue;
  200. if (ops->list_pseudoflavors == NULL) {
  201. array[result++] = ops->au_flavor;
  202. continue;
  203. }
  204. len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
  205. if (len < 0) {
  206. result = len;
  207. break;
  208. }
  209. for (i = 0; i < len; i++) {
  210. if (result >= size) {
  211. result = -ENOMEM;
  212. break;
  213. }
  214. array[result++] = pseudos[i];
  215. }
  216. }
  217. spin_unlock(&rpc_authflavor_lock);
  218. dprintk("RPC: %s returns %d\n", __func__, result);
  219. return result;
  220. }
  221. EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
  222. struct rpc_auth *
  223. rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  224. {
  225. struct rpc_auth *auth;
  226. const struct rpc_authops *ops;
  227. u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
  228. auth = ERR_PTR(-EINVAL);
  229. if (flavor >= RPC_AUTH_MAXFLAVOR)
  230. goto out;
  231. if ((ops = auth_flavors[flavor]) == NULL)
  232. request_module("rpc-auth-%u", flavor);
  233. spin_lock(&rpc_authflavor_lock);
  234. ops = auth_flavors[flavor];
  235. if (ops == NULL || !try_module_get(ops->owner)) {
  236. spin_unlock(&rpc_authflavor_lock);
  237. goto out;
  238. }
  239. spin_unlock(&rpc_authflavor_lock);
  240. auth = ops->create(args, clnt);
  241. module_put(ops->owner);
  242. if (IS_ERR(auth))
  243. return auth;
  244. if (clnt->cl_auth)
  245. rpcauth_release(clnt->cl_auth);
  246. clnt->cl_auth = auth;
  247. out:
  248. return auth;
  249. }
  250. EXPORT_SYMBOL_GPL(rpcauth_create);
  251. void
  252. rpcauth_release(struct rpc_auth *auth)
  253. {
  254. if (!atomic_dec_and_test(&auth->au_count))
  255. return;
  256. auth->au_ops->destroy(auth);
  257. }
  258. static DEFINE_SPINLOCK(rpc_credcache_lock);
  259. static void
  260. rpcauth_unhash_cred_locked(struct rpc_cred *cred)
  261. {
  262. hlist_del_rcu(&cred->cr_hash);
  263. smp_mb__before_atomic();
  264. clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  265. }
  266. static int
  267. rpcauth_unhash_cred(struct rpc_cred *cred)
  268. {
  269. spinlock_t *cache_lock;
  270. int ret;
  271. cache_lock = &cred->cr_auth->au_credcache->lock;
  272. spin_lock(cache_lock);
  273. ret = atomic_read(&cred->cr_count) == 0;
  274. if (ret)
  275. rpcauth_unhash_cred_locked(cred);
  276. spin_unlock(cache_lock);
  277. return ret;
  278. }
  279. /*
  280. * Initialize RPC credential cache
  281. */
  282. int
  283. rpcauth_init_credcache(struct rpc_auth *auth)
  284. {
  285. struct rpc_cred_cache *new;
  286. unsigned int hashsize;
  287. new = kmalloc(sizeof(*new), GFP_KERNEL);
  288. if (!new)
  289. goto out_nocache;
  290. new->hashbits = auth_hashbits;
  291. hashsize = 1U << new->hashbits;
  292. new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
  293. if (!new->hashtable)
  294. goto out_nohashtbl;
  295. spin_lock_init(&new->lock);
  296. auth->au_credcache = new;
  297. return 0;
  298. out_nohashtbl:
  299. kfree(new);
  300. out_nocache:
  301. return -ENOMEM;
  302. }
  303. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  304. /*
  305. * Setup a credential key lifetime timeout notification
  306. */
  307. int
  308. rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
  309. {
  310. if (!cred->cr_auth->au_ops->key_timeout)
  311. return 0;
  312. return cred->cr_auth->au_ops->key_timeout(auth, cred);
  313. }
  314. EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
  315. bool
  316. rpcauth_cred_key_to_expire(struct rpc_cred *cred)
  317. {
  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)
  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 = hash_long(from_kuid(&init_user_ns, acred->uid), 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);
  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. if (req->rq_cred != NULL)
  618. put_rpccred(req->rq_cred);
  619. req->rq_cred = new;
  620. return 0;
  621. }
  622. void
  623. put_rpccred(struct rpc_cred *cred)
  624. {
  625. /* Fast path for unhashed credentials */
  626. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
  627. if (atomic_dec_and_test(&cred->cr_count))
  628. cred->cr_ops->crdestroy(cred);
  629. return;
  630. }
  631. if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
  632. return;
  633. if (!list_empty(&cred->cr_lru)) {
  634. number_cred_unused--;
  635. list_del_init(&cred->cr_lru);
  636. }
  637. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
  638. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  639. cred->cr_expire = jiffies;
  640. list_add_tail(&cred->cr_lru, &cred_unused);
  641. number_cred_unused++;
  642. goto out_nodestroy;
  643. }
  644. if (!rpcauth_unhash_cred(cred)) {
  645. /* We were hashed and someone looked us up... */
  646. goto out_nodestroy;
  647. }
  648. }
  649. spin_unlock(&rpc_credcache_lock);
  650. cred->cr_ops->crdestroy(cred);
  651. return;
  652. out_nodestroy:
  653. spin_unlock(&rpc_credcache_lock);
  654. }
  655. EXPORT_SYMBOL_GPL(put_rpccred);
  656. __be32 *
  657. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  658. {
  659. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  660. dprintk("RPC: %5u marshaling %s cred %p\n",
  661. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  662. return cred->cr_ops->crmarshal(task, p);
  663. }
  664. __be32 *
  665. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  666. {
  667. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  668. dprintk("RPC: %5u validating %s cred %p\n",
  669. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  670. return cred->cr_ops->crvalidate(task, p);
  671. }
  672. static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  673. __be32 *data, void *obj)
  674. {
  675. struct xdr_stream xdr;
  676. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
  677. encode(rqstp, &xdr, obj);
  678. }
  679. int
  680. rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
  681. __be32 *data, void *obj)
  682. {
  683. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  684. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  685. task->tk_pid, cred->cr_ops->cr_name, cred);
  686. if (cred->cr_ops->crwrap_req)
  687. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  688. /* By default, we encode the arguments normally. */
  689. rpcauth_wrap_req_encode(encode, rqstp, data, obj);
  690. return 0;
  691. }
  692. static int
  693. rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  694. __be32 *data, void *obj)
  695. {
  696. struct xdr_stream xdr;
  697. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
  698. return decode(rqstp, &xdr, obj);
  699. }
  700. int
  701. rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
  702. __be32 *data, void *obj)
  703. {
  704. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  705. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  706. task->tk_pid, cred->cr_ops->cr_name, cred);
  707. if (cred->cr_ops->crunwrap_resp)
  708. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  709. data, obj);
  710. /* By default, we decode the arguments normally. */
  711. return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
  712. }
  713. int
  714. rpcauth_refreshcred(struct rpc_task *task)
  715. {
  716. struct rpc_cred *cred;
  717. int err;
  718. cred = task->tk_rqstp->rq_cred;
  719. if (cred == NULL) {
  720. err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
  721. if (err < 0)
  722. goto out;
  723. cred = task->tk_rqstp->rq_cred;
  724. }
  725. dprintk("RPC: %5u refreshing %s cred %p\n",
  726. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  727. err = cred->cr_ops->crrefresh(task);
  728. out:
  729. if (err < 0)
  730. task->tk_status = err;
  731. return err;
  732. }
  733. void
  734. rpcauth_invalcred(struct rpc_task *task)
  735. {
  736. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  737. dprintk("RPC: %5u invalidating %s cred %p\n",
  738. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  739. if (cred)
  740. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  741. }
  742. int
  743. rpcauth_uptodatecred(struct rpc_task *task)
  744. {
  745. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  746. return cred == NULL ||
  747. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  748. }
  749. static struct shrinker rpc_cred_shrinker = {
  750. .count_objects = rpcauth_cache_shrink_count,
  751. .scan_objects = rpcauth_cache_shrink_scan,
  752. .seeks = DEFAULT_SEEKS,
  753. };
  754. int __init rpcauth_init_module(void)
  755. {
  756. int err;
  757. err = rpc_init_authunix();
  758. if (err < 0)
  759. goto out1;
  760. err = rpc_init_generic_auth();
  761. if (err < 0)
  762. goto out2;
  763. register_shrinker(&rpc_cred_shrinker);
  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. rpc_destroy_generic_auth();
  774. unregister_shrinker(&rpc_cred_shrinker);
  775. }