idmap.c 19 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 <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. .preparse = user_preparse,
  160. .free_preparse = user_free_preparse,
  161. .instantiate = generic_key_instantiate,
  162. .match = user_match,
  163. .revoke = user_revoke,
  164. .destroy = user_destroy,
  165. .describe = user_describe,
  166. .read = user_read,
  167. };
  168. static int nfs_idmap_init_keyring(void)
  169. {
  170. struct cred *cred;
  171. struct key *keyring;
  172. int ret = 0;
  173. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  174. key_type_id_resolver.name);
  175. cred = prepare_kernel_cred(NULL);
  176. if (!cred)
  177. return -ENOMEM;
  178. keyring = keyring_alloc(".id_resolver",
  179. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  180. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  181. KEY_USR_VIEW | KEY_USR_READ,
  182. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  183. if (IS_ERR(keyring)) {
  184. ret = PTR_ERR(keyring);
  185. goto failed_put_cred;
  186. }
  187. ret = register_key_type(&key_type_id_resolver);
  188. if (ret < 0)
  189. goto failed_put_key;
  190. ret = register_key_type(&key_type_id_resolver_legacy);
  191. if (ret < 0)
  192. goto failed_reg_legacy;
  193. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  194. cred->thread_keyring = keyring;
  195. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  196. id_resolver_cache = cred;
  197. return 0;
  198. failed_reg_legacy:
  199. unregister_key_type(&key_type_id_resolver);
  200. failed_put_key:
  201. key_put(keyring);
  202. failed_put_cred:
  203. put_cred(cred);
  204. return ret;
  205. }
  206. static void nfs_idmap_quit_keyring(void)
  207. {
  208. key_revoke(id_resolver_cache->thread_keyring);
  209. unregister_key_type(&key_type_id_resolver);
  210. unregister_key_type(&key_type_id_resolver_legacy);
  211. put_cred(id_resolver_cache);
  212. }
  213. /*
  214. * Assemble the description to pass to request_key()
  215. * This function will allocate a new string and update dest to point
  216. * at it. The caller is responsible for freeing dest.
  217. *
  218. * On error 0 is returned. Otherwise, the length of dest is returned.
  219. */
  220. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  221. const char *type, size_t typelen, char **desc)
  222. {
  223. char *cp;
  224. size_t desclen = typelen + namelen + 2;
  225. *desc = kmalloc(desclen, GFP_KERNEL);
  226. if (!*desc)
  227. return -ENOMEM;
  228. cp = *desc;
  229. memcpy(cp, type, typelen);
  230. cp += typelen;
  231. *cp++ = ':';
  232. memcpy(cp, name, namelen);
  233. cp += namelen;
  234. *cp = '\0';
  235. return desclen;
  236. }
  237. static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  238. const char *type, struct idmap *idmap)
  239. {
  240. char *desc;
  241. struct key *rkey;
  242. ssize_t ret;
  243. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  244. if (ret <= 0)
  245. return ERR_PTR(ret);
  246. rkey = request_key(&key_type_id_resolver, desc, "");
  247. if (IS_ERR(rkey)) {
  248. mutex_lock(&idmap->idmap_mutex);
  249. rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  250. desc, "", 0, idmap);
  251. mutex_unlock(&idmap->idmap_mutex);
  252. }
  253. if (!IS_ERR(rkey))
  254. set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
  255. kfree(desc);
  256. return rkey;
  257. }
  258. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  259. const char *type, void *data,
  260. size_t data_size, struct idmap *idmap)
  261. {
  262. const struct cred *saved_cred;
  263. struct key *rkey;
  264. struct user_key_payload *payload;
  265. ssize_t ret;
  266. saved_cred = override_creds(id_resolver_cache);
  267. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  268. revert_creds(saved_cred);
  269. if (IS_ERR(rkey)) {
  270. ret = PTR_ERR(rkey);
  271. goto out;
  272. }
  273. rcu_read_lock();
  274. rkey->perm |= KEY_USR_VIEW;
  275. ret = key_validate(rkey);
  276. if (ret < 0)
  277. goto out_up;
  278. payload = rcu_dereference(rkey->payload.rcudata);
  279. if (IS_ERR_OR_NULL(payload)) {
  280. ret = PTR_ERR(payload);
  281. goto out_up;
  282. }
  283. ret = payload->datalen;
  284. if (ret > 0 && ret <= data_size)
  285. memcpy(data, payload->data, ret);
  286. else
  287. ret = -EINVAL;
  288. out_up:
  289. rcu_read_unlock();
  290. key_put(rkey);
  291. out:
  292. return ret;
  293. }
  294. /* ID -> Name */
  295. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  296. size_t buflen, struct idmap *idmap)
  297. {
  298. char id_str[NFS_UINT_MAXLEN];
  299. int id_len;
  300. ssize_t ret;
  301. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  302. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  303. if (ret < 0)
  304. return -EINVAL;
  305. return ret;
  306. }
  307. /* Name -> ID */
  308. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  309. __u32 *id, struct idmap *idmap)
  310. {
  311. char id_str[NFS_UINT_MAXLEN];
  312. long id_long;
  313. ssize_t data_size;
  314. int ret = 0;
  315. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  316. if (data_size <= 0) {
  317. ret = -EINVAL;
  318. } else {
  319. ret = kstrtol(id_str, 10, &id_long);
  320. *id = (__u32)id_long;
  321. }
  322. return ret;
  323. }
  324. /* idmap classic begins here */
  325. enum {
  326. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  327. };
  328. static const match_table_t nfs_idmap_tokens = {
  329. { Opt_find_uid, "uid:%s" },
  330. { Opt_find_gid, "gid:%s" },
  331. { Opt_find_user, "user:%s" },
  332. { Opt_find_group, "group:%s" },
  333. { Opt_find_err, NULL }
  334. };
  335. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  336. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  337. size_t);
  338. static void idmap_release_pipe(struct inode *);
  339. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  340. static const struct rpc_pipe_ops idmap_upcall_ops = {
  341. .upcall = rpc_pipe_generic_upcall,
  342. .downcall = idmap_pipe_downcall,
  343. .release_pipe = idmap_release_pipe,
  344. .destroy_msg = idmap_pipe_destroy_msg,
  345. };
  346. static struct key_type key_type_id_resolver_legacy = {
  347. .name = "id_legacy",
  348. .preparse = user_preparse,
  349. .free_preparse = user_free_preparse,
  350. .instantiate = generic_key_instantiate,
  351. .match = user_match,
  352. .revoke = user_revoke,
  353. .destroy = user_destroy,
  354. .describe = user_describe,
  355. .read = user_read,
  356. .request_key = nfs_idmap_legacy_upcall,
  357. };
  358. static void nfs_idmap_pipe_destroy(struct dentry *dir,
  359. struct rpc_pipe_dir_object *pdo)
  360. {
  361. struct idmap *idmap = pdo->pdo_data;
  362. struct rpc_pipe *pipe = idmap->idmap_pipe;
  363. if (pipe->dentry) {
  364. rpc_unlink(pipe->dentry);
  365. pipe->dentry = NULL;
  366. }
  367. }
  368. static int nfs_idmap_pipe_create(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. struct dentry *dentry;
  374. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  375. if (IS_ERR(dentry))
  376. return PTR_ERR(dentry);
  377. pipe->dentry = dentry;
  378. return 0;
  379. }
  380. static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  381. .create = nfs_idmap_pipe_create,
  382. .destroy = nfs_idmap_pipe_destroy,
  383. };
  384. int
  385. nfs_idmap_new(struct nfs_client *clp)
  386. {
  387. struct idmap *idmap;
  388. struct rpc_pipe *pipe;
  389. int error;
  390. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  391. if (idmap == NULL)
  392. return -ENOMEM;
  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. mutex_init(&idmap->idmap_mutex);
  403. error = rpc_add_pipe_dir_object(clp->cl_net,
  404. &clp->cl_rpcclient->cl_pipedir_objects,
  405. &idmap->idmap_pdo);
  406. if (error)
  407. goto err_destroy_pipe;
  408. clp->cl_idmap = idmap;
  409. return 0;
  410. err_destroy_pipe:
  411. rpc_destroy_pipe_data(idmap->idmap_pipe);
  412. err:
  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. kfree(idmap);
  428. }
  429. int nfs_idmap_init(void)
  430. {
  431. int ret;
  432. ret = nfs_idmap_init_keyring();
  433. if (ret != 0)
  434. goto out;
  435. out:
  436. return ret;
  437. }
  438. void nfs_idmap_quit(void)
  439. {
  440. nfs_idmap_quit_keyring();
  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. case Opt_find_gid:
  454. im->im_conv = IDMAP_CONV_NAMETOID;
  455. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  456. break;
  457. case Opt_find_user:
  458. im->im_type = IDMAP_TYPE_USER;
  459. case Opt_find_group:
  460. im->im_conv = IDMAP_CONV_IDTONAME;
  461. ret = match_int(&substr, &im->im_id);
  462. break;
  463. default:
  464. ret = -EINVAL;
  465. goto out;
  466. }
  467. msg->data = im;
  468. msg->len = sizeof(struct idmap_msg);
  469. out:
  470. return ret;
  471. }
  472. static bool
  473. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  474. struct idmap_legacy_upcalldata *data)
  475. {
  476. if (idmap->idmap_upcall_data != NULL) {
  477. WARN_ON_ONCE(1);
  478. return false;
  479. }
  480. idmap->idmap_upcall_data = data;
  481. return true;
  482. }
  483. static void
  484. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  485. {
  486. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  487. kfree(idmap->idmap_upcall_data);
  488. idmap->idmap_upcall_data = NULL;
  489. complete_request_key(cons, ret);
  490. }
  491. static void
  492. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  493. {
  494. if (idmap->idmap_upcall_data != NULL)
  495. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  496. }
  497. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  498. const char *op,
  499. void *aux)
  500. {
  501. struct idmap_legacy_upcalldata *data;
  502. struct rpc_pipe_msg *msg;
  503. struct idmap_msg *im;
  504. struct idmap *idmap = (struct idmap *)aux;
  505. struct key *key = cons->key;
  506. int ret = -ENOMEM;
  507. /* msg and im are freed in idmap_pipe_destroy_msg */
  508. data = kzalloc(sizeof(*data), GFP_KERNEL);
  509. if (!data)
  510. goto out1;
  511. msg = &data->pipe_msg;
  512. im = &data->idmap_msg;
  513. data->idmap = idmap;
  514. data->key_cons = cons;
  515. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  516. if (ret < 0)
  517. goto out2;
  518. ret = -EAGAIN;
  519. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  520. goto out2;
  521. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  522. if (ret < 0)
  523. nfs_idmap_abort_pipe_upcall(idmap, ret);
  524. return ret;
  525. out2:
  526. kfree(data);
  527. out1:
  528. complete_request_key(cons, ret);
  529. return ret;
  530. }
  531. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  532. {
  533. return key_instantiate_and_link(key, data, datalen,
  534. id_resolver_cache->thread_keyring,
  535. authkey);
  536. }
  537. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  538. struct idmap_msg *upcall,
  539. struct key *key, struct key *authkey)
  540. {
  541. char id_str[NFS_UINT_MAXLEN];
  542. size_t len;
  543. int ret = -ENOKEY;
  544. /* ret = -ENOKEY */
  545. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  546. goto out;
  547. switch (im->im_conv) {
  548. case IDMAP_CONV_NAMETOID:
  549. if (strcmp(upcall->im_name, im->im_name) != 0)
  550. break;
  551. /* Note: here we store the NUL terminator too */
  552. len = sprintf(id_str, "%d", im->im_id) + 1;
  553. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  554. break;
  555. case IDMAP_CONV_IDTONAME:
  556. if (upcall->im_id != im->im_id)
  557. break;
  558. len = strlen(im->im_name);
  559. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  560. break;
  561. default:
  562. ret = -EINVAL;
  563. }
  564. out:
  565. return ret;
  566. }
  567. static ssize_t
  568. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  569. {
  570. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  571. struct idmap *idmap = (struct idmap *)rpci->private;
  572. struct key_construction *cons;
  573. struct idmap_msg im;
  574. size_t namelen_in;
  575. int ret = -ENOKEY;
  576. /* If instantiation is successful, anyone waiting for key construction
  577. * will have been woken up and someone else may now have used
  578. * idmap_key_cons - so after this point we may no longer touch it.
  579. */
  580. if (idmap->idmap_upcall_data == NULL)
  581. goto out_noupcall;
  582. cons = idmap->idmap_upcall_data->key_cons;
  583. if (mlen != sizeof(im)) {
  584. ret = -ENOSPC;
  585. goto out;
  586. }
  587. if (copy_from_user(&im, src, mlen) != 0) {
  588. ret = -EFAULT;
  589. goto out;
  590. }
  591. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  592. ret = -ENOKEY;
  593. goto out;
  594. }
  595. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  596. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  597. ret = -EINVAL;
  598. goto out;
  599. }
  600. ret = nfs_idmap_read_and_verify_message(&im,
  601. &idmap->idmap_upcall_data->idmap_msg,
  602. cons->key, cons->authkey);
  603. if (ret >= 0) {
  604. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  605. ret = mlen;
  606. }
  607. out:
  608. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  609. out_noupcall:
  610. return ret;
  611. }
  612. static void
  613. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  614. {
  615. struct idmap_legacy_upcalldata *data = container_of(msg,
  616. struct idmap_legacy_upcalldata,
  617. pipe_msg);
  618. struct idmap *idmap = data->idmap;
  619. if (msg->errno)
  620. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  621. }
  622. static void
  623. idmap_release_pipe(struct inode *inode)
  624. {
  625. struct rpc_inode *rpci = RPC_I(inode);
  626. struct idmap *idmap = (struct idmap *)rpci->private;
  627. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  628. }
  629. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  630. {
  631. struct idmap *idmap = server->nfs_client->cl_idmap;
  632. __u32 id = -1;
  633. int ret = 0;
  634. if (!nfs_map_string_to_numeric(name, namelen, &id))
  635. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  636. if (ret == 0) {
  637. *uid = make_kuid(&init_user_ns, id);
  638. if (!uid_valid(*uid))
  639. ret = -ERANGE;
  640. }
  641. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  642. return ret;
  643. }
  644. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  645. {
  646. struct idmap *idmap = server->nfs_client->cl_idmap;
  647. __u32 id = -1;
  648. int ret = 0;
  649. if (!nfs_map_string_to_numeric(name, namelen, &id))
  650. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  651. if (ret == 0) {
  652. *gid = make_kgid(&init_user_ns, id);
  653. if (!gid_valid(*gid))
  654. ret = -ERANGE;
  655. }
  656. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  657. return ret;
  658. }
  659. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  660. {
  661. struct idmap *idmap = server->nfs_client->cl_idmap;
  662. int ret = -EINVAL;
  663. __u32 id;
  664. id = from_kuid(&init_user_ns, uid);
  665. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  666. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  667. if (ret < 0)
  668. ret = nfs_map_numeric_to_string(id, buf, buflen);
  669. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  670. return ret;
  671. }
  672. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  673. {
  674. struct idmap *idmap = server->nfs_client->cl_idmap;
  675. int ret = -EINVAL;
  676. __u32 id;
  677. id = from_kgid(&init_user_ns, gid);
  678. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  679. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  680. if (ret < 0)
  681. ret = nfs_map_numeric_to_string(id, buf, buflen);
  682. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  683. return ret;
  684. }