nfs4namespace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/nfs4namespace.c
  4. *
  5. * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
  6. * - Modified by David Howells <dhowells@redhat.com>
  7. *
  8. * NFSv4 namespace
  9. */
  10. #include <linux/module.h>
  11. #include <linux/dcache.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/nfs_fs.h>
  15. #include <linux/nfs_mount.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/sunrpc/clnt.h>
  19. #include <linux/sunrpc/addr.h>
  20. #include <linux/vfs.h>
  21. #include <linux/inet.h>
  22. #include "internal.h"
  23. #include "nfs4_fs.h"
  24. #include "nfs.h"
  25. #include "dns_resolve.h"
  26. #define NFSDBG_FACILITY NFSDBG_VFS
  27. /*
  28. * Work out the length that an NFSv4 path would render to as a standard posix
  29. * path, with a leading slash but no terminating slash.
  30. */
  31. static ssize_t nfs4_pathname_len(const struct nfs4_pathname *pathname)
  32. {
  33. ssize_t len = 0;
  34. int i;
  35. for (i = 0; i < pathname->ncomponents; i++) {
  36. const struct nfs4_string *component = &pathname->components[i];
  37. if (component->len > NAME_MAX)
  38. goto too_long;
  39. len += 1 + component->len; /* Adding "/foo" */
  40. if (len > PATH_MAX)
  41. goto too_long;
  42. }
  43. return len;
  44. too_long:
  45. return -ENAMETOOLONG;
  46. }
  47. /*
  48. * Convert the NFSv4 pathname components into a standard posix path.
  49. */
  50. static char *nfs4_pathname_string(const struct nfs4_pathname *pathname,
  51. unsigned short *_len)
  52. {
  53. ssize_t len;
  54. char *buf, *p;
  55. int i;
  56. len = nfs4_pathname_len(pathname);
  57. if (len < 0)
  58. return ERR_PTR(len);
  59. *_len = len;
  60. p = buf = kmalloc(len + 1, GFP_KERNEL);
  61. if (!buf)
  62. return ERR_PTR(-ENOMEM);
  63. for (i = 0; i < pathname->ncomponents; i++) {
  64. const struct nfs4_string *component = &pathname->components[i];
  65. *p++ = '/';
  66. memcpy(p, component->data, component->len);
  67. p += component->len;
  68. }
  69. *p = 0;
  70. return buf;
  71. }
  72. /*
  73. * return the path component of "<server>:<path>"
  74. * nfspath - the "<server>:<path>" string
  75. * end - one past the last char that could contain "<server>:"
  76. * returns NULL on failure
  77. */
  78. static char *nfs_path_component(const char *nfspath, const char *end)
  79. {
  80. char *p;
  81. if (*nfspath == '[') {
  82. /* parse [] escaped IPv6 addrs */
  83. p = strchr(nfspath, ']');
  84. if (p != NULL && ++p < end && *p == ':')
  85. return p + 1;
  86. } else {
  87. /* otherwise split on first colon */
  88. p = strchr(nfspath, ':');
  89. if (p != NULL && p < end)
  90. return p + 1;
  91. }
  92. return NULL;
  93. }
  94. /*
  95. * Determine the mount path as a string
  96. */
  97. static char *nfs4_path(struct dentry *dentry, char *buffer, ssize_t buflen)
  98. {
  99. char *limit;
  100. char *path = nfs_path(&limit, dentry, buffer, buflen,
  101. NFS_PATH_CANONICAL);
  102. if (!IS_ERR(path)) {
  103. char *path_component = nfs_path_component(path, limit);
  104. if (path_component)
  105. return path_component;
  106. }
  107. return path;
  108. }
  109. /*
  110. * Check that fs_locations::fs_root [RFC3530 6.3] is a prefix for what we
  111. * believe to be the server path to this dentry
  112. */
  113. static int nfs4_validate_fspath(struct dentry *dentry,
  114. const struct nfs4_fs_locations *locations,
  115. struct nfs_fs_context *ctx)
  116. {
  117. const char *path;
  118. char *fs_path;
  119. unsigned short len;
  120. char *buf;
  121. int n;
  122. buf = kmalloc(4096, GFP_KERNEL);
  123. if (!buf)
  124. return -ENOMEM;
  125. path = nfs4_path(dentry, buf, 4096);
  126. if (IS_ERR(path)) {
  127. kfree(buf);
  128. return PTR_ERR(path);
  129. }
  130. fs_path = nfs4_pathname_string(&locations->fs_path, &len);
  131. if (IS_ERR(fs_path)) {
  132. kfree(buf);
  133. return PTR_ERR(fs_path);
  134. }
  135. n = strncmp(path, fs_path, len);
  136. kfree(buf);
  137. kfree(fs_path);
  138. if (n != 0) {
  139. dprintk("%s: path %s does not begin with fsroot %s\n",
  140. __func__, path, ctx->nfs_server.export_path);
  141. return -ENOENT;
  142. }
  143. return 0;
  144. }
  145. size_t nfs_parse_server_name(char *string, size_t len, struct sockaddr *sa,
  146. size_t salen, struct net *net, int port)
  147. {
  148. ssize_t ret;
  149. ret = rpc_pton(net, string, len, sa, salen);
  150. if (ret == 0) {
  151. ret = rpc_uaddr2sockaddr(net, string, len, sa, salen);
  152. if (ret == 0) {
  153. ret = nfs_dns_resolve_name(net, string, len, sa, salen);
  154. if (ret < 0)
  155. ret = 0;
  156. }
  157. } else if (port) {
  158. rpc_set_port(sa, port);
  159. }
  160. return ret;
  161. }
  162. /**
  163. * nfs_find_best_sec - Find a security mechanism supported locally
  164. * @clnt: pointer to rpc_clnt
  165. * @server: NFS server struct
  166. * @flavors: List of security tuples returned by SECINFO procedure
  167. *
  168. * Return an rpc client that uses the first security mechanism in
  169. * "flavors" that is locally supported. The "flavors" array
  170. * is searched in the order returned from the server, per RFC 3530
  171. * recommendation and each flavor is checked for membership in the
  172. * sec= mount option list if it exists.
  173. *
  174. * Return -EPERM if no matching flavor is found in the array.
  175. *
  176. * Please call rpc_shutdown_client() when you are done with this rpc client.
  177. *
  178. */
  179. static struct rpc_clnt *nfs_find_best_sec(struct rpc_clnt *clnt,
  180. struct nfs_server *server,
  181. struct nfs4_secinfo_flavors *flavors)
  182. {
  183. rpc_authflavor_t pflavor;
  184. struct nfs4_secinfo4 *secinfo;
  185. unsigned int i;
  186. for (i = 0; i < flavors->num_flavors; i++) {
  187. secinfo = &flavors->flavors[i];
  188. switch (secinfo->flavor) {
  189. case RPC_AUTH_NULL:
  190. case RPC_AUTH_UNIX:
  191. case RPC_AUTH_GSS:
  192. pflavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  193. &secinfo->flavor_info);
  194. /* does the pseudoflavor match a sec= mount opt? */
  195. if (pflavor != RPC_AUTH_MAXFLAVOR &&
  196. nfs_auth_info_match(&server->auth_info, pflavor)) {
  197. struct rpc_clnt *new;
  198. struct rpc_cred *cred;
  199. /* Cloning creates an rpc_auth for the flavor */
  200. new = rpc_clone_client_set_auth(clnt, pflavor);
  201. if (IS_ERR(new))
  202. continue;
  203. /**
  204. * Check that the user actually can use the
  205. * flavor. This is mostly for RPC_AUTH_GSS
  206. * where cr_init obtains a gss context
  207. */
  208. cred = rpcauth_lookupcred(new->cl_auth, 0);
  209. if (IS_ERR(cred)) {
  210. rpc_shutdown_client(new);
  211. continue;
  212. }
  213. put_rpccred(cred);
  214. return new;
  215. }
  216. }
  217. }
  218. return ERR_PTR(-EPERM);
  219. }
  220. /**
  221. * nfs4_negotiate_security - in response to an NFS4ERR_WRONGSEC on lookup,
  222. * return an rpc_clnt that uses the best available security flavor with
  223. * respect to the secinfo flavor list and the sec= mount options.
  224. *
  225. * @clnt: RPC client to clone
  226. * @inode: directory inode
  227. * @name: lookup name
  228. *
  229. * Please call rpc_shutdown_client() when you are done with this rpc client.
  230. */
  231. struct rpc_clnt *
  232. nfs4_negotiate_security(struct rpc_clnt *clnt, struct inode *inode,
  233. const struct qstr *name)
  234. {
  235. struct page *page;
  236. struct nfs4_secinfo_flavors *flavors;
  237. struct rpc_clnt *new;
  238. int err;
  239. page = alloc_page(GFP_KERNEL);
  240. if (!page)
  241. return ERR_PTR(-ENOMEM);
  242. flavors = page_address(page);
  243. err = nfs4_proc_secinfo(inode, name, flavors);
  244. if (err < 0) {
  245. new = ERR_PTR(err);
  246. goto out;
  247. }
  248. new = nfs_find_best_sec(clnt, NFS_SERVER(inode), flavors);
  249. out:
  250. put_page(page);
  251. return new;
  252. }
  253. static int try_location(struct fs_context *fc,
  254. const struct nfs4_fs_location *location)
  255. {
  256. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  257. unsigned int len, s;
  258. char *export_path, *source, *p;
  259. int ret = -ENOENT;
  260. /* Allocate a buffer big enough to hold any of the hostnames plus a
  261. * terminating char and also a buffer big enough to hold the hostname
  262. * plus a colon plus the path.
  263. */
  264. len = 0;
  265. for (s = 0; s < location->nservers; s++) {
  266. const struct nfs4_string *buf = &location->servers[s];
  267. if (buf->len > len)
  268. len = buf->len;
  269. }
  270. kfree(ctx->nfs_server.hostname);
  271. ctx->nfs_server.hostname = kmalloc(len + 1, GFP_KERNEL);
  272. if (!ctx->nfs_server.hostname)
  273. return -ENOMEM;
  274. export_path = nfs4_pathname_string(&location->rootpath,
  275. &ctx->nfs_server.export_path_len);
  276. if (IS_ERR(export_path))
  277. return PTR_ERR(export_path);
  278. kfree(ctx->nfs_server.export_path);
  279. ctx->nfs_server.export_path = export_path;
  280. source = kmalloc(len + 1 + ctx->nfs_server.export_path_len + 1,
  281. GFP_KERNEL);
  282. if (!source)
  283. return -ENOMEM;
  284. kfree(fc->source);
  285. fc->source = source;
  286. for (s = 0; s < location->nservers; s++) {
  287. const struct nfs4_string *buf = &location->servers[s];
  288. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  289. continue;
  290. ctx->nfs_server.addrlen =
  291. nfs_parse_server_name(buf->data, buf->len,
  292. &ctx->nfs_server.address,
  293. sizeof(ctx->nfs_server._address),
  294. fc->net_ns, 0);
  295. if (ctx->nfs_server.addrlen == 0)
  296. continue;
  297. rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
  298. memcpy(ctx->nfs_server.hostname, buf->data, buf->len);
  299. ctx->nfs_server.hostname[buf->len] = '\0';
  300. p = source;
  301. memcpy(p, buf->data, buf->len);
  302. p += buf->len;
  303. *p++ = ':';
  304. memcpy(p, ctx->nfs_server.export_path, ctx->nfs_server.export_path_len);
  305. p += ctx->nfs_server.export_path_len;
  306. *p = 0;
  307. ret = nfs4_get_referral_tree(fc);
  308. if (ret == 0)
  309. return 0;
  310. }
  311. return ret;
  312. }
  313. /**
  314. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  315. * @fc: pointer to struct nfs_fs_context
  316. * @locations: array of NFSv4 server location information
  317. *
  318. */
  319. static int nfs_follow_referral(struct fs_context *fc,
  320. const struct nfs4_fs_locations *locations)
  321. {
  322. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  323. int loc, error;
  324. if (locations == NULL || locations->nlocations <= 0)
  325. return -ENOENT;
  326. dprintk("%s: referral at %pd2\n", __func__, ctx->clone_data.dentry);
  327. /* Ensure fs path is a prefix of current dentry path */
  328. error = nfs4_validate_fspath(ctx->clone_data.dentry, locations, ctx);
  329. if (error < 0)
  330. return error;
  331. error = -ENOENT;
  332. for (loc = 0; loc < locations->nlocations; loc++) {
  333. const struct nfs4_fs_location *location = &locations->locations[loc];
  334. if (location == NULL || location->nservers <= 0 ||
  335. location->rootpath.ncomponents == 0)
  336. continue;
  337. error = try_location(fc, location);
  338. if (error == 0)
  339. return 0;
  340. }
  341. return error;
  342. }
  343. /*
  344. * nfs_do_refmount - handle crossing a referral on server
  345. * @dentry - dentry of referral
  346. *
  347. */
  348. static int nfs_do_refmount(struct fs_context *fc, struct rpc_clnt *client)
  349. {
  350. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  351. struct dentry *dentry, *parent;
  352. struct nfs4_fs_locations *fs_locations = NULL;
  353. struct page *page;
  354. int err = -ENOMEM;
  355. /* BUG_ON(IS_ROOT(dentry)); */
  356. page = alloc_page(GFP_KERNEL);
  357. if (!page)
  358. return -ENOMEM;
  359. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  360. if (!fs_locations)
  361. goto out_free;
  362. /* Get locations */
  363. dentry = ctx->clone_data.dentry;
  364. parent = dget_parent(dentry);
  365. dprintk("%s: getting locations for %pd2\n",
  366. __func__, dentry);
  367. err = nfs4_proc_fs_locations(client, d_inode(parent), &dentry->d_name, fs_locations, page);
  368. dput(parent);
  369. if (err != 0)
  370. goto out_free_2;
  371. err = -ENOENT;
  372. if (fs_locations->nlocations <= 0 ||
  373. fs_locations->fs_path.ncomponents <= 0)
  374. goto out_free_2;
  375. err = nfs_follow_referral(fc, fs_locations);
  376. out_free_2:
  377. kfree(fs_locations);
  378. out_free:
  379. __free_page(page);
  380. return err;
  381. }
  382. int nfs4_submount(struct fs_context *fc, struct nfs_server *server)
  383. {
  384. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  385. struct dentry *dentry = ctx->clone_data.dentry;
  386. struct dentry *parent = dget_parent(dentry);
  387. struct inode *dir = d_inode(parent);
  388. struct rpc_clnt *client;
  389. int ret;
  390. /* Look it up again to get its attributes and sec flavor */
  391. client = nfs4_proc_lookup_mountpoint(dir, dentry, ctx->mntfh,
  392. ctx->clone_data.fattr);
  393. dput(parent);
  394. if (IS_ERR(client))
  395. return PTR_ERR(client);
  396. ctx->selected_flavor = client->cl_auth->au_flavor;
  397. if (ctx->clone_data.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  398. ret = nfs_do_refmount(fc, client);
  399. } else {
  400. ret = nfs_do_submount(fc);
  401. }
  402. rpc_shutdown_client(client);
  403. return ret;
  404. }
  405. /*
  406. * Try one location from the fs_locations array.
  407. *
  408. * Returns zero on success, or a negative errno value.
  409. */
  410. static int nfs4_try_replacing_one_location(struct nfs_server *server,
  411. char *page, char *page2,
  412. const struct nfs4_fs_location *location)
  413. {
  414. const size_t addr_bufsize = sizeof(struct sockaddr_storage);
  415. struct net *net = rpc_net_ns(server->client);
  416. struct sockaddr *sap;
  417. unsigned int s;
  418. size_t salen;
  419. int error;
  420. sap = kmalloc(addr_bufsize, GFP_KERNEL);
  421. if (sap == NULL)
  422. return -ENOMEM;
  423. error = -ENOENT;
  424. for (s = 0; s < location->nservers; s++) {
  425. const struct nfs4_string *buf = &location->servers[s];
  426. char *hostname;
  427. if (buf->len <= 0 || buf->len > PAGE_SIZE)
  428. continue;
  429. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len) != NULL)
  430. continue;
  431. salen = nfs_parse_server_name(buf->data, buf->len,
  432. sap, addr_bufsize, net, 0);
  433. if (salen == 0)
  434. continue;
  435. rpc_set_port(sap, NFS_PORT);
  436. error = -ENOMEM;
  437. hostname = kmemdup_nul(buf->data, buf->len, GFP_KERNEL);
  438. if (hostname == NULL)
  439. break;
  440. error = nfs4_update_server(server, hostname, sap, salen, net);
  441. kfree(hostname);
  442. if (error == 0)
  443. break;
  444. }
  445. kfree(sap);
  446. return error;
  447. }
  448. /**
  449. * nfs4_replace_transport - set up transport to destination server
  450. *
  451. * @server: export being migrated
  452. * @locations: fs_locations array
  453. *
  454. * Returns zero on success, or a negative errno value.
  455. *
  456. * The client tries all the entries in the "locations" array, in the
  457. * order returned by the server, until one works or the end of the
  458. * array is reached.
  459. */
  460. int nfs4_replace_transport(struct nfs_server *server,
  461. const struct nfs4_fs_locations *locations)
  462. {
  463. char *page = NULL, *page2 = NULL;
  464. int loc, error;
  465. error = -ENOENT;
  466. if (locations == NULL || locations->nlocations <= 0)
  467. goto out;
  468. error = -ENOMEM;
  469. page = (char *) __get_free_page(GFP_USER);
  470. if (!page)
  471. goto out;
  472. page2 = (char *) __get_free_page(GFP_USER);
  473. if (!page2)
  474. goto out;
  475. for (loc = 0; loc < locations->nlocations; loc++) {
  476. const struct nfs4_fs_location *location =
  477. &locations->locations[loc];
  478. if (location == NULL || location->nservers <= 0 ||
  479. location->rootpath.ncomponents == 0)
  480. continue;
  481. error = nfs4_try_replacing_one_location(server, page,
  482. page2, location);
  483. if (error == 0)
  484. break;
  485. }
  486. out:
  487. free_page((unsigned long)page);
  488. free_page((unsigned long)page2);
  489. return error;
  490. }