Skip to contentSkip to conversation

v7 changelog for mmc ioctl patch

PATCH In review John Calixto 3 participants 8 messages Apr 22, 2011 – Apr 29, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

change history for this patch:

- v7
- simplify 32-bit/64-bit buffer pointer portability
- add pad member to struct mmc_ioc_cmd so its size is the same when built for either 32-bit or 64-bit.
- register ``0xB3`` in Documentation/ioctl/ioctl-number.txt

- v6
- refix 32+64 compat pointer for better portability
- copy userspace pointer *before* using
- apply upper limit to data buffer size
- add flag to allow normal CMD opcodes as well as ACMD opcodes
- remove unnecessary mutex grab

- v5
- fix 32-bit compiler warning about the 32+64 compat pointer

- v4
- replace postsleep udelay() with usleep_range()
- add cmd_timeout_ms field for R1B commands

- v3
- copy data from userspace before claiming host
- break out copy from userspace into its own function
- verify that caller has CAP_SYS_RAWIO
- rename ``struct sd_ioc_cmd`` to ``struct mmc_ioc_cmd`` because it applies generally, not just to SD
- make struct mmc_ioc_cmd the same between 32-bit and 64-bit to simplify compat_ioctl()
- export include/linux/mmc/ioctl.h when you ``make headers_install``

- v2
- make initialization of struct declarations match kernel style
- only allow ioctl() on whole block device, not partition
- remove extraneous printks
- implement compat_ioctl()
- remove version field from ``struct sd_ioc_cmd``

John

#1 lore raw

PATCH v7 1/1 mmc: Add mmc CMD+ACMD passthrough ioctl

Allows appropriately-privileged applications to send CMD (normal) and ACMD (application-specific; preceded with CMD55) commands to cards/devices on the mmc bus. This is primarily useful for enabling the security functionality built in to every SD card.

It can also be used as a generic passthrough (e.g. to enable virtual machines to control mmc bus devices directly). However, this use case has not been tested rigorously. Generic passthrough testing was only conducted for a few non-security opcodes to prove the feasibility of the passthrough.

Since any opcode can be sent using this passthrough, it is very possible to render the card/device unusable. Applications that use this ioctl must have CAP_SYS_RAWIO.

Security commands tested on TI PCIxx12 (SDHCI), Sigma Designs SMP8652 SoC, TI OMAP3621 SoC, TI OMAP3630 SoC, Samsung S5PC110 SoC, Qualcomm MSM7200A SoC.

---
 Documentation/ioctl/ioctl-number.txt |    1 +
 drivers/mmc/card/block.c             |  201 ++++++++++++++++++++++++++++++++++
 drivers/mmc/core/sd_ops.c            |    3 +-
 include/linux/Kbuild                 |    1 +
 include/linux/mmc/Kbuild             |    1 +
 include/linux/mmc/core.h             |    1 +
 include/linux/mmc/ioctl.h            |   54 +++++++++
 7 files changed, 261 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/mmc/Kbuild
 create mode 100644 include/linux/mmc/ioctl.h
Documentation/ioctl/ioctl-number.txt +1 -0
index a0a5d82..2a34d82 100644
@@ -304,6 +304,7 @@ Code  Seq#(hex)	Include File		Comments
 0xB0	all	RATIO devices		in development:
 					<mailto:[email protected]>
 0xB1	00-1F	PPPoX			<mailto:[email protected]>
+0xB3	00	linux/mmc/ioctl.h
 0xC0	00-0F	linux/usb/iowarrior.h
 0xCB	00-1F	CBM serial IEC bus	in development:
 					<mailto:[email protected]>
drivers/mmc/card/block.c +201 -0

219 lines not on this page · raw message

drivers/mmc/core/sd_ops.c +2 -1
index 797cdb5..990dd43 100644
@@ -20,7 +20,7 @@
 #include "core.h"
 #include "sd_ops.h"
 
-static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
+int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
 {
 	int err;
 	struct mmc_command cmd;
@@ -48,6 +48,7 @@ static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(mmc_app_cmd);
 
 /**
  *	mmc_wait_for_app_cmd - start an application command and wait for
include/linux/Kbuild +1 -0
index 75cf611..ed38527 100644
@@ -4,6 +4,7 @@ header-y += caif/
 header-y += dvb/
 header-y += hdlc/
 header-y += isdn/
+header-y += mmc/
 header-y += nfsd/
 header-y += raid/
 header-y += spi/
include/linux/mmc/Kbuild new file +1 -0
new file mode 100644
index 0000000..1fb2644
@@ -0,0 +1 @@
+header-y += ioctl.h
include/linux/mmc/core.h +1 -0
index 07f27af..bfc6127 100644
@@ -133,6 +133,7 @@ struct mmc_card;
 
 extern void mmc_wait_for_req(struct mmc_host *, struct mmc_request *);
 extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
+extern int mmc_app_cmd(struct mmc_host *, struct mmc_card *);
 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
 	struct mmc_command *, int);
 
include/linux/mmc/ioctl.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#2 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

On Thu, 21 Apr 2011, John Calixto wrote:

<snip>

Quoted 19 lines
drivers/mmc/card/block.c +19 -0
+static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
+	struct mmc_ioc_cmd __user *user)
+{
+	struct mmc_blk_ioc_data *idata;
+	int err;
+
+	idata = kzalloc(sizeof(*idata), GFP_KERNEL);
+	if (!idata) {
+		err = -ENOMEM;
+		goto copy_err;
+	}
+
+	if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
+		err = -EFAULT;
+		goto copy_err;
+	}
+
+	idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
+	if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {

drivers/mmc/card/block.c mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) · line 199

Quoted 10 lines
drivers/mmc/card/block.c +10 -0
+		err = -EOVERFLOW;
+		goto copy_err;
+	}
+
+	idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
+	if (!idata->buf) {
+		err = -ENOMEM;
+		goto copy_err;
+	}
+

Per Arnd's recommendation, I just cast the ``data_ptr`` to ``(void *)(unsigned long)`` to solve the compatibility issue with the buffer pointer in struct mmc_ioc_cmd.

Quoted 5 lines
drivers/mmc/card/block.c +5 -0
+	if (copy_from_user(idata->buf, (void __user *)(unsigned long)
+					idata->ic.data_ptr, idata->buf_bytes)) {
+		err = -EFAULT;
+		goto copy_err;
+	}

drivers/mmc/card/block.c mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) · line 214

Quoted 10 lines
drivers/mmc/card/block.c +10 -0
+
+	return idata;
+
+copy_err:
+	kfree(idata->buf);
+	kfree(idata);
+	return ERR_PTR(err);
+
+}
+

<snip>

I also left mmc_blk_ioctl() and mmc_blk_compat_ioctl() as they were in v6. IMHO, a __mmc_blk_ioctl() function that accepts a compat32 boolean arg does not do much to enhance readability or functionality in this case. With the implementation in the 2 functions below, I intended to make it clear that:

- mmc_blk_ioctl() is responsible for delegating to the appropriate handler based on the incoming ioctl cmd. The ``if`` would be replaced with a ``switch`` when adding other values for cmd.

- mmc_blk_compat_ioctl() is responsible for converting the ioctl arg to something suitable for consumtption by mmc_blk_ioctl().

- when CONFIG_COMPAT is undefined, you get no code related to compatibility.

Quoted 19 lines
drivers/mmc/card/block.c +18 -0
+
+static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
+	unsigned int cmd, unsigned long arg)
+{
+	int ret = -EINVAL;
+	if (cmd == MMC_IOC_CMD)
+		ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
+	return ret;
+}
+
+#ifdef CONFIG_COMPAT
+static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
+	unsigned int cmd, unsigned long arg)
+{
+	return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
+}
+#endif
+

drivers/mmc/card/block.c mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) · line 367

Quoted 10 lines
drivers/mmc/card/block.c +4 -0
 static const struct block_device_operations mmc_bdops = {
 	.open			= mmc_blk_open,
 	.release		= mmc_blk_release,
 	.getgeo			= mmc_blk_getgeo,
 	.owner			= THIS_MODULE,
+	.ioctl			= mmc_blk_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl		= mmc_blk_compat_ioctl,
+#endif
 };

<snip>

It makes a lot of sense to me to make sure that struct mmc_ioc_cmd is the same size on 32-bit and 64-bit machines, since it is used by _IOWR() to derive the value for MMC_IOC_CMD. Any change in its size requires having other compatibility logic to deal with a MMC_IOC_CMD32.

Note that as this struct has evolved, I also needed to add a ``__pad`` member to the struct to achieve the goal of keeping it the same size on 32-bit and 64-bit. I could have used packing or alignment compiler directives, but this seemed simplest to me (I can add up the bytes in my head!):

Quoted 38 lines
include/linux/mmc/ioctl.h new file +32 -0
new file mode 100644
index 0000000..225e1e1
@@ -0,0 +1,54 @@
+#ifndef LINUX_MMC_IOCTL_H
+#define LINUX_MMC_IOCTL_H
+struct mmc_ioc_cmd {
+	/* Implies direction of data.  true = write, false = read */
+	int write_flag;
+
+	/* Application-specific command.  true = precede with CMD55 */
+	int is_acmd;
+
+	__u32 opcode;
+	__u32 arg;
+	__u32 response[4];  /* CMD response */
+	unsigned int flags;
+	unsigned int blksz;
+	unsigned int blocks;
+
+	/*
+	 * Sleep at least postsleep_min_us useconds, and at most
+	 * postsleep_max_us useconds *after* issuing command.  Needed for some
+	 * read commands for which cards have no other way of indicating
+	 * they're ready for the next command (i.e. there is no equivalent of a
+	 * "busy" indicator for read operations).
+	 */
+	unsigned int postsleep_min_us;
+	unsigned int postsleep_max_us;
+
+	/*
+	 * Override driver-computed timeouts.  Note the difference in units!
+	 */
+	unsigned int data_timeout_ns;
+	unsigned int cmd_timeout_ms;
+

include/linux/mmc/ioctl.h line 42

Quoted 10 lines
include/linux/mmc/ioctl.h new file +10 -0
+	/*
+	 * For 64-bit machines, the next member, ``__u64 data_ptr``, wants to
+	 * be 8-byte aligned.  Make sure this struct is the same size when
+	 * built for 32-bit.
+	 */
+	__u32 __pad;
+
+	/* DAT buffer */
+	__u64 data_ptr;
+};

To help with the casting in 32-bit userspace, I also provided ``mmc_ioc_cmd_set_data(ic, ptr)`` as a helper macro:

Quoted 15 lines
include/linux/mmc/ioctl.h new file +12 -0
+#define mmc_ioc_cmd_set_data(ic, ptr) ic.data_ptr = (__u64)(unsigned long) ptr
+
+#define MMC_IOC_CMD _IOWR(MMC_BLOCK_MAJOR, 0, struct mmc_ioc_cmd)
+
+/*
+ * Since this ioctl is only meant to enhance (and not replace) normal access to
+ * the mmc bus device, an upper data transfer limit of MMC_IOC_MAX_BYTES is
+ * enforced per ioctl call.  For larger data transfers, use the normal block
+ * device operations.
+ */
+#define MMC_IOC_MAX_BYTES  (512L * 256)
+#endif  /* LINUX_MMC_IOCTL_H */
Signature · 1 line
1.7.4.1

John

Reviewed-by on v7 1/1
#3 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

In reply to John Calixto
Quoted 15 lines · John Calixto

Allows appropriately-privileged applications to send CMD (normal) and ACMD (application-specific; preceded with CMD55) commands to cards/devices on the mmc bus. This is primarily useful for enabling the security functionality built in to every SD card.

It can also be used as a generic passthrough (e.g. to enable virtual machines to control mmc bus devices directly). However, this use case has not been tested rigorously. Generic passthrough testing was only conducted for a few non-security opcodes to prove the feasibility of the passthrough.

Since any opcode can be sent using this passthrough, it is very possible to render the card/device unusable. Applications that use this ioctl must have CAP_SYS_RAWIO.

Quoted 6 lines

Security commands tested on TI PCIxx12 (SDHCI), Sigma Designs SMP8652 SoC, TI OMAP3621 SoC, TI OMAP3630 SoC, Samsung S5PC110 SoC, Qualcomm MSM7200A SoC.

The implementation looks good to me now,

I'll leave the final decision whether this is a good feature to have to Chris. I still believe that we should have per-command ioctls for the security feature, but getting there would require someone to implement it, and I'm not going to do that.

Arnd

#4 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

Quoted 23 lines · Arnd Bergmann
Quoted 15 lines · John Calixto

Allows appropriately-privileged applications to send CMD (normal) and ACMD (application-specific; preceded with CMD55) commands to cards/devices on the mmc bus. This is primarily useful for enabling the security functionality built in to every SD card.

It can also be used as a generic passthrough (e.g. to enable virtual machines to control mmc bus devices directly). However, this use case has not been tested rigorously. Generic passthrough testing was only conducted for a few non-security opcodes to prove the feasibility of the passthrough.

Since any opcode can be sent using this passthrough, it is very possible to render the card/device unusable. Applications that use this ioctl must have CAP_SYS_RAWIO.

Quoted 6 lines

Security commands tested on TI PCIxx12 (SDHCI), Sigma Designs SMP8652 SoC, TI OMAP3621 SoC, TI OMAP3630 SoC, Samsung S5PC110 SoC, Qualcomm MSM7200A SoC.

Quoted 9 lines

The implementation looks good to me now,

I'll leave the final decision whether this is a good feature to have to Chris. I still believe that we should have per-command ioctls for the security feature, but getting there would require someone to implement it, and I'm not going to do that.

Arnd - Thanks a lot for the review and your help getting the implementation right!

Chris - What do you think? How should I proceed here?

John

#5 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

Hi John,

Quoted 4 lines · John Calixto

Arnd - Thanks a lot for the review and your help getting the implementation right!

Chris - What do you think? How should I proceed here?

I think I'll merge it, since I'm not going to work on abstracting the security feature either. I'll send another mail once it's in my tree.

Thanks!

- Chris.

Signature · 2 lines
Chris Ball   <[email protected]>   <http://printf.net/>
One Laptop Per Child
#6 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

Quoted 6 lines · Chris Ball

Hi John,

Quoted 3 lines · John Calixto

Arnd - Thanks a lot for the review and your help getting the implementation right!

Quoted 8 lines
Quoted 1 line

Chris - What do you think? How should I proceed here?

I think I'll merge it, since I'm not going to work on abstracting the security feature either. I'll send another mail once it's in my tree.

Thanks!

- Chris.

Chris,

That's great - thanks a lot!

John

#7 lore raw

Re: [PATCH v7] mmc: Add mmc CMD+ACMD passthrough ioctl

In reply to Chris Ball

Hi John,

Quoted 2 lines · Chris Ball

I think I'll merge it, since I'm not going to work on abstracting the security feature either. I'll send another mail once it's in my tree.

I've pushed this out to mmc-next now.

- Chris.

Signature · 2 lines
Chris Ball   <[email protected]>   <http://printf.net/>
One Laptop Per Child