nfsxdr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * XDR support for nfsd
  4. *
  5. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  6. */
  7. #include "vfs.h"
  8. #include "xdr.h"
  9. #include "auth.h"
  10. #define NFSDDBG_FACILITY NFSDDBG_XDR
  11. /*
  12. * Mapping of S_IF* types to NFS file types
  13. */
  14. static u32 nfs_ftypes[] = {
  15. NFNON, NFCHR, NFCHR, NFBAD,
  16. NFDIR, NFBAD, NFBLK, NFBAD,
  17. NFREG, NFBAD, NFLNK, NFBAD,
  18. NFSOCK, NFBAD, NFLNK, NFBAD,
  19. };
  20. /*
  21. * XDR functions for basic NFS types
  22. */
  23. static __be32 *
  24. decode_fh(__be32 *p, struct svc_fh *fhp)
  25. {
  26. fh_init(fhp, NFS_FHSIZE);
  27. memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
  28. fhp->fh_handle.fh_size = NFS_FHSIZE;
  29. /* FIXME: Look up export pointer here and verify
  30. * Sun Secure RPC if requested */
  31. return p + (NFS_FHSIZE >> 2);
  32. }
  33. /* Helper function for NFSv2 ACL code */
  34. __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
  35. {
  36. return decode_fh(p, fhp);
  37. }
  38. static __be32 *
  39. encode_fh(__be32 *p, struct svc_fh *fhp)
  40. {
  41. memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
  42. return p + (NFS_FHSIZE>> 2);
  43. }
  44. /*
  45. * Decode a file name and make sure that the path contains
  46. * no slashes or null bytes.
  47. */
  48. static __be32 *
  49. decode_filename(__be32 *p, char **namp, unsigned int *lenp)
  50. {
  51. char *name;
  52. unsigned int i;
  53. if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
  54. for (i = 0, name = *namp; i < *lenp; i++, name++) {
  55. if (*name == '\0' || *name == '/')
  56. return NULL;
  57. }
  58. }
  59. return p;
  60. }
  61. static __be32 *
  62. decode_sattr(__be32 *p, struct iattr *iap)
  63. {
  64. u32 tmp, tmp1;
  65. iap->ia_valid = 0;
  66. /* Sun client bug compatibility check: some sun clients seem to
  67. * put 0xffff in the mode field when they mean 0xffffffff.
  68. * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
  69. */
  70. if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
  71. iap->ia_valid |= ATTR_MODE;
  72. iap->ia_mode = tmp;
  73. }
  74. if ((tmp = ntohl(*p++)) != (u32)-1) {
  75. iap->ia_uid = make_kuid(&init_user_ns, tmp);
  76. if (uid_valid(iap->ia_uid))
  77. iap->ia_valid |= ATTR_UID;
  78. }
  79. if ((tmp = ntohl(*p++)) != (u32)-1) {
  80. iap->ia_gid = make_kgid(&init_user_ns, tmp);
  81. if (gid_valid(iap->ia_gid))
  82. iap->ia_valid |= ATTR_GID;
  83. }
  84. if ((tmp = ntohl(*p++)) != (u32)-1) {
  85. iap->ia_valid |= ATTR_SIZE;
  86. iap->ia_size = tmp;
  87. }
  88. tmp = ntohl(*p++); tmp1 = ntohl(*p++);
  89. if (tmp != (u32)-1 && tmp1 != (u32)-1) {
  90. iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
  91. iap->ia_atime.tv_sec = tmp;
  92. iap->ia_atime.tv_nsec = tmp1 * 1000;
  93. }
  94. tmp = ntohl(*p++); tmp1 = ntohl(*p++);
  95. if (tmp != (u32)-1 && tmp1 != (u32)-1) {
  96. iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
  97. iap->ia_mtime.tv_sec = tmp;
  98. iap->ia_mtime.tv_nsec = tmp1 * 1000;
  99. /*
  100. * Passing the invalid value useconds=1000000 for mtime
  101. * is a Sun convention for "set both mtime and atime to
  102. * current server time". It's needed to make permissions
  103. * checks for the "touch" program across v2 mounts to
  104. * Solaris and Irix boxes work correctly. See description of
  105. * sattr in section 6.1 of "NFS Illustrated" by
  106. * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
  107. */
  108. if (tmp1 == 1000000)
  109. iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
  110. }
  111. return p;
  112. }
  113. static __be32 *
  114. encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
  115. struct kstat *stat)
  116. {
  117. struct dentry *dentry = fhp->fh_dentry;
  118. int type;
  119. struct timespec64 time;
  120. u32 f;
  121. type = (stat->mode & S_IFMT);
  122. *p++ = htonl(nfs_ftypes[type >> 12]);
  123. *p++ = htonl((u32) stat->mode);
  124. *p++ = htonl((u32) stat->nlink);
  125. *p++ = htonl((u32) from_kuid(&init_user_ns, stat->uid));
  126. *p++ = htonl((u32) from_kgid(&init_user_ns, stat->gid));
  127. if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
  128. *p++ = htonl(NFS_MAXPATHLEN);
  129. } else {
  130. *p++ = htonl((u32) stat->size);
  131. }
  132. *p++ = htonl((u32) stat->blksize);
  133. if (S_ISCHR(type) || S_ISBLK(type))
  134. *p++ = htonl(new_encode_dev(stat->rdev));
  135. else
  136. *p++ = htonl(0xffffffff);
  137. *p++ = htonl((u32) stat->blocks);
  138. switch (fsid_source(fhp)) {
  139. default:
  140. case FSIDSOURCE_DEV:
  141. *p++ = htonl(new_encode_dev(stat->dev));
  142. break;
  143. case FSIDSOURCE_FSID:
  144. *p++ = htonl((u32) fhp->fh_export->ex_fsid);
  145. break;
  146. case FSIDSOURCE_UUID:
  147. f = ((u32*)fhp->fh_export->ex_uuid)[0];
  148. f ^= ((u32*)fhp->fh_export->ex_uuid)[1];
  149. f ^= ((u32*)fhp->fh_export->ex_uuid)[2];
  150. f ^= ((u32*)fhp->fh_export->ex_uuid)[3];
  151. *p++ = htonl(f);
  152. break;
  153. }
  154. *p++ = htonl((u32) stat->ino);
  155. *p++ = htonl((u32) stat->atime.tv_sec);
  156. *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
  157. time = stat->mtime;
  158. lease_get_mtime(d_inode(dentry), &time);
  159. *p++ = htonl((u32) time.tv_sec);
  160. *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0);
  161. *p++ = htonl((u32) stat->ctime.tv_sec);
  162. *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0);
  163. return p;
  164. }
  165. /* Helper function for NFSv2 ACL code */
  166. __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, struct kstat *stat)
  167. {
  168. return encode_fattr(rqstp, p, fhp, stat);
  169. }
  170. /*
  171. * XDR decode functions
  172. */
  173. int
  174. nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p)
  175. {
  176. return xdr_argsize_check(rqstp, p);
  177. }
  178. int
  179. nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
  180. {
  181. struct nfsd_fhandle *args = rqstp->rq_argp;
  182. p = decode_fh(p, &args->fh);
  183. if (!p)
  184. return 0;
  185. return xdr_argsize_check(rqstp, p);
  186. }
  187. int
  188. nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
  189. {
  190. struct nfsd_sattrargs *args = rqstp->rq_argp;
  191. p = decode_fh(p, &args->fh);
  192. if (!p)
  193. return 0;
  194. p = decode_sattr(p, &args->attrs);
  195. return xdr_argsize_check(rqstp, p);
  196. }
  197. int
  198. nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
  199. {
  200. struct nfsd_diropargs *args = rqstp->rq_argp;
  201. if (!(p = decode_fh(p, &args->fh))
  202. || !(p = decode_filename(p, &args->name, &args->len)))
  203. return 0;
  204. return xdr_argsize_check(rqstp, p);
  205. }
  206. int
  207. nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
  208. {
  209. struct nfsd_readargs *args = rqstp->rq_argp;
  210. unsigned int len;
  211. int v;
  212. p = decode_fh(p, &args->fh);
  213. if (!p)
  214. return 0;
  215. args->offset = ntohl(*p++);
  216. len = args->count = ntohl(*p++);
  217. p++; /* totalcount - unused */
  218. len = min_t(unsigned int, len, NFSSVC_MAXBLKSIZE_V2);
  219. /* set up somewhere to store response.
  220. * We take pages, put them on reslist and include in iovec
  221. */
  222. v=0;
  223. while (len > 0) {
  224. struct page *p = *(rqstp->rq_next_page++);
  225. rqstp->rq_vec[v].iov_base = page_address(p);
  226. rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
  227. len -= rqstp->rq_vec[v].iov_len;
  228. v++;
  229. }
  230. args->vlen = v;
  231. return xdr_argsize_check(rqstp, p);
  232. }
  233. int
  234. nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
  235. {
  236. struct nfsd_writeargs *args = rqstp->rq_argp;
  237. unsigned int len, hdr, dlen;
  238. struct kvec *head = rqstp->rq_arg.head;
  239. p = decode_fh(p, &args->fh);
  240. if (!p)
  241. return 0;
  242. p++; /* beginoffset */
  243. args->offset = ntohl(*p++); /* offset */
  244. p++; /* totalcount */
  245. len = args->len = ntohl(*p++);
  246. /*
  247. * The protocol specifies a maximum of 8192 bytes.
  248. */
  249. if (len > NFSSVC_MAXBLKSIZE_V2)
  250. return 0;
  251. /*
  252. * Check to make sure that we got the right number of
  253. * bytes.
  254. */
  255. hdr = (void*)p - head->iov_base;
  256. if (hdr > head->iov_len)
  257. return 0;
  258. dlen = head->iov_len + rqstp->rq_arg.page_len - hdr;
  259. /*
  260. * Round the length of the data which was specified up to
  261. * the next multiple of XDR units and then compare that
  262. * against the length which was actually received.
  263. * Note that when RPCSEC/GSS (for example) is used, the
  264. * data buffer can be padded so dlen might be larger
  265. * than required. It must never be smaller.
  266. */
  267. if (dlen < XDR_QUADLEN(len)*4)
  268. return 0;
  269. args->first.iov_base = (void *)p;
  270. args->first.iov_len = head->iov_len - hdr;
  271. return 1;
  272. }
  273. int
  274. nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
  275. {
  276. struct nfsd_createargs *args = rqstp->rq_argp;
  277. if ( !(p = decode_fh(p, &args->fh))
  278. || !(p = decode_filename(p, &args->name, &args->len)))
  279. return 0;
  280. p = decode_sattr(p, &args->attrs);
  281. return xdr_argsize_check(rqstp, p);
  282. }
  283. int
  284. nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
  285. {
  286. struct nfsd_renameargs *args = rqstp->rq_argp;
  287. if (!(p = decode_fh(p, &args->ffh))
  288. || !(p = decode_filename(p, &args->fname, &args->flen))
  289. || !(p = decode_fh(p, &args->tfh))
  290. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  291. return 0;
  292. return xdr_argsize_check(rqstp, p);
  293. }
  294. int
  295. nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
  296. {
  297. struct nfsd_readlinkargs *args = rqstp->rq_argp;
  298. p = decode_fh(p, &args->fh);
  299. if (!p)
  300. return 0;
  301. args->buffer = page_address(*(rqstp->rq_next_page++));
  302. return xdr_argsize_check(rqstp, p);
  303. }
  304. int
  305. nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
  306. {
  307. struct nfsd_linkargs *args = rqstp->rq_argp;
  308. if (!(p = decode_fh(p, &args->ffh))
  309. || !(p = decode_fh(p, &args->tfh))
  310. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  311. return 0;
  312. return xdr_argsize_check(rqstp, p);
  313. }
  314. int
  315. nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
  316. {
  317. struct nfsd_symlinkargs *args = rqstp->rq_argp;
  318. char *base = (char *)p;
  319. size_t xdrlen;
  320. if ( !(p = decode_fh(p, &args->ffh))
  321. || !(p = decode_filename(p, &args->fname, &args->flen)))
  322. return 0;
  323. args->tlen = ntohl(*p++);
  324. if (args->tlen == 0)
  325. return 0;
  326. args->first.iov_base = p;
  327. args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
  328. args->first.iov_len -= (char *)p - base;
  329. /* This request is never larger than a page. Therefore,
  330. * transport will deliver either:
  331. * 1. pathname in the pagelist -> sattr is in the tail.
  332. * 2. everything in the head buffer -> sattr is in the head.
  333. */
  334. if (rqstp->rq_arg.page_len) {
  335. if (args->tlen != rqstp->rq_arg.page_len)
  336. return 0;
  337. p = rqstp->rq_arg.tail[0].iov_base;
  338. } else {
  339. xdrlen = XDR_QUADLEN(args->tlen);
  340. if (xdrlen > args->first.iov_len - (8 * sizeof(__be32)))
  341. return 0;
  342. p += xdrlen;
  343. }
  344. decode_sattr(p, &args->attrs);
  345. return 1;
  346. }
  347. int
  348. nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
  349. {
  350. struct nfsd_readdirargs *args = rqstp->rq_argp;
  351. p = decode_fh(p, &args->fh);
  352. if (!p)
  353. return 0;
  354. args->cookie = ntohl(*p++);
  355. args->count = ntohl(*p++);
  356. args->count = min_t(u32, args->count, PAGE_SIZE);
  357. args->buffer = page_address(*(rqstp->rq_next_page++));
  358. return xdr_argsize_check(rqstp, p);
  359. }
  360. /*
  361. * XDR encode functions
  362. */
  363. int
  364. nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p)
  365. {
  366. return xdr_ressize_check(rqstp, p);
  367. }
  368. int
  369. nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
  370. {
  371. struct nfsd_attrstat *resp = rqstp->rq_resp;
  372. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  373. return xdr_ressize_check(rqstp, p);
  374. }
  375. int
  376. nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
  377. {
  378. struct nfsd_diropres *resp = rqstp->rq_resp;
  379. p = encode_fh(p, &resp->fh);
  380. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  381. return xdr_ressize_check(rqstp, p);
  382. }
  383. int
  384. nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
  385. {
  386. struct nfsd_readlinkres *resp = rqstp->rq_resp;
  387. *p++ = htonl(resp->len);
  388. xdr_ressize_check(rqstp, p);
  389. rqstp->rq_res.page_len = resp->len;
  390. if (resp->len & 3) {
  391. /* need to pad the tail */
  392. rqstp->rq_res.tail[0].iov_base = p;
  393. *p = 0;
  394. rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
  395. }
  396. return 1;
  397. }
  398. int
  399. nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
  400. {
  401. struct nfsd_readres *resp = rqstp->rq_resp;
  402. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  403. *p++ = htonl(resp->count);
  404. xdr_ressize_check(rqstp, p);
  405. /* now update rqstp->rq_res to reflect data as well */
  406. rqstp->rq_res.page_len = resp->count;
  407. if (resp->count & 3) {
  408. /* need to pad the tail */
  409. rqstp->rq_res.tail[0].iov_base = p;
  410. *p = 0;
  411. rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
  412. }
  413. return 1;
  414. }
  415. int
  416. nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
  417. {
  418. struct nfsd_readdirres *resp = rqstp->rq_resp;
  419. xdr_ressize_check(rqstp, p);
  420. p = resp->buffer;
  421. *p++ = 0; /* no more entries */
  422. *p++ = htonl((resp->common.err == nfserr_eof));
  423. rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
  424. return 1;
  425. }
  426. int
  427. nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p)
  428. {
  429. struct nfsd_statfsres *resp = rqstp->rq_resp;
  430. struct kstatfs *stat = &resp->stats;
  431. *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */
  432. *p++ = htonl(stat->f_bsize);
  433. *p++ = htonl(stat->f_blocks);
  434. *p++ = htonl(stat->f_bfree);
  435. *p++ = htonl(stat->f_bavail);
  436. return xdr_ressize_check(rqstp, p);
  437. }
  438. int
  439. nfssvc_encode_entry(void *ccdv, const char *name,
  440. int namlen, loff_t offset, u64 ino, unsigned int d_type)
  441. {
  442. struct readdir_cd *ccd = ccdv;
  443. struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
  444. __be32 *p = cd->buffer;
  445. int buflen, slen;
  446. /*
  447. dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
  448. namlen, name, offset, ino);
  449. */
  450. if (offset > ~((u32) 0)) {
  451. cd->common.err = nfserr_fbig;
  452. return -EINVAL;
  453. }
  454. if (cd->offset)
  455. *cd->offset = htonl(offset);
  456. /* truncate filename */
  457. namlen = min(namlen, NFS2_MAXNAMLEN);
  458. slen = XDR_QUADLEN(namlen);
  459. if ((buflen = cd->buflen - slen - 4) < 0) {
  460. cd->common.err = nfserr_toosmall;
  461. return -EINVAL;
  462. }
  463. if (ino > ~((u32) 0)) {
  464. cd->common.err = nfserr_fbig;
  465. return -EINVAL;
  466. }
  467. *p++ = xdr_one; /* mark entry present */
  468. *p++ = htonl((u32) ino); /* file id */
  469. p = xdr_encode_array(p, name, namlen);/* name length & name */
  470. cd->offset = p; /* remember pointer */
  471. *p++ = htonl(~0U); /* offset of next entry */
  472. cd->buflen = buflen;
  473. cd->buffer = p;
  474. cd->common.err = nfs_ok;
  475. return 0;
  476. }
  477. /*
  478. * XDR release functions
  479. */
  480. void
  481. nfssvc_release_fhandle(struct svc_rqst *rqstp)
  482. {
  483. struct nfsd_fhandle *resp = rqstp->rq_resp;
  484. fh_put(&resp->fh);
  485. }