idmap.c 21 KB

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