From patchwork Tue Dec 4 15:27:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 1838731 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id F00E63FCA5 for ; Tue, 4 Dec 2012 15:29:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753734Ab2LDP2j (ORCPT ); Tue, 4 Dec 2012 10:28:39 -0500 Received: from mail-wi0-f174.google.com ([209.85.212.174]:42161 "EHLO mail-wi0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753717Ab2LDP2h (ORCPT ); Tue, 4 Dec 2012 10:28:37 -0500 Received: by mail-wi0-f174.google.com with SMTP id hm9so940747wib.1 for ; Tue, 04 Dec 2012 07:28:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:x-mailer:in-reply-to:references; bh=HZOq6IfLull4F6vWuIiBwKTe+K7/bYp7hbjJw04lmm0=; b=OjCHhApiQsmrVQLoWu9obUemf+Kt7JbA+mcTD7pzmEpHL5emX8XYCPCpMFhqo+ok50 uA9fdvLIg3PPDy1LvDbN5pAATY4rQTroniLO3HnwUfYJdgH/3oprugEE/j6eZ7WX5IzE 7frrGrRT/IByZBgdBhlcuXu7NXlMtoclMOPSGgauoLVkpndKDhxQ84D/u+fu2YFTHHVa CJEfPLM4KQvwZal4RU6UuXtrlcfXkxfdZE6m7JH00pVZ5PfDVyE+bthbnFqwa4PRzFV4 ol8rQtIbPPZGg+H6SrjqqEdSl160mAdca5GWjFIK3Gu+tJlTsDw4ylq42VyUa+wiSyN7 ICLg== Received: by 10.216.207.139 with SMTP id n11mr5156616weo.211.1354634916618; Tue, 04 Dec 2012 07:28:36 -0800 (PST) Received: from localhost.localdomain.com (lan31-8-82-247-176-67.fbx.proxad.net. [82.247.176.67]) by mx.google.com with ESMTPS id p3sm15443846wic.8.2012.12.04.07.28.35 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 04 Dec 2012 07:28:35 -0800 (PST) From: Benjamin Tissoires To: Benjamin Tissoires , Jiri Kosina , Jean Delvare , linux-input@vger.kernel.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 14/14] HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches Date: Tue, 4 Dec 2012 16:27:55 +0100 Message-Id: <1354634875-5182-15-git-send-email-benjamin.tissoires@gmail.com> X-Mailer: git-send-email 1.8.0.1 In-Reply-To: <1354634875-5182-1-git-send-email-benjamin.tissoires@gmail.com> References: <1354634875-5182-1-git-send-email-benjamin.tissoires@gmail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 where the total length is supplied by the device. Signed-off-by: Benjamin Tissoires --- drivers/hid/i2c-hid/i2c-hid.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 62988f1..de3566f 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -503,13 +503,14 @@ 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 rcount; int ret; if (report_type == HID_OUTPUT_REPORT) return -EINVAL; - if (count > ihid->bufsize) - count = ihid->bufsize; + if (count > ihid->bufsize - 2) + count = ihid->bufsize - 2; ret = i2c_hid_get_report(client, report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, @@ -518,7 +519,13 @@ static int i2c_hid_get_raw_report(struct hid_device *hid, if (ret < 0) return ret; - count = ihid->inbuf[0] | (ihid->inbuf[1] << 8); + rcount = ihid->inbuf[0] | (ihid->inbuf[1] << 8); + + if (!rcount) + return 0; + + if (count > rcount - 2) + count = rcount - 2; memcpy(buf, ihid->inbuf + 2, count);