idmap.c 22 KB

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