idmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. #define NFS_UINT_MAXLEN 11
  52. static const struct cred *id_resolver_cache;
  53. static struct key_type key_type_id_resolver_legacy;
  54. struct idmap {
  55. struct rpc_pipe *idmap_pipe;
  56. struct key_construction *idmap_key_cons;
  57. struct mutex idmap_mutex;
  58. };
  59. /**
  60. * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  61. * @fattr: fully initialised struct nfs_fattr
  62. * @owner_name: owner name string cache
  63. * @group_name: group name string cache
  64. */
  65. void nfs_fattr_init_names(struct nfs_fattr *fattr,
  66. struct nfs4_string *owner_name,
  67. struct nfs4_string *group_name)
  68. {
  69. fattr->owner_name = owner_name;
  70. fattr->group_name = group_name;
  71. }
  72. static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  73. {
  74. fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  75. kfree(fattr->owner_name->data);
  76. }
  77. static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  78. {
  79. fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  80. kfree(fattr->group_name->data);
  81. }
  82. static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  83. {
  84. struct nfs4_string *owner = fattr->owner_name;
  85. __u32 uid;
  86. if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  87. return false;
  88. if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  89. fattr->uid = uid;
  90. fattr->valid |= NFS_ATTR_FATTR_OWNER;
  91. }
  92. return true;
  93. }
  94. static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  95. {
  96. struct nfs4_string *group = fattr->group_name;
  97. __u32 gid;
  98. if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  99. return false;
  100. if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  101. fattr->gid = gid;
  102. fattr->valid |= NFS_ATTR_FATTR_GROUP;
  103. }
  104. return true;
  105. }
  106. /**
  107. * nfs_fattr_free_names - free up the NFSv4 owner and group strings
  108. * @fattr: a fully initialised nfs_fattr structure
  109. */
  110. void nfs_fattr_free_names(struct nfs_fattr *fattr)
  111. {
  112. if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  113. nfs_fattr_free_owner_name(fattr);
  114. if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  115. nfs_fattr_free_group_name(fattr);
  116. }
  117. /**
  118. * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
  119. * @server: pointer to the filesystem nfs_server structure
  120. * @fattr: a fully initialised nfs_fattr structure
  121. *
  122. * This helper maps the cached NFSv4 owner/group strings in fattr into
  123. * their numeric uid/gid equivalents, and then frees the cached strings.
  124. */
  125. void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  126. {
  127. if (nfs_fattr_map_owner_name(server, fattr))
  128. nfs_fattr_free_owner_name(fattr);
  129. if (nfs_fattr_map_group_name(server, fattr))
  130. nfs_fattr_free_group_name(fattr);
  131. }
  132. static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  133. {
  134. unsigned long val;
  135. char buf[16];
  136. if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  137. return 0;
  138. memcpy(buf, name, namelen);
  139. buf[namelen] = '\0';
  140. if (strict_strtoul(buf, 0, &val) != 0)
  141. return 0;
  142. *res = val;
  143. return 1;
  144. }
  145. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  146. {
  147. return snprintf(buf, buflen, "%u", id);
  148. }
  149. static struct key_type key_type_id_resolver = {
  150. .name = "id_resolver",
  151. .instantiate = user_instantiate,
  152. .match = user_match,
  153. .revoke = user_revoke,
  154. .destroy = user_destroy,
  155. .describe = user_describe,
  156. .read = user_read,
  157. };
  158. static int nfs_idmap_init_keyring(void)
  159. {
  160. struct cred *cred;
  161. struct key *keyring;
  162. int ret = 0;
  163. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  164. key_type_id_resolver.name);
  165. cred = prepare_kernel_cred(NULL);
  166. if (!cred)
  167. return -ENOMEM;
  168. keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
  169. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  170. KEY_USR_VIEW | KEY_USR_READ,
  171. KEY_ALLOC_NOT_IN_QUOTA);
  172. if (IS_ERR(keyring)) {
  173. ret = PTR_ERR(keyring);
  174. goto failed_put_cred;
  175. }
  176. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  177. if (ret < 0)
  178. goto failed_put_key;
  179. ret = register_key_type(&key_type_id_resolver);
  180. if (ret < 0)
  181. goto failed_put_key;
  182. ret = register_key_type(&key_type_id_resolver_legacy);
  183. if (ret < 0)
  184. goto failed_reg_legacy;
  185. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  186. cred->thread_keyring = keyring;
  187. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  188. id_resolver_cache = cred;
  189. return 0;
  190. failed_reg_legacy:
  191. unregister_key_type(&key_type_id_resolver);
  192. failed_put_key:
  193. key_put(keyring);
  194. failed_put_cred:
  195. put_cred(cred);
  196. return ret;
  197. }
  198. static void nfs_idmap_quit_keyring(void)
  199. {
  200. key_revoke(id_resolver_cache->thread_keyring);
  201. unregister_key_type(&key_type_id_resolver);
  202. unregister_key_type(&key_type_id_resolver_legacy);
  203. put_cred(id_resolver_cache);
  204. }
  205. /*
  206. * Assemble the description to pass to request_key()
  207. * This function will allocate a new string and update dest to point
  208. * at it. The caller is responsible for freeing dest.
  209. *
  210. * On error 0 is returned. Otherwise, the length of dest is returned.
  211. */
  212. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  213. const char *type, size_t typelen, char **desc)
  214. {
  215. char *cp;
  216. size_t desclen = typelen + namelen + 2;
  217. *desc = kmalloc(desclen, GFP_KERNEL);
  218. if (!*desc)
  219. return -ENOMEM;
  220. cp = *desc;
  221. memcpy(cp, type, typelen);
  222. cp += typelen;
  223. *cp++ = ':';
  224. memcpy(cp, name, namelen);
  225. cp += namelen;
  226. *cp = '\0';
  227. return desclen;
  228. }
  229. static ssize_t nfs_idmap_request_key(struct key_type *key_type,
  230. const char *name, size_t namelen,
  231. const char *type, void *data,
  232. size_t data_size, struct idmap *idmap)
  233. {
  234. const struct cred *saved_cred;
  235. struct key *rkey;
  236. char *desc;
  237. struct user_key_payload *payload;
  238. ssize_t ret;
  239. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  240. if (ret <= 0)
  241. goto out;
  242. saved_cred = override_creds(id_resolver_cache);
  243. if (idmap)
  244. rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
  245. else
  246. rkey = request_key(&key_type_id_resolver, desc, "");
  247. revert_creds(saved_cred);
  248. kfree(desc);
  249. if (IS_ERR(rkey)) {
  250. ret = PTR_ERR(rkey);
  251. goto out;
  252. }
  253. rcu_read_lock();
  254. rkey->perm |= KEY_USR_VIEW;
  255. ret = key_validate(rkey);
  256. if (ret < 0)
  257. goto out_up;
  258. payload = rcu_dereference(rkey->payload.data);
  259. if (IS_ERR_OR_NULL(payload)) {
  260. ret = PTR_ERR(payload);
  261. goto out_up;
  262. }
  263. ret = payload->datalen;
  264. if (ret > 0 && ret <= data_size)
  265. memcpy(data, payload->data, ret);
  266. else
  267. ret = -EINVAL;
  268. out_up:
  269. rcu_read_unlock();
  270. key_put(rkey);
  271. out:
  272. return ret;
  273. }
  274. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  275. const char *type, void *data,
  276. size_t data_size, struct idmap *idmap)
  277. {
  278. ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
  279. name, namelen, type, data,
  280. data_size, NULL);
  281. if (ret < 0) {
  282. mutex_lock(&idmap->idmap_mutex);
  283. ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
  284. name, namelen, type, data,
  285. data_size, idmap);
  286. mutex_unlock(&idmap->idmap_mutex);
  287. }
  288. return ret;
  289. }
  290. /* ID -> Name */
  291. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  292. size_t buflen, struct idmap *idmap)
  293. {
  294. char id_str[NFS_UINT_MAXLEN];
  295. int id_len;
  296. ssize_t ret;
  297. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  298. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  299. if (ret < 0)
  300. return -EINVAL;
  301. return ret;
  302. }
  303. /* Name -> ID */
  304. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  305. __u32 *id, struct idmap *idmap)
  306. {
  307. char id_str[NFS_UINT_MAXLEN];
  308. long id_long;
  309. ssize_t data_size;
  310. int ret = 0;
  311. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  312. if (data_size <= 0) {
  313. ret = -EINVAL;
  314. } else {
  315. ret = strict_strtol(id_str, 10, &id_long);
  316. *id = (__u32)id_long;
  317. }
  318. return ret;
  319. }
  320. /* idmap classic begins here */
  321. enum {
  322. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  323. };
  324. static const match_table_t nfs_idmap_tokens = {
  325. { Opt_find_uid, "uid:%s" },
  326. { Opt_find_gid, "gid:%s" },
  327. { Opt_find_user, "user:%s" },
  328. { Opt_find_group, "group:%s" },
  329. { Opt_find_err, NULL }
  330. };
  331. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  332. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  333. size_t);
  334. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  335. static const struct rpc_pipe_ops idmap_upcall_ops = {
  336. .upcall = rpc_pipe_generic_upcall,
  337. .downcall = idmap_pipe_downcall,
  338. .destroy_msg = idmap_pipe_destroy_msg,
  339. };
  340. static struct key_type key_type_id_resolver_legacy = {
  341. .name = "id_legacy",
  342. .instantiate = user_instantiate,
  343. .match = user_match,
  344. .revoke = user_revoke,
  345. .destroy = user_destroy,
  346. .describe = user_describe,
  347. .read = user_read,
  348. .request_key = nfs_idmap_legacy_upcall,
  349. };
  350. static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
  351. {
  352. if (pipe->dentry)
  353. rpc_unlink(pipe->dentry);
  354. }
  355. static int __nfs_idmap_register(struct dentry *dir,
  356. struct idmap *idmap,
  357. struct rpc_pipe *pipe)
  358. {
  359. struct dentry *dentry;
  360. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  361. if (IS_ERR(dentry))
  362. return PTR_ERR(dentry);
  363. pipe->dentry = dentry;
  364. return 0;
  365. }
  366. static void nfs_idmap_unregister(struct nfs_client *clp,
  367. struct rpc_pipe *pipe)
  368. {
  369. struct net *net = clp->cl_net;
  370. struct super_block *pipefs_sb;
  371. pipefs_sb = rpc_get_sb_net(net);
  372. if (pipefs_sb) {
  373. __nfs_idmap_unregister(pipe);
  374. rpc_put_sb_net(net);
  375. }
  376. }
  377. static int nfs_idmap_register(struct nfs_client *clp,
  378. struct idmap *idmap,
  379. struct rpc_pipe *pipe)
  380. {
  381. struct net *net = clp->cl_net;
  382. struct super_block *pipefs_sb;
  383. int err = 0;
  384. pipefs_sb = rpc_get_sb_net(net);
  385. if (pipefs_sb) {
  386. if (clp->cl_rpcclient->cl_dentry)
  387. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  388. idmap, pipe);
  389. rpc_put_sb_net(net);
  390. }
  391. return err;
  392. }
  393. int
  394. nfs_idmap_new(struct nfs_client *clp)
  395. {
  396. struct idmap *idmap;
  397. struct rpc_pipe *pipe;
  398. int error;
  399. BUG_ON(clp->cl_idmap != NULL);
  400. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  401. if (idmap == NULL)
  402. return -ENOMEM;
  403. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  404. if (IS_ERR(pipe)) {
  405. error = PTR_ERR(pipe);
  406. kfree(idmap);
  407. return error;
  408. }
  409. error = nfs_idmap_register(clp, idmap, pipe);
  410. if (error) {
  411. rpc_destroy_pipe_data(pipe);
  412. kfree(idmap);
  413. return error;
  414. }
  415. idmap->idmap_pipe = pipe;
  416. mutex_init(&idmap->idmap_mutex);
  417. clp->cl_idmap = idmap;
  418. return 0;
  419. }
  420. void
  421. nfs_idmap_delete(struct nfs_client *clp)
  422. {
  423. struct idmap *idmap = clp->cl_idmap;
  424. if (!idmap)
  425. return;
  426. nfs_idmap_unregister(clp, idmap->idmap_pipe);
  427. rpc_destroy_pipe_data(idmap->idmap_pipe);
  428. clp->cl_idmap = NULL;
  429. kfree(idmap);
  430. }
  431. static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
  432. struct super_block *sb)
  433. {
  434. int err = 0;
  435. switch (event) {
  436. case RPC_PIPEFS_MOUNT:
  437. BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
  438. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  439. clp->cl_idmap,
  440. clp->cl_idmap->idmap_pipe);
  441. break;
  442. case RPC_PIPEFS_UMOUNT:
  443. if (clp->cl_idmap->idmap_pipe) {
  444. struct dentry *parent;
  445. parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
  446. __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
  447. /*
  448. * Note: This is a dirty hack. SUNRPC hook has been
  449. * called already but simple_rmdir() call for the
  450. * directory returned with error because of idmap pipe
  451. * inside. Thus now we have to remove this directory
  452. * here.
  453. */
  454. if (rpc_rmdir(parent))
  455. printk(KERN_ERR "NFS: %s: failed to remove "
  456. "clnt dir!\n", __func__);
  457. }
  458. break;
  459. default:
  460. printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
  461. event);
  462. return -ENOTSUPP;
  463. }
  464. return err;
  465. }
  466. static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
  467. {
  468. struct nfs_net *nn = net_generic(net, nfs_net_id);
  469. struct dentry *cl_dentry;
  470. struct nfs_client *clp;
  471. int err;
  472. restart:
  473. spin_lock(&nn->nfs_client_lock);
  474. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  475. /* Wait for initialisation to finish */
  476. if (clp->cl_cons_state == NFS_CS_INITING) {
  477. atomic_inc(&clp->cl_count);
  478. spin_unlock(&nn->nfs_client_lock);
  479. err = nfs_wait_client_init_complete(clp);
  480. nfs_put_client(clp);
  481. if (err)
  482. return NULL;
  483. goto restart;
  484. }
  485. /* Skip nfs_clients that failed to initialise */
  486. if (clp->cl_cons_state < 0)
  487. continue;
  488. smp_rmb();
  489. if (clp->rpc_ops != &nfs_v4_clientops)
  490. continue;
  491. cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
  492. if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
  493. ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
  494. continue;
  495. atomic_inc(&clp->cl_count);
  496. spin_unlock(&nn->nfs_client_lock);
  497. return clp;
  498. }
  499. spin_unlock(&nn->nfs_client_lock);
  500. return NULL;
  501. }
  502. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  503. void *ptr)
  504. {
  505. struct super_block *sb = ptr;
  506. struct nfs_client *clp;
  507. int error = 0;
  508. if (!try_module_get(THIS_MODULE))
  509. return 0;
  510. while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
  511. error = __rpc_pipefs_event(clp, event, sb);
  512. nfs_put_client(clp);
  513. if (error)
  514. break;
  515. }
  516. module_put(THIS_MODULE);
  517. return error;
  518. }
  519. #define PIPEFS_NFS_PRIO 1
  520. static struct notifier_block nfs_idmap_block = {
  521. .notifier_call = rpc_pipefs_event,
  522. .priority = SUNRPC_PIPEFS_NFS_PRIO,
  523. };
  524. int nfs_idmap_init(void)
  525. {
  526. int ret;
  527. ret = nfs_idmap_init_keyring();
  528. if (ret != 0)
  529. goto out;
  530. ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
  531. if (ret != 0)
  532. nfs_idmap_quit_keyring();
  533. out:
  534. return ret;
  535. }
  536. void nfs_idmap_quit(void)
  537. {
  538. rpc_pipefs_notifier_unregister(&nfs_idmap_block);
  539. nfs_idmap_quit_keyring();
  540. }
  541. static int nfs_idmap_prepare_message(char *desc, struct idmap_msg *im,
  542. struct rpc_pipe_msg *msg)
  543. {
  544. substring_t substr;
  545. int token, ret;
  546. memset(im, 0, sizeof(*im));
  547. memset(msg, 0, sizeof(*msg));
  548. im->im_type = IDMAP_TYPE_GROUP;
  549. token = match_token(desc, nfs_idmap_tokens, &substr);
  550. switch (token) {
  551. case Opt_find_uid:
  552. im->im_type = IDMAP_TYPE_USER;
  553. case Opt_find_gid:
  554. im->im_conv = IDMAP_CONV_NAMETOID;
  555. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  556. break;
  557. case Opt_find_user:
  558. im->im_type = IDMAP_TYPE_USER;
  559. case Opt_find_group:
  560. im->im_conv = IDMAP_CONV_IDTONAME;
  561. ret = match_int(&substr, &im->im_id);
  562. break;
  563. default:
  564. ret = -EINVAL;
  565. goto out;
  566. }
  567. msg->data = im;
  568. msg->len = sizeof(struct idmap_msg);
  569. out:
  570. return ret;
  571. }
  572. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  573. const char *op,
  574. void *aux)
  575. {
  576. struct rpc_pipe_msg *msg;
  577. struct idmap_msg *im;
  578. struct idmap *idmap = (struct idmap *)aux;
  579. struct key *key = cons->key;
  580. int ret = -ENOMEM;
  581. /* msg and im are freed in idmap_pipe_destroy_msg */
  582. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  583. if (!msg)
  584. goto out0;
  585. im = kmalloc(sizeof(*im), GFP_KERNEL);
  586. if (!im)
  587. goto out1;
  588. ret = nfs_idmap_prepare_message(key->description, im, msg);
  589. if (ret < 0)
  590. goto out2;
  591. BUG_ON(idmap->idmap_key_cons != NULL);
  592. idmap->idmap_key_cons = cons;
  593. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  594. if (ret < 0)
  595. goto out2;
  596. return ret;
  597. out2:
  598. kfree(im);
  599. out1:
  600. kfree(msg);
  601. out0:
  602. complete_request_key(cons, ret);
  603. return ret;
  604. }
  605. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
  606. {
  607. return key_instantiate_and_link(key, data, strlen(data) + 1,
  608. id_resolver_cache->thread_keyring,
  609. authkey);
  610. }
  611. static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
  612. {
  613. char id_str[NFS_UINT_MAXLEN];
  614. int ret = -EINVAL;
  615. switch (im->im_conv) {
  616. case IDMAP_CONV_NAMETOID:
  617. sprintf(id_str, "%d", im->im_id);
  618. ret = nfs_idmap_instantiate(key, authkey, id_str);
  619. break;
  620. case IDMAP_CONV_IDTONAME:
  621. ret = nfs_idmap_instantiate(key, authkey, im->im_name);
  622. break;
  623. }
  624. return ret;
  625. }
  626. static ssize_t
  627. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  628. {
  629. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  630. struct idmap *idmap = (struct idmap *)rpci->private;
  631. struct key_construction *cons;
  632. struct idmap_msg im;
  633. size_t namelen_in;
  634. int ret;
  635. /* If instantiation is successful, anyone waiting for key construction
  636. * will have been woken up and someone else may now have used
  637. * idmap_key_cons - so after this point we may no longer touch it.
  638. */
  639. cons = ACCESS_ONCE(idmap->idmap_key_cons);
  640. idmap->idmap_key_cons = NULL;
  641. if (mlen != sizeof(im)) {
  642. ret = -ENOSPC;
  643. goto out;
  644. }
  645. if (copy_from_user(&im, src, mlen) != 0) {
  646. ret = -EFAULT;
  647. goto out;
  648. }
  649. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  650. ret = mlen;
  651. complete_request_key(cons, -ENOKEY);
  652. goto out_incomplete;
  653. }
  654. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  655. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
  660. if (ret >= 0) {
  661. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  662. ret = mlen;
  663. }
  664. out:
  665. complete_request_key(cons, ret);
  666. out_incomplete:
  667. return ret;
  668. }
  669. static void
  670. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  671. {
  672. /* Free memory allocated in nfs_idmap_legacy_upcall() */
  673. kfree(msg->data);
  674. kfree(msg);
  675. }
  676. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  677. {
  678. struct idmap *idmap = server->nfs_client->cl_idmap;
  679. if (nfs_map_string_to_numeric(name, namelen, uid))
  680. return 0;
  681. return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
  682. }
  683. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
  684. {
  685. struct idmap *idmap = server->nfs_client->cl_idmap;
  686. if (nfs_map_string_to_numeric(name, namelen, gid))
  687. return 0;
  688. return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
  689. }
  690. int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  691. {
  692. struct idmap *idmap = server->nfs_client->cl_idmap;
  693. int ret = -EINVAL;
  694. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  695. ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
  696. if (ret < 0)
  697. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  698. return ret;
  699. }
  700. int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
  701. {
  702. struct idmap *idmap = server->nfs_client->cl_idmap;
  703. int ret = -EINVAL;
  704. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  705. ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
  706. if (ret < 0)
  707. ret = nfs_map_numeric_to_string(gid, buf, buflen);
  708. return ret;
  709. }