nfs4idmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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(NULL);
  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. const struct cred *saved_cred;
  273. struct key *rkey;
  274. const struct user_key_payload *payload;
  275. ssize_t ret;
  276. saved_cred = override_creds(id_resolver_cache);
  277. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  278. revert_creds(saved_cred);
  279. if (IS_ERR(rkey)) {
  280. ret = PTR_ERR(rkey);
  281. goto out;
  282. }
  283. rcu_read_lock();
  284. rkey->perm |= KEY_USR_VIEW;
  285. ret = key_validate(rkey);
  286. if (ret < 0)
  287. goto out_up;
  288. payload = user_key_payload_rcu(rkey);
  289. if (IS_ERR_OR_NULL(payload)) {
  290. ret = PTR_ERR(payload);
  291. goto out_up;
  292. }
  293. ret = payload->datalen;
  294. if (ret > 0 && ret <= data_size)
  295. memcpy(data, payload->data, ret);
  296. else
  297. ret = -EINVAL;
  298. out_up:
  299. rcu_read_unlock();
  300. key_put(rkey);
  301. out:
  302. return ret;
  303. }
  304. /* ID -> Name */
  305. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  306. size_t buflen, struct idmap *idmap)
  307. {
  308. char id_str[NFS_UINT_MAXLEN];
  309. int id_len;
  310. ssize_t ret;
  311. id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
  312. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  313. if (ret < 0)
  314. return -EINVAL;
  315. return ret;
  316. }
  317. /* Name -> ID */
  318. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  319. __u32 *id, struct idmap *idmap)
  320. {
  321. char id_str[NFS_UINT_MAXLEN];
  322. long id_long;
  323. ssize_t data_size;
  324. int ret = 0;
  325. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  326. if (data_size <= 0) {
  327. ret = -EINVAL;
  328. } else {
  329. ret = kstrtol(id_str, 10, &id_long);
  330. if (!ret)
  331. *id = (__u32)id_long;
  332. }
  333. return ret;
  334. }
  335. /* idmap classic begins here */
  336. enum {
  337. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  338. };
  339. static const match_table_t nfs_idmap_tokens = {
  340. { Opt_find_uid, "uid:%s" },
  341. { Opt_find_gid, "gid:%s" },
  342. { Opt_find_user, "user:%s" },
  343. { Opt_find_group, "group:%s" },
  344. { Opt_find_err, NULL }
  345. };
  346. static int nfs_idmap_legacy_upcall(struct key *, void *);
  347. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  348. size_t);
  349. static void idmap_release_pipe(struct inode *);
  350. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  351. static const struct rpc_pipe_ops idmap_upcall_ops = {
  352. .upcall = rpc_pipe_generic_upcall,
  353. .downcall = idmap_pipe_downcall,
  354. .release_pipe = idmap_release_pipe,
  355. .destroy_msg = idmap_pipe_destroy_msg,
  356. };
  357. static struct key_type key_type_id_resolver_legacy = {
  358. .name = "id_legacy",
  359. .preparse = user_preparse,
  360. .free_preparse = user_free_preparse,
  361. .instantiate = generic_key_instantiate,
  362. .revoke = user_revoke,
  363. .destroy = user_destroy,
  364. .describe = user_describe,
  365. .read = user_read,
  366. .request_key = nfs_idmap_legacy_upcall,
  367. };
  368. static void nfs_idmap_pipe_destroy(struct dentry *dir,
  369. struct rpc_pipe_dir_object *pdo)
  370. {
  371. struct idmap *idmap = pdo->pdo_data;
  372. struct rpc_pipe *pipe = idmap->idmap_pipe;
  373. if (pipe->dentry) {
  374. rpc_unlink(pipe->dentry);
  375. pipe->dentry = NULL;
  376. }
  377. }
  378. static int nfs_idmap_pipe_create(struct dentry *dir,
  379. struct rpc_pipe_dir_object *pdo)
  380. {
  381. struct idmap *idmap = pdo->pdo_data;
  382. struct rpc_pipe *pipe = idmap->idmap_pipe;
  383. struct dentry *dentry;
  384. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  385. if (IS_ERR(dentry))
  386. return PTR_ERR(dentry);
  387. pipe->dentry = dentry;
  388. return 0;
  389. }
  390. static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  391. .create = nfs_idmap_pipe_create,
  392. .destroy = nfs_idmap_pipe_destroy,
  393. };
  394. int
  395. nfs_idmap_new(struct nfs_client *clp)
  396. {
  397. struct idmap *idmap;
  398. struct rpc_pipe *pipe;
  399. int error;
  400. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  401. if (idmap == NULL)
  402. return -ENOMEM;
  403. mutex_init(&idmap->idmap_mutex);
  404. idmap->user_ns = get_user_ns(clp->cl_rpcclient->cl_cred->user_ns);
  405. rpc_init_pipe_dir_object(&idmap->idmap_pdo,
  406. &nfs_idmap_pipe_dir_object_ops,
  407. idmap);
  408. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  409. if (IS_ERR(pipe)) {
  410. error = PTR_ERR(pipe);
  411. goto err;
  412. }
  413. idmap->idmap_pipe = pipe;
  414. error = rpc_add_pipe_dir_object(clp->cl_net,
  415. &clp->cl_rpcclient->cl_pipedir_objects,
  416. &idmap->idmap_pdo);
  417. if (error)
  418. goto err_destroy_pipe;
  419. clp->cl_idmap = idmap;
  420. return 0;
  421. err_destroy_pipe:
  422. rpc_destroy_pipe_data(idmap->idmap_pipe);
  423. err:
  424. put_user_ns(idmap->user_ns);
  425. kfree(idmap);
  426. return error;
  427. }
  428. void
  429. nfs_idmap_delete(struct nfs_client *clp)
  430. {
  431. struct idmap *idmap = clp->cl_idmap;
  432. if (!idmap)
  433. return;
  434. clp->cl_idmap = NULL;
  435. rpc_remove_pipe_dir_object(clp->cl_net,
  436. &clp->cl_rpcclient->cl_pipedir_objects,
  437. &idmap->idmap_pdo);
  438. rpc_destroy_pipe_data(idmap->idmap_pipe);
  439. put_user_ns(idmap->user_ns);
  440. kfree(idmap);
  441. }
  442. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  443. struct idmap_msg *im,
  444. struct rpc_pipe_msg *msg)
  445. {
  446. substring_t substr;
  447. int token, ret;
  448. im->im_type = IDMAP_TYPE_GROUP;
  449. token = match_token(desc, nfs_idmap_tokens, &substr);
  450. switch (token) {
  451. case Opt_find_uid:
  452. im->im_type = IDMAP_TYPE_USER;
  453. fallthrough;
  454. case Opt_find_gid:
  455. im->im_conv = IDMAP_CONV_NAMETOID;
  456. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  457. break;
  458. case Opt_find_user:
  459. im->im_type = IDMAP_TYPE_USER;
  460. fallthrough;
  461. case Opt_find_group:
  462. im->im_conv = IDMAP_CONV_IDTONAME;
  463. ret = match_int(&substr, &im->im_id);
  464. if (ret)
  465. goto out;
  466. break;
  467. default:
  468. ret = -EINVAL;
  469. goto out;
  470. }
  471. msg->data = im;
  472. msg->len = sizeof(struct idmap_msg);
  473. out:
  474. return ret;
  475. }
  476. static bool
  477. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  478. struct idmap_legacy_upcalldata *data)
  479. {
  480. if (idmap->idmap_upcall_data != NULL) {
  481. WARN_ON_ONCE(1);
  482. return false;
  483. }
  484. idmap->idmap_upcall_data = data;
  485. return true;
  486. }
  487. static void
  488. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  489. {
  490. struct key *authkey = idmap->idmap_upcall_data->authkey;
  491. kfree(idmap->idmap_upcall_data);
  492. idmap->idmap_upcall_data = NULL;
  493. complete_request_key(authkey, ret);
  494. key_put(authkey);
  495. }
  496. static void
  497. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  498. {
  499. if (idmap->idmap_upcall_data != NULL)
  500. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  501. }
  502. static int nfs_idmap_legacy_upcall(struct key *authkey, void *aux)
  503. {
  504. struct idmap_legacy_upcalldata *data;
  505. struct request_key_auth *rka = get_request_key_auth(authkey);
  506. struct rpc_pipe_msg *msg;
  507. struct idmap_msg *im;
  508. struct idmap *idmap = (struct idmap *)aux;
  509. struct key *key = rka->target_key;
  510. int ret = -ENOKEY;
  511. if (!aux)
  512. goto out1;
  513. /* msg and im are freed in idmap_pipe_destroy_msg */
  514. ret = -ENOMEM;
  515. data = kzalloc(sizeof(*data), GFP_KERNEL);
  516. if (!data)
  517. goto out1;
  518. msg = &data->pipe_msg;
  519. im = &data->idmap_msg;
  520. data->idmap = idmap;
  521. data->authkey = key_get(authkey);
  522. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  523. if (ret < 0)
  524. goto out2;
  525. ret = -EAGAIN;
  526. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  527. goto out2;
  528. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  529. if (ret < 0)
  530. nfs_idmap_abort_pipe_upcall(idmap, ret);
  531. return ret;
  532. out2:
  533. kfree(data);
  534. out1:
  535. complete_request_key(authkey, ret);
  536. return ret;
  537. }
  538. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  539. {
  540. return key_instantiate_and_link(key, data, datalen,
  541. id_resolver_cache->thread_keyring,
  542. authkey);
  543. }
  544. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  545. struct idmap_msg *upcall,
  546. struct key *key, struct key *authkey)
  547. {
  548. char id_str[NFS_UINT_MAXLEN];
  549. size_t len;
  550. int ret = -ENOKEY;
  551. /* ret = -ENOKEY */
  552. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  553. goto out;
  554. switch (im->im_conv) {
  555. case IDMAP_CONV_NAMETOID:
  556. if (strcmp(upcall->im_name, im->im_name) != 0)
  557. break;
  558. /* Note: here we store the NUL terminator too */
  559. len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
  560. sizeof(id_str));
  561. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  562. break;
  563. case IDMAP_CONV_IDTONAME:
  564. if (upcall->im_id != im->im_id)
  565. break;
  566. len = strlen(im->im_name);
  567. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  568. break;
  569. default:
  570. ret = -EINVAL;
  571. }
  572. out:
  573. return ret;
  574. }
  575. static ssize_t
  576. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  577. {
  578. struct request_key_auth *rka;
  579. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  580. struct idmap *idmap = (struct idmap *)rpci->private;
  581. struct key *authkey;
  582. struct idmap_msg im;
  583. size_t namelen_in;
  584. int ret = -ENOKEY;
  585. /* If instantiation is successful, anyone waiting for key construction
  586. * will have been woken up and someone else may now have used
  587. * idmap_key_cons - so after this point we may no longer touch it.
  588. */
  589. if (idmap->idmap_upcall_data == NULL)
  590. goto out_noupcall;
  591. authkey = idmap->idmap_upcall_data->authkey;
  592. rka = get_request_key_auth(authkey);
  593. if (mlen != sizeof(im)) {
  594. ret = -ENOSPC;
  595. goto out;
  596. }
  597. if (copy_from_user(&im, src, mlen) != 0) {
  598. ret = -EFAULT;
  599. goto out;
  600. }
  601. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  602. ret = -ENOKEY;
  603. goto out;
  604. }
  605. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  606. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  607. ret = -EINVAL;
  608. goto out;
  609. }
  610. ret = nfs_idmap_read_and_verify_message(&im,
  611. &idmap->idmap_upcall_data->idmap_msg,
  612. rka->target_key, authkey);
  613. if (ret >= 0) {
  614. key_set_timeout(rka->target_key, nfs_idmap_cache_timeout);
  615. ret = mlen;
  616. }
  617. out:
  618. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  619. out_noupcall:
  620. return ret;
  621. }
  622. static void
  623. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  624. {
  625. struct idmap_legacy_upcalldata *data = container_of(msg,
  626. struct idmap_legacy_upcalldata,
  627. pipe_msg);
  628. struct idmap *idmap = data->idmap;
  629. if (msg->errno)
  630. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  631. }
  632. static void
  633. idmap_release_pipe(struct inode *inode)
  634. {
  635. struct rpc_inode *rpci = RPC_I(inode);
  636. struct idmap *idmap = (struct idmap *)rpci->private;
  637. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  638. }
  639. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  640. {
  641. struct idmap *idmap = server->nfs_client->cl_idmap;
  642. __u32 id = -1;
  643. int ret = 0;
  644. if (!nfs_map_string_to_numeric(name, namelen, &id))
  645. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  646. if (ret == 0) {
  647. *uid = make_kuid(idmap_userns(idmap), id);
  648. if (!uid_valid(*uid))
  649. ret = -ERANGE;
  650. }
  651. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  652. return ret;
  653. }
  654. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  655. {
  656. struct idmap *idmap = server->nfs_client->cl_idmap;
  657. __u32 id = -1;
  658. int ret = 0;
  659. if (!nfs_map_string_to_numeric(name, namelen, &id))
  660. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  661. if (ret == 0) {
  662. *gid = make_kgid(idmap_userns(idmap), id);
  663. if (!gid_valid(*gid))
  664. ret = -ERANGE;
  665. }
  666. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  667. return ret;
  668. }
  669. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  670. {
  671. struct idmap *idmap = server->nfs_client->cl_idmap;
  672. int ret = -EINVAL;
  673. __u32 id;
  674. id = from_kuid_munged(idmap_userns(idmap), uid);
  675. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  676. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  677. if (ret < 0)
  678. ret = nfs_map_numeric_to_string(id, buf, buflen);
  679. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  680. return ret;
  681. }
  682. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  683. {
  684. struct idmap *idmap = server->nfs_client->cl_idmap;
  685. int ret = -EINVAL;
  686. __u32 id;
  687. id = from_kgid_munged(idmap_userns(idmap), gid);
  688. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  689. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  690. if (ret < 0)
  691. ret = nfs_map_numeric_to_string(id, buf, buflen);
  692. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  693. return ret;
  694. }