[v3,08/26] compat_ioctl: add compat_ptr_ioctl()
diff mbox series

Message ID 20190416202013.4034148-9-arnd@arndb.de
State New
Headers show
Series
  • compat_ioctl: cleanups
Related show

Commit Message

Arnd Bergmann April 16, 2019, 8:19 p.m. UTC
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@arndb.de>
---
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(+)

Comments

Al Viro April 17, 2019, 9:19 p.m. UTC | #1
On Tue, Apr 16, 2019 at 10:19:46PM +0200, Arnd Bergmann wrote:
> 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.

You forgot to mention that it even gets the previous commit to build ;-)
IOW, that needs to be reordered.
Arnd Bergmann April 17, 2019, 9:34 p.m. UTC | #2
On Wed, Apr 17, 2019 at 11:19 PM Al Viro <viro@zeniv.linux.org.uk> wrote:
>
> On Tue, Apr 16, 2019 at 10:19:46PM +0200, Arnd Bergmann wrote:
> > 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.
>
> You forgot to mention that it even gets the previous commit to build ;-)
> IOW, that needs to be reordered.

Right. I had reordered the series multiple times and got this part wrong
in the end.

       Arnd

Patch
diff mbox series

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.
  */