linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@gmail.com>
To: Benjamin Tissoires <benjamin.tissoires@gmail.com>,
	Jiri Kosina <jkosina@suse.cz>, Jean Delvare <khali@linux-fr.org>,
	linux-input@vger.kernel.org, linux-i2c@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/4] HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches
Date: Wed,  5 Dec 2012 15:02:56 +0100	[thread overview]
Message-ID: <1354716176-12558-5-git-send-email-benjamin.tissoires@gmail.com> (raw)
In-Reply-To: <1354716176-12558-1-git-send-email-benjamin.tissoires@gmail.com>

The previous memcpy implementation relied on the size advertized by the
device. There were no guarantees that buf was big enough.

Some gymnastic is also required with the +2/-2 to take into account
the first 2 bytes of the returned buffer where the total returned
length is supplied by the device.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
---
 drivers/hid/i2c-hid/i2c-hid.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c
index c6630d4..ce01d59 100644
--- a/drivers/hid/i2c-hid/i2c-hid.c
+++ b/drivers/hid/i2c-hid/i2c-hid.c
@@ -502,23 +502,31 @@ static int i2c_hid_get_raw_report(struct hid_device *hid,
 {
 	struct i2c_client *client = hid->driver_data;
 	struct i2c_hid *ihid = i2c_get_clientdata(client);
+	size_t ret_count, ask_count;
 	int ret;
 
 	if (report_type == HID_OUTPUT_REPORT)
 		return -EINVAL;
 
-	if (count > ihid->bufsize)
-		count = ihid->bufsize;
+	/* +2 bytes to include the size of the reply in the query buffer */
+	ask_count = min(count + 2, (size_t)ihid->bufsize);
 
 	ret = i2c_hid_get_report(client,
 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
-			report_number, ihid->inbuf, count);
+			report_number, ihid->inbuf, ask_count);
 
 	if (ret < 0)
 		return ret;
 
-	count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
+	ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
 
+	if (!ret_count)
+		return 0;
+
+	ret_count = min(ret_count, ask_count);
+
+	/* The query buffer contains the size, dropping it in the reply */
+	count = min(count, ret_count - 2);
 	memcpy(buf, ihid->inbuf + 2, count);
 
 	return count;
-- 
1.8.0.1

  parent reply	other threads:[~2012-12-05 14:02 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-05 14:02 [PATCH v2 0/4] i2c-hid cleanup and bug fixes Benjamin Tissoires
2012-12-05 14:02 ` [PATCH v2 1/4] HID: i2c-hid: fix memory corruption due to missing hid declaration Benjamin Tissoires
2012-12-05 17:25   ` Jean Delvare
2012-12-06  9:54     ` Jiri Kosina
2012-12-05 14:02 ` [PATCH v2 2/4] HID: i2c-hid: reorder allocation/free of buffers Benjamin Tissoires
     [not found]   ` <1354716176-12558-3-git-send-email-benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2012-12-06  9:56     ` Jiri Kosina
2012-12-05 14:02 ` [PATCH v2 3/4] HID: i2c-hid: remove extra .irq field in struct i2c_hid Benjamin Tissoires
2012-12-05 17:28   ` Jean Delvare
     [not found]     ` <20121205182812.57262216-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-06  9:56       ` Jiri Kosina
2012-12-05 14:02 ` Benjamin Tissoires [this message]
2012-12-05 19:59   ` [PATCH v2 4/4] HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches Jean Delvare
2012-12-06 10:01     ` Jiri Kosina
2012-12-07 15:13       ` Benjamin Tissoires

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=1354716176-12558-5-git-send-email-benjamin.tissoires@gmail.com \
    --to=benjamin.tissoires@gmail.com \
    --cc=jkosina@suse.cz \
    --cc=khali@linux-fr.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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).