idmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * fs/nfs/idmap.c
  3. *
  4. * UID and GID to name mapping for clients.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Marius Aamodt Eriksen <marius@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/types.h>
  37. #include <linux/parser.h>
  38. #include <linux/fs.h>
  39. #include <linux/nfs_idmap.h>
  40. #include <net/net_namespace.h>
  41. #include <linux/sunrpc/rpc_pipe_fs.h>
  42. #include <linux/nfs_fs.h>
  43. #include <linux/nfs_fs_sb.h>
  44. #include <linux/key.h>
  45. #include <linux/keyctl.h>
  46. #include <linux/key-type.h>
  47. #include <keys/user-type.h>
  48. #include <linux/module.h>
  49. #include "internal.h"
  50. #include "netns.h"
  51. #include "nfs4trace.h"
  52. #define NFS_UINT_MAXLEN 11
  53. static const struct cred *id_resolver_cache;
  54. static struct key_type key_type_id_resolver_legacy;
  55. struct idmap_legacy_upcalldata {
  56. struct rpc_pipe_msg pipe_msg;
  57. struct idmap_msg idmap_msg;
  58. struct key_construction *key_cons;
  59. struct idmap *idmap;
  60. };
  61. struct idmap {
  62. struct rpc_pipe *idmap_pipe;
  63. struct idmap_legacy_upcalldata *idmap_upcall_data;
  64. struct mutex idmap_mutex;
  65. };
  66. /**
  67. * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
  68. * @fattr: fully initialised struct nfs_fattr
  69. * @owner_name: owner name string cache
  70. * @group_name: group name string cache
  71. */
  72. void nfs_fattr_init_names(struct nfs_fattr *fattr,
  73. struct nfs4_string *owner_name,
  74. struct nfs4_string *group_name)
  75. {
  76. fattr->owner_name = owner_name;
  77. fattr->group_name = group_name;
  78. }
  79. static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
  80. {
  81. fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
  82. kfree(fattr->owner_name->data);
  83. }
  84. static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
  85. {
  86. fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
  87. kfree(fattr->group_name->data);
  88. }
  89. static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
  90. {
  91. struct nfs4_string *owner = fattr->owner_name;
  92. kuid_t uid;
  93. if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
  94. return false;
  95. if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
  96. fattr->uid = uid;
  97. fattr->valid |= NFS_ATTR_FATTR_OWNER;
  98. }
  99. return true;
  100. }
  101. static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
  102. {
  103. struct nfs4_string *group = fattr->group_name;
  104. kgid_t gid;
  105. if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
  106. return false;
  107. if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
  108. fattr->gid = gid;
  109. fattr->valid |= NFS_ATTR_FATTR_GROUP;
  110. }
  111. return true;
  112. }
  113. /**
  114. * nfs_fattr_free_names - free up the NFSv4 owner and group strings
  115. * @fattr: a fully initialised nfs_fattr structure
  116. */
  117. void nfs_fattr_free_names(struct nfs_fattr *fattr)
  118. {
  119. if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
  120. nfs_fattr_free_owner_name(fattr);
  121. if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
  122. nfs_fattr_free_group_name(fattr);
  123. }
  124. /**
  125. * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
  126. * @server: pointer to the filesystem nfs_server structure
  127. * @fattr: a fully initialised nfs_fattr structure
  128. *
  129. * This helper maps the cached NFSv4 owner/group strings in fattr into
  130. * their numeric uid/gid equivalents, and then frees the cached strings.
  131. */
  132. void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
  133. {
  134. if (nfs_fattr_map_owner_name(server, fattr))
  135. nfs_fattr_free_owner_name(fattr);
  136. if (nfs_fattr_map_group_name(server, fattr))
  137. nfs_fattr_free_group_name(fattr);
  138. }
  139. static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
  140. {
  141. unsigned long val;
  142. char buf[16];
  143. if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
  144. return 0;
  145. memcpy(buf, name, namelen);
  146. buf[namelen] = '\0';
  147. if (kstrtoul(buf, 0, &val) != 0)
  148. return 0;
  149. *res = val;
  150. return 1;
  151. }
  152. static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
  153. {
  154. return snprintf(buf, buflen, "%u", id);
  155. }
  156. static struct key_type key_type_id_resolver = {
  157. .name = "id_resolver",
  158. .instantiate = user_instantiate,
  159. .match = user_match,
  160. .revoke = user_revoke,
  161. .destroy = user_destroy,
  162. .describe = user_describe,
  163. .read = user_read,
  164. };
  165. static int nfs_idmap_init_keyring(void)
  166. {
  167. struct cred *cred;
  168. struct key *keyring;
  169. int ret = 0;
  170. printk(KERN_NOTICE "NFS: Registering the %s key type\n",
  171. key_type_id_resolver.name);
  172. cred = prepare_kernel_cred(NULL);
  173. if (!cred)
  174. return -ENOMEM;
  175. keyring = keyring_alloc(".id_resolver",
  176. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  177. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  178. KEY_USR_VIEW | KEY_USR_READ,
  179. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  180. if (IS_ERR(keyring)) {
  181. ret = PTR_ERR(keyring);
  182. goto failed_put_cred;
  183. }
  184. ret = register_key_type(&key_type_id_resolver);
  185. if (ret < 0)
  186. goto failed_put_key;
  187. ret = register_key_type(&key_type_id_resolver_legacy);
  188. if (ret < 0)
  189. goto failed_reg_legacy;
  190. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  191. cred->thread_keyring = keyring;
  192. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  193. id_resolver_cache = cred;
  194. return 0;
  195. failed_reg_legacy:
  196. unregister_key_type(&key_type_id_resolver);
  197. failed_put_key:
  198. key_put(keyring);
  199. failed_put_cred:
  200. put_cred(cred);
  201. return ret;
  202. }
  203. static void nfs_idmap_quit_keyring(void)
  204. {
  205. key_revoke(id_resolver_cache->thread_keyring);
  206. unregister_key_type(&key_type_id_resolver);
  207. unregister_key_type(&key_type_id_resolver_legacy);
  208. put_cred(id_resolver_cache);
  209. }
  210. /*
  211. * Assemble the description to pass to request_key()
  212. * This function will allocate a new string and update dest to point
  213. * at it. The caller is responsible for freeing dest.
  214. *
  215. * On error 0 is returned. Otherwise, the length of dest is returned.
  216. */
  217. static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
  218. const char *type, size_t typelen, char **desc)
  219. {
  220. char *cp;
  221. size_t desclen = typelen + namelen + 2;
  222. *desc = kmalloc(desclen, GFP_KERNEL);
  223. if (!*desc)
  224. return -ENOMEM;
  225. cp = *desc;
  226. memcpy(cp, type, typelen);
  227. cp += typelen;
  228. *cp++ = ':';
  229. memcpy(cp, name, namelen);
  230. cp += namelen;
  231. *cp = '\0';
  232. return desclen;
  233. }
  234. static struct key *nfs_idmap_request_key(const char *name, size_t namelen,
  235. const char *type, struct idmap *idmap)
  236. {
  237. char *desc;
  238. struct key *rkey;
  239. ssize_t ret;
  240. ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
  241. if (ret <= 0)
  242. return ERR_PTR(ret);
  243. rkey = request_key(&key_type_id_resolver, desc, "");
  244. if (IS_ERR(rkey)) {
  245. mutex_lock(&idmap->idmap_mutex);
  246. rkey = request_key_with_auxdata(&key_type_id_resolver_legacy,
  247. desc, "", 0, idmap);
  248. mutex_unlock(&idmap->idmap_mutex);
  249. }
  250. kfree(desc);
  251. return rkey;
  252. }
  253. static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
  254. const char *type, void *data,
  255. size_t data_size, struct idmap *idmap)
  256. {
  257. const struct cred *saved_cred;
  258. struct key *rkey;
  259. struct user_key_payload *payload;
  260. ssize_t ret;
  261. saved_cred = override_creds(id_resolver_cache);
  262. rkey = nfs_idmap_request_key(name, namelen, type, idmap);
  263. revert_creds(saved_cred);
  264. if (IS_ERR(rkey)) {
  265. ret = PTR_ERR(rkey);
  266. goto out;
  267. }
  268. rcu_read_lock();
  269. rkey->perm |= KEY_USR_VIEW;
  270. ret = key_validate(rkey);
  271. if (ret < 0)
  272. goto out_up;
  273. payload = rcu_dereference(rkey->payload.rcudata);
  274. if (IS_ERR_OR_NULL(payload)) {
  275. ret = PTR_ERR(payload);
  276. goto out_up;
  277. }
  278. ret = payload->datalen;
  279. if (ret > 0 && ret <= data_size)
  280. memcpy(data, payload->data, ret);
  281. else
  282. ret = -EINVAL;
  283. out_up:
  284. rcu_read_unlock();
  285. key_put(rkey);
  286. out:
  287. return ret;
  288. }
  289. /* ID -> Name */
  290. static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
  291. size_t buflen, struct idmap *idmap)
  292. {
  293. char id_str[NFS_UINT_MAXLEN];
  294. int id_len;
  295. ssize_t ret;
  296. id_len = snprintf(id_str, sizeof(id_str), "%u", id);
  297. ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
  298. if (ret < 0)
  299. return -EINVAL;
  300. return ret;
  301. }
  302. /* Name -> ID */
  303. static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
  304. __u32 *id, struct idmap *idmap)
  305. {
  306. char id_str[NFS_UINT_MAXLEN];
  307. long id_long;
  308. ssize_t data_size;
  309. int ret = 0;
  310. data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
  311. if (data_size <= 0) {
  312. ret = -EINVAL;
  313. } else {
  314. ret = kstrtol(id_str, 10, &id_long);
  315. *id = (__u32)id_long;
  316. }
  317. return ret;
  318. }
  319. /* idmap classic begins here */
  320. enum {
  321. Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
  322. };
  323. static const match_table_t nfs_idmap_tokens = {
  324. { Opt_find_uid, "uid:%s" },
  325. { Opt_find_gid, "gid:%s" },
  326. { Opt_find_user, "user:%s" },
  327. { Opt_find_group, "group:%s" },
  328. { Opt_find_err, NULL }
  329. };
  330. static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
  331. static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
  332. size_t);
  333. static void idmap_release_pipe(struct inode *);
  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. .release_pipe = idmap_release_pipe,
  339. .destroy_msg = idmap_pipe_destroy_msg,
  340. };
  341. static struct key_type key_type_id_resolver_legacy = {
  342. .name = "id_legacy",
  343. .instantiate = user_instantiate,
  344. .match = user_match,
  345. .revoke = user_revoke,
  346. .destroy = user_destroy,
  347. .describe = user_describe,
  348. .read = user_read,
  349. .request_key = nfs_idmap_legacy_upcall,
  350. };
  351. static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
  352. {
  353. if (pipe->dentry)
  354. rpc_unlink(pipe->dentry);
  355. }
  356. static int __nfs_idmap_register(struct dentry *dir,
  357. struct idmap *idmap,
  358. struct rpc_pipe *pipe)
  359. {
  360. struct dentry *dentry;
  361. dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
  362. if (IS_ERR(dentry))
  363. return PTR_ERR(dentry);
  364. pipe->dentry = dentry;
  365. return 0;
  366. }
  367. static void nfs_idmap_unregister(struct nfs_client *clp,
  368. struct rpc_pipe *pipe)
  369. {
  370. struct net *net = clp->cl_net;
  371. struct super_block *pipefs_sb;
  372. pipefs_sb = rpc_get_sb_net(net);
  373. if (pipefs_sb) {
  374. __nfs_idmap_unregister(pipe);
  375. rpc_put_sb_net(net);
  376. }
  377. }
  378. static int nfs_idmap_register(struct nfs_client *clp,
  379. struct idmap *idmap,
  380. struct rpc_pipe *pipe)
  381. {
  382. struct net *net = clp->cl_net;
  383. struct super_block *pipefs_sb;
  384. int err = 0;
  385. pipefs_sb = rpc_get_sb_net(net);
  386. if (pipefs_sb) {
  387. if (clp->cl_rpcclient->cl_dentry)
  388. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  389. idmap, pipe);
  390. rpc_put_sb_net(net);
  391. }
  392. return err;
  393. }
  394. int
  395. nfs_idmap_new(struct nfs_client *clp)
  396. {
  397. struct idmap *idmap;
  398. struct rpc_pipe *pipe;
  399. int error;
  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. err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
  438. clp->cl_idmap,
  439. clp->cl_idmap->idmap_pipe);
  440. break;
  441. case RPC_PIPEFS_UMOUNT:
  442. if (clp->cl_idmap->idmap_pipe) {
  443. struct dentry *parent;
  444. parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
  445. __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
  446. /*
  447. * Note: This is a dirty hack. SUNRPC hook has been
  448. * called already but simple_rmdir() call for the
  449. * directory returned with error because of idmap pipe
  450. * inside. Thus now we have to remove this directory
  451. * here.
  452. */
  453. if (rpc_rmdir(parent))
  454. printk(KERN_ERR "NFS: %s: failed to remove "
  455. "clnt dir!\n", __func__);
  456. }
  457. break;
  458. default:
  459. printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
  460. event);
  461. return -ENOTSUPP;
  462. }
  463. return err;
  464. }
  465. static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
  466. {
  467. struct nfs_net *nn = net_generic(net, nfs_net_id);
  468. struct dentry *cl_dentry;
  469. struct nfs_client *clp;
  470. int err;
  471. restart:
  472. spin_lock(&nn->nfs_client_lock);
  473. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  474. /* Wait for initialisation to finish */
  475. if (clp->cl_cons_state == NFS_CS_INITING) {
  476. atomic_inc(&clp->cl_count);
  477. spin_unlock(&nn->nfs_client_lock);
  478. err = nfs_wait_client_init_complete(clp);
  479. nfs_put_client(clp);
  480. if (err)
  481. return NULL;
  482. goto restart;
  483. }
  484. /* Skip nfs_clients that failed to initialise */
  485. if (clp->cl_cons_state < 0)
  486. continue;
  487. smp_rmb();
  488. if (clp->rpc_ops != &nfs_v4_clientops)
  489. continue;
  490. cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
  491. if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
  492. ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
  493. continue;
  494. atomic_inc(&clp->cl_count);
  495. spin_unlock(&nn->nfs_client_lock);
  496. return clp;
  497. }
  498. spin_unlock(&nn->nfs_client_lock);
  499. return NULL;
  500. }
  501. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  502. void *ptr)
  503. {
  504. struct super_block *sb = ptr;
  505. struct nfs_client *clp;
  506. int error = 0;
  507. if (!try_module_get(THIS_MODULE))
  508. return 0;
  509. while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
  510. error = __rpc_pipefs_event(clp, event, sb);
  511. nfs_put_client(clp);
  512. if (error)
  513. break;
  514. }
  515. module_put(THIS_MODULE);
  516. return error;
  517. }
  518. #define PIPEFS_NFS_PRIO 1
  519. static struct notifier_block nfs_idmap_block = {
  520. .notifier_call = rpc_pipefs_event,
  521. .priority = SUNRPC_PIPEFS_NFS_PRIO,
  522. };
  523. int nfs_idmap_init(void)
  524. {
  525. int ret;
  526. ret = nfs_idmap_init_keyring();
  527. if (ret != 0)
  528. goto out;
  529. ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
  530. if (ret != 0)
  531. nfs_idmap_quit_keyring();
  532. out:
  533. return ret;
  534. }
  535. void nfs_idmap_quit(void)
  536. {
  537. rpc_pipefs_notifier_unregister(&nfs_idmap_block);
  538. nfs_idmap_quit_keyring();
  539. }
  540. static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
  541. struct idmap_msg *im,
  542. struct rpc_pipe_msg *msg)
  543. {
  544. substring_t substr;
  545. int token, ret;
  546. im->im_type = IDMAP_TYPE_GROUP;
  547. token = match_token(desc, nfs_idmap_tokens, &substr);
  548. switch (token) {
  549. case Opt_find_uid:
  550. im->im_type = IDMAP_TYPE_USER;
  551. case Opt_find_gid:
  552. im->im_conv = IDMAP_CONV_NAMETOID;
  553. ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
  554. break;
  555. case Opt_find_user:
  556. im->im_type = IDMAP_TYPE_USER;
  557. case Opt_find_group:
  558. im->im_conv = IDMAP_CONV_IDTONAME;
  559. ret = match_int(&substr, &im->im_id);
  560. break;
  561. default:
  562. ret = -EINVAL;
  563. goto out;
  564. }
  565. msg->data = im;
  566. msg->len = sizeof(struct idmap_msg);
  567. out:
  568. return ret;
  569. }
  570. static bool
  571. nfs_idmap_prepare_pipe_upcall(struct idmap *idmap,
  572. struct idmap_legacy_upcalldata *data)
  573. {
  574. if (idmap->idmap_upcall_data != NULL) {
  575. WARN_ON_ONCE(1);
  576. return false;
  577. }
  578. idmap->idmap_upcall_data = data;
  579. return true;
  580. }
  581. static void
  582. nfs_idmap_complete_pipe_upcall_locked(struct idmap *idmap, int ret)
  583. {
  584. struct key_construction *cons = idmap->idmap_upcall_data->key_cons;
  585. kfree(idmap->idmap_upcall_data);
  586. idmap->idmap_upcall_data = NULL;
  587. complete_request_key(cons, ret);
  588. }
  589. static void
  590. nfs_idmap_abort_pipe_upcall(struct idmap *idmap, int ret)
  591. {
  592. if (idmap->idmap_upcall_data != NULL)
  593. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  594. }
  595. static int nfs_idmap_legacy_upcall(struct key_construction *cons,
  596. const char *op,
  597. void *aux)
  598. {
  599. struct idmap_legacy_upcalldata *data;
  600. struct rpc_pipe_msg *msg;
  601. struct idmap_msg *im;
  602. struct idmap *idmap = (struct idmap *)aux;
  603. struct key *key = cons->key;
  604. int ret = -ENOMEM;
  605. /* msg and im are freed in idmap_pipe_destroy_msg */
  606. data = kzalloc(sizeof(*data), GFP_KERNEL);
  607. if (!data)
  608. goto out1;
  609. msg = &data->pipe_msg;
  610. im = &data->idmap_msg;
  611. data->idmap = idmap;
  612. data->key_cons = cons;
  613. ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
  614. if (ret < 0)
  615. goto out2;
  616. ret = -EAGAIN;
  617. if (!nfs_idmap_prepare_pipe_upcall(idmap, data))
  618. goto out2;
  619. ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
  620. if (ret < 0)
  621. nfs_idmap_abort_pipe_upcall(idmap, ret);
  622. return ret;
  623. out2:
  624. kfree(data);
  625. out1:
  626. complete_request_key(cons, ret);
  627. return ret;
  628. }
  629. static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data, size_t datalen)
  630. {
  631. return key_instantiate_and_link(key, data, datalen,
  632. id_resolver_cache->thread_keyring,
  633. authkey);
  634. }
  635. static int nfs_idmap_read_and_verify_message(struct idmap_msg *im,
  636. struct idmap_msg *upcall,
  637. struct key *key, struct key *authkey)
  638. {
  639. char id_str[NFS_UINT_MAXLEN];
  640. size_t len;
  641. int ret = -ENOKEY;
  642. /* ret = -ENOKEY */
  643. if (upcall->im_type != im->im_type || upcall->im_conv != im->im_conv)
  644. goto out;
  645. switch (im->im_conv) {
  646. case IDMAP_CONV_NAMETOID:
  647. if (strcmp(upcall->im_name, im->im_name) != 0)
  648. break;
  649. /* Note: here we store the NUL terminator too */
  650. len = sprintf(id_str, "%d", im->im_id) + 1;
  651. ret = nfs_idmap_instantiate(key, authkey, id_str, len);
  652. break;
  653. case IDMAP_CONV_IDTONAME:
  654. if (upcall->im_id != im->im_id)
  655. break;
  656. len = strlen(im->im_name);
  657. ret = nfs_idmap_instantiate(key, authkey, im->im_name, len);
  658. break;
  659. default:
  660. ret = -EINVAL;
  661. }
  662. out:
  663. return ret;
  664. }
  665. static ssize_t
  666. idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  667. {
  668. struct rpc_inode *rpci = RPC_I(file_inode(filp));
  669. struct idmap *idmap = (struct idmap *)rpci->private;
  670. struct key_construction *cons;
  671. struct idmap_msg im;
  672. size_t namelen_in;
  673. int ret = -ENOKEY;
  674. /* If instantiation is successful, anyone waiting for key construction
  675. * will have been woken up and someone else may now have used
  676. * idmap_key_cons - so after this point we may no longer touch it.
  677. */
  678. if (idmap->idmap_upcall_data == NULL)
  679. goto out_noupcall;
  680. cons = idmap->idmap_upcall_data->key_cons;
  681. if (mlen != sizeof(im)) {
  682. ret = -ENOSPC;
  683. goto out;
  684. }
  685. if (copy_from_user(&im, src, mlen) != 0) {
  686. ret = -EFAULT;
  687. goto out;
  688. }
  689. if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
  690. ret = -ENOKEY;
  691. goto out;
  692. }
  693. namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
  694. if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
  695. ret = -EINVAL;
  696. goto out;
  697. }
  698. ret = nfs_idmap_read_and_verify_message(&im,
  699. &idmap->idmap_upcall_data->idmap_msg,
  700. cons->key, cons->authkey);
  701. if (ret >= 0) {
  702. key_set_timeout(cons->key, nfs_idmap_cache_timeout);
  703. ret = mlen;
  704. }
  705. out:
  706. nfs_idmap_complete_pipe_upcall_locked(idmap, ret);
  707. out_noupcall:
  708. return ret;
  709. }
  710. static void
  711. idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  712. {
  713. struct idmap_legacy_upcalldata *data = container_of(msg,
  714. struct idmap_legacy_upcalldata,
  715. pipe_msg);
  716. struct idmap *idmap = data->idmap;
  717. if (msg->errno)
  718. nfs_idmap_abort_pipe_upcall(idmap, msg->errno);
  719. }
  720. static void
  721. idmap_release_pipe(struct inode *inode)
  722. {
  723. struct rpc_inode *rpci = RPC_I(inode);
  724. struct idmap *idmap = (struct idmap *)rpci->private;
  725. nfs_idmap_abort_pipe_upcall(idmap, -EPIPE);
  726. }
  727. int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, kuid_t *uid)
  728. {
  729. struct idmap *idmap = server->nfs_client->cl_idmap;
  730. __u32 id = -1;
  731. int ret = 0;
  732. if (!nfs_map_string_to_numeric(name, namelen, &id))
  733. ret = nfs_idmap_lookup_id(name, namelen, "uid", &id, idmap);
  734. if (ret == 0) {
  735. *uid = make_kuid(&init_user_ns, id);
  736. if (!uid_valid(*uid))
  737. ret = -ERANGE;
  738. }
  739. trace_nfs4_map_name_to_uid(name, namelen, id, ret);
  740. return ret;
  741. }
  742. int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, kgid_t *gid)
  743. {
  744. struct idmap *idmap = server->nfs_client->cl_idmap;
  745. __u32 id = -1;
  746. int ret = 0;
  747. if (!nfs_map_string_to_numeric(name, namelen, &id))
  748. ret = nfs_idmap_lookup_id(name, namelen, "gid", &id, idmap);
  749. if (ret == 0) {
  750. *gid = make_kgid(&init_user_ns, id);
  751. if (!gid_valid(*gid))
  752. ret = -ERANGE;
  753. }
  754. trace_nfs4_map_group_to_gid(name, namelen, id, ret);
  755. return ret;
  756. }
  757. int nfs_map_uid_to_name(const struct nfs_server *server, kuid_t uid, char *buf, size_t buflen)
  758. {
  759. struct idmap *idmap = server->nfs_client->cl_idmap;
  760. int ret = -EINVAL;
  761. __u32 id;
  762. id = from_kuid(&init_user_ns, uid);
  763. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  764. ret = nfs_idmap_lookup_name(id, "user", buf, buflen, idmap);
  765. if (ret < 0)
  766. ret = nfs_map_numeric_to_string(id, buf, buflen);
  767. trace_nfs4_map_uid_to_name(buf, ret, id, ret);
  768. return ret;
  769. }
  770. int nfs_map_gid_to_group(const struct nfs_server *server, kgid_t gid, char *buf, size_t buflen)
  771. {
  772. struct idmap *idmap = server->nfs_client->cl_idmap;
  773. int ret = -EINVAL;
  774. __u32 id;
  775. id = from_kgid(&init_user_ns, gid);
  776. if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
  777. ret = nfs_idmap_lookup_name(id, "group", buf, buflen, idmap);
  778. if (ret < 0)
  779. ret = nfs_map_numeric_to_string(id, buf, buflen);
  780. trace_nfs4_map_gid_to_group(buf, ret, id, ret);
  781. return ret;
  782. }