idmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 = key_alloc(&key_type_keyring, ".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);
  178. if (IS_ERR(keyring)) {
  179. ret = PTR_ERR(keyring);
  180. goto failed_put_cred;
  181. }
  182. ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
  183. if (ret < 0)
  184. goto failed_put_key;
  185. ret = register_key_type(&key_type_id_resolver);
  186. if (ret < 0)
  187. goto failed_put_key;
  188. ret = register_key_type(&key_type_id_resolver_legacy);
  189. if (ret < 0)
  190. goto failed_reg_legacy;
  191. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  192. cred->thread_keyring = keyring;
  193. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  194. id_resolver_cache = cred;
  195. return 0;
  196. failed_reg_legacy:
  197. unregister_key_type(&key_type_id_resolver);
  198. failed_put_key:
  199. key_put(keyring);
  200. failed_put_cred:
  201. put_cred(cred);
  202. return ret;
  203. }
  204. static void nfs_idmap_quit_keyring(void)
  205. {
  206. key_revoke(id_resolver_cache->thread_keyring);
  207. unregister_key_type(&key_type_id_resolver);
  208. unregister_key_type(&key_type_id_resolver_legacy);
  209. put_cred(id_resolver_cache);
  210. }
  211. /*
  212. * Assemble the description to pass to request_key()
  213. * This function will allocate a new string and update dest to point
  214. * at it. The caller is responsible for freeing dest.
  215. *
  216. * On error 0 is returned. Otherwise, the length of dest is returned.
  217. */
  218. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  219. const char *type, size_t typelen, char **desc)
  220. {
  221. char *cp;
  222. size_t desclen = typelen + namelen + 2;
  223. *desc = kmalloc(desclen, GFP_KERNEL);
  224. if (!*desc)
  225. return -ENOMEM;
  226. cp = *desc;
  227. memcpy(cp, type, typelen);
  228. cp += typelen;
  229. *cp++ = ':';
  230. memcpy(cp, name, namelen);
  231. cp += namelen;
  232. *cp = '\0';
  233. return desclen;
  234. }
  235. static ssize_t nfs_idmap_request_key(struct key_type *key_type,
  236. const char *name, size_t namelen,
  237. const char *type, void *data,
  238. size_t data_size, struct idmap *idmap)
  239. {
  240. const struct cred *saved_cred;
  241. struct key *rkey;
  242. char *desc;
  243. struct user_key_payload *payload;
  244. ssize_t ret;
  245. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  246. if (ret <= 0)
  247. goto out;
  248. saved_cred = override_creds(id_resolver_cache);
  249. if (idmap)
  250. rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
  251. else
  252. rkey = request_key(&key_type_id_resolver, desc, "");
  253. revert_creds(saved_cred);
  254. kfree(desc);
  255. if (IS_ERR(rkey)) {
  256. ret = PTR_ERR(rkey);
  257. goto out;
  258. }
  259. rcu_read_lock();
  260. rkey->perm |= KEY_USR_VIEW;
  261. ret = key_validate(rkey);
  262. if (ret < 0)
  263. goto out_up;
  264. payload = rcu_dereference(rkey->payload.data);
  265. if (IS_ERR_OR_NULL(payload)) {
  266. ret = PTR_ERR(payload);
  267. goto out_up;
  268. }
  269. ret = payload->datalen;
  270. if (ret > 0 && ret <= data_size)
  271. memcpy(data, payload->data, ret);
  272. else
  273. ret = -EINVAL;
  274. out_up:
  275. rcu_read_unlock();
  276. key_put(rkey);
  277. out:
  278. return ret;
  279. }
  280. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  281. const char *type, void *data,
  282. size_t data_size, struct idmap *idmap)
  283. {
  284. ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
  285. name, namelen, type, data,
  286. data_size, NULL);
  287. if (ret < 0) {
  288. mutex_lock(&idmap->idmap_mutex);
  289. ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
  290. name, namelen, type, data,
  291. data_size, idmap);
  292. mutex_unlock(&idmap->idmap_mutex);
  293. }
  294. return ret;
  295. }
  296. /* ID -> Name */
  297. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  298. size_t buflen, struct idmap *idmap)
  299. {
  300. char id_str[NFS_UINT_MAXLEN];
  301. int id_len;
  302. ssize_t ret;
  303. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  304. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  305. if (ret < 0)
  306. return -EINVAL;
  307. return ret;
  308. }
  309. /* Name -> ID */
  310. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  311. __u32 *id, struct idmap *idmap)
  312. {
  313. char id_str[NFS_UINT_MAXLEN];
  314. long id_long;
  315. ssize_t data_size;
  316. int ret = 0;
  317. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  318. if (data_size <= 0) {
  319. ret = -EINVAL;
  320. } else {
  321. ret = kstrtol(id_str, 10, &id_long);
  322. *id = (__u32)id_long;
  323. }
  324. return ret;
  325. }
  326. /* idmap classic begins here */
  327. enum {
  328. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  329. };
  330. static const match_table_t nfs_idmap_tokens = {
  331. { Opt_find_uid, "uid:%s" },
  332. { Opt_find_gid, "gid:%s" },
  333. { Opt_find_user, "user:%s" },
  334. { Opt_find_group, "group:%s" },
  335. { Opt_find_err, NULL }
  336. };
  337. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  338. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  339. size_t);
  340. static void idmap_release_pipe(struct inode *);
  341. static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
  342. static const struct rpc_pipe_ops idmap_upcall_ops = {
  343. .upcall = rpc_pipe_generic_upcall,
  344. .downcall = idmap_pipe_downcall,
  345. .release_pipe = idmap_release_pipe,
  346. .destroy_msg = idmap_pipe_destroy_msg,
  347. };
  348. static struct key_type key_type_id_resolver_legacy = {
  349. .name = "id_legacy",
  350. .instantiate = user_instantiate,
  351. .match = user_match,
  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_unregister(struct rpc_pipe *pipe)
  359. {
  360. if (pipe->dentry)
  361. rpc_unlink(pipe->dentry);
  362. }
  363. static int __nfs_idmap_register(struct dentry *dir,
  364. struct idmap *idmap,
  365. struct rpc_pipe *pipe)
  366. {
  367. struct dentry *dentry;
  368. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  369. if (IS_ERR(dentry))
  370. return PTR_ERR(dentry);
  371. pipe->dentry = dentry;
  372. return 0;
  373. }
  374. static void nfs_idmap_unregister(struct nfs_client *clp,
  375. struct rpc_pipe *pipe)
  376. {
  377. struct net *net = clp->cl_net;
  378. struct super_block *pipefs_sb;
  379. pipefs_sb = rpc_get_sb_net(net);
  380. if (pipefs_sb) {
  381. __nfs_idmap_unregister(pipe);
  382. rpc_put_sb_net(net);
  383. }
  384. }
  385. static int nfs_idmap_register(struct nfs_client *clp,
  386. struct idmap *idmap,
  387. struct rpc_pipe *pipe)
  388. {
  389. struct net *net = clp->cl_net;
  390. struct super_block *pipefs_sb;
  391. int err = 0;
  392. pipefs_sb = rpc_get_sb_net(net);
  393. if (pipefs_sb) {
  394. if (clp->cl_rpcclient->cl_dentry)
  395. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  396. idmap, pipe);
  397. rpc_put_sb_net(net);
  398. }
  399. return err;
  400. }
  401. int
  402. nfs_idmap_new(struct nfs_client *clp)
  403. {
  404. struct idmap *idmap;
  405. struct rpc_pipe *pipe;
  406. int error;
  407. idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
  408. if (idmap == NULL)
  409. return -ENOMEM;
  410. pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
  411. if (IS_ERR(pipe)) {
  412. error = PTR_ERR(pipe);
  413. kfree(idmap);
  414. return error;
  415. }
  416. error = nfs_idmap_register(clp, idmap, pipe);
  417. if (error) {
  418. rpc_destroy_pipe_data(pipe);
  419. kfree(idmap);
  420. return error;
  421. }
  422. idmap->idmap_pipe = pipe;
  423. mutex_init(&idmap->idmap_mutex);
  424. clp->cl_idmap = idmap;
  425. return 0;
  426. }
  427. void
  428. nfs_idmap_delete(struct nfs_client *clp)
  429. {
  430. struct idmap *idmap = clp->cl_idmap;
  431. if (!idmap)
  432. return;
  433. nfs_idmap_unregister(clp, idmap->idmap_pipe);
  434. rpc_destroy_pipe_data(idmap->idmap_pipe);
  435. clp->cl_idmap = NULL;
  436. kfree(idmap);
  437. }
  438. static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
  439. struct super_block *sb)
  440. {
  441. int err = 0;
  442. switch (event) {
  443. case RPC_PIPEFS_MOUNT:
  444. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  445. clp->cl_idmap,
  446. clp->cl_idmap->idmap_pipe);
  447. break;
  448. case RPC_PIPEFS_UMOUNT:
  449. if (clp->cl_idmap->idmap_pipe) {
  450. struct dentry *parent;
  451. parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
  452. __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
  453. /*
  454. * Note: This is a dirty hack. SUNRPC hook has been
  455. * called already but simple_rmdir() call for the
  456. * directory returned with error because of idmap pipe
  457. * inside. Thus now we have to remove this directory
  458. * here.
  459. */
  460. if (rpc_rmdir(parent))
  461. printk(KERN_ERR "NFS: %s: failed to remove "
  462. "clnt dir!\n", __func__);
  463. }
  464. break;
  465. default:
  466. printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
  467. event);
  468. return -ENOTSUPP;
  469. }
  470. return err;
  471. }
  472. static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
  473. {
  474. struct nfs_net *nn = net_generic(net, nfs_net_id);
  475. struct dentry *cl_dentry;
  476. struct nfs_client *clp;
  477. int err;
  478. restart:
  479. spin_lock(&nn->nfs_client_lock);
  480. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  481. /* Wait for initialisation to finish */
  482. if (clp->cl_cons_state == NFS_CS_INITING) {
  483. atomic_inc(&clp->cl_count);
  484. spin_unlock(&nn->nfs_client_lock);
  485. err = nfs_wait_client_init_complete(clp);
  486. nfs_put_client(clp);
  487. if (err)
  488. return NULL;
  489. goto restart;
  490. }
  491. /* Skip nfs_clients that failed to initialise */
  492. if (clp->cl_cons_state < 0)
  493. continue;
  494. smp_rmb();
  495. if (clp->rpc_ops != &nfs_v4_clientops)
  496. continue;
  497. cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
  498. if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
  499. ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
  500. continue;
  501. atomic_inc(&clp->cl_count);
  502. spin_unlock(&nn->nfs_client_lock);
  503. return clp;
  504. }
  505. spin_unlock(&nn->nfs_client_lock);
  506. return NULL;
  507. }
  508. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  509. void *ptr)
  510. {
  511. struct super_block *sb = ptr;
  512. struct nfs_client *clp;
  513. int error = 0;
  514. if (!try_module_get(THIS_MODULE))
  515. return 0;
  516. while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
  517. error = __rpc_pipefs_event(clp, event, sb);
  518. nfs_put_client(clp);
  519. if (error)
  520. break;
  521. }
  522. module_put(THIS_MODULE);
  523. return error;
  524. }
  525. #define PIPEFS_NFS_PRIO 1
  526. static struct notifier_block nfs_idmap_block = {
  527. .notifier_call = rpc_pipefs_event,
  528. .priority = SUNRPC_PIPEFS_NFS_PRIO,
  529. };
  530. int nfs_idmap_init(void)
  531. {
  532. int ret;
  533. ret = nfs_idmap_init_keyring();
  534. if (ret != 0)
  535. goto out;
  536. ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
  537. if (ret != 0)
  538. nfs_idmap_quit_keyring();
  539. out:
  540. return ret;
  541. }
  542. void nfs_idmap_quit(void)
  543. {
  544. rpc_pipefs_notifier_unregister(&nfs_idmap_block);
  545. nfs_idmap_quit_keyring();
  546. }
  547. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  548. struct idmap_msg *im,
  549. struct rpc_pipe_msg *msg)
  550. {
  551. substring_t substr;
  552. int token, ret;
  553. im->im_type = IDMAP_TYPE_GROUP;
  554. token = match_token(desc, nfs_idmap_tokens, &substr);
  555. switch (token) {
  556. case Opt_find_uid:
  557. im->im_type = IDMAP_TYPE_USER;
  558. case Opt_find_gid:
  559. im->im_conv = IDMAP_CONV_NAMETOID;
  560. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  561. break;
  562. case Opt_find_user:
  563. im->im_type = IDMAP_TYPE_USER;
  564. case Opt_find_group:
  565. im->im_conv = IDMAP_CONV_IDTONAME;
  566. ret = match_int(&substr, &im->im_id);
  567. break;
  568. default:
  569. ret = -EINVAL;
  570. goto out;
  571. }
  572. msg->data = im;
  573. msg->len = sizeof(struct idmap_msg);
  574. out:
  575. return ret;
  576. }
  577. static bool
  578. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  579. struct idmap_legacy_upcalldata *data)
  580. {
  581. if (idmap->idmap_upcall_data != NULL) {
  582. WARN_ON_ONCE(1);
  583. return false;
  584. }
  585. idmap->idmap_upcall_data = data;
  586. return true;
  587. }
  588. static void
  589. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  590. {
  591. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  592. kfree(idmap->idmap_upcall_data);
  593. idmap->idmap_upcall_data = NULL;
  594. complete_request_key(cons, ret);
  595. }
  596. static void
  597. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  598. {
  599. if (idmap->idmap_upcall_data != NULL)
  600. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  601. }
  602. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  603. const char *op,
  604. void *aux)
  605. {
  606. struct idmap_legacy_upcalldata *data;
  607. struct rpc_pipe_msg *msg;
  608. struct idmap_msg *im;
  609. struct idmap *idmap = (struct idmap *)aux;
  610. struct key *key = cons->key;
  611. int ret = -ENOMEM;
  612. /* msg and im are freed in idmap_pipe_destroy_msg */
  613. data = kzalloc(sizeof(*data), GFP_KERNEL);
  614. if (!data)
  615. goto out1;
  616. msg = &data->pipe_msg;
  617. im = &data->idmap_msg;
  618. data->idmap = idmap;
  619. data->key_cons = cons;
  620. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  621. if (ret < 0)
  622. goto out2;
  623. ret = -EAGAIN;
  624. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  625. goto out2;
  626. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  627. if (ret < 0)
  628. nfs_idmap_abort_pipe_upcall(idmap, ret);
  629. return ret;
  630. out2:
  631. kfree(data);
  632. out1:
  633. complete_request_key(cons, ret);
  634. return ret;
  635. }
  636. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
  637. {
  638. return key_instantiate_and_link(key, data, strlen(data) + 1,
  639. id_resolver_cache->thread_keyring,
  640. authkey);
  641. }
  642. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  643. struct idmap_msg *upcall,
  644. struct key *key, struct key *authkey)
  645. {
  646. char id_str[NFS_UINT_MAXLEN];
  647. int ret = -ENOKEY;
  648. /* ret = -ENOKEY */
  649. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  650. goto out;
  651. switch (im->im_conv) {
  652. case IDMAP_CONV_NAMETOID:
  653. if (strcmp(upcall->im_name, im->im_name) != 0)
  654. break;
  655. sprintf(id_str, "%d", im->im_id);
  656. ret = nfs_idmap_instantiate(key, authkey, id_str);
  657. break;
  658. case IDMAP_CONV_IDTONAME:
  659. if (upcall->im_id != im->im_id)
  660. break;
  661. ret = nfs_idmap_instantiate(key, authkey, im->im_name);
  662. break;
  663. default:
  664. ret = -EINVAL;
  665. }
  666. out:
  667. return ret;
  668. }
  669. static ssize_t
  670. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  671. {
  672. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  673. struct idmap *idmap = (struct idmap *)rpci->private;
  674. struct key_construction *cons;
  675. struct idmap_msg im;
  676. size_t namelen_in;
  677. int ret = -ENOKEY;
  678. /* If instantiation is successful, anyone waiting for key construction
  679. * will have been woken up and someone else may now have used
  680. * idmap_key_cons - so after this point we may no longer touch it.
  681. */
  682. if (idmap->idmap_upcall_data == NULL)
  683. goto out_noupcall;
  684. cons = idmap->idmap_upcall_data->key_cons;
  685. if (mlen != sizeof(im)) {
  686. ret = -ENOSPC;
  687. goto out;
  688. }
  689. if (copy_from_user(&im, src, mlen) != 0) {
  690. ret = -EFAULT;
  691. goto out;
  692. }
  693. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  694. ret = -ENOKEY;
  695. goto out;
  696. }
  697. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  698. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  699. ret = -EINVAL;
  700. goto out;
  701. }
  702. ret = nfs_idmap_read_and_verify_message(&im,
  703. &idmap->idmap_upcall_data->idmap_msg,
  704. cons->key, cons->authkey);
  705. if (ret >= 0) {
  706. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  707. ret = mlen;
  708. }
  709. out:
  710. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  711. out_noupcall:
  712. return ret;
  713. }
  714. static void
  715. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  716. {
  717. struct idmap_legacy_upcalldata *data = container_of(msg,
  718. struct idmap_legacy_upcalldata,
  719. pipe_msg);
  720. struct idmap *idmap = data->idmap;
  721. if (msg->errno)
  722. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  723. }
  724. static void
  725. idmap_release_pipe(struct inode *inode)
  726. {
  727. struct rpc_inode *rpci = RPC_I(inode);
  728. struct idmap *idmap = (struct idmap *)rpci->private;
  729. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  730. }
  731. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
  732. {
  733. struct idmap *idmap = server->nfs_client->cl_idmap;
  734. if (nfs_map_string_to_numeric(name, namelen, uid))
  735. return 0;
  736. return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
  737. }
  738. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
  739. {
  740. struct idmap *idmap = server->nfs_client->cl_idmap;
  741. if (nfs_map_string_to_numeric(name, namelen, gid))
  742. return 0;
  743. return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
  744. }
  745. int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
  746. {
  747. struct idmap *idmap = server->nfs_client->cl_idmap;
  748. int ret = -EINVAL;
  749. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  750. ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
  751. if (ret < 0)
  752. ret = nfs_map_numeric_to_string(uid, buf, buflen);
  753. return ret;
  754. }
  755. int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
  756. {
  757. struct idmap *idmap = server->nfs_client->cl_idmap;
  758. int ret = -EINVAL;
  759. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  760. ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
  761. if (ret < 0)
  762. ret = nfs_map_numeric_to_string(gid, buf, buflen);
  763. return ret;
  764. }