nfs4idmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * fs/nfs/idmap.c
  3. *
  4. * UID and GID to name mapping for clients.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Marius Aamodt Eriksen <marius@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/types.h>
  37. #include <linux/parser.h>
  38. #include <linux/fs.h>
  39. #include <net/net_namespace.h>
  40. #include <linux/sunrpc/rpc_pipe_fs.h>
  41. #include <linux/nfs_fs.h>
  42. #include <linux/nfs_fs_sb.h>
  43. #include <linux/key.h>
  44. #include <linux/keyctl.h>
  45. #include <linux/key-type.h>
  46. #include <keys/user-type.h>
  47. #include <keys/request_key_auth-type.h>
  48. #include <linux/module.h>
  49. #include <linux/user_namespace.h>
  50. #include "internal.h"
  51. #include "netns.h"
  52. #include "nfs4idmap.h"
  53. #include "nfs4trace.h"
  54. #define NFS_UINT_MAXLEN 11
  55. static const struct cred *id_resolver_cache;
  56. static struct key_type key_type_id_resolver_legacy;
  57. struct idmap_legacy_upcalldata {
  58. struct rpc_pipe_msg pipe_msg;
  59. struct idmap_msg idmap_msg;
  60. struct key *authkey;
  61. struct idmap *idmap;
  62. };
  63. struct idmap {
  64. struct rpc_pipe_dir_object idmap_pdo;
  65. struct rpc_pipe *idmap_pipe;
  66. struct idmap_legacy_upcalldata *idmap_upcall_data;
  67. struct mutex idmap_mutex;
  68. struct user_namespace *user_ns;
  69. };
  70. static struct user_namespace *idmap_userns(const struct idmap *idmap)
  71. {
  72. if (idmap && idmap->user_ns)
  73. return idmap->user_ns;
  74. return &init_user_ns;
  75. }
  76. /**
  77. * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  78. * @fattr: fully initialised struct nfs_fattr
  79. * @owner_name: owner name string cache
  80. * @group_name: group name string cache
  81. */
  82. void nfs_fattr_init_names(struct nfs_fattr *fattr,
  83. struct nfs4_string *owner_name,
  84. struct nfs4_string *group_name)
  85. {
  86. fattr->owner_name = owner_name;
  87. fattr->group_name = group_name;
  88. }
  89. static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  90. {
  91. fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  92. kfree(fattr->owner_name->data);
  93. }
  94. static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  95. {
  96. fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  97. kfree(fattr->group_name->data);
  98. }
  99. static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  100. {
  101. struct nfs4_string *owner = fattr->owner_name;
  102. kuid_t uid;
  103. if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  104. return false;
  105. if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  106. fattr->uid = uid;
  107. fattr->valid |= NFS_ATTR_FATTR_OWNER;
  108. }
  109. return true;
  110. }
  111. static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  112. {
  113. struct nfs4_string *group = fattr->group_name;
  114. kgid_t gid;
  115. if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  116. return false;
  117. if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  118. fattr->gid = gid;
  119. fattr->valid |= NFS_ATTR_FATTR_GROUP;
  120. }
  121. return true;
  122. }
  123. /**
  124. * nfs_fattr_free_names - free up the NFSv4 owner and group strings
  125. * @fattr: a fully initialised nfs_fattr structure
  126. */
  127. void nfs_fattr_free_names(struct nfs_fattr *fattr)
  128. {
  129. if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  130. nfs_fattr_free_owner_name(fattr);
  131. if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  132. nfs_fattr_free_group_name(fattr);
  133. }
  134. /**
  135. * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
  136. * @server: pointer to the filesystem nfs_server structure
  137. * @fattr: a fully initialised nfs_fattr structure
  138. *
  139. * This helper maps the cached NFSv4 owner/group strings in fattr into
  140. * their numeric uid/gid equivalents, and then frees the cached strings.
  141. */
  142. void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  143. {
  144. if (nfs_fattr_map_owner_name(server, fattr))
  145. nfs_fattr_free_owner_name(fattr);
  146. if (nfs_fattr_map_group_name(server, fattr))
  147. nfs_fattr_free_group_name(fattr);
  148. }
  149. int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  150. {
  151. unsigned long val;
  152. char buf[16];
  153. if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  154. return 0;
  155. memcpy(buf, name, namelen);
  156. buf[namelen] = '\0';
  157. if (kstrtoul(buf, 0, &val) != 0)
  158. return 0;
  159. *res = val;
  160. return 1;
  161. }
  162. EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
  163. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  164. {
  165. return snprintf(buf, buflen, "%u", id);
  166. }
  167. static struct key_type key_type_id_resolver = {
  168. .name = "id_resolver",
  169. .preparse = user_preparse,
  170. .free_preparse = user_free_preparse,
  171. .instantiate = generic_key_instantiate,
  172. .revoke = user_revoke,
  173. .destroy = user_destroy,
  174. .describe = user_describe,
  175. .read = user_read,
  176. };
  177. int nfs_idmap_init(void)
  178. {
  179. struct cred *cred;
  180. struct key *keyring;
  181. int ret = 0;
  182. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  183. key_type_id_resolver.name);
  184. cred = prepare_kernel_cred(&init_task);
  185. if (!cred)
  186. return -ENOMEM;
  187. keyring = keyring_alloc(".id_resolver",
  188. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  189. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  190. KEY_USR_VIEW | KEY_USR_READ,
  191. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  192. if (IS_ERR(keyring)) {
  193. ret = PTR_ERR(keyring);
  194. goto failed_put_cred;
  195. }
  196. ret = register_key_type(&key_type_id_resolver);
  197. if (ret < 0)
  198. goto failed_put_key;
  199. ret = register_key_type(&key_type_id_resolver_legacy);
  200. if (ret < 0)
  201. goto failed_reg_legacy;
  202. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  203. cred->thread_keyring = keyring;
  204. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  205. id_resolver_cache = cred;
  206. return 0;
  207. failed_reg_legacy:
  208. unregister_key_type(&key_type_id_resolver);
  209. failed_put_key:
  210. key_put(keyring);
  211. failed_put_cred:
  212. put_cred(cred);
  213. return ret;
  214. }
  215. void nfs_idmap_quit(void)
  216. {
  217. key_revoke(id_resolver_cache->thread_keyring);
  218. unregister_key_type(&key_type_id_resolver);
  219. unregister_key_type(&key_type_id_resolver_legacy);
  220. put_cred(id_resolver_cache);
  221. }
  222. /*
  223. * Assemble the description to pass to request_key()
  224. * This function will allocate a new string and update dest to point
  225. * at it. The caller is responsible for freeing dest.
  226. *
  227. * On error 0 is returned. Otherwise, the length of dest is returned.
  228. */
  229. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  230. const char *type, size_t typelen, char **desc)
  231. {
  232. char *cp;
  233. size_t desclen = typelen + namelen + 2;
  234. *desc = kmalloc(desclen, GFP_KERNEL);
  235. if (!*desc)
  236. return -ENOMEM;
  237. cp = *desc;
  238. memcpy(cp, type, typelen);
  239. cp += typelen;
  240. *cp++ = ':';
  241. memcpy(cp, name, namelen);
  242. cp += namelen;
  243. *cp = '\0';
  244. return desclen;
  245. }
  246. static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  247. const char *type, struct idmap *idmap)
  248. {
  249. char *desc;
  250. struct key *rkey = ERR_PTR(-EAGAIN);
  251. ssize_t ret;
  252. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  253. if (ret < 0)
  254. return ERR_PTR(ret);
  255. if (!idmap->user_ns || idmap->user_ns == &init_user_ns)
  256. rkey = request_key(&key_type_id_resolver, desc, "");
  257. if (IS_ERR(rkey)) {
  258. mutex_lock(&idmap->idmap_mutex);
  259. rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  260. desc, NULL, "", 0, idmap);
  261. mutex_unlock(&idmap->idmap_mutex);
  262. }
  263. if (!IS_ERR(rkey))
  264. set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
  265. kfree(desc);
  266. return rkey;
  267. }
  268. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  269. const char *type, void *data,
  270. size_t data_size, struct idmap *idmap)
  271. {
  272. struct key *rkey;
  273. const struct user_key_payload *payload;
  274. ssize_t ret;
  275. scoped_with_creds(id_resolver_cache)
  276. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  277. if (IS_ERR(rkey)) {
  278. ret = PTR_ERR(rkey);
  279. goto out;
  280. }
  281. rcu_read_lock();
  282. rkey->perm |= KEY_USR_VIEW;
  283. ret = key_validate(rkey);
  284. if (ret < 0)
  285. goto out_up;
  286. payload = user_key_payload_rcu(rkey);
  287. if (IS_ERR_OR_NULL(payload)) {
  288. ret = PTR_ERR(payload);
  289. goto out_up;
  290. }
  291. ret = payload->datalen;
  292. if (ret > 0 && ret <= data_size)
  293. memcpy(data, payload->data, ret);
  294. else
  295. ret = -EINVAL;
  296. out_up:
  297. rcu_read_unlock();
  298. key_put(rkey);
  299. out:
  300. return ret;
  301. }
  302. /* ID -> Name */
  303. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  304. size_t buflen, struct idmap *idmap)
  305. {
  306. char id_str[NFS_UINT_MAXLEN];
  307. int id_len;
  308. ssize_t ret;
  309. id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
  310. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  311. if (ret < 0)
  312. return -EINVAL;
  313. return ret;
  314. }
  315. /* Name -> ID */
  316. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  317. __u32 *id, struct idmap *idmap)
  318. {
  319. char id_str[NFS_UINT_MAXLEN];
  320. long id_long;
  321. ssize_t data_size;
  322. int ret = 0;
  323. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  324. if (data_size <= 0) {
  325. ret = -EINVAL;
  326. } else {
  327. ret = kstrtol(id_str, 10, &id_long);
  328. if (!ret)
  329. *id = (__u32)id_long;
  330. }
  331. return ret;
  332. }
  333. /* idmap classic begins here */
  334. enum {
  335. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  336. };
  337. static const match_table_t nfs_idmap_tokens = {
  338. { Opt_find_uid, "uid:%s" },
  339. { Opt_find_gid, "gid:%s" },
  340. { Opt_find_user, "user:%s" },
  341. { Opt_find_group, "group:%s" },
  342. { Opt_find_err, NULL }
  343. };
  344. static int nfs_idmap_legacy_upcall(struct key *, void *);
  345. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  346. size_t);
  347. static void idmap_release_pipe(struct inode *);
  348. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  349. static const struct rpc_pipe_ops idmap_upcall_ops = {
  350. .upcall = rpc_pipe_generic_upcall,
  351. .downcall = idmap_pipe_downcall,
  352. .release_pipe = idmap_release_pipe,
  353. .destroy_msg = idmap_pipe_destroy_msg,
  354. };
  355. static struct key_type key_type_id_resolver_legacy = {
  356. .name = "id_legacy",
  357. .preparse = user_preparse,
  358. .free_preparse = user_free_preparse,
  359. .instantiate = generic_key_instantiate,
  360. .revoke = user_revoke,
  361. .destroy = user_destroy,
  362. .describe = user_describe,
  363. .read = user_read,
  364. .request_key = nfs_idmap_legacy_upcall,
  365. };
  366. static void nfs_idmap_pipe_destroy(struct dentry *dir,
  367. struct rpc_pipe_dir_object *pdo)
  368. {
  369. struct idmap *idmap = pdo->pdo_data;
  370. rpc_unlink(idmap->idmap_pipe);
  371. }
  372. static int nfs_idmap_pipe_create(struct dentry *dir,
  373. struct rpc_pipe_dir_object *pdo)
  374. {
  375. struct idmap *idmap = pdo->pdo_data;
  376. return rpc_mkpipe_dentry(dir, "idmap", idmap, idmap->idmap_pipe);
  377. }
  378. static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  379. .create = nfs_idmap_pipe_create,
  380. .destroy = nfs_idmap_pipe_destroy,
  381. };
  382. int
  383. nfs_idmap_new(struct nfs_client *clp)
  384. {
  385. struct idmap *idmap;
  386. struct rpc_pipe *pipe;
  387. int error;
  388. idmap = kzalloc_obj(*idmap);
  389. if (idmap == NULL)
  390. return -ENOMEM;
  391. mutex_init(&idmap->idmap_mutex);
  392. idmap->user_ns = get_user_ns(clp->cl_rpcclient->cl_cred->user_ns);
  393. rpc_init_pipe_dir_object(&idmap->idmap_pdo,
  394. &nfs_idmap_pipe_dir_object_ops,
  395. idmap);
  396. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  397. if (IS_ERR(pipe)) {
  398. error = PTR_ERR(pipe);
  399. goto err;
  400. }
  401. idmap->idmap_pipe = pipe;
  402. error = rpc_add_pipe_dir_object(clp->cl_net,
  403. &clp->cl_rpcclient->cl_pipedir_objects,
  404. &idmap->idmap_pdo);
  405. if (error)
  406. goto err_destroy_pipe;
  407. clp->cl_idmap = idmap;
  408. return 0;
  409. err_destroy_pipe:
  410. rpc_destroy_pipe_data(idmap->idmap_pipe);
  411. err:
  412. put_user_ns(idmap->user_ns);
  413. kfree(idmap);
  414. return error;
  415. }
  416. void
  417. nfs_idmap_delete(struct nfs_client *clp)
  418. {
  419. struct idmap *idmap = clp->cl_idmap;
  420. if (!idmap)
  421. return;
  422. clp->cl_idmap = NULL;
  423. rpc_remove_pipe_dir_object(clp->cl_net,
  424. &clp->cl_rpcclient->cl_pipedir_objects,
  425. &idmap->idmap_pdo);
  426. rpc_destroy_pipe_data(idmap->idmap_pipe);
  427. put_user_ns(idmap->user_ns);
  428. kfree(idmap);
  429. }
  430. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  431. struct idmap_msg *im,
  432. struct rpc_pipe_msg *msg)
  433. {
  434. substring_t substr;
  435. int token, ret;
  436. im->im_type = IDMAP_TYPE_GROUP;
  437. token = match_token(desc, nfs_idmap_tokens, &substr);
  438. switch (token) {
  439. case Opt_find_uid:
  440. im->im_type = IDMAP_TYPE_USER;
  441. fallthrough;
  442. case Opt_find_gid:
  443. im->im_conv = IDMAP_CONV_NAMETOID;
  444. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  445. break;
  446. case Opt_find_user:
  447. im->im_type = IDMAP_TYPE_USER;
  448. fallthrough;
  449. case Opt_find_group:
  450. im->im_conv = IDMAP_CONV_IDTONAME;
  451. ret = match_int(&substr, &im->im_id);
  452. if (ret)
  453. goto out;
  454. break;
  455. default:
  456. ret = -EINVAL;
  457. goto out;
  458. }
  459. msg->data = im;
  460. msg->len = sizeof(struct idmap_msg);
  461. out:
  462. return ret;
  463. }
  464. static bool
  465. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  466. struct idmap_legacy_upcalldata *data)
  467. {
  468. if (idmap->idmap_upcall_data != NULL) {
  469. WARN_ON_ONCE(1);
  470. return false;
  471. }
  472. idmap->idmap_upcall_data = data;
  473. return true;
  474. }
  475. static void nfs_idmap_complete_pipe_upcall(struct idmap_legacy_upcalldata *data,
  476. int ret)
  477. {
  478. complete_request_key(data->authkey, ret);
  479. key_put(data->authkey);
  480. kfree(data);
  481. }
  482. static void nfs_idmap_abort_pipe_upcall(struct idmap *idmap,
  483. struct idmap_legacy_upcalldata *data,
  484. int ret)
  485. {
  486. if (cmpxchg(&idmap->idmap_upcall_data, data, NULL) == data)
  487. nfs_idmap_complete_pipe_upcall(data, ret);
  488. }
  489. static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
  490. {
  491. struct idmap_legacy_upcalldata *data;
  492. struct request_key_auth *rka = get_request_key_auth(authkey);
  493. struct rpc_pipe_msg *msg;
  494. struct idmap_msg *im;
  495. struct idmap *idmap = aux;
  496. struct key *key = rka->target_key;
  497. int ret = -ENOKEY;
  498. if (!aux)
  499. goto out1;
  500. /* msg and im are freed in idmap_pipe_destroy_msg */
  501. ret = -ENOMEM;
  502. data = kzalloc_obj(*data);
  503. if (!data)
  504. goto out1;
  505. msg = &data->pipe_msg;
  506. im = &data->idmap_msg;
  507. data->idmap = idmap;
  508. data->authkey = key_get(authkey);
  509. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  510. if (ret < 0)
  511. goto out2;
  512. ret = -EAGAIN;
  513. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  514. goto out2;
  515. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  516. if (ret < 0)
  517. nfs_idmap_abort_pipe_upcall(idmap, data, ret);
  518. return ret;
  519. out2:
  520. kfree(data);
  521. out1:
  522. complete_request_key(authkey, ret);
  523. return ret;
  524. }
  525. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  526. {
  527. return key_instantiate_and_link(key, data, datalen,
  528. id_resolver_cache->thread_keyring,
  529. authkey);
  530. }
  531. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  532. struct idmap_msg *upcall,
  533. struct key *key, struct key *authkey)
  534. {
  535. char id_str[NFS_UINT_MAXLEN];
  536. size_t len;
  537. int ret = -ENOKEY;
  538. /* ret = -ENOKEY */
  539. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  540. goto out;
  541. switch (im->im_conv) {
  542. case IDMAP_CONV_NAMETOID:
  543. if (strcmp(upcall->im_name, im->im_name) != 0)
  544. break;
  545. /* Note: here we store the NUL terminator too */
  546. len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
  547. sizeof(id_str));
  548. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  549. break;
  550. case IDMAP_CONV_IDTONAME:
  551. if (upcall->im_id != im->im_id)
  552. break;
  553. len = strlen(im->im_name);
  554. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  555. break;
  556. default:
  557. ret = -EINVAL;
  558. }
  559. out:
  560. return ret;
  561. }
  562. static ssize_t
  563. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  564. {
  565. struct request_key_auth *rka;
  566. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  567. struct idmap *idmap = (struct idmap *)rpci->private;
  568. struct idmap_legacy_upcalldata *data;
  569. struct key *authkey;
  570. struct idmap_msg im;
  571. size_t namelen_in;
  572. int ret = -ENOKEY;
  573. /* If instantiation is successful, anyone waiting for key construction
  574. * will have been woken up and someone else may now have used
  575. * idmap_key_cons - so after this point we may no longer touch it.
  576. */
  577. data = xchg(&idmap->idmap_upcall_data, NULL);
  578. if (data == NULL)
  579. goto out_noupcall;
  580. authkey = data->authkey;
  581. rka = get_request_key_auth(authkey);
  582. if (mlen != sizeof(im)) {
  583. ret = -ENOSPC;
  584. goto out;
  585. }
  586. if (copy_from_user(&im, src, mlen) != 0) {
  587. ret = -EFAULT;
  588. goto out;
  589. }
  590. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  591. ret = -ENOKEY;
  592. goto out;
  593. }
  594. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  595. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  596. ret = -EINVAL;
  597. goto out;
  598. }
  599. ret = nfs_idmap_read_and_verify_message(&im, &data->idmap_msg,
  600. rka->target_key, authkey);
  601. if (ret >= 0) {
  602. key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
  603. ret = mlen;
  604. }
  605. out:
  606. nfs_idmap_complete_pipe_upcall(data, ret);
  607. out_noupcall:
  608. return ret;
  609. }
  610. static void
  611. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  612. {
  613. struct idmap_legacy_upcalldata *data = container_of(msg,
  614. struct idmap_legacy_upcalldata,
  615. pipe_msg);
  616. struct idmap *idmap = data->idmap;
  617. if (msg->errno)
  618. nfs_idmap_abort_pipe_upcall(idmap, data, msg->errno);
  619. }
  620. static void
  621. idmap_release_pipe(struct inode *inode)
  622. {
  623. struct rpc_inode *rpci = RPC_I(inode);
  624. struct idmap *idmap = (struct idmap *)rpci->private;
  625. struct idmap_legacy_upcalldata *data;
  626. data = xchg(&idmap->idmap_upcall_data, NULL);
  627. if (data)
  628. nfs_idmap_complete_pipe_upcall(data, -EPIPE);
  629. }
  630. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  631. {
  632. struct idmap *idmap = server->nfs_client->cl_idmap;
  633. __u32 id = -1;
  634. int ret = 0;
  635. if (!nfs_map_string_to_numeric(name, namelen, &id))
  636. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  637. if (ret == 0) {
  638. *uid = make_kuid(idmap_userns(idmap), id);
  639. if (!uid_valid(*uid))
  640. ret = -ERANGE;
  641. }
  642. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  643. return ret;
  644. }
  645. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  646. {
  647. struct idmap *idmap = server->nfs_client->cl_idmap;
  648. __u32 id = -1;
  649. int ret = 0;
  650. if (!nfs_map_string_to_numeric(name, namelen, &id))
  651. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  652. if (ret == 0) {
  653. *gid = make_kgid(idmap_userns(idmap), id);
  654. if (!gid_valid(*gid))
  655. ret = -ERANGE;
  656. }
  657. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  658. return ret;
  659. }
  660. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  661. {
  662. struct idmap *idmap = server->nfs_client->cl_idmap;
  663. int ret = -EINVAL;
  664. __u32 id;
  665. id = from_kuid_munged(idmap_userns(idmap), uid);
  666. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  667. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  668. if (ret < 0)
  669. ret = nfs_map_numeric_to_string(id, buf, buflen);
  670. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  671. return ret;
  672. }
  673. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  674. {
  675. struct idmap *idmap = server->nfs_client->cl_idmap;
  676. int ret = -EINVAL;
  677. __u32 id;
  678. id = from_kgid_munged(idmap_userns(idmap), gid);
  679. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  680. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  681. if (ret < 0)
  682. ret = nfs_map_numeric_to_string(id, buf, buflen);
  683. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  684. return ret;
  685. }