linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Arnd Bergmann <arnd@arndb.de>,
	Amir Goldstein <amir73il@gmail.com>,
	Dave Chinner <david@fromorbit.com>,
	"Darrick J. Wong" <darrick.wong@oracle.com>,
	Jan Kara <jack@suse.cz>, David Howells <dhowells@redhat.com>,
	Jens Axboe <axboe@kernel.dk>,
	Matthew Wilcox <willy@infradead.org>,
	Phillip Potter <phil@philpotter.co.uk>
Subject: [PATCH v5 10/29] compat_ioctl: add compat_ptr_ioctl()
Date: Tue, 30 Jul 2019 21:25:21 +0200	[thread overview]
Message-ID: <20190730192552.4014288-11-arnd@arndb.de> (raw)
In-Reply-To: <20190730192552.4014288-1-arnd@arndb.de>

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
---
 fs/ioctl.c         | 35 +++++++++++++++++++++++++++++++++++
 include/linux/fs.h |  7 +++++++
 2 files changed, 42 insertions(+)

diff --git a/fs/ioctl.c b/fs/ioctl.c
index 9d26251f34a9..812061ba667a 100644
--- a/fs/ioctl.c
+++ b/fs/ioctl.c
@@ -8,6 +8,7 @@
 #include <linux/syscalls.h>
 #include <linux/mm.h>
 #include <linux/capability.h>
+#include <linux/compat.h>
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/security.h>
@@ -748,3 +749,37 @@ SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
 {
 	return ksys_ioctl(fd, cmd, arg);
 }
+
+#ifdef CONFIG_COMPAT
+/**
+ * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
+ *
+ * This is not normally called as a function, but instead set in struct
+ * file_operations as
+ *
+ *     .compat_ioctl = compat_ptr_ioctl,
+ *
+ * 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.
+ */
+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);
+#endif
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 56b8e358af5c..07b032e58032 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1702,6 +1702,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


  parent reply	other threads:[~2019-07-30 19:30 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-30 19:25 [PATCH v5 00/29] compat_ioctl.c removal, part 1/3 Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 01/29] fix compat handling of FICLONERANGE, FIDEDUPERANGE and FS_IOC_FIEMAP Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 02/29] FIGETBSZ: fix compat Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 03/29] compat: itanic doesn't have one Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 04/29] do_vfs_ioctl(): use saner types Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 05/29] compat: move FS_IOC_RESVSP_32 handling to fs/ioctl.c Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 06/29] compat_sys_ioctl(): make parallel to do_vfs_ioctl() Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 07/29] ceph: fix compat_ioctl for ceph_dir_operations Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 08/29] compat_ioctl: drop FIOQSIZE table entry Arnd Bergmann
2019-07-30 19:25 ` [PATCH v5 09/29] compat_ioctl: pppoe: fix PPPOEIOCSFWD handling Arnd Bergmann
2019-07-30 21:42   ` David Miller
2019-07-30 19:25 ` Arnd Bergmann [this message]
2019-07-30 19:25 ` [PATCH v5 11/29] compat_ioctl: move rtc handling into rtc-dev.c Arnd Bergmann
2019-07-30 19:50 ` [PATCH v5 12/29] compat_ioctl: move drivers to compat_ptr_ioctl Arnd Bergmann
2019-07-30 21:43   ` David Miller
2019-07-31  8:37   ` Cornelia Huck
2019-07-30 19:55 ` [PATCH v5 13/29] compat_ioctl: move more " Arnd Bergmann
2019-07-30 19:55   ` [PATCH v5 14/29] compat_ioctl: use correct compat_ptr() translation in drivers Arnd Bergmann
2019-07-30 19:55   ` [PATCH v5 15/29] compat_ioctl: move tape handling into drivers Arnd Bergmann
2019-07-31 10:50     ` Heiko Carstens
2019-07-31 15:47       ` Arnd Bergmann
2019-07-30 19:55   ` [PATCH v5 16/29] compat_ioctl: move ATYFB_CLK handling to atyfb driver Arnd Bergmann
2019-07-30 19:55   ` [PATCH v5 17/29] compat_ioctl: move isdn/capi ioctl translation into driver Arnd Bergmann
2019-07-30 19:55   ` [PATCH v5 18/29] compat_ioctl: move rfcomm handlers " Arnd Bergmann
2019-08-12 16:29     ` Marcel Holtmann
2019-07-30 19:55   ` [PATCH v5 19/29] compat_ioctl: move hci_sock " Arnd Bergmann
2019-08-12 16:29     ` Marcel Holtmann
2019-07-30 20:14   ` [PATCH v5 13/29] compat_ioctl: move more drivers to compat_ptr_ioctl Dan Williams
2019-07-30 20:01 ` [PATCH v5 20/29] compat_ioctl: remove HCIUART handling Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 21/29] compat_ioctl: remove HIDIO translation Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 22/29] compat_ioctl: remove translation for sound ioctls Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 23/29] compat_ioctl: remove IGNORE_IOCTL() Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 24/29] compat_ioctl: remove /dev/random commands Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 25/29] compat_ioctl: remove joystick ioctl translation Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 26/29] compat_ioctl: remove PCI " Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 27/29] compat_ioctl: remove /dev/raw " Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 28/29] compat_ioctl: remove last RAID handling code Arnd Bergmann
2019-07-30 20:01   ` [PATCH v5 29/29] compat_ioctl: remove unused convert_in_user macro Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190730192552.4014288-11-arnd@arndb.de \
    --to=arnd@arndb.de \
    --cc=amir73il@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=darrick.wong@oracle.com \
    --cc=david@fromorbit.com \
    --cc=dhowells@redhat.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phil@philpotter.co.uk \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).