1 // SPDX-License-Identifier: GPL-2.0
3 * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
5 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
6 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
8 * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
10 * These routines maintain argument size conversion between 32bit and 64bit
14 #include <linux/joystick.h>
16 #include <linux/types.h>
17 #include <linux/compat.h>
18 #include <linux/kernel.h>
19 #include <linux/capability.h>
20 #include <linux/compiler.h>
21 #include <linux/sched.h>
22 #include <linux/smp.h>
23 #include <linux/ioctl.h>
25 #include <linux/raid/md_u.h>
26 #include <linux/falloc.h>
27 #include <linux/file.h>
28 #include <linux/ppp-ioctl.h>
29 #include <linux/if_pppox.h>
30 #include <linux/tty.h>
31 #include <linux/vt_kern.h>
32 #include <linux/raw.h>
33 #include <linux/blkdev.h>
34 #include <linux/pci.h>
35 #include <linux/serial.h>
36 #include <linux/ctype.h>
37 #include <linux/syscalls.h>
38 #include <linux/gfp.h>
39 #include <linux/cec.h>
44 #include <linux/cdrom.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_ioctl.h>
51 #include <linux/uaccess.h>
52 #include <linux/watchdog.h>
54 #include <linux/hiddev.h>
57 #include <linux/sort.h>
64 #define convert_in_user(srcptr, dstptr) \
66 typeof(*srcptr) val; \
68 get_user(val, srcptr) || put_user(val, dstptr); \
71 static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
75 err = security_file_ioctl(file, cmd, arg);
79 return vfs_ioctl(file, cmd, arg);
83 typedef struct sg_io_hdr32 {
84 compat_int_t interface_id; /* [i] 'S' for SCSI generic (required) */
85 compat_int_t dxfer_direction; /* [i] data transfer direction */
86 unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */
87 unsigned char mx_sb_len; /* [i] max length to write to sbp */
88 unsigned short iovec_count; /* [i] 0 implies no scatter gather */
89 compat_uint_t dxfer_len; /* [i] byte count of data transfer */
90 compat_uint_t dxferp; /* [i], [*io] points to data transfer memory
91 or scatter gather list */
92 compat_uptr_t cmdp; /* [i], [*i] points to command to perform */
93 compat_uptr_t sbp; /* [i], [*o] points to sense_buffer memory */
94 compat_uint_t timeout; /* [i] MAX_UINT->no timeout (unit: millisec) */
95 compat_uint_t flags; /* [i] 0 -> default, see SG_FLAG... */
96 compat_int_t pack_id; /* [i->o] unused internally (normally) */
97 compat_uptr_t usr_ptr; /* [i->o] unused internally */
98 unsigned char status; /* [o] scsi status */
99 unsigned char masked_status; /* [o] shifted, masked scsi status */
100 unsigned char msg_status; /* [o] messaging level data (optional) */
101 unsigned char sb_len_wr; /* [o] byte count actually written to sbp */
102 unsigned short host_status; /* [o] errors from host adapter */
103 unsigned short driver_status; /* [o] errors from software driver */
104 compat_int_t resid; /* [o] dxfer_len - actual_transferred */
105 compat_uint_t duration; /* [o] time taken by cmd (unit: millisec) */
106 compat_uint_t info; /* [o] auxiliary information */
107 } sg_io_hdr32_t; /* 64 bytes long (on sparc32) */
109 typedef struct sg_iovec32 {
110 compat_uint_t iov_base;
111 compat_uint_t iov_len;
114 static int sg_build_iovec(sg_io_hdr_t __user *sgio, void __user *dxferp, u16 iovec_count)
116 sg_iovec_t __user *iov = (sg_iovec_t __user *) (sgio + 1);
117 sg_iovec32_t __user *iov32 = dxferp;
120 for (i = 0; i < iovec_count; i++) {
123 if (get_user(base, &iov32[i].iov_base) ||
124 get_user(len, &iov32[i].iov_len) ||
125 put_user(compat_ptr(base), &iov[i].iov_base) ||
126 put_user(len, &iov[i].iov_len))
130 if (put_user(iov, &sgio->dxferp))
135 static int sg_ioctl_trans(struct file *file, unsigned int cmd,
136 sg_io_hdr32_t __user *sgio32)
138 sg_io_hdr_t __user *sgio;
145 if (get_user(interface_id, &sgio32->interface_id))
147 if (interface_id != 'S')
148 return do_ioctl(file, cmd, (unsigned long)sgio32);
150 if (get_user(iovec_count, &sgio32->iovec_count))
154 void __user *top = compat_alloc_user_space(0);
155 void __user *new = compat_alloc_user_space(sizeof(sg_io_hdr_t) +
156 (iovec_count * sizeof(sg_iovec_t)));
163 /* Ok, now construct. */
164 if (copy_in_user(&sgio->interface_id, &sgio32->interface_id,
166 (2 * sizeof(unsigned char)) +
167 (1 * sizeof(unsigned short)) +
168 (1 * sizeof(unsigned int))))
171 if (get_user(data, &sgio32->dxferp))
173 dxferp = compat_ptr(data);
175 if (sg_build_iovec(sgio, dxferp, iovec_count))
178 if (put_user(dxferp, &sgio->dxferp))
183 unsigned char __user *cmdp;
184 unsigned char __user *sbp;
186 if (get_user(data, &sgio32->cmdp))
188 cmdp = compat_ptr(data);
190 if (get_user(data, &sgio32->sbp))
192 sbp = compat_ptr(data);
194 if (put_user(cmdp, &sgio->cmdp) ||
195 put_user(sbp, &sgio->sbp))
199 if (copy_in_user(&sgio->timeout, &sgio32->timeout,
203 if (get_user(data, &sgio32->usr_ptr))
205 if (put_user(compat_ptr(data), &sgio->usr_ptr))
208 err = do_ioctl(file, cmd, (unsigned long) sgio);
213 if (copy_in_user(&sgio32->pack_id, &sgio->pack_id,
215 get_user(datap, &sgio->usr_ptr) ||
216 put_user((u32)(unsigned long)datap,
218 copy_in_user(&sgio32->status, &sgio->status,
219 (4 * sizeof(unsigned char)) +
220 (2 * sizeof(unsigned short)) +
228 struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
234 compat_uptr_t usr_ptr;
235 unsigned int duration;
239 static int sg_grt_trans(struct file *file,
240 unsigned int cmd, struct compat_sg_req_info __user *o)
243 sg_req_info_t __user *r;
244 r = compat_alloc_user_space(sizeof(sg_req_info_t)*SG_MAX_QUEUE);
245 err = do_ioctl(file, cmd, (unsigned long)r);
248 for (i = 0; i < SG_MAX_QUEUE; i++) {
252 if (copy_in_user(o + i, r + i, offsetof(sg_req_info_t, usr_ptr)) ||
253 get_user(ptr, &r[i].usr_ptr) ||
254 get_user(d, &r[i].duration) ||
255 put_user((u32)(unsigned long)(ptr), &o[i].usr_ptr) ||
256 put_user(d, &o[i].duration))
261 #endif /* CONFIG_BLOCK */
263 struct sock_fprog32 {
265 compat_caddr_t filter;
268 #define PPPIOCSPASS32 _IOW('t', 71, struct sock_fprog32)
269 #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
271 static int ppp_sock_fprog_ioctl_trans(struct file *file,
272 unsigned int cmd, struct sock_fprog32 __user *u_fprog32)
274 struct sock_fprog __user *u_fprog64 = compat_alloc_user_space(sizeof(struct sock_fprog));
279 if (get_user(flen, &u_fprog32->len) ||
280 get_user(fptr32, &u_fprog32->filter))
283 fptr64 = compat_ptr(fptr32);
285 if (put_user(flen, &u_fprog64->len) ||
286 put_user(fptr64, &u_fprog64->filter))
289 if (cmd == PPPIOCSPASS32)
294 return do_ioctl(file, cmd, (unsigned long) u_fprog64);
297 struct ppp_option_data32 {
300 compat_int_t transmit;
302 #define PPPIOCSCOMPRESS32 _IOW('t', 77, struct ppp_option_data32)
305 compat_time_t xmit_idle;
306 compat_time_t recv_idle;
308 #define PPPIOCGIDLE32 _IOR('t', 63, struct ppp_idle32)
310 static int ppp_gidle(struct file *file, unsigned int cmd,
311 struct ppp_idle32 __user *idle32)
313 struct ppp_idle __user *idle;
314 __kernel_time_t xmit, recv;
317 idle = compat_alloc_user_space(sizeof(*idle));
319 err = do_ioctl(file, PPPIOCGIDLE, (unsigned long) idle);
322 if (get_user(xmit, &idle->xmit_idle) ||
323 get_user(recv, &idle->recv_idle) ||
324 put_user(xmit, &idle32->xmit_idle) ||
325 put_user(recv, &idle32->recv_idle))
331 static int ppp_scompress(struct file *file, unsigned int cmd,
332 struct ppp_option_data32 __user *odata32)
334 struct ppp_option_data __user *odata;
338 odata = compat_alloc_user_space(sizeof(*odata));
340 if (get_user(data, &odata32->ptr))
343 datap = compat_ptr(data);
344 if (put_user(datap, &odata->ptr))
347 if (copy_in_user(&odata->length, &odata32->length,
348 sizeof(__u32) + sizeof(int)))
351 return do_ioctl(file, PPPIOCSCOMPRESS, (unsigned long) odata);
355 * simple reversible transform to make our table more evenly
356 * distributed after sorting.
358 #define XFORM(i) (((i) ^ ((i) << 27) ^ ((i) << 17)) & 0xffffffff)
360 #define COMPATIBLE_IOCTL(cmd) XFORM((u32)cmd),
361 /* ioctl should not be warned about even if it's not implemented.
362 Valid reasons to use this:
363 - It is implemented with ->compat_ioctl on some device, but programs
364 call it on others too.
365 - The ioctl is not implemented in the native kernel, but programs
366 call it commonly anyways.
367 Most other reasons are not valid. */
368 #define IGNORE_IOCTL(cmd) COMPATIBLE_IOCTL(cmd)
370 static unsigned int ioctl_pointer[] = {
371 /* compatible ioctls first */
373 COMPATIBLE_IOCTL(TIOCOUTQ)
374 /* 'X' - originally XFS but some now in the VFS */
375 COMPATIBLE_IOCTL(FITRIM)
378 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_IDLUN)
379 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORLOCK)
380 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORUNLOCK)
381 COMPATIBLE_IOCTL(SCSI_IOCTL_TEST_UNIT_READY)
382 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_BUS_NUMBER)
383 COMPATIBLE_IOCTL(SCSI_IOCTL_SEND_COMMAND)
384 COMPATIBLE_IOCTL(SCSI_IOCTL_PROBE_HOST)
385 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_PCI)
387 /* Big V (don't complain on serial console) */
388 IGNORE_IOCTL(VT_OPENQRY)
389 IGNORE_IOCTL(VT_GETMODE)
391 * These two are only for the sbus rtc driver, but
392 * hwclock tries them on every rtc device first when
393 * running on sparc. On other architectures the entries
394 * are useless but harmless.
396 COMPATIBLE_IOCTL(_IOR('p', 20, int[7])) /* RTCGET */
397 COMPATIBLE_IOCTL(_IOW('p', 21, int[7])) /* RTCSET */
399 /* md calls this on random blockdevs */
400 IGNORE_IOCTL(RAID_VERSION)
401 /* qemu/qemu-img might call these two on plain files for probing */
402 IGNORE_IOCTL(CDROM_DRIVE_STATUS)
403 IGNORE_IOCTL(FDGETPRM32)
405 COMPATIBLE_IOCTL(SG_SET_TIMEOUT)
406 COMPATIBLE_IOCTL(SG_GET_TIMEOUT)
407 COMPATIBLE_IOCTL(SG_EMULATED_HOST)
408 COMPATIBLE_IOCTL(SG_GET_TRANSFORM)
409 COMPATIBLE_IOCTL(SG_SET_RESERVED_SIZE)
410 COMPATIBLE_IOCTL(SG_GET_RESERVED_SIZE)
411 COMPATIBLE_IOCTL(SG_GET_SCSI_ID)
412 COMPATIBLE_IOCTL(SG_SET_FORCE_LOW_DMA)
413 COMPATIBLE_IOCTL(SG_GET_LOW_DMA)
414 COMPATIBLE_IOCTL(SG_SET_FORCE_PACK_ID)
415 COMPATIBLE_IOCTL(SG_GET_PACK_ID)
416 COMPATIBLE_IOCTL(SG_GET_NUM_WAITING)
417 COMPATIBLE_IOCTL(SG_SET_DEBUG)
418 COMPATIBLE_IOCTL(SG_GET_SG_TABLESIZE)
419 COMPATIBLE_IOCTL(SG_GET_COMMAND_Q)
420 COMPATIBLE_IOCTL(SG_SET_COMMAND_Q)
421 COMPATIBLE_IOCTL(SG_GET_VERSION_NUM)
422 COMPATIBLE_IOCTL(SG_NEXT_CMD_LEN)
423 COMPATIBLE_IOCTL(SG_SCSI_RESET)
424 COMPATIBLE_IOCTL(SG_GET_REQUEST_TABLE)
425 COMPATIBLE_IOCTL(SG_SET_KEEP_ORPHAN)
426 COMPATIBLE_IOCTL(SG_GET_KEEP_ORPHAN)
429 COMPATIBLE_IOCTL(PPPIOCGFLAGS)
430 COMPATIBLE_IOCTL(PPPIOCSFLAGS)
431 COMPATIBLE_IOCTL(PPPIOCGASYNCMAP)
432 COMPATIBLE_IOCTL(PPPIOCSASYNCMAP)
433 COMPATIBLE_IOCTL(PPPIOCGUNIT)
434 COMPATIBLE_IOCTL(PPPIOCGRASYNCMAP)
435 COMPATIBLE_IOCTL(PPPIOCSRASYNCMAP)
436 COMPATIBLE_IOCTL(PPPIOCGMRU)
437 COMPATIBLE_IOCTL(PPPIOCSMRU)
438 COMPATIBLE_IOCTL(PPPIOCSMAXCID)
439 COMPATIBLE_IOCTL(PPPIOCGXASYNCMAP)
440 COMPATIBLE_IOCTL(PPPIOCSXASYNCMAP)
441 COMPATIBLE_IOCTL(PPPIOCXFERUNIT)
442 /* PPPIOCSCOMPRESS is translated */
443 COMPATIBLE_IOCTL(PPPIOCGNPMODE)
444 COMPATIBLE_IOCTL(PPPIOCSNPMODE)
445 COMPATIBLE_IOCTL(PPPIOCGDEBUG)
446 COMPATIBLE_IOCTL(PPPIOCSDEBUG)
447 /* PPPIOCSPASS is translated */
448 /* PPPIOCSACTIVE is translated */
449 /* PPPIOCGIDLE is translated */
450 COMPATIBLE_IOCTL(PPPIOCNEWUNIT)
451 COMPATIBLE_IOCTL(PPPIOCATTACH)
452 COMPATIBLE_IOCTL(PPPIOCDETACH)
453 COMPATIBLE_IOCTL(PPPIOCSMRRU)
454 COMPATIBLE_IOCTL(PPPIOCCONNECT)
455 COMPATIBLE_IOCTL(PPPIOCDISCONN)
456 COMPATIBLE_IOCTL(PPPIOCATTCHAN)
457 COMPATIBLE_IOCTL(PPPIOCGCHAN)
458 COMPATIBLE_IOCTL(PPPIOCGL2TPSTATS)
460 COMPATIBLE_IOCTL(RAW_SETBIND)
461 COMPATIBLE_IOCTL(RAW_GETBIND)
463 COMPATIBLE_IOCTL(WDIOC_GETSUPPORT)
464 COMPATIBLE_IOCTL(WDIOC_GETSTATUS)
465 COMPATIBLE_IOCTL(WDIOC_GETBOOTSTATUS)
466 COMPATIBLE_IOCTL(WDIOC_GETTEMP)
467 COMPATIBLE_IOCTL(WDIOC_SETOPTIONS)
468 COMPATIBLE_IOCTL(WDIOC_KEEPALIVE)
469 COMPATIBLE_IOCTL(WDIOC_SETTIMEOUT)
470 COMPATIBLE_IOCTL(WDIOC_GETTIMEOUT)
471 COMPATIBLE_IOCTL(WDIOC_SETPRETIMEOUT)
472 COMPATIBLE_IOCTL(WDIOC_GETPRETIMEOUT)
474 COMPATIBLE_IOCTL(RNDGETENTCNT)
475 COMPATIBLE_IOCTL(RNDADDTOENTCNT)
476 COMPATIBLE_IOCTL(RNDGETPOOL)
477 COMPATIBLE_IOCTL(RNDADDENTROPY)
478 COMPATIBLE_IOCTL(RNDZAPENTCNT)
479 COMPATIBLE_IOCTL(RNDCLEARPOOL)
481 COMPATIBLE_IOCTL(PCIIOC_CONTROLLER)
482 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_IO)
483 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_MEM)
484 COMPATIBLE_IOCTL(PCIIOC_WRITE_COMBINE)
486 COMPATIBLE_IOCTL(JSIOCGVERSION)
487 COMPATIBLE_IOCTL(JSIOCGAXES)
488 COMPATIBLE_IOCTL(JSIOCGBUTTONS)
489 COMPATIBLE_IOCTL(JSIOCGNAME(0))
491 /* fat 'r' ioctls. These are handled by fat with ->compat_ioctl,
492 but we don't want warnings on other file systems. So declare
493 them as compatible here. */
494 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
495 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
497 IGNORE_IOCTL(VFAT_IOCTL_READDIR_BOTH32)
498 IGNORE_IOCTL(VFAT_IOCTL_READDIR_SHORT32)
501 /* Sparc framebuffers, handled in sbusfb_compat_ioctl() */
502 IGNORE_IOCTL(FBIOGTYPE)
503 IGNORE_IOCTL(FBIOSATTR)
504 IGNORE_IOCTL(FBIOGATTR)
505 IGNORE_IOCTL(FBIOSVIDEO)
506 IGNORE_IOCTL(FBIOGVIDEO)
507 IGNORE_IOCTL(FBIOSCURPOS)
508 IGNORE_IOCTL(FBIOGCURPOS)
509 IGNORE_IOCTL(FBIOGCURMAX)
510 IGNORE_IOCTL(FBIOPUTCMAP32)
511 IGNORE_IOCTL(FBIOGETCMAP32)
512 IGNORE_IOCTL(FBIOSCURSOR32)
513 IGNORE_IOCTL(FBIOGCURSOR32)
518 * Convert common ioctl arguments based on their command number
520 * Please do not add any code in here. Instead, implement
521 * a compat_ioctl operation in the place that handleѕ the
522 * ioctl for the native case.
524 static long do_ioctl_trans(unsigned int cmd,
525 unsigned long arg, struct file *file)
527 void __user *argp = compat_ptr(arg);
531 return ppp_gidle(file, cmd, argp);
532 case PPPIOCSCOMPRESS32:
533 return ppp_scompress(file, cmd, argp);
535 case PPPIOCSACTIVE32:
536 return ppp_sock_fprog_ioctl_trans(file, cmd, argp);
539 return sg_ioctl_trans(file, cmd, argp);
540 case SG_GET_REQUEST_TABLE:
541 return sg_grt_trans(file, cmd, argp);
546 * These take an integer instead of a pointer as 'arg',
547 * so we must not do a compat_ptr() translation.
551 case HOT_REMOVE_DISK:
553 case SET_DISK_FAULTY:
554 case SET_BITMAP_FILE:
555 return vfs_ioctl(file, cmd, arg);
561 static int compat_ioctl_check_table(unsigned int xcmd)
564 const int max = ARRAY_SIZE(ioctl_pointer) - 1;
566 BUILD_BUG_ON(max >= (1 << 16));
568 /* guess initial offset into table, assuming a
569 normalized distribution */
570 i = ((xcmd >> 16) * max) >> 16;
572 /* do linear search up first, until greater or equal */
573 while (ioctl_pointer[i] < xcmd && i < max)
576 /* then do linear search down */
577 while (ioctl_pointer[i] > xcmd && i > 0)
580 return ioctl_pointer[i] == xcmd;
583 COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
584 compat_ulong_t, arg32)
586 unsigned long arg = arg32;
587 struct fd f = fdget(fd);
592 /* RED-PEN how should LSM module know it's handling 32bit? */
593 error = security_file_ioctl(f.file, cmd, arg);
598 /* these are never seen by ->ioctl(), no argument or int argument */
605 /* these are never seen by ->ioctl(), pointer argument */
615 * The next group is the stuff handled inside file_ioctl().
616 * For regular files these never reach ->ioctl(); for
617 * devices, sockets, etc. they do and one (FIONREAD) is
618 * even accepted in some cases. In all those cases
619 * argument has the same type, so we can handle these
620 * here, shunting them towards do_vfs_ioctl().
621 * ->compat_ioctl() will never see any of those.
623 /* pointer argument, never actually handled by ->ioctl() */
626 /* handled by some ->ioctl(); always a pointer to int */
629 /* these two get messy on amd64 due to alignment differences */
630 #if defined(CONFIG_X86_64)
631 case FS_IOC_RESVSP_32:
632 case FS_IOC_RESVSP64_32:
633 error = compat_ioctl_preallocate(f.file, compat_ptr(arg));
637 case FS_IOC_RESVSP64:
642 if (f.file->f_op->compat_ioctl) {
643 error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
644 if (error != -ENOIOCTLCMD)
648 if (!f.file->f_op->unlocked_ioctl)
653 if (compat_ioctl_check_table(XFORM(cmd)))
656 error = do_ioctl_trans(cmd, arg, f.file);
657 if (error == -ENOIOCTLCMD)
663 arg = (unsigned long)compat_ptr(arg);
665 error = do_vfs_ioctl(f.file, fd, cmd, arg);
672 static int __init init_sys32_ioctl_cmp(const void *p, const void *q)
675 a = *(unsigned int *)p;
676 b = *(unsigned int *)q;
684 static int __init init_sys32_ioctl(void)
686 sort(ioctl_pointer, ARRAY_SIZE(ioctl_pointer), sizeof(*ioctl_pointer),
687 init_sys32_ioctl_cmp, NULL);
690 __initcall(init_sys32_ioctl);