nfs4idmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 <linux/module.h>
  48. #include "internal.h"
  49. #include "netns.h"
  50. #include "nfs4idmap.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. 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. EXPORT_SYMBOL_GPL(nfs_map_string_to_numeric);
  154. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  155. {
  156. return snprintf(buf, buflen, "%u", id);
  157. }
  158. static struct key_type key_type_id_resolver = {
  159. .name = "id_resolver",
  160. .preparse = user_preparse,
  161. .free_preparse = user_free_preparse,
  162. .instantiate = generic_key_instantiate,
  163. .revoke = user_revoke,
  164. .destroy = user_destroy,
  165. .describe = user_describe,
  166. .read = user_read,
  167. };
  168. int nfs_idmap_init(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, 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. void nfs_idmap_quit(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. const 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 = user_key_payload_rcu(rkey);
  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 = nfs_map_numeric_to_string(id, id_str, sizeof(id_str));
  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. if (!ret)
  321. *id = (__u32)id_long;
  322. }
  323. return ret;
  324. }
  325. /* idmap classic begins here */
  326. enum {
  327. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  328. };
  329. static const match_table_t nfs_idmap_tokens = {
  330. { Opt_find_uid, "uid:%s" },
  331. { Opt_find_gid, "gid:%s" },
  332. { Opt_find_user, "user:%s" },
  333. { Opt_find_group, "group:%s" },
  334. { Opt_find_err, NULL }
  335. };
  336. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  337. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  338. size_t);
  339. static void idmap_release_pipe(struct inode *);
  340. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  341. static const struct rpc_pipe_ops idmap_upcall_ops = {
  342. .upcall = rpc_pipe_generic_upcall,
  343. .downcall = idmap_pipe_downcall,
  344. .release_pipe = idmap_release_pipe,
  345. .destroy_msg = idmap_pipe_destroy_msg,
  346. };
  347. static struct key_type key_type_id_resolver_legacy = {
  348. .name = "id_legacy",
  349. .preparse = user_preparse,
  350. .free_preparse = user_free_preparse,
  351. .instantiate = generic_key_instantiate,
  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. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  430. struct idmap_msg *im,
  431. struct rpc_pipe_msg *msg)
  432. {
  433. substring_t substr;
  434. int token, ret;
  435. im->im_type = IDMAP_TYPE_GROUP;
  436. token = match_token(desc, nfs_idmap_tokens, &substr);
  437. switch (token) {
  438. case Opt_find_uid:
  439. im->im_type = IDMAP_TYPE_USER;
  440. case Opt_find_gid:
  441. im->im_conv = IDMAP_CONV_NAMETOID;
  442. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  443. break;
  444. case Opt_find_user:
  445. im->im_type = IDMAP_TYPE_USER;
  446. case Opt_find_group:
  447. im->im_conv = IDMAP_CONV_IDTONAME;
  448. ret = match_int(&substr, &im->im_id);
  449. break;
  450. default:
  451. ret = -EINVAL;
  452. goto out;
  453. }
  454. msg->data = im;
  455. msg->len = sizeof(struct idmap_msg);
  456. out:
  457. return ret;
  458. }
  459. static bool
  460. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  461. struct idmap_legacy_upcalldata *data)
  462. {
  463. if (idmap->idmap_upcall_data != NULL) {
  464. WARN_ON_ONCE(1);
  465. return false;
  466. }
  467. idmap->idmap_upcall_data = data;
  468. return true;
  469. }
  470. static void
  471. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  472. {
  473. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  474. kfree(idmap->idmap_upcall_data);
  475. idmap->idmap_upcall_data = NULL;
  476. complete_request_key(cons, ret);
  477. }
  478. static void
  479. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  480. {
  481. if (idmap->idmap_upcall_data != NULL)
  482. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  483. }
  484. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  485. const char *op,
  486. void *aux)
  487. {
  488. struct idmap_legacy_upcalldata *data;
  489. struct rpc_pipe_msg *msg;
  490. struct idmap_msg *im;
  491. struct idmap *idmap = (struct idmap *)aux;
  492. struct key *key = cons->key;
  493. int ret = -ENOKEY;
  494. if (!aux)
  495. goto out1;
  496. /* msg and im are freed in idmap_pipe_destroy_msg */
  497. ret = -ENOMEM;
  498. data = kzalloc(sizeof(*data), GFP_KERNEL);
  499. if (!data)
  500. goto out1;
  501. msg = &data->pipe_msg;
  502. im = &data->idmap_msg;
  503. data->idmap = idmap;
  504. data->key_cons = cons;
  505. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  506. if (ret < 0)
  507. goto out2;
  508. ret = -EAGAIN;
  509. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  510. goto out2;
  511. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  512. if (ret < 0)
  513. nfs_idmap_abort_pipe_upcall(idmap, ret);
  514. return ret;
  515. out2:
  516. kfree(data);
  517. out1:
  518. complete_request_key(cons, ret);
  519. return ret;
  520. }
  521. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  522. {
  523. return key_instantiate_and_link(key, data, datalen,
  524. id_resolver_cache->thread_keyring,
  525. authkey);
  526. }
  527. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  528. struct idmap_msg *upcall,
  529. struct key *key, struct key *authkey)
  530. {
  531. char id_str[NFS_UINT_MAXLEN];
  532. size_t len;
  533. int ret = -ENOKEY;
  534. /* ret = -ENOKEY */
  535. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  536. goto out;
  537. switch (im->im_conv) {
  538. case IDMAP_CONV_NAMETOID:
  539. if (strcmp(upcall->im_name, im->im_name) != 0)
  540. break;
  541. /* Note: here we store the NUL terminator too */
  542. len = 1 + nfs_map_numeric_to_string(im->im_id, id_str,
  543. sizeof(id_str));
  544. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  545. break;
  546. case IDMAP_CONV_IDTONAME:
  547. if (upcall->im_id != im->im_id)
  548. break;
  549. len = strlen(im->im_name);
  550. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  551. break;
  552. default:
  553. ret = -EINVAL;
  554. }
  555. out:
  556. return ret;
  557. }
  558. static ssize_t
  559. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  560. {
  561. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  562. struct idmap *idmap = (struct idmap *)rpci->private;
  563. struct key_construction *cons;
  564. struct idmap_msg im;
  565. size_t namelen_in;
  566. int ret = -ENOKEY;
  567. /* If instantiation is successful, anyone waiting for key construction
  568. * will have been woken up and someone else may now have used
  569. * idmap_key_cons - so after this point we may no longer touch it.
  570. */
  571. if (idmap->idmap_upcall_data == NULL)
  572. goto out_noupcall;
  573. cons = idmap->idmap_upcall_data->key_cons;
  574. if (mlen != sizeof(im)) {
  575. ret = -ENOSPC;
  576. goto out;
  577. }
  578. if (copy_from_user(&im, src, mlen) != 0) {
  579. ret = -EFAULT;
  580. goto out;
  581. }
  582. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  583. ret = -ENOKEY;
  584. goto out;
  585. }
  586. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  587. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  588. ret = -EINVAL;
  589. goto out;
  590. }
  591. ret = nfs_idmap_read_and_verify_message(&im,
  592. &idmap->idmap_upcall_data->idmap_msg,
  593. cons->key, cons->authkey);
  594. if (ret >= 0) {
  595. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  596. ret = mlen;
  597. }
  598. out:
  599. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  600. out_noupcall:
  601. return ret;
  602. }
  603. static void
  604. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  605. {
  606. struct idmap_legacy_upcalldata *data = container_of(msg,
  607. struct idmap_legacy_upcalldata,
  608. pipe_msg);
  609. struct idmap *idmap = data->idmap;
  610. if (msg->errno)
  611. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  612. }
  613. static void
  614. idmap_release_pipe(struct inode *inode)
  615. {
  616. struct rpc_inode *rpci = RPC_I(inode);
  617. struct idmap *idmap = (struct idmap *)rpci->private;
  618. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  619. }
  620. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  621. {
  622. struct idmap *idmap = server->nfs_client->cl_idmap;
  623. __u32 id = -1;
  624. int ret = 0;
  625. if (!nfs_map_string_to_numeric(name, namelen, &id))
  626. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  627. if (ret == 0) {
  628. *uid = make_kuid(&init_user_ns, id);
  629. if (!uid_valid(*uid))
  630. ret = -ERANGE;
  631. }
  632. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  633. return ret;
  634. }
  635. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  636. {
  637. struct idmap *idmap = server->nfs_client->cl_idmap;
  638. __u32 id = -1;
  639. int ret = 0;
  640. if (!nfs_map_string_to_numeric(name, namelen, &id))
  641. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  642. if (ret == 0) {
  643. *gid = make_kgid(&init_user_ns, id);
  644. if (!gid_valid(*gid))
  645. ret = -ERANGE;
  646. }
  647. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  648. return ret;
  649. }
  650. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  651. {
  652. struct idmap *idmap = server->nfs_client->cl_idmap;
  653. int ret = -EINVAL;
  654. __u32 id;
  655. id = from_kuid(&init_user_ns, uid);
  656. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  657. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  658. if (ret < 0)
  659. ret = nfs_map_numeric_to_string(id, buf, buflen);
  660. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  661. return ret;
  662. }
  663. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  664. {
  665. struct idmap *idmap = server->nfs_client->cl_idmap;
  666. int ret = -EINVAL;
  667. __u32 id;
  668. id = from_kgid(&init_user_ns, gid);
  669. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  670. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  671. if (ret < 0)
  672. ret = nfs_map_numeric_to_string(id, buf, buflen);
  673. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  674. return ret;
  675. }