xref: /openbmc/linux/fs/nfsd/nfsxdr.c (revision 6562c9ac)
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 
8 #include "vfs.h"
9 #include "xdr.h"
10 #include "auth.h"
11 
12 /*
13  * Mapping of S_IF* types to NFS file types
14  */
15 static const u32 nfs_ftypes[] = {
16 	NFNON,  NFCHR,  NFCHR, NFBAD,
17 	NFDIR,  NFBAD,  NFBLK, NFBAD,
18 	NFREG,  NFBAD,  NFLNK, NFBAD,
19 	NFSOCK, NFBAD,  NFLNK, NFBAD,
20 };
21 
22 
23 /*
24  * Basic NFSv2 data types (RFC 1094 Section 2.3)
25  */
26 
27 /**
28  * svcxdr_encode_stat - Encode an NFSv2 status code
29  * @xdr: XDR stream
30  * @status: status value to encode
31  *
32  * Return values:
33  *   %false: Send buffer space was exhausted
34  *   %true: Success
35  */
36 bool
37 svcxdr_encode_stat(struct xdr_stream *xdr, __be32 status)
38 {
39 	__be32 *p;
40 
41 	p = xdr_reserve_space(xdr, sizeof(status));
42 	if (!p)
43 		return false;
44 	*p = status;
45 
46 	return true;
47 }
48 
49 /**
50  * svcxdr_decode_fhandle - Decode an NFSv2 file handle
51  * @xdr: XDR stream positioned at an encoded NFSv2 FH
52  * @fhp: OUT: filled-in server file handle
53  *
54  * Return values:
55  *  %false: The encoded file handle was not valid
56  *  %true: @fhp has been initialized
57  */
58 bool
59 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp)
60 {
61 	__be32 *p;
62 
63 	p = xdr_inline_decode(xdr, NFS_FHSIZE);
64 	if (!p)
65 		return false;
66 	fh_init(fhp, NFS_FHSIZE);
67 	memcpy(&fhp->fh_handle.fh_raw, p, NFS_FHSIZE);
68 	fhp->fh_handle.fh_size = NFS_FHSIZE;
69 
70 	return true;
71 }
72 
73 static bool
74 svcxdr_encode_fhandle(struct xdr_stream *xdr, const struct svc_fh *fhp)
75 {
76 	__be32 *p;
77 
78 	p = xdr_reserve_space(xdr, NFS_FHSIZE);
79 	if (!p)
80 		return false;
81 	memcpy(p, &fhp->fh_handle.fh_raw, NFS_FHSIZE);
82 
83 	return true;
84 }
85 
86 static __be32 *
87 encode_timeval(__be32 *p, const struct timespec64 *time)
88 {
89 	*p++ = cpu_to_be32((u32)time->tv_sec);
90 	if (time->tv_nsec)
91 		*p++ = cpu_to_be32(time->tv_nsec / NSEC_PER_USEC);
92 	else
93 		*p++ = xdr_zero;
94 	return p;
95 }
96 
97 static bool
98 svcxdr_decode_filename(struct xdr_stream *xdr, char **name, unsigned int *len)
99 {
100 	u32 size, i;
101 	__be32 *p;
102 	char *c;
103 
104 	if (xdr_stream_decode_u32(xdr, &size) < 0)
105 		return false;
106 	if (size == 0 || size > NFS_MAXNAMLEN)
107 		return false;
108 	p = xdr_inline_decode(xdr, size);
109 	if (!p)
110 		return false;
111 
112 	*len = size;
113 	*name = (char *)p;
114 	for (i = 0, c = *name; i < size; i++, c++)
115 		if (*c == '\0' || *c == '/')
116 			return false;
117 
118 	return true;
119 }
120 
121 static bool
122 svcxdr_decode_diropargs(struct xdr_stream *xdr, struct svc_fh *fhp,
123 			char **name, unsigned int *len)
124 {
125 	return svcxdr_decode_fhandle(xdr, fhp) &&
126 		svcxdr_decode_filename(xdr, name, len);
127 }
128 
129 static bool
130 svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
131 		    struct iattr *iap)
132 {
133 	u32 tmp1, tmp2;
134 	__be32 *p;
135 
136 	p = xdr_inline_decode(xdr, XDR_UNIT * 8);
137 	if (!p)
138 		return false;
139 
140 	iap->ia_valid = 0;
141 
142 	/*
143 	 * Some Sun clients put 0xffff in the mode field when they
144 	 * mean 0xffffffff.
145 	 */
146 	tmp1 = be32_to_cpup(p++);
147 	if (tmp1 != (u32)-1 && tmp1 != 0xffff) {
148 		iap->ia_valid |= ATTR_MODE;
149 		iap->ia_mode = tmp1;
150 	}
151 
152 	tmp1 = be32_to_cpup(p++);
153 	if (tmp1 != (u32)-1) {
154 		iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), tmp1);
155 		if (uid_valid(iap->ia_uid))
156 			iap->ia_valid |= ATTR_UID;
157 	}
158 
159 	tmp1 = be32_to_cpup(p++);
160 	if (tmp1 != (u32)-1) {
161 		iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), tmp1);
162 		if (gid_valid(iap->ia_gid))
163 			iap->ia_valid |= ATTR_GID;
164 	}
165 
166 	tmp1 = be32_to_cpup(p++);
167 	if (tmp1 != (u32)-1) {
168 		iap->ia_valid |= ATTR_SIZE;
169 		iap->ia_size = tmp1;
170 	}
171 
172 	tmp1 = be32_to_cpup(p++);
173 	tmp2 = be32_to_cpup(p++);
174 	if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
175 		iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
176 		iap->ia_atime.tv_sec = tmp1;
177 		iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
178 	}
179 
180 	tmp1 = be32_to_cpup(p++);
181 	tmp2 = be32_to_cpup(p++);
182 	if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
183 		iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
184 		iap->ia_mtime.tv_sec = tmp1;
185 		iap->ia_mtime.tv_nsec = tmp2 * NSEC_PER_USEC;
186 		/*
187 		 * Passing the invalid value useconds=1000000 for mtime
188 		 * is a Sun convention for "set both mtime and atime to
189 		 * current server time".  It's needed to make permissions
190 		 * checks for the "touch" program across v2 mounts to
191 		 * Solaris and Irix boxes work correctly. See description of
192 		 * sattr in section 6.1 of "NFS Illustrated" by
193 		 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
194 		 */
195 		if (tmp2 == 1000000)
196 			iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
197 	}
198 
199 	return true;
200 }
201 
202 /**
203  * svcxdr_encode_fattr - Encode NFSv2 file attributes
204  * @rqstp: Context of a completed RPC transaction
205  * @xdr: XDR stream
206  * @fhp: File handle to encode
207  * @stat: Attributes to encode
208  *
209  * Return values:
210  *   %false: Send buffer space was exhausted
211  *   %true: Success
212  */
213 bool
214 svcxdr_encode_fattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
215 		    const struct svc_fh *fhp, const struct kstat *stat)
216 {
217 	struct user_namespace *userns = nfsd_user_namespace(rqstp);
218 	struct dentry *dentry = fhp->fh_dentry;
219 	int type = stat->mode & S_IFMT;
220 	struct timespec64 time;
221 	__be32 *p;
222 	u32 fsid;
223 
224 	p = xdr_reserve_space(xdr, XDR_UNIT * 17);
225 	if (!p)
226 		return false;
227 
228 	*p++ = cpu_to_be32(nfs_ftypes[type >> 12]);
229 	*p++ = cpu_to_be32((u32)stat->mode);
230 	*p++ = cpu_to_be32((u32)stat->nlink);
231 	*p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
232 	*p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
233 
234 	if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN)
235 		*p++ = cpu_to_be32(NFS_MAXPATHLEN);
236 	else
237 		*p++ = cpu_to_be32((u32) stat->size);
238 	*p++ = cpu_to_be32((u32) stat->blksize);
239 	if (S_ISCHR(type) || S_ISBLK(type))
240 		*p++ = cpu_to_be32(new_encode_dev(stat->rdev));
241 	else
242 		*p++ = cpu_to_be32(0xffffffff);
243 	*p++ = cpu_to_be32((u32)stat->blocks);
244 
245 	switch (fsid_source(fhp)) {
246 	case FSIDSOURCE_FSID:
247 		fsid = (u32)fhp->fh_export->ex_fsid;
248 		break;
249 	case FSIDSOURCE_UUID:
250 		fsid = ((u32 *)fhp->fh_export->ex_uuid)[0];
251 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[1];
252 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[2];
253 		fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[3];
254 		break;
255 	default:
256 		fsid = new_encode_dev(stat->dev);
257 		break;
258 	}
259 	*p++ = cpu_to_be32(fsid);
260 
261 	*p++ = cpu_to_be32((u32)stat->ino);
262 	p = encode_timeval(p, &stat->atime);
263 	time = stat->mtime;
264 	lease_get_mtime(d_inode(dentry), &time);
265 	p = encode_timeval(p, &time);
266 	encode_timeval(p, &stat->ctime);
267 
268 	return true;
269 }
270 
271 /*
272  * XDR decode functions
273  */
274 
275 bool
276 nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
277 {
278 	struct nfsd_fhandle *args = rqstp->rq_argp;
279 
280 	return svcxdr_decode_fhandle(xdr, &args->fh);
281 }
282 
283 bool
284 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
285 {
286 	struct nfsd_sattrargs *args = rqstp->rq_argp;
287 
288 	return svcxdr_decode_fhandle(xdr, &args->fh) &&
289 		svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
290 }
291 
292 bool
293 nfssvc_decode_diropargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
294 {
295 	struct nfsd_diropargs *args = rqstp->rq_argp;
296 
297 	return svcxdr_decode_diropargs(xdr, &args->fh, &args->name, &args->len);
298 }
299 
300 bool
301 nfssvc_decode_readargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
302 {
303 	struct nfsd_readargs *args = rqstp->rq_argp;
304 	u32 totalcount;
305 
306 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
307 		return false;
308 	if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
309 		return false;
310 	if (xdr_stream_decode_u32(xdr, &args->count) < 0)
311 		return false;
312 	/* totalcount is ignored */
313 	if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
314 		return false;
315 
316 	return true;
317 }
318 
319 bool
320 nfssvc_decode_writeargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
321 {
322 	struct nfsd_writeargs *args = rqstp->rq_argp;
323 	u32 beginoffset, totalcount;
324 
325 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
326 		return false;
327 	/* beginoffset is ignored */
328 	if (xdr_stream_decode_u32(xdr, &beginoffset) < 0)
329 		return false;
330 	if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
331 		return false;
332 	/* totalcount is ignored */
333 	if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
334 		return false;
335 
336 	/* opaque data */
337 	if (xdr_stream_decode_u32(xdr, &args->len) < 0)
338 		return false;
339 	if (args->len > NFSSVC_MAXBLKSIZE_V2)
340 		return false;
341 
342 	return xdr_stream_subsegment(xdr, &args->payload, args->len);
343 }
344 
345 bool
346 nfssvc_decode_createargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
347 {
348 	struct nfsd_createargs *args = rqstp->rq_argp;
349 
350 	return svcxdr_decode_diropargs(xdr, &args->fh,
351 				       &args->name, &args->len) &&
352 		svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
353 }
354 
355 bool
356 nfssvc_decode_renameargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
357 {
358 	struct nfsd_renameargs *args = rqstp->rq_argp;
359 
360 	return svcxdr_decode_diropargs(xdr, &args->ffh,
361 				       &args->fname, &args->flen) &&
362 		svcxdr_decode_diropargs(xdr, &args->tfh,
363 					&args->tname, &args->tlen);
364 }
365 
366 bool
367 nfssvc_decode_linkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
368 {
369 	struct nfsd_linkargs *args = rqstp->rq_argp;
370 
371 	return svcxdr_decode_fhandle(xdr, &args->ffh) &&
372 		svcxdr_decode_diropargs(xdr, &args->tfh,
373 					&args->tname, &args->tlen);
374 }
375 
376 bool
377 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
378 {
379 	struct nfsd_symlinkargs *args = rqstp->rq_argp;
380 	struct kvec *head = rqstp->rq_arg.head;
381 
382 	if (!svcxdr_decode_diropargs(xdr, &args->ffh, &args->fname, &args->flen))
383 		return false;
384 	if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
385 		return false;
386 	if (args->tlen == 0)
387 		return false;
388 
389 	args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
390 	args->first.iov_base = xdr_inline_decode(xdr, args->tlen);
391 	if (!args->first.iov_base)
392 		return false;
393 	return svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
394 }
395 
396 bool
397 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
398 {
399 	struct nfsd_readdirargs *args = rqstp->rq_argp;
400 
401 	if (!svcxdr_decode_fhandle(xdr, &args->fh))
402 		return false;
403 	if (xdr_stream_decode_u32(xdr, &args->cookie) < 0)
404 		return false;
405 	if (xdr_stream_decode_u32(xdr, &args->count) < 0)
406 		return false;
407 
408 	return true;
409 }
410 
411 /*
412  * XDR encode functions
413  */
414 
415 bool
416 nfssvc_encode_statres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
417 {
418 	struct nfsd_stat *resp = rqstp->rq_resp;
419 
420 	return svcxdr_encode_stat(xdr, resp->status);
421 }
422 
423 bool
424 nfssvc_encode_attrstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
425 {
426 	struct nfsd_attrstat *resp = rqstp->rq_resp;
427 
428 	if (!svcxdr_encode_stat(xdr, resp->status))
429 		return false;
430 	switch (resp->status) {
431 	case nfs_ok:
432 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
433 			return false;
434 		break;
435 	}
436 
437 	return true;
438 }
439 
440 bool
441 nfssvc_encode_diropres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
442 {
443 	struct nfsd_diropres *resp = rqstp->rq_resp;
444 
445 	if (!svcxdr_encode_stat(xdr, resp->status))
446 		return false;
447 	switch (resp->status) {
448 	case nfs_ok:
449 		if (!svcxdr_encode_fhandle(xdr, &resp->fh))
450 			return false;
451 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
452 			return false;
453 		break;
454 	}
455 
456 	return true;
457 }
458 
459 bool
460 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
461 {
462 	struct nfsd_readlinkres *resp = rqstp->rq_resp;
463 	struct kvec *head = rqstp->rq_res.head;
464 
465 	if (!svcxdr_encode_stat(xdr, resp->status))
466 		return false;
467 	switch (resp->status) {
468 	case nfs_ok:
469 		if (xdr_stream_encode_u32(xdr, resp->len) < 0)
470 			return false;
471 		xdr_write_pages(xdr, &resp->page, 0, resp->len);
472 		if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
473 			return false;
474 		break;
475 	}
476 
477 	return true;
478 }
479 
480 bool
481 nfssvc_encode_readres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
482 {
483 	struct nfsd_readres *resp = rqstp->rq_resp;
484 	struct kvec *head = rqstp->rq_res.head;
485 
486 	if (!svcxdr_encode_stat(xdr, resp->status))
487 		return false;
488 	switch (resp->status) {
489 	case nfs_ok:
490 		if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
491 			return false;
492 		if (xdr_stream_encode_u32(xdr, resp->count) < 0)
493 			return false;
494 		xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
495 				resp->count);
496 		if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
497 			return false;
498 		break;
499 	}
500 
501 	return true;
502 }
503 
504 bool
505 nfssvc_encode_readdirres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
506 {
507 	struct nfsd_readdirres *resp = rqstp->rq_resp;
508 	struct xdr_buf *dirlist = &resp->dirlist;
509 
510 	if (!svcxdr_encode_stat(xdr, resp->status))
511 		return false;
512 	switch (resp->status) {
513 	case nfs_ok:
514 		xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
515 		/* no more entries */
516 		if (xdr_stream_encode_item_absent(xdr) < 0)
517 			return false;
518 		if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
519 			return false;
520 		break;
521 	}
522 
523 	return true;
524 }
525 
526 bool
527 nfssvc_encode_statfsres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
528 {
529 	struct nfsd_statfsres *resp = rqstp->rq_resp;
530 	struct kstatfs	*stat = &resp->stats;
531 	__be32 *p;
532 
533 	if (!svcxdr_encode_stat(xdr, resp->status))
534 		return false;
535 	switch (resp->status) {
536 	case nfs_ok:
537 		p = xdr_reserve_space(xdr, XDR_UNIT * 5);
538 		if (!p)
539 			return false;
540 		*p++ = cpu_to_be32(NFSSVC_MAXBLKSIZE_V2);
541 		*p++ = cpu_to_be32(stat->f_bsize);
542 		*p++ = cpu_to_be32(stat->f_blocks);
543 		*p++ = cpu_to_be32(stat->f_bfree);
544 		*p = cpu_to_be32(stat->f_bavail);
545 		break;
546 	}
547 
548 	return true;
549 }
550 
551 /**
552  * nfssvc_encode_nfscookie - Encode a directory offset cookie
553  * @resp: readdir result context
554  * @offset: offset cookie to encode
555  *
556  * The buffer space for the offset cookie has already been reserved
557  * by svcxdr_encode_entry_common().
558  */
559 void nfssvc_encode_nfscookie(struct nfsd_readdirres *resp, u32 offset)
560 {
561 	__be32 cookie = cpu_to_be32(offset);
562 
563 	if (!resp->cookie_offset)
564 		return;
565 
566 	write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
567 			       sizeof(cookie));
568 	resp->cookie_offset = 0;
569 }
570 
571 static bool
572 svcxdr_encode_entry_common(struct nfsd_readdirres *resp, const char *name,
573 			   int namlen, loff_t offset, u64 ino)
574 {
575 	struct xdr_buf *dirlist = &resp->dirlist;
576 	struct xdr_stream *xdr = &resp->xdr;
577 
578 	if (xdr_stream_encode_item_present(xdr) < 0)
579 		return false;
580 	/* fileid */
581 	if (xdr_stream_encode_u32(xdr, (u32)ino) < 0)
582 		return false;
583 	/* name */
584 	if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS2_MAXNAMLEN)) < 0)
585 		return false;
586 	/* cookie */
587 	resp->cookie_offset = dirlist->len;
588 	if (xdr_stream_encode_u32(xdr, ~0U) < 0)
589 		return false;
590 
591 	return true;
592 }
593 
594 /**
595  * nfssvc_encode_entry - encode one NFSv2 READDIR entry
596  * @data: directory context
597  * @name: name of the object to be encoded
598  * @namlen: length of that name, in bytes
599  * @offset: the offset of the previous entry
600  * @ino: the fileid of this entry
601  * @d_type: unused
602  *
603  * Return values:
604  *   %0: Entry was successfully encoded.
605  *   %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
606  *
607  * On exit, the following fields are updated:
608  *   - resp->xdr
609  *   - resp->common.err
610  *   - resp->cookie_offset
611  */
612 int nfssvc_encode_entry(void *data, const char *name, int namlen,
613 			loff_t offset, u64 ino, unsigned int d_type)
614 {
615 	struct readdir_cd *ccd = data;
616 	struct nfsd_readdirres *resp = container_of(ccd,
617 						    struct nfsd_readdirres,
618 						    common);
619 	unsigned int starting_length = resp->dirlist.len;
620 
621 	/* The offset cookie for the previous entry */
622 	nfssvc_encode_nfscookie(resp, offset);
623 
624 	if (!svcxdr_encode_entry_common(resp, name, namlen, offset, ino))
625 		goto out_toosmall;
626 
627 	xdr_commit_encode(&resp->xdr);
628 	resp->common.err = nfs_ok;
629 	return 0;
630 
631 out_toosmall:
632 	resp->cookie_offset = 0;
633 	resp->common.err = nfserr_toosmall;
634 	resp->dirlist.len = starting_length;
635 	return -EINVAL;
636 }
637 
638 /*
639  * XDR release functions
640  */
641 void nfssvc_release_attrstat(struct svc_rqst *rqstp)
642 {
643 	struct nfsd_attrstat *resp = rqstp->rq_resp;
644 
645 	fh_put(&resp->fh);
646 }
647 
648 void nfssvc_release_diropres(struct svc_rqst *rqstp)
649 {
650 	struct nfsd_diropres *resp = rqstp->rq_resp;
651 
652 	fh_put(&resp->fh);
653 }
654 
655 void nfssvc_release_readres(struct svc_rqst *rqstp)
656 {
657 	struct nfsd_readres *resp = rqstp->rq_resp;
658 
659 	fh_put(&resp->fh);
660 }
661