nfs4namespace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/nfs4namespace.c
  4. *
  5. * Copyright (C) 2005 Trond Myklebust <[email protected]>
  6. * - Modified by David Howells <[email protected]>
  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_storage *ss,
  146. size_t salen, struct net *net, int port)
  147. {
  148. struct sockaddr *sa = (struct sockaddr *)ss;
  149. ssize_t ret;
  150. ret = rpc_pton(net, string, len, sa, salen);
  151. if (ret == 0) {
  152. ret = rpc_uaddr2sockaddr(net, string, len, sa, salen);
  153. if (ret == 0) {
  154. ret = nfs_dns_resolve_name(net, string, len, ss, salen);
  155. if (ret < 0)
  156. ret = 0;
  157. }
  158. } else if (port) {
  159. rpc_set_port(sa, port);
  160. }
  161. return ret;
  162. }
  163. /**
  164. * nfs_find_best_sec - Find a security mechanism supported locally
  165. * @clnt: pointer to rpc_clnt
  166. * @server: NFS server struct
  167. * @flavors: List of security tuples returned by SECINFO procedure
  168. *
  169. * Return an rpc client that uses the first security mechanism in
  170. * "flavors" that is locally supported. The "flavors" array
  171. * is searched in the order returned from the server, per RFC 3530
  172. * recommendation and each flavor is checked for membership in the
  173. * sec= mount option list if it exists.
  174. *
  175. * Return -EPERM if no matching flavor is found in the array.
  176. *
  177. * Please call rpc_shutdown_client() when you are done with this rpc client.
  178. *
  179. */
  180. static struct rpc_clnt *nfs_find_best_sec(struct rpc_clnt *clnt,
  181. struct nfs_server *server,
  182. struct nfs4_secinfo_flavors *flavors)
  183. {
  184. rpc_authflavor_t pflavor;
  185. struct nfs4_secinfo4 *secinfo;
  186. unsigned int i;
  187. for (i = 0; i < flavors->num_flavors; i++) {
  188. secinfo = &flavors->flavors[i];
  189. switch (secinfo->flavor) {
  190. case RPC_AUTH_NULL:
  191. case RPC_AUTH_UNIX:
  192. case RPC_AUTH_GSS:
  193. pflavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  194. &secinfo->flavor_info);
  195. /* does the pseudoflavor match a sec= mount opt? */
  196. if (pflavor != RPC_AUTH_MAXFLAVOR &&
  197. nfs_auth_info_match(&server->auth_info, pflavor)) {
  198. struct rpc_clnt *new;
  199. struct rpc_cred *cred;
  200. /* Cloning creates an rpc_auth for the flavor */
  201. new = rpc_clone_client_set_auth(clnt, pflavor);
  202. if (IS_ERR(new))
  203. continue;
  204. /**
  205. * Check that the user actually can use the
  206. * flavor. This is mostly for RPC_AUTH_GSS
  207. * where cr_init obtains a gss context
  208. */
  209. cred = rpcauth_lookupcred(new->cl_auth, 0);
  210. if (IS_ERR(cred)) {
  211. rpc_shutdown_client(new);
  212. continue;
  213. }
  214. put_rpccred(cred);
  215. return new;
  216. }
  217. }
  218. }
  219. return ERR_PTR(-EPERM);
  220. }
  221. /**
  222. * nfs4_negotiate_security - in response to an NFS4ERR_WRONGSEC on lookup,
  223. * return an rpc_clnt that uses the best available security flavor with
  224. * respect to the secinfo flavor list and the sec= mount options.
  225. *
  226. * @clnt: RPC client to clone
  227. * @inode: directory inode
  228. * @name: lookup name
  229. *
  230. * Please call rpc_shutdown_client() when you are done with this rpc client.
  231. */
  232. struct rpc_clnt *
  233. nfs4_negotiate_security(struct rpc_clnt *clnt, struct inode *inode,
  234. const struct qstr *name)
  235. {
  236. struct page *page;
  237. struct nfs4_secinfo_flavors *flavors;
  238. struct rpc_clnt *new;
  239. int err;
  240. page = alloc_page(GFP_KERNEL);
  241. if (!page)
  242. return ERR_PTR(-ENOMEM);
  243. flavors = page_address(page);
  244. err = nfs4_proc_secinfo(inode, name, flavors);
  245. if (err < 0) {
  246. new = ERR_PTR(err);
  247. goto out;
  248. }
  249. new = nfs_find_best_sec(clnt, NFS_SERVER(inode), flavors);
  250. out:
  251. put_page(page);
  252. return new;
  253. }
  254. static int try_location(struct fs_context *fc,
  255. const struct nfs4_fs_location *location)
  256. {
  257. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  258. unsigned int len, s;
  259. char *export_path, *source, *p;
  260. int ret = -ENOENT;
  261. /* Allocate a buffer big enough to hold any of the hostnames plus a
  262. * terminating char and also a buffer big enough to hold the hostname
  263. * plus a colon plus the path.
  264. */
  265. len = 0;
  266. for (s = 0; s < location->nservers; s++) {
  267. const struct nfs4_string *buf = &location->servers[s];
  268. if (buf->len > len)
  269. len = buf->len;
  270. }
  271. kfree(ctx->nfs_server.hostname);
  272. ctx->nfs_server.hostname = kmalloc(len + 1, GFP_KERNEL);
  273. if (!ctx->nfs_server.hostname)
  274. return -ENOMEM;
  275. export_path = nfs4_pathname_string(&location->rootpath,
  276. &ctx->nfs_server.export_path_len);
  277. if (IS_ERR(export_path))
  278. return PTR_ERR(export_path);
  279. kfree(ctx->nfs_server.export_path);
  280. ctx->nfs_server.export_path = export_path;
  281. source = kmalloc(len + 1 + ctx->nfs_server.export_path_len + 1,
  282. GFP_KERNEL);
  283. if (!source)
  284. return -ENOMEM;
  285. kfree(fc->source);
  286. fc->source = source;
  287. for (s = 0; s < location->nservers; s++) {
  288. const struct nfs4_string *buf = &location->servers[s];
  289. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
  290. continue;
  291. ctx->nfs_server.addrlen =
  292. nfs_parse_server_name(buf->data, buf->len,
  293. &ctx->nfs_server._address,
  294. sizeof(ctx->nfs_server._address),
  295. fc->net_ns, 0);
  296. if (ctx->nfs_server.addrlen == 0)
  297. continue;
  298. rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
  299. memcpy(ctx->nfs_server.hostname, buf->data, buf->len);
  300. ctx->nfs_server.hostname[buf->len] = '\0';
  301. p = source;
  302. memcpy(p, buf->data, buf->len);
  303. p += buf->len;
  304. *p++ = ':';
  305. memcpy(p, ctx->nfs_server.export_path, ctx->nfs_server.export_path_len);
  306. p += ctx->nfs_server.export_path_len;
  307. *p = 0;
  308. ret = nfs4_get_referral_tree(fc);
  309. if (ret == 0)
  310. return 0;
  311. }
  312. return ret;
  313. }
  314. /**
  315. * nfs_follow_referral - set up mountpoint when hitting a referral on moved error
  316. * @fc: pointer to struct nfs_fs_context
  317. * @locations: array of NFSv4 server location information
  318. *
  319. */
  320. static int nfs_follow_referral(struct fs_context *fc,
  321. const struct nfs4_fs_locations *locations)
  322. {
  323. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  324. int loc, error;
  325. if (locations == NULL || locations->nlocations <= 0)
  326. return -ENOENT;
  327. dprintk("%s: referral at %pd2\n", __func__, ctx->clone_data.dentry);
  328. /* Ensure fs path is a prefix of current dentry path */
  329. error = nfs4_validate_fspath(ctx->clone_data.dentry, locations, ctx);
  330. if (error < 0)
  331. return error;
  332. error = -ENOENT;
  333. for (loc = 0; loc < locations->nlocations; loc++) {
  334. const struct nfs4_fs_location *location = &locations->locations[loc];
  335. if (location == NULL || location->nservers <= 0 ||
  336. location->rootpath.ncomponents == 0)
  337. continue;
  338. error = try_location(fc, location);
  339. if (error == 0)
  340. return 0;
  341. }
  342. return error;
  343. }
  344. /*
  345. * nfs_do_refmount - handle crossing a referral on server
  346. * @dentry - dentry of referral
  347. *
  348. */
  349. static int nfs_do_refmount(struct fs_context *fc, struct rpc_clnt *client)
  350. {
  351. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  352. struct dentry *dentry, *parent;
  353. struct nfs4_fs_locations *fs_locations = NULL;
  354. struct page *page;
  355. int err = -ENOMEM;
  356. /* BUG_ON(IS_ROOT(dentry)); */
  357. page = alloc_page(GFP_KERNEL);
  358. if (!page)
  359. return -ENOMEM;
  360. fs_locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  361. if (!fs_locations)
  362. goto out_free;
  363. fs_locations->fattr = nfs_alloc_fattr();
  364. if (!fs_locations->fattr)
  365. goto out_free_2;
  366. /* Get locations */
  367. dentry = ctx->clone_data.dentry;
  368. parent = dget_parent(dentry);
  369. dprintk("%s: getting locations for %pd2\n",
  370. __func__, dentry);
  371. err = nfs4_proc_fs_locations(client, d_inode(parent), &dentry->d_name, fs_locations, page);
  372. dput(parent);
  373. if (err != 0)
  374. goto out_free_3;
  375. err = -ENOENT;
  376. if (fs_locations->nlocations <= 0 ||
  377. fs_locations->fs_path.ncomponents <= 0)
  378. goto out_free_3;
  379. err = nfs_follow_referral(fc, fs_locations);
  380. out_free_3:
  381. kfree(fs_locations->fattr);
  382. out_free_2:
  383. kfree(fs_locations);
  384. out_free:
  385. __free_page(page);
  386. return err;
  387. }
  388. int nfs4_submount(struct fs_context *fc, struct nfs_server *server)
  389. {
  390. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  391. struct dentry *dentry = ctx->clone_data.dentry;
  392. struct dentry *parent = dget_parent(dentry);
  393. struct inode *dir = d_inode(parent);
  394. struct rpc_clnt *client;
  395. int ret;
  396. /* Look it up again to get its attributes and sec flavor */
  397. client = nfs4_proc_lookup_mountpoint(dir, dentry, ctx->mntfh,
  398. ctx->clone_data.fattr);
  399. dput(parent);
  400. if (IS_ERR(client))
  401. return PTR_ERR(client);
  402. ctx->selected_flavor = client->cl_auth->au_flavor;
  403. if (ctx->clone_data.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  404. ret = nfs_do_refmount(fc, client);
  405. } else {
  406. ret = nfs_do_submount(fc);
  407. }
  408. rpc_shutdown_client(client);
  409. return ret;
  410. }
  411. /*
  412. * Try one location from the fs_locations array.
  413. *
  414. * Returns zero on success, or a negative errno value.
  415. */
  416. static int nfs4_try_replacing_one_location(struct nfs_server *server,
  417. char *page, char *page2,
  418. const struct nfs4_fs_location *location)
  419. {
  420. struct net *net = rpc_net_ns(server->client);
  421. struct sockaddr_storage *sap;
  422. unsigned int s;
  423. size_t salen;
  424. int error;
  425. sap = kmalloc(sizeof(*sap), GFP_KERNEL);
  426. if (sap == NULL)
  427. return -ENOMEM;
  428. error = -ENOENT;
  429. for (s = 0; s < location->nservers; s++) {
  430. const struct nfs4_string *buf = &location->servers[s];
  431. char *hostname;
  432. if (buf->len <= 0 || buf->len > PAGE_SIZE)
  433. continue;
  434. if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len) != NULL)
  435. continue;
  436. salen = nfs_parse_server_name(buf->data, buf->len,
  437. sap, sizeof(*sap), net, 0);
  438. if (salen == 0)
  439. continue;
  440. rpc_set_port((struct sockaddr *)sap, NFS_PORT);
  441. error = -ENOMEM;
  442. hostname = kmemdup_nul(buf->data, buf->len, GFP_KERNEL);
  443. if (hostname == NULL)
  444. break;
  445. error = nfs4_update_server(server, hostname, sap, salen, net);
  446. kfree(hostname);
  447. if (error == 0)
  448. break;
  449. }
  450. kfree(sap);
  451. return error;
  452. }
  453. /**
  454. * nfs4_replace_transport - set up transport to destination server
  455. *
  456. * @server: export being migrated
  457. * @locations: fs_locations array
  458. *
  459. * Returns zero on success, or a negative errno value.
  460. *
  461. * The client tries all the entries in the "locations" array, in the
  462. * order returned by the server, until one works or the end of the
  463. * array is reached.
  464. */
  465. int nfs4_replace_transport(struct nfs_server *server,
  466. const struct nfs4_fs_locations *locations)
  467. {
  468. char *page = NULL, *page2 = NULL;
  469. int loc, error;
  470. error = -ENOENT;
  471. if (locations == NULL || locations->nlocations <= 0)
  472. goto out;
  473. error = -ENOMEM;
  474. page = (char *) __get_free_page(GFP_USER);
  475. if (!page)
  476. goto out;
  477. page2 = (char *) __get_free_page(GFP_USER);
  478. if (!page2)
  479. goto out;
  480. for (loc = 0; loc < locations->nlocations; loc++) {
  481. const struct nfs4_fs_location *location =
  482. &locations->locations[loc];
  483. if (location == NULL || location->nservers <= 0 ||
  484. location->rootpath.ncomponents == 0)
  485. continue;
  486. error = nfs4_try_replacing_one_location(server, page,
  487. page2, location);
  488. if (error == 0)
  489. break;
  490. }
  491. out:
  492. free_page((unsigned long)page);
  493. free_page((unsigned long)page2);
  494. return error;
  495. }