Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

compat_ioctl: add compat_ptr_ioctl()

Many drivers have ioctl() handlers that are completely compatible between
32-bit and 64-bit architectures, except for the argument that is passed
down from user space and may have to be passed through compat_ptr()
in order to become a valid 64-bit pointer.

Using ".compat_ptr = compat_ptr_ioctl" in file operations should let
us simplify a lot of those drivers to avoid #ifdef checks, and convert
additional drivers that don't have proper compat handling yet.

On most architectures, the compat_ptr_ioctl() just passes all arguments
to the corresponding ->ioctl handler. The exception is arch/s390, where
compat_ptr() clears the top bit of a 32-bit pointer value, so user space
pointers to the second 2GB alias the first 2GB, as is the case for native
32-bit s390 user space.

The compat_ptr_ioctl() function must therefore be used only with
ioctl functions that either ignore the argument or pass a pointer to a
compatible data type.

If any ioctl command handled by fops->unlocked_ioctl passes a plain
integer instead of a pointer, or any of the passed data types is
incompatible between 32-bit and 64-bit architectures, a proper handler
is required instead of compat_ptr_ioctl.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
v3: add a better description
v2: use compat_ptr_ioctl instead of generic_compat_ioctl_ptrarg,
as suggested by Al Viro

+42
+35
fs/ioctl.c
··· 8 8 #include <linux/syscalls.h> 9 9 #include <linux/mm.h> 10 10 #include <linux/capability.h> 11 + #include <linux/compat.h> 11 12 #include <linux/file.h> 12 13 #include <linux/fs.h> 13 14 #include <linux/security.h> ··· 720 719 { 721 720 return ksys_ioctl(fd, cmd, arg); 722 721 } 722 + 723 + #ifdef CONFIG_COMPAT 724 + /** 725 + * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation 726 + * 727 + * This is not normally called as a function, but instead set in struct 728 + * file_operations as 729 + * 730 + * .compat_ioctl = compat_ptr_ioctl, 731 + * 732 + * On most architectures, the compat_ptr_ioctl() just passes all arguments 733 + * to the corresponding ->ioctl handler. The exception is arch/s390, where 734 + * compat_ptr() clears the top bit of a 32-bit pointer value, so user space 735 + * pointers to the second 2GB alias the first 2GB, as is the case for 736 + * native 32-bit s390 user space. 737 + * 738 + * The compat_ptr_ioctl() function must therefore be used only with ioctl 739 + * functions that either ignore the argument or pass a pointer to a 740 + * compatible data type. 741 + * 742 + * If any ioctl command handled by fops->unlocked_ioctl passes a plain 743 + * integer instead of a pointer, or any of the passed data types 744 + * is incompatible between 32-bit and 64-bit architectures, a proper 745 + * handler is required instead of compat_ptr_ioctl. 746 + */ 747 + long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 748 + { 749 + if (!file->f_op->unlocked_ioctl) 750 + return -ENOIOCTLCMD; 751 + 752 + return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 753 + } 754 + EXPORT_SYMBOL(compat_ptr_ioctl); 755 + #endif
+7
include/linux/fs.h
··· 1727 1727 1728 1728 extern long vfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 1729 1729 1730 + #ifdef CONFIG_COMPAT 1731 + extern long compat_ptr_ioctl(struct file *file, unsigned int cmd, 1732 + unsigned long arg); 1733 + #else 1734 + #define compat_ptr_ioctl NULL 1735 + #endif 1736 + 1730 1737 /* 1731 1738 * VFS file helper functions. 1732 1739 */