From patchwork Wed Dec 5 14:02:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 1841391 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 E6FDE3FCF2 for ; Wed, 5 Dec 2012 14:04:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753499Ab2LEODn (ORCPT ); Wed, 5 Dec 2012 09:03:43 -0500 Received: from mail-wi0-f180.google.com ([209.85.212.180]:45821 "EHLO mail-wi0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752721Ab2LEODN (ORCPT ); Wed, 5 Dec 2012 09:03:13 -0500 Received: by mail-wi0-f180.google.com with SMTP id hj13so1719252wib.1 for ; Wed, 05 Dec 2012 06:03:12 -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=TGiMte8n8pY9S6gnubb9gawSCUfHsbV7eRL7CtaNUcY=; b=V2C3w3koJZ1j20UZCP13XrfVFk9ov5/OiB4krmuURxNE3eNGFogYJRug1gzvfvAzvD UYH7BGVUjDJjZvMzLuOHhFxopks63psUZMnMuZMW8aGyrbWJnwq819IQfxQZZL309EEW MMij0SS8mkZax3eZCrz9Wm4dMKhV1IN/+DUBcXJemeZB41GxEuZpKNvAyUQ2sBxXn3PK C7vRYkLCg5RA7UdW8/eHDqRO7ykA+zc3WHH64xJ7cWcFWGCf1ETVwcv/eLWegNmL2S7g +R5vsQc4VpfIwGD76vhz1dfWEHhp16g+QL/D266dAVjeBZNraBh5kujzpjSc0jB4XS2z 0iKg== Received: by 10.180.98.67 with SMTP id eg3mr3411766wib.9.1354716192785; Wed, 05 Dec 2012 06:03:12 -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 cf6sm6544288wib.3.2012.12.05.06.03.11 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 05 Dec 2012 06:03:12 -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 v2 4/4] HID: i2c-hid: fix i2c_hid_get_raw_report count mismatches Date: Wed, 5 Dec 2012 15:02:56 +0100 Message-Id: <1354716176-12558-5-git-send-email-benjamin.tissoires@gmail.com> X-Mailer: git-send-email 1.8.0.1 In-Reply-To: <1354716176-12558-1-git-send-email-benjamin.tissoires@gmail.com> References: <1354716176-12558-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 of the returned buffer where the total returned length is supplied by the device. Signed-off-by: Benjamin Tissoires --- 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;