a1ea56b
From 096cdc6f52225835ff503f987a0d68ef770bb78e Mon Sep 17 00:00:00 2001
a1ea56b
From: Dan Carpenter <dan.carpenter@oracle.com>
a1ea56b
Date: Tue, 21 Jun 2016 16:58:46 +0300
a1ea56b
Subject: [PATCH] platform/chrome: cros_ec_dev - double fetch bug in ioctl
a1ea56b
a1ea56b
We verify "u_cmd.outsize" and "u_cmd.insize" but we need to make sure
a1ea56b
that those values have not changed between the two copy_from_user()
a1ea56b
calls.  Otherwise it could lead to a buffer overflow.
a1ea56b
a1ea56b
Additionally, cros_ec_cmd_xfer() can set s_cmd->insize to a lower value.
a1ea56b
We should use the new smaller value so we don't copy too much data to
a1ea56b
the user.
a1ea56b
a1ea56b
Reported-by: Pengfei Wang <wpengfeinudt@gmail.com>
a1ea56b
Fixes: a841178445bb ('mfd: cros_ec: Use a zero-length array for command data')
a1ea56b
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
a1ea56b
Reviewed-by: Kees Cook <keescook@chromium.org>
a1ea56b
Tested-by: Gwendal Grignou <gwendal@chromium.org>
a1ea56b
Cc: <stable@vger.kernel.org> # v4.2+
a1ea56b
Signed-off-by: Olof Johansson <olof@lixom.net>
a1ea56b
---
a1ea56b
 drivers/platform/chrome/cros_ec_dev.c | 8 +++++++-
a1ea56b
 1 file changed, 7 insertions(+), 1 deletion(-)
a1ea56b
a1ea56b
diff --git a/drivers/platform/chrome/cros_ec_dev.c b/drivers/platform/chrome/cros_ec_dev.c
a1ea56b
index 6d8ee3b15872..8abd80dbcbed 100644
a1ea56b
--- a/drivers/platform/chrome/cros_ec_dev.c
a1ea56b
+++ b/drivers/platform/chrome/cros_ec_dev.c
a1ea56b
@@ -151,13 +151,19 @@ static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
a1ea56b
 		goto exit;
a1ea56b
 	}
a1ea56b
 
a1ea56b
+	if (u_cmd.outsize != s_cmd->outsize ||
a1ea56b
+	    u_cmd.insize != s_cmd->insize) {
a1ea56b
+		ret = -EINVAL;
a1ea56b
+		goto exit;
a1ea56b
+	}
a1ea56b
+
a1ea56b
 	s_cmd->command += ec->cmd_offset;
a1ea56b
 	ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
a1ea56b
 	/* Only copy data to userland if data was received. */
a1ea56b
 	if (ret < 0)
a1ea56b
 		goto exit;
a1ea56b
 
a1ea56b
-	if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + u_cmd.insize))
a1ea56b
+	if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
a1ea56b
 		ret = -EFAULT;
a1ea56b
 exit:
a1ea56b
 	kfree(s_cmd);
a1ea56b
-- 
a1ea56b
2.5.5
a1ea56b