nfs4namespace.c 13 KB

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