Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp265181ybt; Sat, 13 Jun 2020 03:52:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxQYCH/wMsL9vZa0vuSDFhT+idVwRQfAveWJlLbEq0xJGP3tdA/iThEq5zpQ8lHLOd/qox X-Received: by 2002:a17:906:d153:: with SMTP id br19mr17599071ejb.201.1592045531027; Sat, 13 Jun 2020 03:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592045531; cv=none; d=google.com; s=arc-20160816; b=jilRwV89ujZq7MlnNAnYLzvpnf7D8EQDAfAhpOqRgS32Zfh/WTTA8ltaAR4WAapq6l gQEkGpajtJ8jVizuCYbXDptmSxgbJ//fsVpbYyy1xwMuN8DEvKQiUz1Y4mJ2Z8QN1D+a 4O6R2oSL02axGZ9KtcvxePZyNBUSUiUHqrePDmxDxD41nEgSqG1UNlAekMUSNcy6ZLgD inrtDDlLhXBI98NulQH4z6U3XYLciSyJ8qdABwW0vgCRvPntBIsCUavcFalFaf1myeuk uNJsWtoAfWwRhrCDYHx7mHAj7dVIKuSJjiMz+bbEb3WrKylGhzzEjDJ+raaW8nu7VDT5 G8tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=MyVnlelAsNcoR7Qmvsmux1lfabayjooh1spRuG6IV2o=; b=0JM9iK3DKrTU+PXB+N/fU98MSLROozWQfqyxRBC1313eDviWjx/i0W7Kf62Pfa05vR yEpsCwZJtZzVMzIZIAdG7/bMN5lUnsre7K/5JUyK44jllf/jwuppLTW2qn3Bu1HBTvGV aPjQcW5qc00ItQsCQvRfosKZYDP4e4ZdCf9tuY+d2H/Z/9neJORMTU765m9bKkr5ViEA 9VK8hVCt96vYXd7vF+2MdkdyxSbscPIJTOzx37juz+W1KW/7IPVD5cBmxlTkQCfQdtLt /2O6BpwnJrhTZHFRkV+ABK98vML0fHltPPC++YYaph+z2sXFG8/jFCVsQaiUHk7SbGkP qaNw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mansr.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dr16si7676332ejc.320.2020.06.13.03.51.36; Sat, 13 Jun 2020 03:52:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mansr.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726279AbgFMKsq (ORCPT + 99 others); Sat, 13 Jun 2020 06:48:46 -0400 Received: from unicorn.mansr.com ([81.2.72.234]:54318 "EHLO unicorn.mansr.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726045AbgFMKsp (ORCPT ); Sat, 13 Jun 2020 06:48:45 -0400 X-Greylist: delayed 416 seconds by postgrey-1.27 at vger.kernel.org; Sat, 13 Jun 2020 06:48:45 EDT Received: by unicorn.mansr.com (Postfix, from userid 51770) id D4C3D15360; Sat, 13 Jun 2020 11:41:46 +0100 (BST) From: Mans Rullgard To: Wolfram Sang , linux-i2c@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] i2c: core: check returned size of emulated smbus block read Date: Sat, 13 Jun 2020 11:41:09 +0100 Message-Id: <20200613104109.2989-1-mans@mansr.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the i2c bus driver ignores the I2C_M_RECV_LEN flag (as some of them do), it is possible for an I2C_SMBUS_BLOCK_DATA read issued on some random device to return an arbitrary value in the first byte (and nothing else). When this happens, i2c_smbus_xfer_emulated() will happily write past the end of the supplied data buffer, thus causing Bad Things to happen. To prevent this, check the size before copying the data block and return an error if it is too large. Fixes: 209d27c3b167 ("i2c: Emulate SMBus block read over I2C") Signed-off-by: Mans Rullgard --- drivers/i2c/i2c-core-smbus.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c index 3ac426a8ab5a..a719c26b98ac 100644 --- a/drivers/i2c/i2c-core-smbus.c +++ b/drivers/i2c/i2c-core-smbus.c @@ -495,6 +495,13 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, break; case I2C_SMBUS_BLOCK_DATA: case I2C_SMBUS_BLOCK_PROC_CALL: + if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) { + dev_err(&adapter->dev, + "Invalid block size returned: %d\n", + msg[1].buf[0]); + status = -EINVAL; + goto cleanup; + } for (i = 0; i < msg[1].buf[0] + 1; i++) data->block[i] = msg[1].buf[i]; break; -- 2.27.0