1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/callback_xdr.c
4  *
5  * Copyright (C) 2004 Trond Myklebust
6  *
7  * NFSv4 callback encode/decode procedures
8  */
9 #include <linux/kernel.h>
10 #include <linux/sunrpc/svc.h>
11 #include <linux/nfs4.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/ratelimit.h>
14 #include <linux/printk.h>
15 #include <linux/slab.h>
16 #include <linux/sunrpc/bc_xprt.h>
17 #include "nfs4_fs.h"
18 #include "callback.h"
19 #include "internal.h"
20 #include "nfs4session.h"
21 
22 #define CB_OP_TAGLEN_MAXSZ		(512)
23 #define CB_OP_HDR_RES_MAXSZ		(2 * 4) // opcode, status
24 #define CB_OP_GETATTR_BITMAP_MAXSZ	(4 * 4) // bitmap length, 3 bitmaps
25 #define CB_OP_GETATTR_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ + \
26 					 CB_OP_GETATTR_BITMAP_MAXSZ + \
27 					 /* change, size, ctime, mtime */\
28 					 (2 + 2 + 3 + 3) * 4)
29 #define CB_OP_RECALL_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ)
30 
31 #if defined(CONFIG_NFS_V4_1)
32 #define CB_OP_LAYOUTRECALL_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
33 #define CB_OP_DEVICENOTIFY_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
34 #define CB_OP_SEQUENCE_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ + \
35 					 NFS4_MAX_SESSIONID_LEN + \
36 					 (1 + 3) * 4) // seqid, 3 slotids
37 #define CB_OP_RECALLANY_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
38 #define CB_OP_RECALLSLOT_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
39 #define CB_OP_NOTIFY_LOCK_RES_MAXSZ	(CB_OP_HDR_RES_MAXSZ)
40 #endif /* CONFIG_NFS_V4_1 */
41 #ifdef CONFIG_NFS_V4_2
42 #define CB_OP_OFFLOAD_RES_MAXSZ		(CB_OP_HDR_RES_MAXSZ)
43 #endif /* CONFIG_NFS_V4_2 */
44 
45 #define NFSDBG_FACILITY NFSDBG_CALLBACK
46 
47 /* Internal error code */
48 #define NFS4ERR_RESOURCE_HDR	11050
49 
50 struct callback_op {
51 	__be32 (*process_op)(void *, void *, struct cb_process_state *);
52 	__be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *);
53 	__be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *,
54 			const void *);
55 	long res_maxsize;
56 };
57 
58 static struct callback_op callback_ops[];
59 
nfs4_callback_null(struct svc_rqst * rqstp)60 static __be32 nfs4_callback_null(struct svc_rqst *rqstp)
61 {
62 	return htonl(NFS4_OK);
63 }
64 
nfs4_decode_void(struct svc_rqst * rqstp,__be32 * p)65 static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p)
66 {
67 	return xdr_argsize_check(rqstp, p);
68 }
69 
nfs4_encode_void(struct svc_rqst * rqstp,__be32 * p)70 static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p)
71 {
72 	return xdr_ressize_check(rqstp, p);
73 }
74 
read_buf(struct xdr_stream * xdr,size_t nbytes)75 static __be32 *read_buf(struct xdr_stream *xdr, size_t nbytes)
76 {
77 	__be32 *p;
78 
79 	p = xdr_inline_decode(xdr, nbytes);
80 	if (unlikely(p == NULL))
81 		printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
82 	return p;
83 }
84 
decode_string(struct xdr_stream * xdr,unsigned int * len,const char ** str,size_t maxlen)85 static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
86 		const char **str, size_t maxlen)
87 {
88 	ssize_t err;
89 
90 	err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
91 	if (err < 0)
92 		return cpu_to_be32(NFS4ERR_RESOURCE);
93 	*len = err;
94 	return 0;
95 }
96 
decode_fh(struct xdr_stream * xdr,struct nfs_fh * fh)97 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
98 {
99 	__be32 *p;
100 
101 	p = read_buf(xdr, 4);
102 	if (unlikely(p == NULL))
103 		return htonl(NFS4ERR_RESOURCE);
104 	fh->size = ntohl(*p);
105 	if (fh->size > NFS4_FHSIZE)
106 		return htonl(NFS4ERR_BADHANDLE);
107 	p = read_buf(xdr, fh->size);
108 	if (unlikely(p == NULL))
109 		return htonl(NFS4ERR_RESOURCE);
110 	memcpy(&fh->data[0], p, fh->size);
111 	memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
112 	return 0;
113 }
114 
decode_bitmap(struct xdr_stream * xdr,uint32_t * bitmap)115 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
116 {
117 	__be32 *p;
118 	unsigned int attrlen;
119 
120 	p = read_buf(xdr, 4);
121 	if (unlikely(p == NULL))
122 		return htonl(NFS4ERR_RESOURCE);
123 	attrlen = ntohl(*p);
124 	p = read_buf(xdr, attrlen << 2);
125 	if (unlikely(p == NULL))
126 		return htonl(NFS4ERR_RESOURCE);
127 	if (likely(attrlen > 0))
128 		bitmap[0] = ntohl(*p++);
129 	if (attrlen > 1)
130 		bitmap[1] = ntohl(*p);
131 	return 0;
132 }
133 
decode_stateid(struct xdr_stream * xdr,nfs4_stateid * stateid)134 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
135 {
136 	__be32 *p;
137 
138 	p = read_buf(xdr, NFS4_STATEID_SIZE);
139 	if (unlikely(p == NULL))
140 		return htonl(NFS4ERR_RESOURCE);
141 	memcpy(stateid->data, p, NFS4_STATEID_SIZE);
142 	return 0;
143 }
144 
decode_delegation_stateid(struct xdr_stream * xdr,nfs4_stateid * stateid)145 static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
146 {
147 	stateid->type = NFS4_DELEGATION_STATEID_TYPE;
148 	return decode_stateid(xdr, stateid);
149 }
150 
decode_compound_hdr_arg(struct xdr_stream * xdr,struct cb_compound_hdr_arg * hdr)151 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
152 {
153 	__be32 *p;
154 	__be32 status;
155 
156 	status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
157 	if (unlikely(status != 0))
158 		return status;
159 	p = read_buf(xdr, 12);
160 	if (unlikely(p == NULL))
161 		return htonl(NFS4ERR_RESOURCE);
162 	hdr->minorversion = ntohl(*p++);
163 	/* Check for minor version support */
164 	if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
165 		hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
166 	} else {
167 		pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
168 			"illegal minor version %u!\n",
169 			__func__, hdr->minorversion);
170 		return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
171 	}
172 	hdr->nops = ntohl(*p);
173 	return 0;
174 }
175 
decode_op_hdr(struct xdr_stream * xdr,unsigned int * op)176 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
177 {
178 	__be32 *p;
179 	p = read_buf(xdr, 4);
180 	if (unlikely(p == NULL))
181 		return htonl(NFS4ERR_RESOURCE_HDR);
182 	*op = ntohl(*p);
183 	return 0;
184 }
185 
decode_getattr_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)186 static __be32 decode_getattr_args(struct svc_rqst *rqstp,
187 		struct xdr_stream *xdr, void *argp)
188 {
189 	struct cb_getattrargs *args = argp;
190 	__be32 status;
191 
192 	status = decode_fh(xdr, &args->fh);
193 	if (unlikely(status != 0))
194 		return status;
195 	return decode_bitmap(xdr, args->bitmap);
196 }
197 
decode_recall_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)198 static __be32 decode_recall_args(struct svc_rqst *rqstp,
199 		struct xdr_stream *xdr, void *argp)
200 {
201 	struct cb_recallargs *args = argp;
202 	__be32 *p;
203 	__be32 status;
204 
205 	status = decode_delegation_stateid(xdr, &args->stateid);
206 	if (unlikely(status != 0))
207 		return status;
208 	p = read_buf(xdr, 4);
209 	if (unlikely(p == NULL))
210 		return htonl(NFS4ERR_RESOURCE);
211 	args->truncate = ntohl(*p);
212 	return decode_fh(xdr, &args->fh);
213 }
214 
215 #if defined(CONFIG_NFS_V4_1)
decode_layout_stateid(struct xdr_stream * xdr,nfs4_stateid * stateid)216 static __be32 decode_layout_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
217 {
218 	stateid->type = NFS4_LAYOUT_STATEID_TYPE;
219 	return decode_stateid(xdr, stateid);
220 }
221 
decode_layoutrecall_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)222 static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
223 				       struct xdr_stream *xdr, void *argp)
224 {
225 	struct cb_layoutrecallargs *args = argp;
226 	__be32 *p;
227 	__be32 status = 0;
228 	uint32_t iomode;
229 
230 	p = read_buf(xdr, 4 * sizeof(uint32_t));
231 	if (unlikely(p == NULL))
232 		return htonl(NFS4ERR_BADXDR);
233 
234 	args->cbl_layout_type = ntohl(*p++);
235 	/* Depite the spec's xdr, iomode really belongs in the FILE switch,
236 	 * as it is unusable and ignored with the other types.
237 	 */
238 	iomode = ntohl(*p++);
239 	args->cbl_layoutchanged = ntohl(*p++);
240 	args->cbl_recall_type = ntohl(*p++);
241 
242 	if (args->cbl_recall_type == RETURN_FILE) {
243 		args->cbl_range.iomode = iomode;
244 		status = decode_fh(xdr, &args->cbl_fh);
245 		if (unlikely(status != 0))
246 			return status;
247 
248 		p = read_buf(xdr, 2 * sizeof(uint64_t));
249 		if (unlikely(p == NULL))
250 			return htonl(NFS4ERR_BADXDR);
251 		p = xdr_decode_hyper(p, &args->cbl_range.offset);
252 		p = xdr_decode_hyper(p, &args->cbl_range.length);
253 		return decode_layout_stateid(xdr, &args->cbl_stateid);
254 	} else if (args->cbl_recall_type == RETURN_FSID) {
255 		p = read_buf(xdr, 2 * sizeof(uint64_t));
256 		if (unlikely(p == NULL))
257 			return htonl(NFS4ERR_BADXDR);
258 		p = xdr_decode_hyper(p, &args->cbl_fsid.major);
259 		p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
260 	} else if (args->cbl_recall_type != RETURN_ALL)
261 		return htonl(NFS4ERR_BADXDR);
262 	return 0;
263 }
264 
265 static
decode_devicenotify_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)266 __be32 decode_devicenotify_args(struct svc_rqst *rqstp,
267 				struct xdr_stream *xdr,
268 				void *argp)
269 {
270 	struct cb_devicenotifyargs *args = argp;
271 	uint32_t tmp, n, i;
272 	__be32 *p;
273 	__be32 status = 0;
274 
275 	/* Num of device notifications */
276 	p = read_buf(xdr, sizeof(uint32_t));
277 	if (unlikely(p == NULL)) {
278 		status = htonl(NFS4ERR_BADXDR);
279 		goto out;
280 	}
281 	n = ntohl(*p++);
282 	if (n == 0)
283 		goto out;
284 
285 	args->devs = kmalloc_array(n, sizeof(*args->devs), GFP_KERNEL);
286 	if (!args->devs) {
287 		status = htonl(NFS4ERR_DELAY);
288 		goto out;
289 	}
290 
291 	/* Decode each dev notification */
292 	for (i = 0; i < n; i++) {
293 		struct cb_devicenotifyitem *dev = &args->devs[i];
294 
295 		p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
296 		if (unlikely(p == NULL)) {
297 			status = htonl(NFS4ERR_BADXDR);
298 			goto err;
299 		}
300 
301 		tmp = ntohl(*p++);	/* bitmap size */
302 		if (tmp != 1) {
303 			status = htonl(NFS4ERR_INVAL);
304 			goto err;
305 		}
306 		dev->cbd_notify_type = ntohl(*p++);
307 		if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
308 		    dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
309 			status = htonl(NFS4ERR_INVAL);
310 			goto err;
311 		}
312 
313 		tmp = ntohl(*p++);	/* opaque size */
314 		if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
315 		     (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
316 		    ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
317 		     (tmp != NFS4_DEVICEID4_SIZE + 4))) {
318 			status = htonl(NFS4ERR_INVAL);
319 			goto err;
320 		}
321 		dev->cbd_layout_type = ntohl(*p++);
322 		memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
323 		p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
324 
325 		if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
326 			p = read_buf(xdr, sizeof(uint32_t));
327 			if (unlikely(p == NULL)) {
328 				status = htonl(NFS4ERR_BADXDR);
329 				goto err;
330 			}
331 			dev->cbd_immediate = ntohl(*p++);
332 		} else {
333 			dev->cbd_immediate = 0;
334 		}
335 
336 		dprintk("%s: type %d layout 0x%x immediate %d\n",
337 			__func__, dev->cbd_notify_type, dev->cbd_layout_type,
338 			dev->cbd_immediate);
339 	}
340 	args->ndevs = n;
341 	dprintk("%s: ndevs %d\n", __func__, args->ndevs);
342 	return 0;
343 err:
344 	kfree(args->devs);
345 out:
346 	args->devs = NULL;
347 	args->ndevs = 0;
348 	dprintk("%s: status %d ndevs %d\n",
349 		__func__, ntohl(status), args->ndevs);
350 	return status;
351 }
352 
decode_sessionid(struct xdr_stream * xdr,struct nfs4_sessionid * sid)353 static __be32 decode_sessionid(struct xdr_stream *xdr,
354 				 struct nfs4_sessionid *sid)
355 {
356 	__be32 *p;
357 
358 	p = read_buf(xdr, NFS4_MAX_SESSIONID_LEN);
359 	if (unlikely(p == NULL))
360 		return htonl(NFS4ERR_RESOURCE);
361 
362 	memcpy(sid->data, p, NFS4_MAX_SESSIONID_LEN);
363 	return 0;
364 }
365 
decode_rc_list(struct xdr_stream * xdr,struct referring_call_list * rc_list)366 static __be32 decode_rc_list(struct xdr_stream *xdr,
367 			       struct referring_call_list *rc_list)
368 {
369 	__be32 *p;
370 	int i;
371 	__be32 status;
372 
373 	status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
374 	if (status)
375 		goto out;
376 
377 	status = htonl(NFS4ERR_RESOURCE);
378 	p = read_buf(xdr, sizeof(uint32_t));
379 	if (unlikely(p == NULL))
380 		goto out;
381 
382 	rc_list->rcl_nrefcalls = ntohl(*p++);
383 	if (rc_list->rcl_nrefcalls) {
384 		p = read_buf(xdr,
385 			     rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
386 		if (unlikely(p == NULL))
387 			goto out;
388 		rc_list->rcl_refcalls = kmalloc_array(rc_list->rcl_nrefcalls,
389 						sizeof(*rc_list->rcl_refcalls),
390 						GFP_KERNEL);
391 		if (unlikely(rc_list->rcl_refcalls == NULL))
392 			goto out;
393 		for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
394 			rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
395 			rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
396 		}
397 	}
398 	status = 0;
399 
400 out:
401 	return status;
402 }
403 
decode_cb_sequence_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)404 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
405 					struct xdr_stream *xdr,
406 					void *argp)
407 {
408 	struct cb_sequenceargs *args = argp;
409 	__be32 *p;
410 	int i;
411 	__be32 status;
412 
413 	status = decode_sessionid(xdr, &args->csa_sessionid);
414 	if (status)
415 		return status;
416 
417 	p = read_buf(xdr, 5 * sizeof(uint32_t));
418 	if (unlikely(p == NULL))
419 		return htonl(NFS4ERR_RESOURCE);
420 
421 	args->csa_addr = svc_addr(rqstp);
422 	args->csa_sequenceid = ntohl(*p++);
423 	args->csa_slotid = ntohl(*p++);
424 	args->csa_highestslotid = ntohl(*p++);
425 	args->csa_cachethis = ntohl(*p++);
426 	args->csa_nrclists = ntohl(*p++);
427 	args->csa_rclists = NULL;
428 	if (args->csa_nrclists) {
429 		args->csa_rclists = kmalloc_array(args->csa_nrclists,
430 						  sizeof(*args->csa_rclists),
431 						  GFP_KERNEL);
432 		if (unlikely(args->csa_rclists == NULL))
433 			return htonl(NFS4ERR_RESOURCE);
434 
435 		for (i = 0; i < args->csa_nrclists; i++) {
436 			status = decode_rc_list(xdr, &args->csa_rclists[i]);
437 			if (status) {
438 				args->csa_nrclists = i;
439 				goto out_free;
440 			}
441 		}
442 	}
443 	return 0;
444 
445 out_free:
446 	for (i = 0; i < args->csa_nrclists; i++)
447 		kfree(args->csa_rclists[i].rcl_refcalls);
448 	kfree(args->csa_rclists);
449 	return status;
450 }
451 
decode_recallany_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)452 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
453 				      struct xdr_stream *xdr,
454 				      void *argp)
455 {
456 	struct cb_recallanyargs *args = argp;
457 	uint32_t bitmap[2];
458 	__be32 *p, status;
459 
460 	p = read_buf(xdr, 4);
461 	if (unlikely(p == NULL))
462 		return htonl(NFS4ERR_BADXDR);
463 	args->craa_objs_to_keep = ntohl(*p++);
464 	status = decode_bitmap(xdr, bitmap);
465 	if (unlikely(status))
466 		return status;
467 	args->craa_type_mask = bitmap[0];
468 
469 	return 0;
470 }
471 
decode_recallslot_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)472 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
473 					struct xdr_stream *xdr,
474 					void *argp)
475 {
476 	struct cb_recallslotargs *args = argp;
477 	__be32 *p;
478 
479 	p = read_buf(xdr, 4);
480 	if (unlikely(p == NULL))
481 		return htonl(NFS4ERR_BADXDR);
482 	args->crsa_target_highest_slotid = ntohl(*p++);
483 	return 0;
484 }
485 
decode_lockowner(struct xdr_stream * xdr,struct cb_notify_lock_args * args)486 static __be32 decode_lockowner(struct xdr_stream *xdr, struct cb_notify_lock_args *args)
487 {
488 	__be32		*p;
489 	unsigned int	len;
490 
491 	p = read_buf(xdr, 12);
492 	if (unlikely(p == NULL))
493 		return htonl(NFS4ERR_BADXDR);
494 
495 	p = xdr_decode_hyper(p, &args->cbnl_owner.clientid);
496 	len = be32_to_cpu(*p);
497 
498 	p = read_buf(xdr, len);
499 	if (unlikely(p == NULL))
500 		return htonl(NFS4ERR_BADXDR);
501 
502 	/* Only try to decode if the length is right */
503 	if (len == 20) {
504 		p += 2;	/* skip "lock id:" */
505 		args->cbnl_owner.s_dev = be32_to_cpu(*p++);
506 		xdr_decode_hyper(p, &args->cbnl_owner.id);
507 		args->cbnl_valid = true;
508 	} else {
509 		args->cbnl_owner.s_dev = 0;
510 		args->cbnl_owner.id = 0;
511 		args->cbnl_valid = false;
512 	}
513 	return 0;
514 }
515 
decode_notify_lock_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * argp)516 static __be32 decode_notify_lock_args(struct svc_rqst *rqstp,
517 		struct xdr_stream *xdr, void *argp)
518 {
519 	struct cb_notify_lock_args *args = argp;
520 	__be32 status;
521 
522 	status = decode_fh(xdr, &args->cbnl_fh);
523 	if (unlikely(status != 0))
524 		return status;
525 	return decode_lockowner(xdr, args);
526 }
527 
528 #endif /* CONFIG_NFS_V4_1 */
529 #ifdef CONFIG_NFS_V4_2
decode_write_response(struct xdr_stream * xdr,struct cb_offloadargs * args)530 static __be32 decode_write_response(struct xdr_stream *xdr,
531 					struct cb_offloadargs *args)
532 {
533 	__be32 *p;
534 
535 	/* skip the always zero field */
536 	p = read_buf(xdr, 4);
537 	if (unlikely(!p))
538 		goto out;
539 	p++;
540 
541 	/* decode count, stable_how, verifier */
542 	p = xdr_inline_decode(xdr, 8 + 4);
543 	if (unlikely(!p))
544 		goto out;
545 	p = xdr_decode_hyper(p, &args->wr_count);
546 	args->wr_writeverf.committed = be32_to_cpup(p);
547 	p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE);
548 	if (likely(p)) {
549 		memcpy(&args->wr_writeverf.verifier.data[0], p,
550 			NFS4_VERIFIER_SIZE);
551 		return 0;
552 	}
553 out:
554 	return htonl(NFS4ERR_RESOURCE);
555 }
556 
decode_offload_args(struct svc_rqst * rqstp,struct xdr_stream * xdr,void * data)557 static __be32 decode_offload_args(struct svc_rqst *rqstp,
558 					struct xdr_stream *xdr,
559 					void *data)
560 {
561 	struct cb_offloadargs *args = data;
562 	__be32 *p;
563 	__be32 status;
564 
565 	/* decode fh */
566 	status = decode_fh(xdr, &args->coa_fh);
567 	if (unlikely(status != 0))
568 		return status;
569 
570 	/* decode stateid */
571 	status = decode_stateid(xdr, &args->coa_stateid);
572 	if (unlikely(status != 0))
573 		return status;
574 
575 	/* decode status */
576 	p = read_buf(xdr, 4);
577 	if (unlikely(!p))
578 		goto out;
579 	args->error = ntohl(*p++);
580 	if (!args->error) {
581 		status = decode_write_response(xdr, args);
582 		if (unlikely(status != 0))
583 			return status;
584 	} else {
585 		p = xdr_inline_decode(xdr, 8);
586 		if (unlikely(!p))
587 			goto out;
588 		p = xdr_decode_hyper(p, &args->wr_count);
589 	}
590 	return 0;
591 out:
592 	return htonl(NFS4ERR_RESOURCE);
593 }
594 #endif /* CONFIG_NFS_V4_2 */
encode_string(struct xdr_stream * xdr,unsigned int len,const char * str)595 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
596 {
597 	if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
598 		return cpu_to_be32(NFS4ERR_RESOURCE);
599 	return 0;
600 }
601 
encode_attr_bitmap(struct xdr_stream * xdr,const uint32_t * bitmap,size_t sz)602 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz)
603 {
604 	if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0)
605 		return cpu_to_be32(NFS4ERR_RESOURCE);
606 	return 0;
607 }
608 
encode_attr_change(struct xdr_stream * xdr,const uint32_t * bitmap,uint64_t change)609 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
610 {
611 	__be32 *p;
612 
613 	if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
614 		return 0;
615 	p = xdr_reserve_space(xdr, 8);
616 	if (unlikely(!p))
617 		return htonl(NFS4ERR_RESOURCE);
618 	p = xdr_encode_hyper(p, change);
619 	return 0;
620 }
621 
encode_attr_size(struct xdr_stream * xdr,const uint32_t * bitmap,uint64_t size)622 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
623 {
624 	__be32 *p;
625 
626 	if (!(bitmap[0] & FATTR4_WORD0_SIZE))
627 		return 0;
628 	p = xdr_reserve_space(xdr, 8);
629 	if (unlikely(!p))
630 		return htonl(NFS4ERR_RESOURCE);
631 	p = xdr_encode_hyper(p, size);
632 	return 0;
633 }
634 
encode_attr_time(struct xdr_stream * xdr,const struct timespec * time)635 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
636 {
637 	__be32 *p;
638 
639 	p = xdr_reserve_space(xdr, 12);
640 	if (unlikely(!p))
641 		return htonl(NFS4ERR_RESOURCE);
642 	p = xdr_encode_hyper(p, time->tv_sec);
643 	*p = htonl(time->tv_nsec);
644 	return 0;
645 }
646 
encode_attr_ctime(struct xdr_stream * xdr,const uint32_t * bitmap,const struct timespec * time)647 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
648 {
649 	if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
650 		return 0;
651 	return encode_attr_time(xdr,time);
652 }
653 
encode_attr_mtime(struct xdr_stream * xdr,const uint32_t * bitmap,const struct timespec * time)654 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
655 {
656 	if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
657 		return 0;
658 	return encode_attr_time(xdr,time);
659 }
660 
encode_compound_hdr_res(struct xdr_stream * xdr,struct cb_compound_hdr_res * hdr)661 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
662 {
663 	__be32 status;
664 
665 	hdr->status = xdr_reserve_space(xdr, 4);
666 	if (unlikely(hdr->status == NULL))
667 		return htonl(NFS4ERR_RESOURCE);
668 	status = encode_string(xdr, hdr->taglen, hdr->tag);
669 	if (unlikely(status != 0))
670 		return status;
671 	hdr->nops = xdr_reserve_space(xdr, 4);
672 	if (unlikely(hdr->nops == NULL))
673 		return htonl(NFS4ERR_RESOURCE);
674 	return 0;
675 }
676 
encode_op_hdr(struct xdr_stream * xdr,uint32_t op,__be32 res)677 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
678 {
679 	__be32 *p;
680 
681 	p = xdr_reserve_space(xdr, 8);
682 	if (unlikely(p == NULL))
683 		return htonl(NFS4ERR_RESOURCE_HDR);
684 	*p++ = htonl(op);
685 	*p = res;
686 	return 0;
687 }
688 
encode_getattr_res(struct svc_rqst * rqstp,struct xdr_stream * xdr,const void * resp)689 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
690 		const void *resp)
691 {
692 	const struct cb_getattrres *res = resp;
693 	__be32 *savep = NULL;
694 	__be32 status = res->status;
695 
696 	if (unlikely(status != 0))
697 		goto out;
698 	status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap));
699 	if (unlikely(status != 0))
700 		goto out;
701 	status = cpu_to_be32(NFS4ERR_RESOURCE);
702 	savep = xdr_reserve_space(xdr, sizeof(*savep));
703 	if (unlikely(!savep))
704 		goto out;
705 	status = encode_attr_change(xdr, res->bitmap, res->change_attr);
706 	if (unlikely(status != 0))
707 		goto out;
708 	status = encode_attr_size(xdr, res->bitmap, res->size);
709 	if (unlikely(status != 0))
710 		goto out;
711 	status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
712 	if (unlikely(status != 0))
713 		goto out;
714 	status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
715 	*savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
716 out:
717 	return status;
718 }
719 
720 #if defined(CONFIG_NFS_V4_1)
721 
encode_sessionid(struct xdr_stream * xdr,const struct nfs4_sessionid * sid)722 static __be32 encode_sessionid(struct xdr_stream *xdr,
723 				 const struct nfs4_sessionid *sid)
724 {
725 	__be32 *p;
726 
727 	p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
728 	if (unlikely(p == NULL))
729 		return htonl(NFS4ERR_RESOURCE);
730 
731 	memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
732 	return 0;
733 }
734 
encode_cb_sequence_res(struct svc_rqst * rqstp,struct xdr_stream * xdr,const void * resp)735 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
736 				       struct xdr_stream *xdr,
737 				       const void *resp)
738 {
739 	const struct cb_sequenceres *res = resp;
740 	__be32 *p;
741 	__be32 status = res->csr_status;
742 
743 	if (unlikely(status != 0))
744 		return status;
745 
746 	status = encode_sessionid(xdr, &res->csr_sessionid);
747 	if (status)
748 		return status;
749 
750 	p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
751 	if (unlikely(p == NULL))
752 		return htonl(NFS4ERR_RESOURCE);
753 
754 	*p++ = htonl(res->csr_sequenceid);
755 	*p++ = htonl(res->csr_slotid);
756 	*p++ = htonl(res->csr_highestslotid);
757 	*p++ = htonl(res->csr_target_highestslotid);
758 	return 0;
759 }
760 
761 static __be32
preprocess_nfs41_op(int nop,unsigned int op_nr,struct callback_op ** op)762 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
763 {
764 	if (op_nr == OP_CB_SEQUENCE) {
765 		if (nop != 0)
766 			return htonl(NFS4ERR_SEQUENCE_POS);
767 	} else {
768 		if (nop == 0)
769 			return htonl(NFS4ERR_OP_NOT_IN_SESSION);
770 	}
771 
772 	switch (op_nr) {
773 	case OP_CB_GETATTR:
774 	case OP_CB_RECALL:
775 	case OP_CB_SEQUENCE:
776 	case OP_CB_RECALL_ANY:
777 	case OP_CB_RECALL_SLOT:
778 	case OP_CB_LAYOUTRECALL:
779 	case OP_CB_NOTIFY_DEVICEID:
780 	case OP_CB_NOTIFY_LOCK:
781 		*op = &callback_ops[op_nr];
782 		break;
783 
784 	case OP_CB_NOTIFY:
785 	case OP_CB_PUSH_DELEG:
786 	case OP_CB_RECALLABLE_OBJ_AVAIL:
787 	case OP_CB_WANTS_CANCELLED:
788 		return htonl(NFS4ERR_NOTSUPP);
789 
790 	default:
791 		return htonl(NFS4ERR_OP_ILLEGAL);
792 	}
793 
794 	return htonl(NFS_OK);
795 }
796 
nfs4_callback_free_slot(struct nfs4_session * session,struct nfs4_slot * slot)797 static void nfs4_callback_free_slot(struct nfs4_session *session,
798 		struct nfs4_slot *slot)
799 {
800 	struct nfs4_slot_table *tbl = &session->bc_slot_table;
801 
802 	spin_lock(&tbl->slot_tbl_lock);
803 	/*
804 	 * Let the state manager know callback processing done.
805 	 * A single slot, so highest used slotid is either 0 or -1
806 	 */
807 	nfs4_free_slot(tbl, slot);
808 	spin_unlock(&tbl->slot_tbl_lock);
809 }
810 
nfs4_cb_free_slot(struct cb_process_state * cps)811 static void nfs4_cb_free_slot(struct cb_process_state *cps)
812 {
813 	if (cps->slot) {
814 		nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
815 		cps->slot = NULL;
816 	}
817 }
818 
819 #else /* CONFIG_NFS_V4_1 */
820 
821 static __be32
preprocess_nfs41_op(int nop,unsigned int op_nr,struct callback_op ** op)822 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
823 {
824 	return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
825 }
826 
nfs4_cb_free_slot(struct cb_process_state * cps)827 static void nfs4_cb_free_slot(struct cb_process_state *cps)
828 {
829 }
830 #endif /* CONFIG_NFS_V4_1 */
831 
832 #ifdef CONFIG_NFS_V4_2
833 static __be32
preprocess_nfs42_op(int nop,unsigned int op_nr,struct callback_op ** op)834 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
835 {
836 	__be32 status = preprocess_nfs41_op(nop, op_nr, op);
837 	if (status != htonl(NFS4ERR_OP_ILLEGAL))
838 		return status;
839 
840 	if (op_nr == OP_CB_OFFLOAD) {
841 		*op = &callback_ops[op_nr];
842 		return htonl(NFS_OK);
843 	} else
844 		return htonl(NFS4ERR_NOTSUPP);
845 	return htonl(NFS4ERR_OP_ILLEGAL);
846 }
847 #else /* CONFIG_NFS_V4_2 */
848 static __be32
preprocess_nfs42_op(int nop,unsigned int op_nr,struct callback_op ** op)849 preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
850 {
851 	return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
852 }
853 #endif /* CONFIG_NFS_V4_2 */
854 
855 static __be32
preprocess_nfs4_op(unsigned int op_nr,struct callback_op ** op)856 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
857 {
858 	switch (op_nr) {
859 	case OP_CB_GETATTR:
860 	case OP_CB_RECALL:
861 		*op = &callback_ops[op_nr];
862 		break;
863 	default:
864 		return htonl(NFS4ERR_OP_ILLEGAL);
865 	}
866 
867 	return htonl(NFS_OK);
868 }
869 
process_op(int nop,struct svc_rqst * rqstp,struct xdr_stream * xdr_in,void * argp,struct xdr_stream * xdr_out,void * resp,struct cb_process_state * cps)870 static __be32 process_op(int nop, struct svc_rqst *rqstp,
871 		struct xdr_stream *xdr_in, void *argp,
872 		struct xdr_stream *xdr_out, void *resp,
873 		struct cb_process_state *cps)
874 {
875 	struct callback_op *op = &callback_ops[0];
876 	unsigned int op_nr;
877 	__be32 status;
878 	long maxlen;
879 	__be32 res;
880 
881 	status = decode_op_hdr(xdr_in, &op_nr);
882 	if (unlikely(status))
883 		return status;
884 
885 	switch (cps->minorversion) {
886 	case 0:
887 		status = preprocess_nfs4_op(op_nr, &op);
888 		break;
889 	case 1:
890 		status = preprocess_nfs41_op(nop, op_nr, &op);
891 		break;
892 	case 2:
893 		status = preprocess_nfs42_op(nop, op_nr, &op);
894 		break;
895 	default:
896 		status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
897 	}
898 
899 	if (status == htonl(NFS4ERR_OP_ILLEGAL))
900 		op_nr = OP_CB_ILLEGAL;
901 	if (status)
902 		goto encode_hdr;
903 
904 	if (cps->drc_status) {
905 		status = cps->drc_status;
906 		goto encode_hdr;
907 	}
908 
909 	maxlen = xdr_out->end - xdr_out->p;
910 	if (maxlen > 0 && maxlen < PAGE_SIZE) {
911 		status = op->decode_args(rqstp, xdr_in, argp);
912 		if (likely(status == 0))
913 			status = op->process_op(argp, resp, cps);
914 	} else
915 		status = htonl(NFS4ERR_RESOURCE);
916 
917 encode_hdr:
918 	res = encode_op_hdr(xdr_out, op_nr, status);
919 	if (unlikely(res))
920 		return res;
921 	if (op->encode_res != NULL && status == 0)
922 		status = op->encode_res(rqstp, xdr_out, resp);
923 	return status;
924 }
925 
926 /*
927  * Decode, process and encode a COMPOUND
928  */
nfs4_callback_compound(struct svc_rqst * rqstp)929 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
930 {
931 	struct cb_compound_hdr_arg hdr_arg = { 0 };
932 	struct cb_compound_hdr_res hdr_res = { NULL };
933 	struct xdr_stream xdr_in, xdr_out;
934 	__be32 *p, status;
935 	struct cb_process_state cps = {
936 		.drc_status = 0,
937 		.clp = NULL,
938 		.net = SVC_NET(rqstp),
939 	};
940 	unsigned int nops = 0;
941 
942 	xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
943 
944 	p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
945 	xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
946 
947 	status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
948 	if (status == htonl(NFS4ERR_RESOURCE))
949 		return rpc_garbage_args;
950 
951 	if (hdr_arg.minorversion == 0) {
952 		cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
953 		if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp)) {
954 			if (cps.clp)
955 				nfs_put_client(cps.clp);
956 			goto out_invalidcred;
957 		}
958 	}
959 
960 	cps.minorversion = hdr_arg.minorversion;
961 	hdr_res.taglen = hdr_arg.taglen;
962 	hdr_res.tag = hdr_arg.tag;
963 	if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) {
964 		if (cps.clp)
965 			nfs_put_client(cps.clp);
966 		return rpc_system_err;
967 	}
968 	while (status == 0 && nops != hdr_arg.nops) {
969 		status = process_op(nops, rqstp, &xdr_in,
970 				    rqstp->rq_argp, &xdr_out, rqstp->rq_resp,
971 				    &cps);
972 		nops++;
973 	}
974 
975 	/* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
976 	* resource error in cb_compound status without returning op */
977 	if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
978 		status = htonl(NFS4ERR_RESOURCE);
979 		nops--;
980 	}
981 
982 	*hdr_res.status = status;
983 	*hdr_res.nops = htonl(nops);
984 	nfs4_cb_free_slot(&cps);
985 	nfs_put_client(cps.clp);
986 	return rpc_success;
987 
988 out_invalidcred:
989 	pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
990 	return svc_return_autherr(rqstp, rpc_autherr_badcred);
991 }
992 
993 /*
994  * Define NFS4 callback COMPOUND ops.
995  */
996 static struct callback_op callback_ops[] = {
997 	[0] = {
998 		.res_maxsize = CB_OP_HDR_RES_MAXSZ,
999 	},
1000 	[OP_CB_GETATTR] = {
1001 		.process_op = nfs4_callback_getattr,
1002 		.decode_args = decode_getattr_args,
1003 		.encode_res = encode_getattr_res,
1004 		.res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
1005 	},
1006 	[OP_CB_RECALL] = {
1007 		.process_op = nfs4_callback_recall,
1008 		.decode_args = decode_recall_args,
1009 		.res_maxsize = CB_OP_RECALL_RES_MAXSZ,
1010 	},
1011 #if defined(CONFIG_NFS_V4_1)
1012 	[OP_CB_LAYOUTRECALL] = {
1013 		.process_op = nfs4_callback_layoutrecall,
1014 		.decode_args = decode_layoutrecall_args,
1015 		.res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
1016 	},
1017 	[OP_CB_NOTIFY_DEVICEID] = {
1018 		.process_op = nfs4_callback_devicenotify,
1019 		.decode_args = decode_devicenotify_args,
1020 		.res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
1021 	},
1022 	[OP_CB_SEQUENCE] = {
1023 		.process_op = nfs4_callback_sequence,
1024 		.decode_args = decode_cb_sequence_args,
1025 		.encode_res = encode_cb_sequence_res,
1026 		.res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
1027 	},
1028 	[OP_CB_RECALL_ANY] = {
1029 		.process_op = nfs4_callback_recallany,
1030 		.decode_args = decode_recallany_args,
1031 		.res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
1032 	},
1033 	[OP_CB_RECALL_SLOT] = {
1034 		.process_op = nfs4_callback_recallslot,
1035 		.decode_args = decode_recallslot_args,
1036 		.res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
1037 	},
1038 	[OP_CB_NOTIFY_LOCK] = {
1039 		.process_op = nfs4_callback_notify_lock,
1040 		.decode_args = decode_notify_lock_args,
1041 		.res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
1042 	},
1043 #endif /* CONFIG_NFS_V4_1 */
1044 #ifdef CONFIG_NFS_V4_2
1045 	[OP_CB_OFFLOAD] = {
1046 		.process_op = nfs4_callback_offload,
1047 		.decode_args = decode_offload_args,
1048 		.res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ,
1049 	},
1050 #endif /* CONFIG_NFS_V4_2 */
1051 };
1052 
1053 /*
1054  * Define NFS4 callback procedures
1055  */
1056 static const struct svc_procedure nfs4_callback_procedures1[] = {
1057 	[CB_NULL] = {
1058 		.pc_func = nfs4_callback_null,
1059 		.pc_decode = nfs4_decode_void,
1060 		.pc_encode = nfs4_encode_void,
1061 		.pc_xdrressize = 1,
1062 	},
1063 	[CB_COMPOUND] = {
1064 		.pc_func = nfs4_callback_compound,
1065 		.pc_encode = nfs4_encode_void,
1066 		.pc_argsize = 256,
1067 		.pc_ressize = 256,
1068 		.pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
1069 	}
1070 };
1071 
1072 static unsigned int nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)];
1073 const struct svc_version nfs4_callback_version1 = {
1074 	.vs_vers = 1,
1075 	.vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1076 	.vs_proc = nfs4_callback_procedures1,
1077 	.vs_count = nfs4_callback_count1,
1078 	.vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1079 	.vs_dispatch = NULL,
1080 	.vs_hidden = true,
1081 	.vs_need_cong_ctrl = true,
1082 };
1083 
1084 static unsigned int nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)];
1085 const struct svc_version nfs4_callback_version4 = {
1086 	.vs_vers = 4,
1087 	.vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
1088 	.vs_proc = nfs4_callback_procedures1,
1089 	.vs_count = nfs4_callback_count4,
1090 	.vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
1091 	.vs_dispatch = NULL,
1092 	.vs_hidden = true,
1093 	.vs_need_cong_ctrl = true,
1094 };
1095