idmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 <linux/nfs_idmap.h>
  40. #include <net/net_namespace.h>
  41. #include <linux/sunrpc/rpc_pipe_fs.h>
  42. #include <linux/nfs_fs.h>
  43. #include <linux/nfs_fs_sb.h>
  44. #include <linux/key.h>
  45. #include <linux/keyctl.h>
  46. #include <linux/key-type.h>
  47. #include <keys/user-type.h>
  48. #include <linux/module.h>
  49. #include "internal.h"
  50. #include "netns.h"
  51. #include "nfs4trace.h"
  52. #define NFS_UINT_MAXLEN 11
  53. static const struct cred *id_resolver_cache;
  54. static struct key_type key_type_id_resolver_legacy;
  55. struct idmap_legacy_upcalldata {
  56. struct rpc_pipe_msg pipe_msg;
  57. struct idmap_msg idmap_msg;
  58. struct key_construction *key_cons;
  59. struct idmap *idmap;
  60. };
  61. struct idmap {
  62. struct rpc_pipe_dir_object idmap_pdo;
  63. struct rpc_pipe *idmap_pipe;
  64. struct idmap_legacy_upcalldata *idmap_upcall_data;
  65. struct mutex idmap_mutex;
  66. };
  67. /**
  68. * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  69. * @fattr: fully initialised struct nfs_fattr
  70. * @owner_name: owner name string cache
  71. * @group_name: group name string cache
  72. */
  73. void nfs_fattr_init_names(struct nfs_fattr *fattr,
  74. struct nfs4_string *owner_name,
  75. struct nfs4_string *group_name)
  76. {
  77. fattr->owner_name = owner_name;
  78. fattr->group_name = group_name;
  79. }
  80. static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  81. {
  82. fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  83. kfree(fattr->owner_name->data);
  84. }
  85. static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  86. {
  87. fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  88. kfree(fattr->group_name->data);
  89. }
  90. static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  91. {
  92. struct nfs4_string *owner = fattr->owner_name;
  93. kuid_t uid;
  94. if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  95. return false;
  96. if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  97. fattr->uid = uid;
  98. fattr->valid |= NFS_ATTR_FATTR_OWNER;
  99. }
  100. return true;
  101. }
  102. static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  103. {
  104. struct nfs4_string *group = fattr->group_name;
  105. kgid_t gid;
  106. if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  107. return false;
  108. if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  109. fattr->gid = gid;
  110. fattr->valid |= NFS_ATTR_FATTR_GROUP;
  111. }
  112. return true;
  113. }
  114. /**
  115. * nfs_fattr_free_names - free up the NFSv4 owner and group strings
  116. * @fattr: a fully initialised nfs_fattr structure
  117. */
  118. void nfs_fattr_free_names(struct nfs_fattr *fattr)
  119. {
  120. if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  121. nfs_fattr_free_owner_name(fattr);
  122. if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  123. nfs_fattr_free_group_name(fattr);
  124. }
  125. /**
  126. * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
  127. * @server: pointer to the filesystem nfs_server structure
  128. * @fattr: a fully initialised nfs_fattr structure
  129. *
  130. * This helper maps the cached NFSv4 owner/group strings in fattr into
  131. * their numeric uid/gid equivalents, and then frees the cached strings.
  132. */
  133. void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  134. {
  135. if (nfs_fattr_map_owner_name(server, fattr))
  136. nfs_fattr_free_owner_name(fattr);
  137. if (nfs_fattr_map_group_name(server, fattr))
  138. nfs_fattr_free_group_name(fattr);
  139. }
  140. static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  141. {
  142. unsigned long val;
  143. char buf[16];
  144. if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  145. return 0;
  146. memcpy(buf, name, namelen);
  147. buf[namelen] = '\0';
  148. if (kstrtoul(buf, 0, &val) != 0)
  149. return 0;
  150. *res = val;
  151. return 1;
  152. }
  153. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  154. {
  155. return snprintf(buf, buflen, "%u", id);
  156. }
  157. static struct key_type key_type_id_resolver = {
  158. .name = "id_resolver",
  159. .instantiate = user_instantiate,
  160. .match = user_match,
  161. .revoke = user_revoke,
  162. .destroy = user_destroy,
  163. .describe = user_describe,
  164. .read = user_read,
  165. };
  166. static int nfs_idmap_init_keyring(void)
  167. {
  168. struct cred *cred;
  169. struct key *keyring;
  170. int ret = 0;
  171. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  172. key_type_id_resolver.name);
  173. cred = prepare_kernel_cred(NULL);
  174. if (!cred)
  175. return -ENOMEM;
  176. keyring = keyring_alloc(".id_resolver",
  177. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  178. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  179. KEY_USR_VIEW | KEY_USR_READ,
  180. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  181. if (IS_ERR(keyring)) {
  182. ret = PTR_ERR(keyring);
  183. goto failed_put_cred;
  184. }
  185. ret = register_key_type(&key_type_id_resolver);
  186. if (ret < 0)
  187. goto failed_put_key;
  188. ret = register_key_type(&key_type_id_resolver_legacy);
  189. if (ret < 0)
  190. goto failed_reg_legacy;
  191. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  192. cred->thread_keyring = keyring;
  193. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  194. id_resolver_cache = cred;
  195. return 0;
  196. failed_reg_legacy:
  197. unregister_key_type(&key_type_id_resolver);
  198. failed_put_key:
  199. key_put(keyring);
  200. failed_put_cred:
  201. put_cred(cred);
  202. return ret;
  203. }
  204. static void nfs_idmap_quit_keyring(void)
  205. {
  206. key_revoke(id_resolver_cache->thread_keyring);
  207. unregister_key_type(&key_type_id_resolver);
  208. unregister_key_type(&key_type_id_resolver_legacy);
  209. put_cred(id_resolver_cache);
  210. }
  211. /*
  212. * Assemble the description to pass to request_key()
  213. * This function will allocate a new string and update dest to point
  214. * at it. The caller is responsible for freeing dest.
  215. *
  216. * On error 0 is returned. Otherwise, the length of dest is returned.
  217. */
  218. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  219. const char *type, size_t typelen, char **desc)
  220. {
  221. char *cp;
  222. size_t desclen = typelen + namelen + 2;
  223. *desc = kmalloc(desclen, GFP_KERNEL);
  224. if (!*desc)
  225. return -ENOMEM;
  226. cp = *desc;
  227. memcpy(cp, type, typelen);
  228. cp += typelen;
  229. *cp++ = ':';
  230. memcpy(cp, name, namelen);
  231. cp += namelen;
  232. *cp = '\0';
  233. return desclen;
  234. }
  235. static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  236. const char *type, struct idmap *idmap)
  237. {
  238. char *desc;
  239. struct key *rkey;
  240. ssize_t ret;
  241. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  242. if (ret <= 0)
  243. return ERR_PTR(ret);
  244. rkey = request_key(&key_type_id_resolver, desc, "");
  245. if (IS_ERR(rkey)) {
  246. mutex_lock(&idmap->idmap_mutex);
  247. rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  248. desc, "", 0, idmap);
  249. mutex_unlock(&idmap->idmap_mutex);
  250. }
  251. kfree(desc);
  252. return rkey;
  253. }
  254. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  255. const char *type, void *data,
  256. size_t data_size, struct idmap *idmap)
  257. {
  258. const struct cred *saved_cred;
  259. struct key *rkey;
  260. struct user_key_payload *payload;
  261. ssize_t ret;
  262. saved_cred = override_creds(id_resolver_cache);
  263. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  264. revert_creds(saved_cred);
  265. if (IS_ERR(rkey)) {
  266. ret = PTR_ERR(rkey);
  267. goto out;
  268. }
  269. rcu_read_lock();
  270. rkey->perm |= KEY_USR_VIEW;
  271. ret = key_validate(rkey);
  272. if (ret < 0)
  273. goto out_up;
  274. payload = rcu_dereference(rkey->payload.rcudata);
  275. if (IS_ERR_OR_NULL(payload)) {
  276. ret = PTR_ERR(payload);
  277. goto out_up;
  278. }
  279. ret = payload->datalen;
  280. if (ret > 0 && ret <= data_size)
  281. memcpy(data, payload->data, ret);
  282. else
  283. ret = -EINVAL;
  284. out_up:
  285. rcu_read_unlock();
  286. key_put(rkey);
  287. out:
  288. return ret;
  289. }
  290. /* ID -> Name */
  291. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  292. size_t buflen, struct idmap *idmap)
  293. {
  294. char id_str[NFS_UINT_MAXLEN];
  295. int id_len;
  296. ssize_t ret;
  297. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  298. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  299. if (ret < 0)
  300. return -EINVAL;
  301. return ret;
  302. }
  303. /* Name -> ID */
  304. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  305. __u32 *id, struct idmap *idmap)
  306. {
  307. char id_str[NFS_UINT_MAXLEN];
  308. long id_long;
  309. ssize_t data_size;
  310. int ret = 0;
  311. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  312. if (data_size <= 0) {
  313. ret = -EINVAL;
  314. } else {
  315. ret = kstrtol(id_str, 10, &id_long);
  316. *id = (__u32)id_long;
  317. }
  318. return ret;
  319. }
  320. /* idmap classic begins here */
  321. enum {
  322. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  323. };
  324. static const match_table_t nfs_idmap_tokens = {
  325. { Opt_find_uid, "uid:%s" },
  326. { Opt_find_gid, "gid:%s" },
  327. { Opt_find_user, "user:%s" },
  328. { Opt_find_group, "group:%s" },
  329. { Opt_find_err, NULL }
  330. };
  331. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  332. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  333. size_t);
  334. static void idmap_release_pipe(struct inode *);
  335. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  336. static const struct rpc_pipe_ops idmap_upcall_ops = {
  337. .upcall = rpc_pipe_generic_upcall,
  338. .downcall = idmap_pipe_downcall,
  339. .release_pipe = idmap_release_pipe,
  340. .destroy_msg = idmap_pipe_destroy_msg,
  341. };
  342. static struct key_type key_type_id_resolver_legacy = {
  343. .name = "id_legacy",
  344. .instantiate = user_instantiate,
  345. .match = user_match,
  346. .revoke = user_revoke,
  347. .destroy = user_destroy,
  348. .describe = user_describe,
  349. .read = user_read,
  350. .request_key = nfs_idmap_legacy_upcall,
  351. };
  352. static void nfs_idmap_pipe_destroy(struct dentry *dir,
  353. struct rpc_pipe_dir_object *pdo)
  354. {
  355. struct idmap *idmap = pdo->pdo_data;
  356. struct rpc_pipe *pipe = idmap->idmap_pipe;
  357. if (pipe->dentry) {
  358. rpc_unlink(pipe->dentry);
  359. pipe->dentry = NULL;
  360. }
  361. }
  362. static int nfs_idmap_pipe_create(struct dentry *dir,
  363. struct rpc_pipe_dir_object *pdo)
  364. {
  365. struct idmap *idmap = pdo->pdo_data;
  366. struct rpc_pipe *pipe = idmap->idmap_pipe;
  367. struct dentry *dentry;
  368. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  369. if (IS_ERR(dentry))
  370. return PTR_ERR(dentry);
  371. pipe->dentry = dentry;
  372. return 0;
  373. }
  374. static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  375. .create = nfs_idmap_pipe_create,
  376. .destroy = nfs_idmap_pipe_destroy,
  377. };
  378. int
  379. nfs_idmap_new(struct nfs_client *clp)
  380. {
  381. struct idmap *idmap;
  382. struct rpc_pipe *pipe;
  383. int error;
  384. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  385. if (idmap == NULL)
  386. return -ENOMEM;
  387. rpc_init_pipe_dir_object(&idmap->idmap_pdo,
  388. &nfs_idmap_pipe_dir_object_ops,
  389. idmap);
  390. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  391. if (IS_ERR(pipe)) {
  392. error = PTR_ERR(pipe);
  393. goto err;
  394. }
  395. idmap->idmap_pipe = pipe;
  396. mutex_init(&idmap->idmap_mutex);
  397. error = rpc_add_pipe_dir_object(clp->cl_net,
  398. &clp->cl_rpcclient->cl_pipedir_objects,
  399. &idmap->idmap_pdo);
  400. if (error)
  401. goto err_destroy_pipe;
  402. clp->cl_idmap = idmap;
  403. return 0;
  404. err_destroy_pipe:
  405. rpc_destroy_pipe_data(idmap->idmap_pipe);
  406. err:
  407. kfree(idmap);
  408. return error;
  409. }
  410. void
  411. nfs_idmap_delete(struct nfs_client *clp)
  412. {
  413. struct idmap *idmap = clp->cl_idmap;
  414. if (!idmap)
  415. return;
  416. clp->cl_idmap = NULL;
  417. rpc_remove_pipe_dir_object(clp->cl_net,
  418. &clp->cl_rpcclient->cl_pipedir_objects,
  419. &idmap->idmap_pdo);
  420. rpc_destroy_pipe_data(idmap->idmap_pipe);
  421. kfree(idmap);
  422. }
  423. int nfs_idmap_init(void)
  424. {
  425. int ret;
  426. ret = nfs_idmap_init_keyring();
  427. if (ret != 0)
  428. goto out;
  429. out:
  430. return ret;
  431. }
  432. void nfs_idmap_quit(void)
  433. {
  434. nfs_idmap_quit_keyring();
  435. }
  436. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  437. struct idmap_msg *im,
  438. struct rpc_pipe_msg *msg)
  439. {
  440. substring_t substr;
  441. int token, ret;
  442. im->im_type = IDMAP_TYPE_GROUP;
  443. token = match_token(desc, nfs_idmap_tokens, &substr);
  444. switch (token) {
  445. case Opt_find_uid:
  446. im->im_type = IDMAP_TYPE_USER;
  447. case Opt_find_gid:
  448. im->im_conv = IDMAP_CONV_NAMETOID;
  449. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  450. break;
  451. case Opt_find_user:
  452. im->im_type = IDMAP_TYPE_USER;
  453. case Opt_find_group:
  454. im->im_conv = IDMAP_CONV_IDTONAME;
  455. ret = match_int(&substr, &im->im_id);
  456. break;
  457. default:
  458. ret = -EINVAL;
  459. goto out;
  460. }
  461. msg->data = im;
  462. msg->len = sizeof(struct idmap_msg);
  463. out:
  464. return ret;
  465. }
  466. static bool
  467. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  468. struct idmap_legacy_upcalldata *data)
  469. {
  470. if (idmap->idmap_upcall_data != NULL) {
  471. WARN_ON_ONCE(1);
  472. return false;
  473. }
  474. idmap->idmap_upcall_data = data;
  475. return true;
  476. }
  477. static void
  478. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  479. {
  480. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  481. kfree(idmap->idmap_upcall_data);
  482. idmap->idmap_upcall_data = NULL;
  483. complete_request_key(cons, ret);
  484. }
  485. static void
  486. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  487. {
  488. if (idmap->idmap_upcall_data != NULL)
  489. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  490. }
  491. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  492. const char *op,
  493. void *aux)
  494. {
  495. struct idmap_legacy_upcalldata *data;
  496. struct rpc_pipe_msg *msg;
  497. struct idmap_msg *im;
  498. struct idmap *idmap = (struct idmap *)aux;
  499. struct key *key = cons->key;
  500. int ret = -ENOMEM;
  501. /* msg and im are freed in idmap_pipe_destroy_msg */
  502. data = kzalloc(sizeof(*data), GFP_KERNEL);
  503. if (!data)
  504. goto out1;
  505. msg = &data->pipe_msg;
  506. im = &data->idmap_msg;
  507. data->idmap = idmap;
  508. data->key_cons = cons;
  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, ret);
  518. return ret;
  519. out2:
  520. kfree(data);
  521. out1:
  522. complete_request_key(cons, 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 = sprintf(id_str, "%d", im->im_id) + 1;
  547. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  548. break;
  549. case IDMAP_CONV_IDTONAME:
  550. if (upcall->im_id != im->im_id)
  551. break;
  552. len = strlen(im->im_name);
  553. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  554. break;
  555. default:
  556. ret = -EINVAL;
  557. }
  558. out:
  559. return ret;
  560. }
  561. static ssize_t
  562. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  563. {
  564. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  565. struct idmap *idmap = (struct idmap *)rpci->private;
  566. struct key_construction *cons;
  567. struct idmap_msg im;
  568. size_t namelen_in;
  569. int ret = -ENOKEY;
  570. /* If instantiation is successful, anyone waiting for key construction
  571. * will have been woken up and someone else may now have used
  572. * idmap_key_cons - so after this point we may no longer touch it.
  573. */
  574. if (idmap->idmap_upcall_data == NULL)
  575. goto out_noupcall;
  576. cons = idmap->idmap_upcall_data->key_cons;
  577. if (mlen != sizeof(im)) {
  578. ret = -ENOSPC;
  579. goto out;
  580. }
  581. if (copy_from_user(&im, src, mlen) != 0) {
  582. ret = -EFAULT;
  583. goto out;
  584. }
  585. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  586. ret = -ENOKEY;
  587. goto out;
  588. }
  589. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  590. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  591. ret = -EINVAL;
  592. goto out;
  593. }
  594. ret = nfs_idmap_read_and_verify_message(&im,
  595. &idmap->idmap_upcall_data->idmap_msg,
  596. cons->key, cons->authkey);
  597. if (ret >= 0) {
  598. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  599. ret = mlen;
  600. }
  601. out:
  602. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  603. out_noupcall:
  604. return ret;
  605. }
  606. static void
  607. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  608. {
  609. struct idmap_legacy_upcalldata *data = container_of(msg,
  610. struct idmap_legacy_upcalldata,
  611. pipe_msg);
  612. struct idmap *idmap = data->idmap;
  613. if (msg->errno)
  614. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  615. }
  616. static void
  617. idmap_release_pipe(struct inode *inode)
  618. {
  619. struct rpc_inode *rpci = RPC_I(inode);
  620. struct idmap *idmap = (struct idmap *)rpci->private;
  621. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  622. }
  623. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  624. {
  625. struct idmap *idmap = server->nfs_client->cl_idmap;
  626. __u32 id = -1;
  627. int ret = 0;
  628. if (!nfs_map_string_to_numeric(name, namelen, &id))
  629. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  630. if (ret == 0) {
  631. *uid = make_kuid(&init_user_ns, id);
  632. if (!uid_valid(*uid))
  633. ret = -ERANGE;
  634. }
  635. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  636. return ret;
  637. }
  638. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  639. {
  640. struct idmap *idmap = server->nfs_client->cl_idmap;
  641. __u32 id = -1;
  642. int ret = 0;
  643. if (!nfs_map_string_to_numeric(name, namelen, &id))
  644. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  645. if (ret == 0) {
  646. *gid = make_kgid(&init_user_ns, id);
  647. if (!gid_valid(*gid))
  648. ret = -ERANGE;
  649. }
  650. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  651. return ret;
  652. }
  653. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  654. {
  655. struct idmap *idmap = server->nfs_client->cl_idmap;
  656. int ret = -EINVAL;
  657. __u32 id;
  658. id = from_kuid(&init_user_ns, uid);
  659. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  660. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  661. if (ret < 0)
  662. ret = nfs_map_numeric_to_string(id, buf, buflen);
  663. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  664. return ret;
  665. }
  666. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  667. {
  668. struct idmap *idmap = server->nfs_client->cl_idmap;
  669. int ret = -EINVAL;
  670. __u32 id;
  671. id = from_kgid(&init_user_ns, gid);
  672. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  673. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  674. if (ret < 0)
  675. ret = nfs_map_numeric_to_string(id, buf, buflen);
  676. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  677. return ret;
  678. }