diff mbox series

[2/8] backports: fs.h: Add compat_ptr_ioctl()

Message ID 20200320233950.32257-3-hauke@hauke-m.de (mailing list archive)
State New, archived
Headers show
Series backports: Update to version 5.5.10 | expand

Commit Message

Hauke Mehrtens March 20, 2020, 11:39 p.m. UTC
compat_ptr_ioctl() was added in upstream commit 2952db0fd51b
("compat_ioctl: add compat_ptr_ioctl()") and is now used by the cdc-wdm
driver.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 backport/backport-include/linux/fs.h | 10 +++++++
 backport/compat/Makefile             |  1 +
 backport/compat/backport-5.5.c       | 41 ++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 backport/compat/backport-5.5.c
diff mbox series

Patch

diff --git a/backport/backport-include/linux/fs.h b/backport/backport-include/linux/fs.h
index 6e4d4a53..9854290a 100644
--- a/backport/backport-include/linux/fs.h
+++ b/backport/backport-include/linux/fs.h
@@ -49,4 +49,14 @@  static inline struct inode *file_inode(struct file *f)
 extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
 #endif /* < 4.5 && >= 3.2 */
 
+#if LINUX_VERSION_IS_LESS(5,5,0)
+#ifdef CONFIG_COMPAT
+#define compat_ptr_ioctl LINUX_BACKPORT(compat_ptr_ioctl)
+extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
+					unsigned long arg);
+#else
+#define compat_ptr_ioctl NULL
+#endif
+#endif /* < 5.5 */
+
 #endif	/* _COMPAT_LINUX_FS_H */
diff --git a/backport/compat/Makefile b/backport/compat/Makefile
index e92e3120..6f1b0a89 100644
--- a/backport/compat/Makefile
+++ b/backport/compat/Makefile
@@ -38,6 +38,7 @@  compat-$(CPTCFG_KERNEL_4_8) += backport-4.8.o
 compat-$(CPTCFG_KERNEL_4_10) += backport-4.10.o
 compat-$(CPTCFG_KERNEL_4_18) += backport-4.18.o
 compat-$(CPTCFG_KERNEL_5_2) += backport-5.2.o backport-genetlink.o
+compat-$(CPTCFG_KERNEL_5_5) += backport-5.5.o
 
 compat-$(CPTCFG_BPAUTO_BUILD_SYSTEM_DATA_VERIFICATION) += verification/verify.o
 compat-$(CPTCFG_BPAUTO_BUILD_SYSTEM_DATA_VERIFICATION) += verification/pkcs7.asn1.o
diff --git a/backport/compat/backport-5.5.c b/backport/compat/backport-5.5.c
new file mode 100644
index 00000000..6f46be9d
--- /dev/null
+++ b/backport/compat/backport-5.5.c
@@ -0,0 +1,41 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/compat.h>
+
+#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