mtd: provide proper 32/64-bit compat_ioctl() support for BLKPG

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Fri Nov 6 10:59:06 PST 2015


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=53bb724f94f57b67213e08a4c155c8c5eb74c644
Commit:     53bb724f94f57b67213e08a4c155c8c5eb74c644
Parent:     4404bd742db767a7f316b1207c46a04a38ca7198
Author:     Brian Norris <computersforpeace at gmail.com>
AuthorDate: Mon Sep 21 13:26:59 2015 -0700
Committer:  Brian Norris <computersforpeace at gmail.com>
CommitDate: Tue Sep 29 13:37:04 2015 -0700

    mtd: provide proper 32/64-bit compat_ioctl() support for BLKPG
    
    After a bit of poking around wondering why my 32-bit user-space can't
    seem to send a proper ioctl(BLKPG) to an MTD on my 64-bit kernel
    (ARM64), I noticed that struct blkpg_ioctl_arg is actually pretty
    unsuitable for use in the ioctl() ABI, due to its use of raw pointers,
    and its lack of alignment/packing restrictions (32-bit arch'es tend to
    pack the 4 fields into 4 32-bit words, whereas 64-bit arch'es would add
    padding after the third int, and make this 6 32-bit words).
    
    Anyway, this means BLKPG deserves some special compat_ioctl handling. Do
    the conversion in a small shim for MTD.
    
    block/compat_ioctl.c already has compat support for the block subsystem,
    but it does so by a re-marshalling data to/from user-space (see
    compat_blkpg_ioctl()). Personally, I think this approach is cleaner.
    
    Tested only on MTD, with an ARM32 user space on an ARM64 kernel.
    
    Signed-off-by: Brian Norris <computersforpeace at gmail.com>
---
 drivers/mtd/mtdchar.c      | 42 +++++++++++++++++++++++++++++++++---------
 include/linux/blkpg.h      | 21 +++++++++++++++++++++
 include/uapi/linux/blkpg.h |  6 +++---
 3 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 55fa27e..6d19835 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -498,21 +498,17 @@ static int shrink_ecclayout(const struct nand_ecclayout *from,
 }
 
 static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
-			   struct blkpg_ioctl_arg __user *arg)
+			       struct blkpg_ioctl_arg *arg)
 {
-	struct blkpg_ioctl_arg a;
 	struct blkpg_partition p;
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
+	if (copy_from_user(&p, arg->data, sizeof(p)))
 		return -EFAULT;
 
-	if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
-		return -EFAULT;
-
-	switch (a.op) {
+	switch (arg->op) {
 	case BLKPG_ADD_PARTITION:
 
 		/* Only master mtd device must be used to add partitions */
@@ -966,8 +962,13 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 
 	case BLKPG:
 	{
-		ret = mtdchar_blkpg_ioctl(mtd,
-		      (struct blkpg_ioctl_arg __user *)arg);
+		struct blkpg_ioctl_arg __user *blk_arg = argp;
+		struct blkpg_ioctl_arg a;
+
+		if (copy_from_user(&a, blk_arg, sizeof(a)))
+			ret = -EFAULT;
+		else
+			ret = mtdchar_blkpg_ioctl(mtd, &a);
 		break;
 	}
 
@@ -1046,6 +1047,29 @@ static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
 				&buf_user->start);
 		break;
 	}
+
+	case BLKPG:
+	{
+		/* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
+		struct blkpg_compat_ioctl_arg __user *uarg = argp;
+		struct blkpg_compat_ioctl_arg compat_arg;
+		struct blkpg_ioctl_arg a;
+
+		if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
+			ret = -EFAULT;
+			break;
+		}
+
+		memset(&a, 0, sizeof(a));
+		a.op = compat_arg.op;
+		a.flags = compat_arg.flags;
+		a.datalen = compat_arg.datalen;
+		a.data = compat_ptr(compat_arg.data);
+
+		ret = mtdchar_blkpg_ioctl(mtd, &a);
+		break;
+	}
+
 	default:
 		ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
 	}
diff --git a/include/linux/blkpg.h b/include/linux/blkpg.h
new file mode 100644
index 0000000..bef124f
--- /dev/null
+++ b/include/linux/blkpg.h
@@ -0,0 +1,21 @@
+#ifndef _LINUX_BLKPG_H
+#define _LINUX_BLKPG_H
+
+/*
+ * Partition table and disk geometry handling
+ */
+
+#include <linux/compat.h>
+#include <uapi/linux/blkpg.h>
+
+#ifdef CONFIG_COMPAT
+/* For 32-bit/64-bit compatibility of struct blkpg_ioctl_arg */
+struct blkpg_compat_ioctl_arg {
+	compat_int_t op;
+	compat_int_t flags;
+	compat_int_t datalen;
+	compat_uptr_t data;
+};
+#endif
+
+#endif /* _LINUX_BLKPG_H */
diff --git a/include/uapi/linux/blkpg.h b/include/uapi/linux/blkpg.h
index a851944..63739a0 100644
--- a/include/uapi/linux/blkpg.h
+++ b/include/uapi/linux/blkpg.h
@@ -1,5 +1,5 @@
-#ifndef _LINUX_BLKPG_H
-#define _LINUX_BLKPG_H
+#ifndef _UAPI__LINUX_BLKPG_H
+#define _UAPI__LINUX_BLKPG_H
 
 /*
  * Partition table and disk geometry handling
@@ -56,4 +56,4 @@ struct blkpg_partition {
 	char volname[BLKPG_VOLNAMELTH];	/* volume label */
 };
 
-#endif /* _LINUX_BLKPG_H */
+#endif /* _UAPI__LINUX_BLKPG_H */



More information about the linux-mtd-cvs mailing list