idmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. .revoke = user_revoke,
  163. .destroy = user_destroy,
  164. .describe = user_describe,
  165. .read = user_read,
  166. };
  167. static int nfs_idmap_init_keyring(void)
  168. {
  169. struct cred *cred;
  170. struct key *keyring;
  171. int ret = 0;
  172. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  173. key_type_id_resolver.name);
  174. cred = prepare_kernel_cred(NULL);
  175. if (!cred)
  176. return -ENOMEM;
  177. keyring = keyring_alloc(".id_resolver",
  178. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  179. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  180. KEY_USR_VIEW | KEY_USR_READ,
  181. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  182. if (IS_ERR(keyring)) {
  183. ret = PTR_ERR(keyring);
  184. goto failed_put_cred;
  185. }
  186. ret = register_key_type(&key_type_id_resolver);
  187. if (ret < 0)
  188. goto failed_put_key;
  189. ret = register_key_type(&key_type_id_resolver_legacy);
  190. if (ret < 0)
  191. goto failed_reg_legacy;
  192. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  193. cred->thread_keyring = keyring;
  194. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  195. id_resolver_cache = cred;
  196. return 0;
  197. failed_reg_legacy:
  198. unregister_key_type(&key_type_id_resolver);
  199. failed_put_key:
  200. key_put(keyring);
  201. failed_put_cred:
  202. put_cred(cred);
  203. return ret;
  204. }
  205. static void nfs_idmap_quit_keyring(void)
  206. {
  207. key_revoke(id_resolver_cache->thread_keyring);
  208. unregister_key_type(&key_type_id_resolver);
  209. unregister_key_type(&key_type_id_resolver_legacy);
  210. put_cred(id_resolver_cache);
  211. }
  212. /*
  213. * Assemble the description to pass to request_key()
  214. * This function will allocate a new string and update dest to point
  215. * at it. The caller is responsible for freeing dest.
  216. *
  217. * On error 0 is returned. Otherwise, the length of dest is returned.
  218. */
  219. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  220. const char *type, size_t typelen, char **desc)
  221. {
  222. char *cp;
  223. size_t desclen = typelen + namelen + 2;
  224. *desc = kmalloc(desclen, GFP_KERNEL);
  225. if (!*desc)
  226. return -ENOMEM;
  227. cp = *desc;
  228. memcpy(cp, type, typelen);
  229. cp += typelen;
  230. *cp++ = ':';
  231. memcpy(cp, name, namelen);
  232. cp += namelen;
  233. *cp = '\0';
  234. return desclen;
  235. }
  236. static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  237. const char *type, struct idmap *idmap)
  238. {
  239. char *desc;
  240. struct key *rkey;
  241. ssize_t ret;
  242. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  243. if (ret <= 0)
  244. return ERR_PTR(ret);
  245. rkey = request_key(&key_type_id_resolver, desc, "");
  246. if (IS_ERR(rkey)) {
  247. mutex_lock(&idmap->idmap_mutex);
  248. rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  249. desc, "", 0, idmap);
  250. mutex_unlock(&idmap->idmap_mutex);
  251. }
  252. if (!IS_ERR(rkey))
  253. set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
  254. kfree(desc);
  255. return rkey;
  256. }
  257. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  258. const char *type, void *data,
  259. size_t data_size, struct idmap *idmap)
  260. {
  261. const struct cred *saved_cred;
  262. struct key *rkey;
  263. struct user_key_payload *payload;
  264. ssize_t ret;
  265. saved_cred = override_creds(id_resolver_cache);
  266. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  267. revert_creds(saved_cred);
  268. if (IS_ERR(rkey)) {
  269. ret = PTR_ERR(rkey);
  270. goto out;
  271. }
  272. rcu_read_lock();
  273. rkey->perm |= KEY_USR_VIEW;
  274. ret = key_validate(rkey);
  275. if (ret < 0)
  276. goto out_up;
  277. payload = rcu_dereference(rkey->payload.rcudata);
  278. if (IS_ERR_OR_NULL(payload)) {
  279. ret = PTR_ERR(payload);
  280. goto out_up;
  281. }
  282. ret = payload->datalen;
  283. if (ret > 0 && ret <= data_size)
  284. memcpy(data, payload->data, ret);
  285. else
  286. ret = -EINVAL;
  287. out_up:
  288. rcu_read_unlock();
  289. key_put(rkey);
  290. out:
  291. return ret;
  292. }
  293. /* ID -> Name */
  294. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  295. size_t buflen, struct idmap *idmap)
  296. {
  297. char id_str[NFS_UINT_MAXLEN];
  298. int id_len;
  299. ssize_t ret;
  300. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  301. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  302. if (ret < 0)
  303. return -EINVAL;
  304. return ret;
  305. }
  306. /* Name -> ID */
  307. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  308. __u32 *id, struct idmap *idmap)
  309. {
  310. char id_str[NFS_UINT_MAXLEN];
  311. long id_long;
  312. ssize_t data_size;
  313. int ret = 0;
  314. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  315. if (data_size <= 0) {
  316. ret = -EINVAL;
  317. } else {
  318. ret = kstrtol(id_str, 10, &id_long);
  319. *id = (__u32)id_long;
  320. }
  321. return ret;
  322. }
  323. /* idmap classic begins here */
  324. enum {
  325. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  326. };
  327. static const match_table_t nfs_idmap_tokens = {
  328. { Opt_find_uid, "uid:%s" },
  329. { Opt_find_gid, "gid:%s" },
  330. { Opt_find_user, "user:%s" },
  331. { Opt_find_group, "group:%s" },
  332. { Opt_find_err, NULL }
  333. };
  334. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  335. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  336. size_t);
  337. static void idmap_release_pipe(struct inode *);
  338. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  339. static const struct rpc_pipe_ops idmap_upcall_ops = {
  340. .upcall = rpc_pipe_generic_upcall,
  341. .downcall = idmap_pipe_downcall,
  342. .release_pipe = idmap_release_pipe,
  343. .destroy_msg = idmap_pipe_destroy_msg,
  344. };
  345. static struct key_type key_type_id_resolver_legacy = {
  346. .name = "id_legacy",
  347. .preparse = user_preparse,
  348. .free_preparse = user_free_preparse,
  349. .instantiate = generic_key_instantiate,
  350. .revoke = user_revoke,
  351. .destroy = user_destroy,
  352. .describe = user_describe,
  353. .read = user_read,
  354. .request_key = nfs_idmap_legacy_upcall,
  355. };
  356. static void nfs_idmap_pipe_destroy(struct dentry *dir,
  357. struct rpc_pipe_dir_object *pdo)
  358. {
  359. struct idmap *idmap = pdo->pdo_data;
  360. struct rpc_pipe *pipe = idmap->idmap_pipe;
  361. if (pipe->dentry) {
  362. rpc_unlink(pipe->dentry);
  363. pipe->dentry = NULL;
  364. }
  365. }
  366. static int nfs_idmap_pipe_create(struct dentry *dir,
  367. struct rpc_pipe_dir_object *pdo)
  368. {
  369. struct idmap *idmap = pdo->pdo_data;
  370. struct rpc_pipe *pipe = idmap->idmap_pipe;
  371. struct dentry *dentry;
  372. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  373. if (IS_ERR(dentry))
  374. return PTR_ERR(dentry);
  375. pipe->dentry = dentry;
  376. return 0;
  377. }
  378. static const struct rpc_pipe_dir_object_ops nfs_idmap_pipe_dir_object_ops = {
  379. .create = nfs_idmap_pipe_create,
  380. .destroy = nfs_idmap_pipe_destroy,
  381. };
  382. int
  383. nfs_idmap_new(struct nfs_client *clp)
  384. {
  385. struct idmap *idmap;
  386. struct rpc_pipe *pipe;
  387. int error;
  388. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  389. if (idmap == NULL)
  390. return -ENOMEM;
  391. rpc_init_pipe_dir_object(&idmap->idmap_pdo,
  392. &nfs_idmap_pipe_dir_object_ops,
  393. idmap);
  394. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  395. if (IS_ERR(pipe)) {
  396. error = PTR_ERR(pipe);
  397. goto err;
  398. }
  399. idmap->idmap_pipe = pipe;
  400. mutex_init(&idmap->idmap_mutex);
  401. error = rpc_add_pipe_dir_object(clp->cl_net,
  402. &clp->cl_rpcclient->cl_pipedir_objects,
  403. &idmap->idmap_pdo);
  404. if (error)
  405. goto err_destroy_pipe;
  406. clp->cl_idmap = idmap;
  407. return 0;
  408. err_destroy_pipe:
  409. rpc_destroy_pipe_data(idmap->idmap_pipe);
  410. err:
  411. kfree(idmap);
  412. return error;
  413. }
  414. void
  415. nfs_idmap_delete(struct nfs_client *clp)
  416. {
  417. struct idmap *idmap = clp->cl_idmap;
  418. if (!idmap)
  419. return;
  420. clp->cl_idmap = NULL;
  421. rpc_remove_pipe_dir_object(clp->cl_net,
  422. &clp->cl_rpcclient->cl_pipedir_objects,
  423. &idmap->idmap_pdo);
  424. rpc_destroy_pipe_data(idmap->idmap_pipe);
  425. kfree(idmap);
  426. }
  427. int nfs_idmap_init(void)
  428. {
  429. int ret;
  430. ret = nfs_idmap_init_keyring();
  431. if (ret != 0)
  432. goto out;
  433. out:
  434. return ret;
  435. }
  436. void nfs_idmap_quit(void)
  437. {
  438. nfs_idmap_quit_keyring();
  439. }
  440. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  441. struct idmap_msg *im,
  442. struct rpc_pipe_msg *msg)
  443. {
  444. substring_t substr;
  445. int token, ret;
  446. im->im_type = IDMAP_TYPE_GROUP;
  447. token = match_token(desc, nfs_idmap_tokens, &substr);
  448. switch (token) {
  449. case Opt_find_uid:
  450. im->im_type = IDMAP_TYPE_USER;
  451. case Opt_find_gid:
  452. im->im_conv = IDMAP_CONV_NAMETOID;
  453. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  454. break;
  455. case Opt_find_user:
  456. im->im_type = IDMAP_TYPE_USER;
  457. case Opt_find_group:
  458. im->im_conv = IDMAP_CONV_IDTONAME;
  459. ret = match_int(&substr, &im->im_id);
  460. break;
  461. default:
  462. ret = -EINVAL;
  463. goto out;
  464. }
  465. msg->data = im;
  466. msg->len = sizeof(struct idmap_msg);
  467. out:
  468. return ret;
  469. }
  470. static bool
  471. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  472. struct idmap_legacy_upcalldata *data)
  473. {
  474. if (idmap->idmap_upcall_data != NULL) {
  475. WARN_ON_ONCE(1);
  476. return false;
  477. }
  478. idmap->idmap_upcall_data = data;
  479. return true;
  480. }
  481. static void
  482. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  483. {
  484. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  485. kfree(idmap->idmap_upcall_data);
  486. idmap->idmap_upcall_data = NULL;
  487. complete_request_key(cons, ret);
  488. }
  489. static void
  490. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  491. {
  492. if (idmap->idmap_upcall_data != NULL)
  493. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  494. }
  495. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  496. const char *op,
  497. void *aux)
  498. {
  499. struct idmap_legacy_upcalldata *data;
  500. struct rpc_pipe_msg *msg;
  501. struct idmap_msg *im;
  502. struct idmap *idmap = (struct idmap *)aux;
  503. struct key *key = cons->key;
  504. int ret = -ENOMEM;
  505. /* msg and im are freed in idmap_pipe_destroy_msg */
  506. data = kzalloc(sizeof(*data), GFP_KERNEL);
  507. if (!data)
  508. goto out1;
  509. msg = &data->pipe_msg;
  510. im = &data->idmap_msg;
  511. data->idmap = idmap;
  512. data->key_cons = cons;
  513. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  514. if (ret < 0)
  515. goto out2;
  516. ret = -EAGAIN;
  517. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  518. goto out2;
  519. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  520. if (ret < 0)
  521. nfs_idmap_abort_pipe_upcall(idmap, ret);
  522. return ret;
  523. out2:
  524. kfree(data);
  525. out1:
  526. complete_request_key(cons, ret);
  527. return ret;
  528. }
  529. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  530. {
  531. return key_instantiate_and_link(key, data, datalen,
  532. id_resolver_cache->thread_keyring,
  533. authkey);
  534. }
  535. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  536. struct idmap_msg *upcall,
  537. struct key *key, struct key *authkey)
  538. {
  539. char id_str[NFS_UINT_MAXLEN];
  540. size_t len;
  541. int ret = -ENOKEY;
  542. /* ret = -ENOKEY */
  543. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  544. goto out;
  545. switch (im->im_conv) {
  546. case IDMAP_CONV_NAMETOID:
  547. if (strcmp(upcall->im_name, im->im_name) != 0)
  548. break;
  549. /* Note: here we store the NUL terminator too */
  550. len = sprintf(id_str, "%d", im->im_id) + 1;
  551. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  552. break;
  553. case IDMAP_CONV_IDTONAME:
  554. if (upcall->im_id != im->im_id)
  555. break;
  556. len = strlen(im->im_name);
  557. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  558. break;
  559. default:
  560. ret = -EINVAL;
  561. }
  562. out:
  563. return ret;
  564. }
  565. static ssize_t
  566. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  567. {
  568. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  569. struct idmap *idmap = (struct idmap *)rpci->private;
  570. struct key_construction *cons;
  571. struct idmap_msg im;
  572. size_t namelen_in;
  573. int ret = -ENOKEY;
  574. /* If instantiation is successful, anyone waiting for key construction
  575. * will have been woken up and someone else may now have used
  576. * idmap_key_cons - so after this point we may no longer touch it.
  577. */
  578. if (idmap->idmap_upcall_data == NULL)
  579. goto out_noupcall;
  580. cons = idmap->idmap_upcall_data->key_cons;
  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. cons->key, cons->authkey);
  601. if (ret >= 0) {
  602. key_set_timeout(cons->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. }