• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
4  *	Separated from fs stuff by Arnd Bergmann <arnd@arndb.de>
5  *
6  * Copyright (C) 1997-2000  Jakub Jelinek  (jakub@redhat.com)
7  * Copyright (C) 1998  Eddie C. Dost  (ecd@skynet.be)
8  * Copyright (C) 2001,2002  Andi Kleen, SuSE Labs
9  * Copyright (C) 2003       Pavel Machek (pavel@ucw.cz)
10  * Copyright (C) 2005       Philippe De Muyter (phdm@macqel.be)
11  * Copyright (C) 2008       Hans Verkuil <hverkuil@xs4all.nl>
12  *
13  * These routines maintain argument size conversion between 32bit and 64bit
14  * ioctls.
15  */
16 
17 #include <linux/compat.h>
18 #include <linux/module.h>
19 #include <linux/videodev2.h>
20 #include <linux/v4l2-subdev.h>
21 #include <media/v4l2-dev.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-ioctl.h>
25 
26 /**
27  * assign_in_user() - Copy from one __user var to another one
28  *
29  * @to: __user var where data will be stored
30  * @from: __user var where data will be retrieved.
31  *
32  * As this code very often needs to allocate userspace memory, it is easier
33  * to have a macro that will do both get_user() and put_user() at once.
34  *
35  * This function complements the macros defined at asm-generic/uaccess.h.
36  * It uses the same argument order as copy_in_user()
37  */
38 #define assign_in_user(to, from)					\
39 ({									\
40 	typeof(*from) __assign_tmp;					\
41 									\
42 	get_user(__assign_tmp, from) || put_user(__assign_tmp, to);	\
43 })
44 
45 /**
46  * get_user_cast() - Stores at a kernelspace local var the contents from a
47  *		pointer with userspace data that is not tagged with __user.
48  *
49  * @__x: var where data will be stored
50  * @__ptr: var where data will be retrieved.
51  *
52  * Sometimes we need to declare a pointer without __user because it
53  * comes from a pointer struct field that will be retrieved from userspace
54  * by the 64-bit native ioctl handler. This function ensures that the
55  * @__ptr will be cast to __user before calling get_user() in order to
56  * avoid warnings with static code analyzers like smatch.
57  */
58 #define get_user_cast(__x, __ptr)					\
59 ({									\
60 	get_user(__x, (typeof(*__ptr) __user *)(__ptr));		\
61 })
62 
63 /**
64  * put_user_force() - Stores the contents of a kernelspace local var
65  *		      into a userspace pointer, removing any __user cast.
66  *
67  * @__x: var where data will be stored
68  * @__ptr: var where data will be retrieved.
69  *
70  * Sometimes we need to remove the __user attribute from some data,
71  * by passing the __force macro. This function ensures that the
72  * @__ptr will be cast with __force before calling put_user(), in order to
73  * avoid warnings with static code analyzers like smatch.
74  */
75 #define put_user_force(__x, __ptr)					\
76 ({									\
77 	put_user((typeof(*__x) __force *)(__x), __ptr);			\
78 })
79 
80 /**
81  * assign_in_user_cast() - Copy from one __user var to another one
82  *
83  * @to: __user var where data will be stored
84  * @from: var where data will be retrieved that needs to be cast to __user.
85  *
86  * As this code very often needs to allocate userspace memory, it is easier
87  * to have a macro that will do both get_user_cast() and put_user() at once.
88  *
89  * This function should be used instead of assign_in_user() when the @from
90  * variable was not declared as __user. See get_user_cast() for more details.
91  *
92  * This function complements the macros defined at asm-generic/uaccess.h.
93  * It uses the same argument order as copy_in_user()
94  */
95 #define assign_in_user_cast(to, from)					\
96 ({									\
97 	typeof(*from) __assign_tmp;					\
98 									\
99 	get_user_cast(__assign_tmp, from) || put_user(__assign_tmp, to);\
100 })
101 
102 /**
103  * native_ioctl - Ancillary function that calls the native 64 bits ioctl
104  * handler.
105  *
106  * @file: pointer to &struct file with the file handler
107  * @cmd: ioctl to be called
108  * @arg: arguments passed from/to the ioctl handler
109  *
110  * This function calls the native ioctl handler at v4l2-dev, e. g. v4l2_ioctl()
111  */
native_ioctl(struct file * file,unsigned int cmd,unsigned long arg)112 static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
113 {
114 	long ret = -ENOIOCTLCMD;
115 
116 	if (file->f_op->unlocked_ioctl)
117 		ret = file->f_op->unlocked_ioctl(file, cmd, arg);
118 
119 	return ret;
120 }
121 
122 
123 /*
124  * Per-ioctl data copy handlers.
125  *
126  * Those come in pairs, with a get_v4l2_foo() and a put_v4l2_foo() routine,
127  * where "v4l2_foo" is the name of the V4L2 struct.
128  *
129  * They basically get two __user pointers, one with a 32-bits struct that
130  * came from the userspace call and a 64-bits struct, also allocated as
131  * userspace, but filled internally by do_video_ioctl().
132  *
133  * For ioctls that have pointers inside it, the functions will also
134  * receive an ancillary buffer with extra space, used to pass extra
135  * data to the routine.
136  */
137 
138 struct v4l2_clip32 {
139 	struct v4l2_rect        c;
140 	compat_caddr_t		next;
141 };
142 
143 struct v4l2_window32 {
144 	struct v4l2_rect        w;
145 	__u32			field;	/* enum v4l2_field */
146 	__u32			chromakey;
147 	compat_caddr_t		clips; /* actually struct v4l2_clip32 * */
148 	__u32			clipcount;
149 	compat_caddr_t		bitmap;
150 	__u8                    global_alpha;
151 };
152 
get_v4l2_window32(struct v4l2_window __user * p64,struct v4l2_window32 __user * p32,void __user * aux_buf,u32 aux_space)153 static int get_v4l2_window32(struct v4l2_window __user *p64,
154 			     struct v4l2_window32 __user *p32,
155 			     void __user *aux_buf, u32 aux_space)
156 {
157 	struct v4l2_clip32 __user *uclips;
158 	struct v4l2_clip __user *kclips;
159 	compat_caddr_t p;
160 	u32 clipcount;
161 
162 	if (!access_ok(p32, sizeof(*p32)) ||
163 	    copy_in_user(&p64->w, &p32->w, sizeof(p32->w)) ||
164 	    assign_in_user(&p64->field, &p32->field) ||
165 	    assign_in_user(&p64->chromakey, &p32->chromakey) ||
166 	    assign_in_user(&p64->global_alpha, &p32->global_alpha) ||
167 	    get_user(clipcount, &p32->clipcount) ||
168 	    put_user(clipcount, &p64->clipcount))
169 		return -EFAULT;
170 	if (clipcount > 2048)
171 		return -EINVAL;
172 	if (!clipcount)
173 		return put_user(NULL, &p64->clips);
174 
175 	if (get_user(p, &p32->clips))
176 		return -EFAULT;
177 	uclips = compat_ptr(p);
178 	if (aux_space < clipcount * sizeof(*kclips))
179 		return -EFAULT;
180 	kclips = aux_buf;
181 	if (put_user(kclips, &p64->clips))
182 		return -EFAULT;
183 
184 	while (clipcount--) {
185 		if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
186 			return -EFAULT;
187 		if (put_user(clipcount ? kclips + 1 : NULL, &kclips->next))
188 			return -EFAULT;
189 		uclips++;
190 		kclips++;
191 	}
192 	return 0;
193 }
194 
put_v4l2_window32(struct v4l2_window __user * p64,struct v4l2_window32 __user * p32)195 static int put_v4l2_window32(struct v4l2_window __user *p64,
196 			     struct v4l2_window32 __user *p32)
197 {
198 	struct v4l2_clip __user *kclips;
199 	struct v4l2_clip32 __user *uclips;
200 	compat_caddr_t p;
201 	u32 clipcount;
202 
203 	if (copy_in_user(&p32->w, &p64->w, sizeof(p64->w)) ||
204 	    assign_in_user(&p32->field, &p64->field) ||
205 	    assign_in_user(&p32->chromakey, &p64->chromakey) ||
206 	    assign_in_user(&p32->global_alpha, &p64->global_alpha) ||
207 	    get_user(clipcount, &p64->clipcount) ||
208 	    put_user(clipcount, &p32->clipcount))
209 		return -EFAULT;
210 	if (!clipcount)
211 		return 0;
212 
213 	if (get_user(kclips, &p64->clips))
214 		return -EFAULT;
215 	if (get_user(p, &p32->clips))
216 		return -EFAULT;
217 	uclips = compat_ptr(p);
218 	while (clipcount--) {
219 		if (copy_in_user(&uclips->c, &kclips->c, sizeof(uclips->c)))
220 			return -EFAULT;
221 		uclips++;
222 		kclips++;
223 	}
224 	return 0;
225 }
226 
227 struct v4l2_format32 {
228 	__u32	type;	/* enum v4l2_buf_type */
229 	union {
230 		struct v4l2_pix_format	pix;
231 		struct v4l2_pix_format_mplane	pix_mp;
232 		struct v4l2_window32	win;
233 		struct v4l2_vbi_format	vbi;
234 		struct v4l2_sliced_vbi_format	sliced;
235 		struct v4l2_sdr_format	sdr;
236 		struct v4l2_meta_format	meta;
237 		__u8	raw_data[200];        /* user-defined */
238 	} fmt;
239 };
240 
241 /**
242  * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
243  * @index:	on return, index of the first created buffer
244  * @count:	entry: number of requested buffers,
245  *		return: number of created buffers
246  * @memory:	buffer memory type
247  * @format:	frame format, for which buffers are requested
248  * @capabilities: capabilities of this buffer type.
249  * @reserved:	future extensions
250  */
251 struct v4l2_create_buffers32 {
252 	__u32			index;
253 	__u32			count;
254 	__u32			memory;	/* enum v4l2_memory */
255 	struct v4l2_format32	format;
256 	__u32			capabilities;
257 	__u32			reserved[7];
258 };
259 
__bufsize_v4l2_format(struct v4l2_format32 __user * p32,u32 * size)260 static int __bufsize_v4l2_format(struct v4l2_format32 __user *p32, u32 *size)
261 {
262 	u32 type;
263 
264 	if (get_user(type, &p32->type))
265 		return -EFAULT;
266 
267 	switch (type) {
268 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
269 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
270 		u32 clipcount;
271 
272 		if (get_user(clipcount, &p32->fmt.win.clipcount))
273 			return -EFAULT;
274 		if (clipcount > 2048)
275 			return -EINVAL;
276 		*size = clipcount * sizeof(struct v4l2_clip);
277 		return 0;
278 	}
279 	default:
280 		*size = 0;
281 		return 0;
282 	}
283 }
284 
bufsize_v4l2_format(struct v4l2_format32 __user * p32,u32 * size)285 static int bufsize_v4l2_format(struct v4l2_format32 __user *p32, u32 *size)
286 {
287 	if (!access_ok(p32, sizeof(*p32)))
288 		return -EFAULT;
289 	return __bufsize_v4l2_format(p32, size);
290 }
291 
__get_v4l2_format32(struct v4l2_format __user * p64,struct v4l2_format32 __user * p32,void __user * aux_buf,u32 aux_space)292 static int __get_v4l2_format32(struct v4l2_format __user *p64,
293 			       struct v4l2_format32 __user *p32,
294 			       void __user *aux_buf, u32 aux_space)
295 {
296 	u32 type;
297 
298 	if (get_user(type, &p32->type) || put_user(type, &p64->type))
299 		return -EFAULT;
300 
301 	switch (type) {
302 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
303 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
304 		return copy_in_user(&p64->fmt.pix, &p32->fmt.pix,
305 				    sizeof(p64->fmt.pix)) ? -EFAULT : 0;
306 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
307 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
308 		return copy_in_user(&p64->fmt.pix_mp, &p32->fmt.pix_mp,
309 				    sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
310 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
311 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
312 		return get_v4l2_window32(&p64->fmt.win, &p32->fmt.win,
313 					 aux_buf, aux_space);
314 	case V4L2_BUF_TYPE_VBI_CAPTURE:
315 	case V4L2_BUF_TYPE_VBI_OUTPUT:
316 		return copy_in_user(&p64->fmt.vbi, &p32->fmt.vbi,
317 				    sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
318 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
319 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
320 		return copy_in_user(&p64->fmt.sliced, &p32->fmt.sliced,
321 				    sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
322 	case V4L2_BUF_TYPE_SDR_CAPTURE:
323 	case V4L2_BUF_TYPE_SDR_OUTPUT:
324 		return copy_in_user(&p64->fmt.sdr, &p32->fmt.sdr,
325 				    sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
326 	case V4L2_BUF_TYPE_META_CAPTURE:
327 	case V4L2_BUF_TYPE_META_OUTPUT:
328 		return copy_in_user(&p64->fmt.meta, &p32->fmt.meta,
329 				    sizeof(p64->fmt.meta)) ? -EFAULT : 0;
330 	default:
331 		return -EINVAL;
332 	}
333 }
334 
get_v4l2_format32(struct v4l2_format __user * p64,struct v4l2_format32 __user * p32,void __user * aux_buf,u32 aux_space)335 static int get_v4l2_format32(struct v4l2_format __user *p64,
336 			     struct v4l2_format32 __user *p32,
337 			     void __user *aux_buf, u32 aux_space)
338 {
339 	if (!access_ok(p32, sizeof(*p32)))
340 		return -EFAULT;
341 	return __get_v4l2_format32(p64, p32, aux_buf, aux_space);
342 }
343 
bufsize_v4l2_create(struct v4l2_create_buffers32 __user * p32,u32 * size)344 static int bufsize_v4l2_create(struct v4l2_create_buffers32 __user *p32,
345 			       u32 *size)
346 {
347 	if (!access_ok(p32, sizeof(*p32)))
348 		return -EFAULT;
349 	return __bufsize_v4l2_format(&p32->format, size);
350 }
351 
get_v4l2_create32(struct v4l2_create_buffers __user * p64,struct v4l2_create_buffers32 __user * p32,void __user * aux_buf,u32 aux_space)352 static int get_v4l2_create32(struct v4l2_create_buffers __user *p64,
353 			     struct v4l2_create_buffers32 __user *p32,
354 			     void __user *aux_buf, u32 aux_space)
355 {
356 	if (!access_ok(p32, sizeof(*p32)) ||
357 	    copy_in_user(p64, p32,
358 			 offsetof(struct v4l2_create_buffers32, format)))
359 		return -EFAULT;
360 	return __get_v4l2_format32(&p64->format, &p32->format,
361 				   aux_buf, aux_space);
362 }
363 
__put_v4l2_format32(struct v4l2_format __user * p64,struct v4l2_format32 __user * p32)364 static int __put_v4l2_format32(struct v4l2_format __user *p64,
365 			       struct v4l2_format32 __user *p32)
366 {
367 	u32 type;
368 
369 	if (get_user(type, &p64->type))
370 		return -EFAULT;
371 
372 	switch (type) {
373 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
374 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
375 		return copy_in_user(&p32->fmt.pix, &p64->fmt.pix,
376 				    sizeof(p64->fmt.pix)) ? -EFAULT : 0;
377 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
378 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
379 		return copy_in_user(&p32->fmt.pix_mp, &p64->fmt.pix_mp,
380 				    sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
381 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
382 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
383 		return put_v4l2_window32(&p64->fmt.win, &p32->fmt.win);
384 	case V4L2_BUF_TYPE_VBI_CAPTURE:
385 	case V4L2_BUF_TYPE_VBI_OUTPUT:
386 		return copy_in_user(&p32->fmt.vbi, &p64->fmt.vbi,
387 				    sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
388 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
389 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
390 		return copy_in_user(&p32->fmt.sliced, &p64->fmt.sliced,
391 				    sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
392 	case V4L2_BUF_TYPE_SDR_CAPTURE:
393 	case V4L2_BUF_TYPE_SDR_OUTPUT:
394 		return copy_in_user(&p32->fmt.sdr, &p64->fmt.sdr,
395 				    sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
396 	case V4L2_BUF_TYPE_META_CAPTURE:
397 	case V4L2_BUF_TYPE_META_OUTPUT:
398 		return copy_in_user(&p32->fmt.meta, &p64->fmt.meta,
399 				    sizeof(p64->fmt.meta)) ? -EFAULT : 0;
400 	default:
401 		return -EINVAL;
402 	}
403 }
404 
put_v4l2_format32(struct v4l2_format __user * p64,struct v4l2_format32 __user * p32)405 static int put_v4l2_format32(struct v4l2_format __user *p64,
406 			     struct v4l2_format32 __user *p32)
407 {
408 	if (!access_ok(p32, sizeof(*p32)))
409 		return -EFAULT;
410 	return __put_v4l2_format32(p64, p32);
411 }
412 
put_v4l2_create32(struct v4l2_create_buffers __user * p64,struct v4l2_create_buffers32 __user * p32)413 static int put_v4l2_create32(struct v4l2_create_buffers __user *p64,
414 			     struct v4l2_create_buffers32 __user *p32)
415 {
416 	if (!access_ok(p32, sizeof(*p32)) ||
417 	    copy_in_user(p32, p64,
418 			 offsetof(struct v4l2_create_buffers32, format)) ||
419 	    assign_in_user(&p32->capabilities, &p64->capabilities) ||
420 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p64->reserved)))
421 		return -EFAULT;
422 	return __put_v4l2_format32(&p64->format, &p32->format);
423 }
424 
425 struct v4l2_standard32 {
426 	__u32		     index;
427 	compat_u64	     id;
428 	__u8		     name[24];
429 	struct v4l2_fract    frameperiod; /* Frames, not fields */
430 	__u32		     framelines;
431 	__u32		     reserved[4];
432 };
433 
get_v4l2_standard32(struct v4l2_standard __user * p64,struct v4l2_standard32 __user * p32)434 static int get_v4l2_standard32(struct v4l2_standard __user *p64,
435 			       struct v4l2_standard32 __user *p32)
436 {
437 	/* other fields are not set by the user, nor used by the driver */
438 	if (!access_ok(p32, sizeof(*p32)) ||
439 	    assign_in_user(&p64->index, &p32->index))
440 		return -EFAULT;
441 	return 0;
442 }
443 
put_v4l2_standard32(struct v4l2_standard __user * p64,struct v4l2_standard32 __user * p32)444 static int put_v4l2_standard32(struct v4l2_standard __user *p64,
445 			       struct v4l2_standard32 __user *p32)
446 {
447 	if (!access_ok(p32, sizeof(*p32)) ||
448 	    assign_in_user(&p32->index, &p64->index) ||
449 	    assign_in_user(&p32->id, &p64->id) ||
450 	    copy_in_user(p32->name, p64->name, sizeof(p32->name)) ||
451 	    copy_in_user(&p32->frameperiod, &p64->frameperiod,
452 			 sizeof(p32->frameperiod)) ||
453 	    assign_in_user(&p32->framelines, &p64->framelines) ||
454 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
455 		return -EFAULT;
456 	return 0;
457 }
458 
459 struct v4l2_plane32 {
460 	__u32			bytesused;
461 	__u32			length;
462 	union {
463 		__u32		mem_offset;
464 		compat_long_t	userptr;
465 		__s32		fd;
466 	} m;
467 	__u32			data_offset;
468 	/*
469 	 * few userspace clients and drivers use reserved fields
470 	 * and it is up to them how these fields are used. v4l2
471 	 * simply copy reserved fields between them.
472 	 */
473 	__u32			reserved[11];
474 };
475 
476 /*
477  * This is correct for all architectures including i386, but not x32,
478  * which has different alignment requirements for timestamp
479  */
480 struct v4l2_buffer32 {
481 	__u32			index;
482 	__u32			type;	/* enum v4l2_buf_type */
483 	__u32			bytesused;
484 	__u32			flags;
485 	__u32			field;	/* enum v4l2_field */
486 	struct {
487 		compat_s64	tv_sec;
488 		compat_s64	tv_usec;
489 	}			timestamp;
490 	struct v4l2_timecode	timecode;
491 	__u32			sequence;
492 
493 	/* memory location */
494 	__u32			memory;	/* enum v4l2_memory */
495 	union {
496 		__u32           offset;
497 		compat_long_t   userptr;
498 		compat_caddr_t  planes;
499 		__s32		fd;
500 	} m;
501 	__u32			length;
502 	__u32			reserved2;
503 	__s32			request_fd;
504 };
505 
506 struct v4l2_buffer32_time32 {
507 	__u32			index;
508 	__u32			type;	/* enum v4l2_buf_type */
509 	__u32			bytesused;
510 	__u32			flags;
511 	__u32			field;	/* enum v4l2_field */
512 	struct old_timeval32	timestamp;
513 	struct v4l2_timecode	timecode;
514 	__u32			sequence;
515 
516 	/* memory location */
517 	__u32			memory;	/* enum v4l2_memory */
518 	union {
519 		__u32           offset;
520 		compat_long_t   userptr;
521 		compat_caddr_t  planes;
522 		__s32		fd;
523 	} m;
524 	__u32			length;
525 	__u32			reserved2;
526 	__s32			request_fd;
527 };
528 
get_v4l2_plane32(struct v4l2_plane __user * p64,struct v4l2_plane32 __user * p32,enum v4l2_memory memory)529 static int get_v4l2_plane32(struct v4l2_plane __user *p64,
530 			    struct v4l2_plane32 __user *p32,
531 			    enum v4l2_memory memory)
532 {
533 	compat_ulong_t p;
534 
535 	if (copy_in_user(p64, p32, 2 * sizeof(__u32)) ||
536 	    copy_in_user(&p64->data_offset, &p32->data_offset,
537 			 sizeof(p64->data_offset)) ||
538 	    copy_in_user(p64->reserved, p32->reserved,
539 			 sizeof(p64->reserved)))
540 		return -EFAULT;
541 
542 	switch (memory) {
543 	case V4L2_MEMORY_MMAP:
544 	case V4L2_MEMORY_OVERLAY:
545 		if (copy_in_user(&p64->m.mem_offset, &p32->m.mem_offset,
546 				 sizeof(p32->m.mem_offset)))
547 			return -EFAULT;
548 		break;
549 	case V4L2_MEMORY_USERPTR:
550 		if (get_user(p, &p32->m.userptr) ||
551 		    put_user((unsigned long)compat_ptr(p), &p64->m.userptr))
552 			return -EFAULT;
553 		break;
554 	case V4L2_MEMORY_DMABUF:
555 		if (copy_in_user(&p64->m.fd, &p32->m.fd, sizeof(p32->m.fd)))
556 			return -EFAULT;
557 		break;
558 	}
559 
560 	return 0;
561 }
562 
put_v4l2_plane32(struct v4l2_plane __user * p64,struct v4l2_plane32 __user * p32,enum v4l2_memory memory)563 static int put_v4l2_plane32(struct v4l2_plane __user *p64,
564 			    struct v4l2_plane32 __user *p32,
565 			    enum v4l2_memory memory)
566 {
567 	unsigned long p;
568 
569 	if (copy_in_user(p32, p64, 2 * sizeof(__u32)) ||
570 	    copy_in_user(&p32->data_offset, &p64->data_offset,
571 			 sizeof(p64->data_offset)) ||
572 	    copy_in_user(p32->reserved, p64->reserved,
573 			 sizeof(p32->reserved)))
574 		return -EFAULT;
575 
576 	switch (memory) {
577 	case V4L2_MEMORY_MMAP:
578 	case V4L2_MEMORY_OVERLAY:
579 		if (copy_in_user(&p32->m.mem_offset, &p64->m.mem_offset,
580 				 sizeof(p64->m.mem_offset)))
581 			return -EFAULT;
582 		break;
583 	case V4L2_MEMORY_USERPTR:
584 		if (get_user(p, &p64->m.userptr) ||
585 		    put_user((compat_ulong_t)ptr_to_compat((void __user *)p),
586 			     &p32->m.userptr))
587 			return -EFAULT;
588 		break;
589 	case V4L2_MEMORY_DMABUF:
590 		if (copy_in_user(&p32->m.fd, &p64->m.fd, sizeof(p64->m.fd)))
591 			return -EFAULT;
592 		break;
593 	}
594 
595 	return 0;
596 }
597 
bufsize_v4l2_buffer(struct v4l2_buffer32 __user * p32,u32 * size)598 static int bufsize_v4l2_buffer(struct v4l2_buffer32 __user *p32, u32 *size)
599 {
600 	u32 type;
601 	u32 length;
602 
603 	if (!access_ok(p32, sizeof(*p32)) ||
604 	    get_user(type, &p32->type) ||
605 	    get_user(length, &p32->length))
606 		return -EFAULT;
607 
608 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
609 		if (length > VIDEO_MAX_PLANES)
610 			return -EINVAL;
611 
612 		/*
613 		 * We don't really care if userspace decides to kill itself
614 		 * by passing a very big length value
615 		 */
616 		*size = length * sizeof(struct v4l2_plane);
617 	} else {
618 		*size = 0;
619 	}
620 	return 0;
621 }
622 
bufsize_v4l2_buffer_time32(struct v4l2_buffer32_time32 __user * p32,u32 * size)623 static int bufsize_v4l2_buffer_time32(struct v4l2_buffer32_time32 __user *p32, u32 *size)
624 {
625 	u32 type;
626 	u32 length;
627 
628 	if (!access_ok(p32, sizeof(*p32)) ||
629 	    get_user(type, &p32->type) ||
630 	    get_user(length, &p32->length))
631 		return -EFAULT;
632 
633 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
634 		if (length > VIDEO_MAX_PLANES)
635 			return -EINVAL;
636 
637 		/*
638 		 * We don't really care if userspace decides to kill itself
639 		 * by passing a very big length value
640 		 */
641 		*size = length * sizeof(struct v4l2_plane);
642 	} else {
643 		*size = 0;
644 	}
645 	return 0;
646 }
647 
get_v4l2_buffer32(struct v4l2_buffer __user * p64,struct v4l2_buffer32 __user * p32,void __user * aux_buf,u32 aux_space)648 static int get_v4l2_buffer32(struct v4l2_buffer __user *p64,
649 			     struct v4l2_buffer32 __user *p32,
650 			     void __user *aux_buf, u32 aux_space)
651 {
652 	u32 type;
653 	u32 length;
654 	s32 request_fd;
655 	enum v4l2_memory memory;
656 	struct v4l2_plane32 __user *uplane32;
657 	struct v4l2_plane __user *uplane;
658 	compat_caddr_t p;
659 	int ret;
660 
661 	if (!access_ok(p32, sizeof(*p32)) ||
662 	    assign_in_user(&p64->index, &p32->index) ||
663 	    get_user(type, &p32->type) ||
664 	    put_user(type, &p64->type) ||
665 	    assign_in_user(&p64->flags, &p32->flags) ||
666 	    get_user(memory, &p32->memory) ||
667 	    put_user(memory, &p64->memory) ||
668 	    get_user(length, &p32->length) ||
669 	    put_user(length, &p64->length) ||
670 	    get_user(request_fd, &p32->request_fd) ||
671 	    put_user(request_fd, &p64->request_fd))
672 		return -EFAULT;
673 
674 	if (V4L2_TYPE_IS_OUTPUT(type))
675 		if (assign_in_user(&p64->bytesused, &p32->bytesused) ||
676 		    assign_in_user(&p64->field, &p32->field) ||
677 		    assign_in_user(&p64->timestamp.tv_sec,
678 				   &p32->timestamp.tv_sec) ||
679 		    assign_in_user(&p64->timestamp.tv_usec,
680 				   &p32->timestamp.tv_usec))
681 			return -EFAULT;
682 
683 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
684 		u32 num_planes = length;
685 
686 		if (num_planes == 0) {
687 			/*
688 			 * num_planes == 0 is legal, e.g. when userspace doesn't
689 			 * need planes array on DQBUF
690 			 */
691 			return put_user(NULL, &p64->m.planes);
692 		}
693 		if (num_planes > VIDEO_MAX_PLANES)
694 			return -EINVAL;
695 
696 		if (get_user(p, &p32->m.planes))
697 			return -EFAULT;
698 
699 		uplane32 = compat_ptr(p);
700 		if (!access_ok(uplane32,
701 			       num_planes * sizeof(*uplane32)))
702 			return -EFAULT;
703 
704 		/*
705 		 * We don't really care if userspace decides to kill itself
706 		 * by passing a very big num_planes value
707 		 */
708 		if (aux_space < num_planes * sizeof(*uplane))
709 			return -EFAULT;
710 
711 		uplane = aux_buf;
712 		if (put_user_force(uplane, &p64->m.planes))
713 			return -EFAULT;
714 
715 		while (num_planes--) {
716 			ret = get_v4l2_plane32(uplane, uplane32, memory);
717 			if (ret)
718 				return ret;
719 			uplane++;
720 			uplane32++;
721 		}
722 	} else {
723 		switch (memory) {
724 		case V4L2_MEMORY_MMAP:
725 		case V4L2_MEMORY_OVERLAY:
726 			if (assign_in_user(&p64->m.offset, &p32->m.offset))
727 				return -EFAULT;
728 			break;
729 		case V4L2_MEMORY_USERPTR: {
730 			compat_ulong_t userptr;
731 
732 			if (get_user(userptr, &p32->m.userptr) ||
733 			    put_user((unsigned long)compat_ptr(userptr),
734 				     &p64->m.userptr))
735 				return -EFAULT;
736 			break;
737 		}
738 		case V4L2_MEMORY_DMABUF:
739 			if (assign_in_user(&p64->m.fd, &p32->m.fd))
740 				return -EFAULT;
741 			break;
742 		}
743 	}
744 
745 	return 0;
746 }
747 
get_v4l2_buffer32_time32(struct v4l2_buffer_time32 __user * p64,struct v4l2_buffer32_time32 __user * p32,void __user * aux_buf,u32 aux_space)748 static int get_v4l2_buffer32_time32(struct v4l2_buffer_time32 __user *p64,
749 				    struct v4l2_buffer32_time32 __user *p32,
750 				    void __user *aux_buf, u32 aux_space)
751 {
752 	u32 type;
753 	u32 length;
754 	s32 request_fd;
755 	enum v4l2_memory memory;
756 	struct v4l2_plane32 __user *uplane32;
757 	struct v4l2_plane __user *uplane;
758 	compat_caddr_t p;
759 	int ret;
760 
761 	if (!access_ok(p32, sizeof(*p32)) ||
762 	    assign_in_user(&p64->index, &p32->index) ||
763 	    get_user(type, &p32->type) ||
764 	    put_user(type, &p64->type) ||
765 	    assign_in_user(&p64->flags, &p32->flags) ||
766 	    get_user(memory, &p32->memory) ||
767 	    put_user(memory, &p64->memory) ||
768 	    get_user(length, &p32->length) ||
769 	    put_user(length, &p64->length) ||
770 	    get_user(request_fd, &p32->request_fd) ||
771 	    put_user(request_fd, &p64->request_fd))
772 		return -EFAULT;
773 
774 	if (V4L2_TYPE_IS_OUTPUT(type))
775 		if (assign_in_user(&p64->bytesused, &p32->bytesused) ||
776 		    assign_in_user(&p64->field, &p32->field) ||
777 		    assign_in_user(&p64->timestamp.tv_sec,
778 				   &p32->timestamp.tv_sec) ||
779 		    assign_in_user(&p64->timestamp.tv_usec,
780 				   &p32->timestamp.tv_usec))
781 			return -EFAULT;
782 
783 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
784 		u32 num_planes = length;
785 
786 		if (num_planes == 0) {
787 			/*
788 			 * num_planes == 0 is legal, e.g. when userspace doesn't
789 			 * need planes array on DQBUF
790 			 */
791 			return put_user(NULL, &p64->m.planes);
792 		}
793 		if (num_planes > VIDEO_MAX_PLANES)
794 			return -EINVAL;
795 
796 		if (get_user(p, &p32->m.planes))
797 			return -EFAULT;
798 
799 		uplane32 = compat_ptr(p);
800 		if (!access_ok(uplane32,
801 			       num_planes * sizeof(*uplane32)))
802 			return -EFAULT;
803 
804 		/*
805 		 * We don't really care if userspace decides to kill itself
806 		 * by passing a very big num_planes value
807 		 */
808 		if (aux_space < num_planes * sizeof(*uplane))
809 			return -EFAULT;
810 
811 		uplane = aux_buf;
812 		if (put_user_force(uplane, &p64->m.planes))
813 			return -EFAULT;
814 
815 		while (num_planes--) {
816 			ret = get_v4l2_plane32(uplane, uplane32, memory);
817 			if (ret)
818 				return ret;
819 			uplane++;
820 			uplane32++;
821 		}
822 	} else {
823 		switch (memory) {
824 		case V4L2_MEMORY_MMAP:
825 		case V4L2_MEMORY_OVERLAY:
826 			if (assign_in_user(&p64->m.offset, &p32->m.offset))
827 				return -EFAULT;
828 			break;
829 		case V4L2_MEMORY_USERPTR: {
830 			compat_ulong_t userptr;
831 
832 			if (get_user(userptr, &p32->m.userptr) ||
833 			    put_user((unsigned long)compat_ptr(userptr),
834 				     &p64->m.userptr))
835 				return -EFAULT;
836 			break;
837 		}
838 		case V4L2_MEMORY_DMABUF:
839 			if (assign_in_user(&p64->m.fd, &p32->m.fd))
840 				return -EFAULT;
841 			break;
842 		}
843 	}
844 
845 	return 0;
846 }
847 
put_v4l2_buffer32(struct v4l2_buffer __user * p64,struct v4l2_buffer32 __user * p32)848 static int put_v4l2_buffer32(struct v4l2_buffer __user *p64,
849 			     struct v4l2_buffer32 __user *p32)
850 {
851 	u32 type;
852 	u32 length;
853 	enum v4l2_memory memory;
854 	struct v4l2_plane32 __user *uplane32;
855 	struct v4l2_plane *uplane;
856 	compat_caddr_t p;
857 	int ret;
858 
859 	if (!access_ok(p32, sizeof(*p32)) ||
860 	    assign_in_user(&p32->index, &p64->index) ||
861 	    get_user(type, &p64->type) ||
862 	    put_user(type, &p32->type) ||
863 	    assign_in_user(&p32->flags, &p64->flags) ||
864 	    get_user(memory, &p64->memory) ||
865 	    put_user(memory, &p32->memory))
866 		return -EFAULT;
867 
868 	if (assign_in_user(&p32->bytesused, &p64->bytesused) ||
869 	    assign_in_user(&p32->field, &p64->field) ||
870 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
871 	    assign_in_user(&p32->timestamp.tv_usec, &p64->timestamp.tv_usec) ||
872 	    copy_in_user(&p32->timecode, &p64->timecode, sizeof(p64->timecode)) ||
873 	    assign_in_user(&p32->sequence, &p64->sequence) ||
874 	    assign_in_user(&p32->reserved2, &p64->reserved2) ||
875 	    assign_in_user(&p32->request_fd, &p64->request_fd) ||
876 	    get_user(length, &p64->length) ||
877 	    put_user(length, &p32->length))
878 		return -EFAULT;
879 
880 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
881 		u32 num_planes = length;
882 
883 		if (num_planes == 0)
884 			return 0;
885 		/* We need to define uplane without __user, even though
886 		 * it does point to data in userspace here. The reason is
887 		 * that v4l2-ioctl.c copies it from userspace to kernelspace,
888 		 * so its definition in videodev2.h doesn't have a
889 		 * __user markup. Defining uplane with __user causes
890 		 * smatch warnings, so instead declare it without __user
891 		 * and cast it as a userspace pointer to put_v4l2_plane32().
892 		 */
893 		if (get_user(uplane, &p64->m.planes))
894 			return -EFAULT;
895 		if (get_user(p, &p32->m.planes))
896 			return -EFAULT;
897 		uplane32 = compat_ptr(p);
898 
899 		while (num_planes--) {
900 			ret = put_v4l2_plane32((void __user *)uplane,
901 					       uplane32, memory);
902 			if (ret)
903 				return ret;
904 			++uplane;
905 			++uplane32;
906 		}
907 	} else {
908 		switch (memory) {
909 		case V4L2_MEMORY_MMAP:
910 		case V4L2_MEMORY_OVERLAY:
911 			if (assign_in_user(&p32->m.offset, &p64->m.offset))
912 				return -EFAULT;
913 			break;
914 		case V4L2_MEMORY_USERPTR:
915 			if (assign_in_user(&p32->m.userptr, &p64->m.userptr))
916 				return -EFAULT;
917 			break;
918 		case V4L2_MEMORY_DMABUF:
919 			if (assign_in_user(&p32->m.fd, &p64->m.fd))
920 				return -EFAULT;
921 			break;
922 		}
923 	}
924 
925 	return 0;
926 }
927 
put_v4l2_buffer32_time32(struct v4l2_buffer_time32 __user * p64,struct v4l2_buffer32_time32 __user * p32)928 static int put_v4l2_buffer32_time32(struct v4l2_buffer_time32 __user *p64,
929 				    struct v4l2_buffer32_time32 __user *p32)
930 {
931 	u32 type;
932 	u32 length;
933 	enum v4l2_memory memory;
934 	struct v4l2_plane32 __user *uplane32;
935 	struct v4l2_plane *uplane;
936 	compat_caddr_t p;
937 	int ret;
938 
939 	if (!access_ok(p32, sizeof(*p32)) ||
940 	    assign_in_user(&p32->index, &p64->index) ||
941 	    get_user(type, &p64->type) ||
942 	    put_user(type, &p32->type) ||
943 	    assign_in_user(&p32->flags, &p64->flags) ||
944 	    get_user(memory, &p64->memory) ||
945 	    put_user(memory, &p32->memory))
946 		return -EFAULT;
947 
948 	if (assign_in_user(&p32->bytesused, &p64->bytesused) ||
949 	    assign_in_user(&p32->field, &p64->field) ||
950 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
951 	    assign_in_user(&p32->timestamp.tv_usec, &p64->timestamp.tv_usec) ||
952 	    copy_in_user(&p32->timecode, &p64->timecode, sizeof(p64->timecode)) ||
953 	    assign_in_user(&p32->sequence, &p64->sequence) ||
954 	    assign_in_user(&p32->reserved2, &p64->reserved2) ||
955 	    assign_in_user(&p32->request_fd, &p64->request_fd) ||
956 	    get_user(length, &p64->length) ||
957 	    put_user(length, &p32->length))
958 		return -EFAULT;
959 
960 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
961 		u32 num_planes = length;
962 
963 		if (num_planes == 0)
964 			return 0;
965 		/* We need to define uplane without __user, even though
966 		 * it does point to data in userspace here. The reason is
967 		 * that v4l2-ioctl.c copies it from userspace to kernelspace,
968 		 * so its definition in videodev2.h doesn't have a
969 		 * __user markup. Defining uplane with __user causes
970 		 * smatch warnings, so instead declare it without __user
971 		 * and cast it as a userspace pointer to put_v4l2_plane32().
972 		 */
973 		if (get_user(uplane, &p64->m.planes))
974 			return -EFAULT;
975 		if (get_user(p, &p32->m.planes))
976 			return -EFAULT;
977 		uplane32 = compat_ptr(p);
978 
979 		while (num_planes--) {
980 			ret = put_v4l2_plane32((void __user *)uplane,
981 					       uplane32, memory);
982 			if (ret)
983 				return ret;
984 			++uplane;
985 			++uplane32;
986 		}
987 	} else {
988 		switch (memory) {
989 		case V4L2_MEMORY_MMAP:
990 		case V4L2_MEMORY_OVERLAY:
991 			if (assign_in_user(&p32->m.offset, &p64->m.offset))
992 				return -EFAULT;
993 			break;
994 		case V4L2_MEMORY_USERPTR:
995 			if (assign_in_user(&p32->m.userptr, &p64->m.userptr))
996 				return -EFAULT;
997 			break;
998 		case V4L2_MEMORY_DMABUF:
999 			if (assign_in_user(&p32->m.fd, &p64->m.fd))
1000 				return -EFAULT;
1001 			break;
1002 		}
1003 	}
1004 
1005 	return 0;
1006 }
1007 
1008 struct v4l2_framebuffer32 {
1009 	__u32			capability;
1010 	__u32			flags;
1011 	compat_caddr_t		base;
1012 	struct {
1013 		__u32		width;
1014 		__u32		height;
1015 		__u32		pixelformat;
1016 		__u32		field;
1017 		__u32		bytesperline;
1018 		__u32		sizeimage;
1019 		__u32		colorspace;
1020 		__u32		priv;
1021 	} fmt;
1022 };
1023 
get_v4l2_framebuffer32(struct v4l2_framebuffer __user * p64,struct v4l2_framebuffer32 __user * p32)1024 static int get_v4l2_framebuffer32(struct v4l2_framebuffer __user *p64,
1025 				  struct v4l2_framebuffer32 __user *p32)
1026 {
1027 	compat_caddr_t tmp;
1028 
1029 	if (!access_ok(p32, sizeof(*p32)) ||
1030 	    get_user(tmp, &p32->base) ||
1031 	    put_user_force(compat_ptr(tmp), &p64->base) ||
1032 	    assign_in_user(&p64->capability, &p32->capability) ||
1033 	    assign_in_user(&p64->flags, &p32->flags) ||
1034 	    copy_in_user(&p64->fmt, &p32->fmt, sizeof(p64->fmt)))
1035 		return -EFAULT;
1036 	return 0;
1037 }
1038 
put_v4l2_framebuffer32(struct v4l2_framebuffer __user * p64,struct v4l2_framebuffer32 __user * p32)1039 static int put_v4l2_framebuffer32(struct v4l2_framebuffer __user *p64,
1040 				  struct v4l2_framebuffer32 __user *p32)
1041 {
1042 	void *base;
1043 
1044 	if (!access_ok(p32, sizeof(*p32)) ||
1045 	    get_user(base, &p64->base) ||
1046 	    put_user(ptr_to_compat((void __user *)base), &p32->base) ||
1047 	    assign_in_user(&p32->capability, &p64->capability) ||
1048 	    assign_in_user(&p32->flags, &p64->flags) ||
1049 	    copy_in_user(&p32->fmt, &p64->fmt, sizeof(p64->fmt)))
1050 		return -EFAULT;
1051 	return 0;
1052 }
1053 
1054 struct v4l2_input32 {
1055 	__u32	     index;		/*  Which input */
1056 	__u8	     name[32];		/*  Label */
1057 	__u32	     type;		/*  Type of input */
1058 	__u32	     audioset;		/*  Associated audios (bitfield) */
1059 	__u32        tuner;             /*  Associated tuner */
1060 	compat_u64   std;
1061 	__u32	     status;
1062 	__u32	     capabilities;
1063 	__u32	     reserved[3];
1064 };
1065 
1066 /*
1067  * The 64-bit v4l2_input struct has extra padding at the end of the struct.
1068  * Otherwise it is identical to the 32-bit version.
1069  */
get_v4l2_input32(struct v4l2_input __user * p64,struct v4l2_input32 __user * p32)1070 static inline int get_v4l2_input32(struct v4l2_input __user *p64,
1071 				   struct v4l2_input32 __user *p32)
1072 {
1073 	if (copy_in_user(p64, p32, sizeof(*p32)))
1074 		return -EFAULT;
1075 	return 0;
1076 }
1077 
put_v4l2_input32(struct v4l2_input __user * p64,struct v4l2_input32 __user * p32)1078 static inline int put_v4l2_input32(struct v4l2_input __user *p64,
1079 				   struct v4l2_input32 __user *p32)
1080 {
1081 	if (copy_in_user(p32, p64, sizeof(*p32)))
1082 		return -EFAULT;
1083 	return 0;
1084 }
1085 
1086 struct v4l2_ext_controls32 {
1087 	__u32 which;
1088 	__u32 count;
1089 	__u32 error_idx;
1090 	__s32 request_fd;
1091 	__u32 reserved[1];
1092 	compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
1093 };
1094 
1095 struct v4l2_ext_control32 {
1096 	__u32 id;
1097 	__u32 size;
1098 	__u32 reserved2[1];
1099 	union {
1100 		__s32 value;
1101 		__s64 value64;
1102 		compat_caddr_t string; /* actually char * */
1103 	};
1104 } __attribute__ ((packed));
1105 
1106 /* Return true if this control is a pointer type. */
ctrl_is_pointer(struct file * file,u32 id)1107 static inline bool ctrl_is_pointer(struct file *file, u32 id)
1108 {
1109 	struct video_device *vdev = video_devdata(file);
1110 	struct v4l2_fh *fh = NULL;
1111 	struct v4l2_ctrl_handler *hdl = NULL;
1112 	struct v4l2_query_ext_ctrl qec = { id };
1113 	const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
1114 
1115 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
1116 		fh = file->private_data;
1117 
1118 	if (fh && fh->ctrl_handler)
1119 		hdl = fh->ctrl_handler;
1120 	else if (vdev->ctrl_handler)
1121 		hdl = vdev->ctrl_handler;
1122 
1123 	if (hdl) {
1124 		struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, id);
1125 
1126 		return ctrl && ctrl->is_ptr;
1127 	}
1128 
1129 	if (!ops || !ops->vidioc_query_ext_ctrl)
1130 		return false;
1131 
1132 	return !ops->vidioc_query_ext_ctrl(file, fh, &qec) &&
1133 		(qec.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD);
1134 }
1135 
bufsize_v4l2_ext_controls(struct v4l2_ext_controls32 __user * p32,u32 * size)1136 static int bufsize_v4l2_ext_controls(struct v4l2_ext_controls32 __user *p32,
1137 				     u32 *size)
1138 {
1139 	u32 count;
1140 
1141 	if (!access_ok(p32, sizeof(*p32)) ||
1142 	    get_user(count, &p32->count))
1143 		return -EFAULT;
1144 	if (count > V4L2_CID_MAX_CTRLS)
1145 		return -EINVAL;
1146 	*size = count * sizeof(struct v4l2_ext_control);
1147 	return 0;
1148 }
1149 
get_v4l2_ext_controls32(struct file * file,struct v4l2_ext_controls __user * p64,struct v4l2_ext_controls32 __user * p32,void __user * aux_buf,u32 aux_space)1150 static int get_v4l2_ext_controls32(struct file *file,
1151 				   struct v4l2_ext_controls __user *p64,
1152 				   struct v4l2_ext_controls32 __user *p32,
1153 				   void __user *aux_buf, u32 aux_space)
1154 {
1155 	struct v4l2_ext_control32 __user *ucontrols;
1156 	struct v4l2_ext_control __user *kcontrols;
1157 	u32 count;
1158 	u32 n;
1159 	compat_caddr_t p;
1160 
1161 	if (!access_ok(p32, sizeof(*p32)) ||
1162 	    assign_in_user(&p64->which, &p32->which) ||
1163 	    get_user(count, &p32->count) ||
1164 	    put_user(count, &p64->count) ||
1165 	    assign_in_user(&p64->error_idx, &p32->error_idx) ||
1166 	    assign_in_user(&p64->request_fd, &p32->request_fd) ||
1167 	    copy_in_user(p64->reserved, p32->reserved, sizeof(p64->reserved)))
1168 		return -EFAULT;
1169 
1170 	if (count == 0)
1171 		return put_user(NULL, &p64->controls);
1172 	if (count > V4L2_CID_MAX_CTRLS)
1173 		return -EINVAL;
1174 	if (get_user(p, &p32->controls))
1175 		return -EFAULT;
1176 	ucontrols = compat_ptr(p);
1177 	if (!access_ok(ucontrols, count * sizeof(*ucontrols)))
1178 		return -EFAULT;
1179 	if (aux_space < count * sizeof(*kcontrols))
1180 		return -EFAULT;
1181 	kcontrols = aux_buf;
1182 	if (put_user_force(kcontrols, &p64->controls))
1183 		return -EFAULT;
1184 
1185 	for (n = 0; n < count; n++) {
1186 		u32 id;
1187 
1188 		if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
1189 			return -EFAULT;
1190 
1191 		if (get_user(id, &kcontrols->id))
1192 			return -EFAULT;
1193 
1194 		if (ctrl_is_pointer(file, id)) {
1195 			void __user *s;
1196 
1197 			if (get_user(p, &ucontrols->string))
1198 				return -EFAULT;
1199 			s = compat_ptr(p);
1200 			if (put_user(s, &kcontrols->string))
1201 				return -EFAULT;
1202 		}
1203 		ucontrols++;
1204 		kcontrols++;
1205 	}
1206 	return 0;
1207 }
1208 
put_v4l2_ext_controls32(struct file * file,struct v4l2_ext_controls __user * p64,struct v4l2_ext_controls32 __user * p32)1209 static int put_v4l2_ext_controls32(struct file *file,
1210 				   struct v4l2_ext_controls __user *p64,
1211 				   struct v4l2_ext_controls32 __user *p32)
1212 {
1213 	struct v4l2_ext_control32 __user *ucontrols;
1214 	struct v4l2_ext_control *kcontrols;
1215 	u32 count;
1216 	u32 n;
1217 	compat_caddr_t p;
1218 
1219 	/*
1220 	 * We need to define kcontrols without __user, even though it does
1221 	 * point to data in userspace here. The reason is that v4l2-ioctl.c
1222 	 * copies it from userspace to kernelspace, so its definition in
1223 	 * videodev2.h doesn't have a __user markup. Defining kcontrols
1224 	 * with __user causes smatch warnings, so instead declare it
1225 	 * without __user and cast it as a userspace pointer where needed.
1226 	 */
1227 	if (!access_ok(p32, sizeof(*p32)) ||
1228 	    assign_in_user(&p32->which, &p64->which) ||
1229 	    get_user(count, &p64->count) ||
1230 	    put_user(count, &p32->count) ||
1231 	    assign_in_user(&p32->error_idx, &p64->error_idx) ||
1232 	    assign_in_user(&p32->request_fd, &p64->request_fd) ||
1233 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)) ||
1234 	    get_user(kcontrols, &p64->controls))
1235 		return -EFAULT;
1236 
1237 	if (!count || count > (U32_MAX/sizeof(*ucontrols)))
1238 		return 0;
1239 	if (get_user(p, &p32->controls))
1240 		return -EFAULT;
1241 	ucontrols = compat_ptr(p);
1242 	if (!access_ok(ucontrols, count * sizeof(*ucontrols)))
1243 		return -EFAULT;
1244 
1245 	for (n = 0; n < count; n++) {
1246 		unsigned int size = sizeof(*ucontrols);
1247 		u32 id;
1248 
1249 		if (get_user_cast(id, &kcontrols->id) ||
1250 		    put_user(id, &ucontrols->id) ||
1251 		    assign_in_user_cast(&ucontrols->size, &kcontrols->size) ||
1252 		    copy_in_user(&ucontrols->reserved2,
1253 				 (void __user *)&kcontrols->reserved2,
1254 				 sizeof(ucontrols->reserved2)))
1255 			return -EFAULT;
1256 
1257 		/*
1258 		 * Do not modify the pointer when copying a pointer control.
1259 		 * The contents of the pointer was changed, not the pointer
1260 		 * itself.
1261 		 */
1262 		if (ctrl_is_pointer(file, id))
1263 			size -= sizeof(ucontrols->value64);
1264 
1265 		if (copy_in_user(ucontrols,
1266 				 (void __user *)kcontrols, size))
1267 			return -EFAULT;
1268 
1269 		ucontrols++;
1270 		kcontrols++;
1271 	}
1272 	return 0;
1273 }
1274 
1275 #ifdef CONFIG_X86_64
1276 /*
1277  * x86 is the only compat architecture with different struct alignment
1278  * between 32-bit and 64-bit tasks.
1279  *
1280  * On all other architectures, v4l2_event32 and v4l2_event32_time32 are
1281  * the same as v4l2_event and v4l2_event_time32, so we can use the native
1282  * handlers, converting v4l2_event to v4l2_event_time32 if necessary.
1283  */
1284 struct v4l2_event32 {
1285 	__u32				type;
1286 	union {
1287 		compat_s64		value64;
1288 		__u8			data[64];
1289 	} u;
1290 	__u32				pending;
1291 	__u32				sequence;
1292 	struct {
1293 		compat_s64		tv_sec;
1294 		compat_s64		tv_nsec;
1295 	} timestamp;
1296 	__u32				id;
1297 	__u32				reserved[8];
1298 };
1299 
1300 struct v4l2_event32_time32 {
1301 	__u32				type;
1302 	union {
1303 		compat_s64		value64;
1304 		__u8			data[64];
1305 	} u;
1306 	__u32				pending;
1307 	__u32				sequence;
1308 	struct old_timespec32		timestamp;
1309 	__u32				id;
1310 	__u32				reserved[8];
1311 };
1312 
put_v4l2_event32(struct v4l2_event __user * p64,struct v4l2_event32 __user * p32)1313 static int put_v4l2_event32(struct v4l2_event __user *p64,
1314 			    struct v4l2_event32 __user *p32)
1315 {
1316 	if (!access_ok(p32, sizeof(*p32)) ||
1317 	    assign_in_user(&p32->type, &p64->type) ||
1318 	    copy_in_user(&p32->u, &p64->u, sizeof(p64->u)) ||
1319 	    assign_in_user(&p32->pending, &p64->pending) ||
1320 	    assign_in_user(&p32->sequence, &p64->sequence) ||
1321 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
1322 	    assign_in_user(&p32->timestamp.tv_nsec, &p64->timestamp.tv_nsec) ||
1323 	    assign_in_user(&p32->id, &p64->id) ||
1324 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
1325 		return -EFAULT;
1326 	return 0;
1327 }
1328 
put_v4l2_event32_time32(struct v4l2_event_time32 __user * p64,struct v4l2_event32_time32 __user * p32)1329 static int put_v4l2_event32_time32(struct v4l2_event_time32 __user *p64,
1330 				   struct v4l2_event32_time32 __user *p32)
1331 {
1332 	if (!access_ok(p32, sizeof(*p32)) ||
1333 	    assign_in_user(&p32->type, &p64->type) ||
1334 	    copy_in_user(&p32->u, &p64->u, sizeof(p64->u)) ||
1335 	    assign_in_user(&p32->pending, &p64->pending) ||
1336 	    assign_in_user(&p32->sequence, &p64->sequence) ||
1337 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
1338 	    assign_in_user(&p32->timestamp.tv_nsec, &p64->timestamp.tv_nsec) ||
1339 	    assign_in_user(&p32->id, &p64->id) ||
1340 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
1341 		return -EFAULT;
1342 	return 0;
1343 }
1344 #endif
1345 
1346 struct v4l2_edid32 {
1347 	__u32 pad;
1348 	__u32 start_block;
1349 	__u32 blocks;
1350 	__u32 reserved[5];
1351 	compat_caddr_t edid;
1352 };
1353 
get_v4l2_edid32(struct v4l2_edid __user * p64,struct v4l2_edid32 __user * p32)1354 static int get_v4l2_edid32(struct v4l2_edid __user *p64,
1355 			   struct v4l2_edid32 __user *p32)
1356 {
1357 	compat_uptr_t tmp;
1358 
1359 	if (!access_ok(p32, sizeof(*p32)) ||
1360 	    assign_in_user(&p64->pad, &p32->pad) ||
1361 	    assign_in_user(&p64->start_block, &p32->start_block) ||
1362 	    assign_in_user_cast(&p64->blocks, &p32->blocks) ||
1363 	    get_user(tmp, &p32->edid) ||
1364 	    put_user_force(compat_ptr(tmp), &p64->edid) ||
1365 	    copy_in_user(p64->reserved, p32->reserved, sizeof(p64->reserved)))
1366 		return -EFAULT;
1367 	return 0;
1368 }
1369 
put_v4l2_edid32(struct v4l2_edid __user * p64,struct v4l2_edid32 __user * p32)1370 static int put_v4l2_edid32(struct v4l2_edid __user *p64,
1371 			   struct v4l2_edid32 __user *p32)
1372 {
1373 	void *edid;
1374 
1375 	if (!access_ok(p32, sizeof(*p32)) ||
1376 	    assign_in_user(&p32->pad, &p64->pad) ||
1377 	    assign_in_user(&p32->start_block, &p64->start_block) ||
1378 	    assign_in_user(&p32->blocks, &p64->blocks) ||
1379 	    get_user(edid, &p64->edid) ||
1380 	    put_user(ptr_to_compat((void __user *)edid), &p32->edid) ||
1381 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
1382 		return -EFAULT;
1383 	return 0;
1384 }
1385 
1386 /*
1387  * List of ioctls that require 32-bits/64-bits conversion
1388  *
1389  * The V4L2 ioctls that aren't listed there don't have pointer arguments
1390  * and the struct size is identical for both 32 and 64 bits versions, so
1391  * they don't need translations.
1392  */
1393 
1394 #define VIDIOC_G_FMT32		_IOWR('V',  4, struct v4l2_format32)
1395 #define VIDIOC_S_FMT32		_IOWR('V',  5, struct v4l2_format32)
1396 #define VIDIOC_QUERYBUF32	_IOWR('V',  9, struct v4l2_buffer32)
1397 #define VIDIOC_QUERYBUF32_TIME32 _IOWR('V',  9, struct v4l2_buffer32_time32)
1398 #define VIDIOC_G_FBUF32		_IOR ('V', 10, struct v4l2_framebuffer32)
1399 #define VIDIOC_S_FBUF32		_IOW ('V', 11, struct v4l2_framebuffer32)
1400 #define VIDIOC_QBUF32		_IOWR('V', 15, struct v4l2_buffer32)
1401 #define VIDIOC_QBUF32_TIME32	_IOWR('V', 15, struct v4l2_buffer32_time32)
1402 #define VIDIOC_DQBUF32		_IOWR('V', 17, struct v4l2_buffer32)
1403 #define VIDIOC_DQBUF32_TIME32	_IOWR('V', 17, struct v4l2_buffer32_time32)
1404 #define VIDIOC_ENUMSTD32	_IOWR('V', 25, struct v4l2_standard32)
1405 #define VIDIOC_ENUMINPUT32	_IOWR('V', 26, struct v4l2_input32)
1406 #define VIDIOC_G_EDID32		_IOWR('V', 40, struct v4l2_edid32)
1407 #define VIDIOC_S_EDID32		_IOWR('V', 41, struct v4l2_edid32)
1408 #define VIDIOC_TRY_FMT32	_IOWR('V', 64, struct v4l2_format32)
1409 #define VIDIOC_G_EXT_CTRLS32    _IOWR('V', 71, struct v4l2_ext_controls32)
1410 #define VIDIOC_S_EXT_CTRLS32    _IOWR('V', 72, struct v4l2_ext_controls32)
1411 #define VIDIOC_TRY_EXT_CTRLS32  _IOWR('V', 73, struct v4l2_ext_controls32)
1412 #define	VIDIOC_DQEVENT32	_IOR ('V', 89, struct v4l2_event32)
1413 #define	VIDIOC_DQEVENT32_TIME32	_IOR ('V', 89, struct v4l2_event32_time32)
1414 #define VIDIOC_CREATE_BUFS32	_IOWR('V', 92, struct v4l2_create_buffers32)
1415 #define VIDIOC_PREPARE_BUF32	_IOWR('V', 93, struct v4l2_buffer32)
1416 #define VIDIOC_PREPARE_BUF32_TIME32 _IOWR('V', 93, struct v4l2_buffer32_time32)
1417 
1418 #define VIDIOC_OVERLAY32	_IOW ('V', 14, s32)
1419 #define VIDIOC_STREAMON32	_IOW ('V', 18, s32)
1420 #define VIDIOC_STREAMOFF32	_IOW ('V', 19, s32)
1421 #define VIDIOC_G_INPUT32	_IOR ('V', 38, s32)
1422 #define VIDIOC_S_INPUT32	_IOWR('V', 39, s32)
1423 #define VIDIOC_G_OUTPUT32	_IOR ('V', 46, s32)
1424 #define VIDIOC_S_OUTPUT32	_IOWR('V', 47, s32)
1425 
1426 /**
1427  * alloc_userspace() - Allocates a 64-bits userspace pointer compatible
1428  *	for calling the native 64-bits version of an ioctl.
1429  *
1430  * @size:	size of the structure itself to be allocated.
1431  * @aux_space:	extra size needed to store "extra" data, e.g. space for
1432  *		other __user data that is pointed to fields inside the
1433  *		structure.
1434  * @new_p64:	pointer to a pointer to be filled with the allocated struct.
1435  *
1436  * Return:
1437  *
1438  * if it can't allocate memory, either -ENOMEM or -EFAULT will be returned.
1439  * Zero otherwise.
1440  */
alloc_userspace(unsigned int size,u32 aux_space,void __user ** new_p64)1441 static int alloc_userspace(unsigned int size, u32 aux_space,
1442 			   void __user **new_p64)
1443 {
1444 	*new_p64 = compat_alloc_user_space(size + aux_space);
1445 	if (!*new_p64)
1446 		return -ENOMEM;
1447 	if (clear_user(*new_p64, size))
1448 		return -EFAULT;
1449 	return 0;
1450 }
1451 
1452 /**
1453  * do_video_ioctl() - Ancillary function with handles a compat32 ioctl call
1454  *
1455  * @file: pointer to &struct file with the file handler
1456  * @cmd: ioctl to be called
1457  * @arg: arguments passed from/to the ioctl handler
1458  *
1459  * This function is called when a 32 bits application calls a V4L2 ioctl
1460  * and the Kernel is compiled with 64 bits.
1461  *
1462  * This function is called by v4l2_compat_ioctl32() when the function is
1463  * not private to some specific driver.
1464  *
1465  * It converts a 32-bits struct into a 64 bits one, calls the native 64-bits
1466  * ioctl handler and fills back the 32-bits struct with the results of the
1467  * native call.
1468  */
do_video_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1469 static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1470 {
1471 	void __user *p32 = compat_ptr(arg);
1472 	void __user *new_p64 = NULL;
1473 	void __user *aux_buf;
1474 	u32 aux_space;
1475 	int compatible_arg = 1;
1476 	long err = 0;
1477 	unsigned int ncmd;
1478 
1479 	/*
1480 	 * 1. When struct size is different, converts the command.
1481 	 */
1482 	switch (cmd) {
1483 	case VIDIOC_G_FMT32: ncmd = VIDIOC_G_FMT; break;
1484 	case VIDIOC_S_FMT32: ncmd = VIDIOC_S_FMT; break;
1485 	case VIDIOC_QUERYBUF32: ncmd = VIDIOC_QUERYBUF; break;
1486 	case VIDIOC_QUERYBUF32_TIME32: ncmd = VIDIOC_QUERYBUF_TIME32; break;
1487 	case VIDIOC_G_FBUF32: ncmd = VIDIOC_G_FBUF; break;
1488 	case VIDIOC_S_FBUF32: ncmd = VIDIOC_S_FBUF; break;
1489 	case VIDIOC_QBUF32: ncmd = VIDIOC_QBUF; break;
1490 	case VIDIOC_QBUF32_TIME32: ncmd = VIDIOC_QBUF_TIME32; break;
1491 	case VIDIOC_DQBUF32: ncmd = VIDIOC_DQBUF; break;
1492 	case VIDIOC_DQBUF32_TIME32: ncmd = VIDIOC_DQBUF_TIME32; break;
1493 	case VIDIOC_ENUMSTD32: ncmd = VIDIOC_ENUMSTD; break;
1494 	case VIDIOC_ENUMINPUT32: ncmd = VIDIOC_ENUMINPUT; break;
1495 	case VIDIOC_TRY_FMT32: ncmd = VIDIOC_TRY_FMT; break;
1496 	case VIDIOC_G_EXT_CTRLS32: ncmd = VIDIOC_G_EXT_CTRLS; break;
1497 	case VIDIOC_S_EXT_CTRLS32: ncmd = VIDIOC_S_EXT_CTRLS; break;
1498 	case VIDIOC_TRY_EXT_CTRLS32: ncmd = VIDIOC_TRY_EXT_CTRLS; break;
1499 #ifdef CONFIG_X86_64
1500 	case VIDIOC_DQEVENT32: ncmd = VIDIOC_DQEVENT; break;
1501 	case VIDIOC_DQEVENT32_TIME32: ncmd = VIDIOC_DQEVENT_TIME32; break;
1502 #endif
1503 	case VIDIOC_OVERLAY32: ncmd = VIDIOC_OVERLAY; break;
1504 	case VIDIOC_STREAMON32: ncmd = VIDIOC_STREAMON; break;
1505 	case VIDIOC_STREAMOFF32: ncmd = VIDIOC_STREAMOFF; break;
1506 	case VIDIOC_G_INPUT32: ncmd = VIDIOC_G_INPUT; break;
1507 	case VIDIOC_S_INPUT32: ncmd = VIDIOC_S_INPUT; break;
1508 	case VIDIOC_G_OUTPUT32: ncmd = VIDIOC_G_OUTPUT; break;
1509 	case VIDIOC_S_OUTPUT32: ncmd = VIDIOC_S_OUTPUT; break;
1510 	case VIDIOC_CREATE_BUFS32: ncmd = VIDIOC_CREATE_BUFS; break;
1511 	case VIDIOC_PREPARE_BUF32: ncmd = VIDIOC_PREPARE_BUF; break;
1512 	case VIDIOC_PREPARE_BUF32_TIME32: ncmd = VIDIOC_PREPARE_BUF_TIME32; break;
1513 	case VIDIOC_G_EDID32: ncmd = VIDIOC_G_EDID; break;
1514 	case VIDIOC_S_EDID32: ncmd = VIDIOC_S_EDID; break;
1515 	default: ncmd = cmd; break;
1516 	}
1517 
1518 	/*
1519 	 * 2. Allocates a 64-bits userspace pointer to store the
1520 	 * values of the ioctl and copy data from the 32-bits __user
1521 	 * argument into it.
1522 	 */
1523 	switch (cmd) {
1524 	case VIDIOC_OVERLAY32:
1525 	case VIDIOC_STREAMON32:
1526 	case VIDIOC_STREAMOFF32:
1527 	case VIDIOC_S_INPUT32:
1528 	case VIDIOC_S_OUTPUT32:
1529 		err = alloc_userspace(sizeof(unsigned int), 0, &new_p64);
1530 		if (!err && assign_in_user((unsigned int __user *)new_p64,
1531 					   (compat_uint_t __user *)p32))
1532 			err = -EFAULT;
1533 		compatible_arg = 0;
1534 		break;
1535 
1536 	case VIDIOC_G_INPUT32:
1537 	case VIDIOC_G_OUTPUT32:
1538 		err = alloc_userspace(sizeof(unsigned int), 0, &new_p64);
1539 		compatible_arg = 0;
1540 		break;
1541 
1542 	case VIDIOC_G_EDID32:
1543 	case VIDIOC_S_EDID32:
1544 		err = alloc_userspace(sizeof(struct v4l2_edid), 0, &new_p64);
1545 		if (!err)
1546 			err = get_v4l2_edid32(new_p64, p32);
1547 		compatible_arg = 0;
1548 		break;
1549 
1550 	case VIDIOC_G_FMT32:
1551 	case VIDIOC_S_FMT32:
1552 	case VIDIOC_TRY_FMT32:
1553 		err = bufsize_v4l2_format(p32, &aux_space);
1554 		if (!err)
1555 			err = alloc_userspace(sizeof(struct v4l2_format),
1556 					      aux_space, &new_p64);
1557 		if (!err) {
1558 			aux_buf = new_p64 + sizeof(struct v4l2_format);
1559 			err = get_v4l2_format32(new_p64, p32,
1560 						aux_buf, aux_space);
1561 		}
1562 		compatible_arg = 0;
1563 		break;
1564 
1565 	case VIDIOC_CREATE_BUFS32:
1566 		err = bufsize_v4l2_create(p32, &aux_space);
1567 		if (!err)
1568 			err = alloc_userspace(sizeof(struct v4l2_create_buffers),
1569 					      aux_space, &new_p64);
1570 		if (!err) {
1571 			aux_buf = new_p64 + sizeof(struct v4l2_create_buffers);
1572 			err = get_v4l2_create32(new_p64, p32,
1573 						aux_buf, aux_space);
1574 		}
1575 		compatible_arg = 0;
1576 		break;
1577 
1578 	case VIDIOC_PREPARE_BUF32:
1579 	case VIDIOC_QUERYBUF32:
1580 	case VIDIOC_QBUF32:
1581 	case VIDIOC_DQBUF32:
1582 		err = bufsize_v4l2_buffer(p32, &aux_space);
1583 		if (!err)
1584 			err = alloc_userspace(sizeof(struct v4l2_buffer),
1585 					      aux_space, &new_p64);
1586 		if (!err) {
1587 			aux_buf = new_p64 + sizeof(struct v4l2_buffer);
1588 			err = get_v4l2_buffer32(new_p64, p32,
1589 						aux_buf, aux_space);
1590 		}
1591 		compatible_arg = 0;
1592 		break;
1593 
1594 	case VIDIOC_PREPARE_BUF32_TIME32:
1595 	case VIDIOC_QUERYBUF32_TIME32:
1596 	case VIDIOC_QBUF32_TIME32:
1597 	case VIDIOC_DQBUF32_TIME32:
1598 		err = bufsize_v4l2_buffer_time32(p32, &aux_space);
1599 		if (!err)
1600 			err = alloc_userspace(sizeof(struct v4l2_buffer),
1601 					      aux_space, &new_p64);
1602 		if (!err) {
1603 			aux_buf = new_p64 + sizeof(struct v4l2_buffer);
1604 			err = get_v4l2_buffer32_time32(new_p64, p32,
1605 						       aux_buf, aux_space);
1606 		}
1607 		compatible_arg = 0;
1608 		break;
1609 
1610 	case VIDIOC_S_FBUF32:
1611 		err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
1612 				      &new_p64);
1613 		if (!err)
1614 			err = get_v4l2_framebuffer32(new_p64, p32);
1615 		compatible_arg = 0;
1616 		break;
1617 
1618 	case VIDIOC_G_FBUF32:
1619 		err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
1620 				      &new_p64);
1621 		compatible_arg = 0;
1622 		break;
1623 
1624 	case VIDIOC_ENUMSTD32:
1625 		err = alloc_userspace(sizeof(struct v4l2_standard), 0,
1626 				      &new_p64);
1627 		if (!err)
1628 			err = get_v4l2_standard32(new_p64, p32);
1629 		compatible_arg = 0;
1630 		break;
1631 
1632 	case VIDIOC_ENUMINPUT32:
1633 		err = alloc_userspace(sizeof(struct v4l2_input), 0, &new_p64);
1634 		if (!err)
1635 			err = get_v4l2_input32(new_p64, p32);
1636 		compatible_arg = 0;
1637 		break;
1638 
1639 	case VIDIOC_G_EXT_CTRLS32:
1640 	case VIDIOC_S_EXT_CTRLS32:
1641 	case VIDIOC_TRY_EXT_CTRLS32:
1642 		err = bufsize_v4l2_ext_controls(p32, &aux_space);
1643 		if (!err)
1644 			err = alloc_userspace(sizeof(struct v4l2_ext_controls),
1645 					      aux_space, &new_p64);
1646 		if (!err) {
1647 			aux_buf = new_p64 + sizeof(struct v4l2_ext_controls);
1648 			err = get_v4l2_ext_controls32(file, new_p64, p32,
1649 						      aux_buf, aux_space);
1650 		}
1651 		compatible_arg = 0;
1652 		break;
1653 #ifdef CONFIG_X86_64
1654 	case VIDIOC_DQEVENT32:
1655 		err = alloc_userspace(sizeof(struct v4l2_event), 0, &new_p64);
1656 		compatible_arg = 0;
1657 		break;
1658 	case VIDIOC_DQEVENT32_TIME32:
1659 		err = alloc_userspace(sizeof(struct v4l2_event_time32), 0, &new_p64);
1660 		compatible_arg = 0;
1661 		break;
1662 #endif
1663 	}
1664 	if (err)
1665 		return err;
1666 
1667 	/*
1668 	 * 3. Calls the native 64-bits ioctl handler.
1669 	 *
1670 	 * For the functions where a conversion was not needed,
1671 	 * compatible_arg is true, and it will call it with the arguments
1672 	 * provided by userspace and stored at @p32 var.
1673 	 *
1674 	 * Otherwise, it will pass the newly allocated @new_p64 argument.
1675 	 */
1676 	if (compatible_arg)
1677 		err = native_ioctl(file, ncmd, (unsigned long)p32);
1678 	else
1679 		err = native_ioctl(file, ncmd, (unsigned long)new_p64);
1680 
1681 	if (err == -ENOTTY)
1682 		return err;
1683 
1684 	/*
1685 	 * 4. Special case: even after an error we need to put the
1686 	 * results back for some ioctls.
1687 	 *
1688 	 * In the case of EXT_CTRLS, the error_idx will contain information
1689 	 * on which control failed.
1690 	 *
1691 	 * In the case of S_EDID, the driver can return E2BIG and set
1692 	 * the blocks to maximum allowed value.
1693 	 */
1694 	switch (cmd) {
1695 	case VIDIOC_G_EXT_CTRLS32:
1696 	case VIDIOC_S_EXT_CTRLS32:
1697 	case VIDIOC_TRY_EXT_CTRLS32:
1698 		if (put_v4l2_ext_controls32(file, new_p64, p32))
1699 			err = -EFAULT;
1700 		break;
1701 	case VIDIOC_S_EDID32:
1702 		if (put_v4l2_edid32(new_p64, p32))
1703 			err = -EFAULT;
1704 		break;
1705 	}
1706 	if (err)
1707 		return err;
1708 
1709 	/*
1710 	 * 5. Copy the data returned at the 64 bits userspace pointer to
1711 	 * the original 32 bits structure.
1712 	 */
1713 	switch (cmd) {
1714 	case VIDIOC_S_INPUT32:
1715 	case VIDIOC_S_OUTPUT32:
1716 	case VIDIOC_G_INPUT32:
1717 	case VIDIOC_G_OUTPUT32:
1718 		if (assign_in_user((compat_uint_t __user *)p32,
1719 				   ((unsigned int __user *)new_p64)))
1720 			err = -EFAULT;
1721 		break;
1722 
1723 	case VIDIOC_G_FBUF32:
1724 		err = put_v4l2_framebuffer32(new_p64, p32);
1725 		break;
1726 
1727 #ifdef CONFIG_X86_64
1728 	case VIDIOC_DQEVENT32:
1729 		err = put_v4l2_event32(new_p64, p32);
1730 		break;
1731 
1732 	case VIDIOC_DQEVENT32_TIME32:
1733 		err = put_v4l2_event32_time32(new_p64, p32);
1734 		break;
1735 #endif
1736 
1737 	case VIDIOC_G_EDID32:
1738 		err = put_v4l2_edid32(new_p64, p32);
1739 		break;
1740 
1741 	case VIDIOC_G_FMT32:
1742 	case VIDIOC_S_FMT32:
1743 	case VIDIOC_TRY_FMT32:
1744 		err = put_v4l2_format32(new_p64, p32);
1745 		break;
1746 
1747 	case VIDIOC_CREATE_BUFS32:
1748 		err = put_v4l2_create32(new_p64, p32);
1749 		break;
1750 
1751 	case VIDIOC_PREPARE_BUF32:
1752 	case VIDIOC_QUERYBUF32:
1753 	case VIDIOC_QBUF32:
1754 	case VIDIOC_DQBUF32:
1755 		err = put_v4l2_buffer32(new_p64, p32);
1756 		break;
1757 
1758 	case VIDIOC_PREPARE_BUF32_TIME32:
1759 	case VIDIOC_QUERYBUF32_TIME32:
1760 	case VIDIOC_QBUF32_TIME32:
1761 	case VIDIOC_DQBUF32_TIME32:
1762 		err = put_v4l2_buffer32_time32(new_p64, p32);
1763 		break;
1764 
1765 	case VIDIOC_ENUMSTD32:
1766 		err = put_v4l2_standard32(new_p64, p32);
1767 		break;
1768 
1769 	case VIDIOC_ENUMINPUT32:
1770 		err = put_v4l2_input32(new_p64, p32);
1771 		break;
1772 	}
1773 	return err;
1774 }
1775 
1776 /**
1777  * v4l2_compat_ioctl32() - Handles a compat32 ioctl call
1778  *
1779  * @file: pointer to &struct file with the file handler
1780  * @cmd: ioctl to be called
1781  * @arg: arguments passed from/to the ioctl handler
1782  *
1783  * This function is meant to be used as .compat_ioctl fops at v4l2-dev.c
1784  * in order to deal with 32-bit calls on a 64-bits Kernel.
1785  *
1786  * This function calls do_video_ioctl() for non-private V4L2 ioctls.
1787  * If the function is a private one it calls vdev->fops->compat_ioctl32
1788  * instead.
1789  */
v4l2_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)1790 long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
1791 {
1792 	struct video_device *vdev = video_devdata(file);
1793 	long ret = -ENOIOCTLCMD;
1794 
1795 	if (!file->f_op->unlocked_ioctl)
1796 		return ret;
1797 
1798 	if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
1799 		ret = do_video_ioctl(file, cmd, arg);
1800 	else if (vdev->fops->compat_ioctl32)
1801 		ret = vdev->fops->compat_ioctl32(file, cmd, arg);
1802 
1803 	if (ret == -ENOIOCTLCMD)
1804 		pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
1805 			 _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
1806 	return ret;
1807 }
1808 EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);
1809