idmap.c 22 KB

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