nfs4idmap.c 20 KB

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