[PATCH v3 08/26] compat_ioctl: add compat_ptr_ioctl()

From: Arnd Bergmann
Date: Tue Apr 16 2019 - 16:22:12 EST


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.

Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
---
v2: use compat_ptr_ioctl instead of generic_compat_ioctl_ptrarg,
as suggested by Al Viro
---
fs/compat_ioctl.c | 10 ++++++++++
include/linux/fs.h | 7 +++++++
2 files changed, 17 insertions(+)

diff --git a/fs/compat_ioctl.c b/fs/compat_ioctl.c
index fee116e822d8..ab2ff530313b 100644
--- a/fs/compat_ioctl.c
+++ b/fs/compat_ioctl.c
@@ -78,6 +78,16 @@
get_user(val, srcptr) || put_user(val, dstptr); \
})

+/* helper function to avoid trivial compat_ioctl() implementations */
+long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ if (!file->f_op->unlocked_ioctl)
+ return -ENOIOCTLCMD;
+
+ return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
+}
+EXPORT_SYMBOL(compat_ptr_ioctl);
+
static inline int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int err;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index dd28e7679089..dc4138314635 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1698,6 +1698,13 @@ int vfs_mkobj(struct dentry *, umode_t,

extern long vfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);

+#ifdef CONFIG_COMPAT
+extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg);
+#else
+#define compat_ptr_ioctl NULL
+#endif
+
/*
* VFS file helper functions.
*/
--
2.20.0