Received: by 10.192.165.148 with SMTP id m20csp451476imm; Fri, 4 May 2018 00:20:03 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrqXZK8wN25MJ9+K4qNtbztaWTaUDUFOuObt3zH8x9EB0RIcicp4ZXBZHqSsuAcBFMxntIR X-Received: by 2002:a63:2ace:: with SMTP id q197-v6mr21828249pgq.60.1525418403581; Fri, 04 May 2018 00:20:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525418403; cv=none; d=google.com; s=arc-20160816; b=Fs6f76zMxNZ/qE2w//1EkvACuidbw8OOsrSLr2tdOVSmpLh+but8oELL8JpGx0uwew ErogQbNo/urKYhXKWv5qc9VBdNE1kGS+xh6+Ig7cJx1PkvUCDDpzfPrLxtOk+qQHTGdb MwQ0fdII9nuddJSamJZmYog+9bSOdaMA5chjsq3ot8YScHJVj7VJq4v2aYhICQUYhAFR AJExlgqFou5YT89nCM+KIncoqSYpD8ZsTv8VZfEAVr8ELEZnlOBHxqAtZXJVXzdkHVYq 0Lw6xURYwJsSTb/43mfU5vPVwqDSqyKNu1WjMhisAWBjS/4yvbsF82s6kflljwOenYuk QDyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=+fTiRzoKnzs6glRWj2vzqf/iYwjOceF92ztgmGVS28s=; b=S2suy+XCIJYxVzwef+esW57uxBmM6M5vNVUWe+3Wx2Pdu17K1N+U3J/CBJI1mp56IE XoTryMdtMDR5kvfYQYh9pwCajM1JilC+ijViM+DZAkdwRXhdh81lkosH3ngJ2mBi5rlj pQXsOhRVlxoQ4T2O379d0zNddGn00qWklvJmWUNAd1SvrgcK1cm7HYkJ1jVAV4gf/dye ka48eVqEY2HHboX5nXc9yP/HFm9jP+D2gW6i17G9RGOZTfb5+ISto7zvoFJXWUg6xkVy JzavHCG5fdf42YIjOUpbSdp5mT9GGM5sqO48wwSQA8lOfUMvpRAQdYswePWIyhtTQYzp iS8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umn.edu header.s=20160920 header.b=i7VuliCq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umn.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q14-v6si14490309pll.542.2018.05.04.00.19.49; Fri, 04 May 2018 00:20:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@umn.edu header.s=20160920 header.b=i7VuliCq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umn.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751340AbeEDHSZ (ORCPT + 99 others); Fri, 4 May 2018 03:18:25 -0400 Received: from mta-p3.oit.umn.edu ([134.84.196.203]:42750 "EHLO mta-p3.oit.umn.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751107AbeEDHSX (ORCPT ); Fri, 4 May 2018 03:18:23 -0400 Received: from localhost (localhost [127.0.0.1]) by mta-p3.oit.umn.edu (Postfix) with ESMTP id 11A47527; Fri, 4 May 2018 07:18:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=umn.edu; h= content-type:content-type:subject:subject:message-id:date:date :from:from:references:in-reply-to:received:mime-version:received :received:received; s=20160920; t=1525418302; x=1527232703; bh=l 9dcRIKzGl7MGBTVnpTXvvWUuwYCs8EOrILEbjndrYg=; b=i7VuliCqLqgb1V9gD Q+l8yNdXrH9ZfG6nLmuy26mlr7ZBxjH1M6Xs4qsVMb5EUH4+KGEzaa9mXCU3COfC eHxLbCv+AX+1sCeSQsvHXG3QMS/Y8e62Pj5kQ2PHDpTlyB0bOZgkdjkIoJGnIr0A jCPuXJYxCc18qLqmtIXnotD/Bd1eRL09Tv6CySY7SGb62RLZPpfOYZj1Tst07bCe K7f/zphczYrZl1Dm+3Tx9cAKNHHCFyVA0p0GksRZl1U2j2qiyDHgIv+N6dntpOH/ h8/ecBj0Dx9NKft5+P2Yhh/nxTH6QRK+qfvsn2G7IWBwSN18nCT0eM3AlkjRV+cj U0UoA== X-Virus-Scanned: amavisd-new at umn.edu Received: from mta-p3.oit.umn.edu ([127.0.0.1]) by localhost (mta-p3.oit.umn.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ogOu7-XKbyK4; Fri, 4 May 2018 02:18:22 -0500 (CDT) Received: from mail-io0-f179.google.com (mail-io0-f179.google.com [209.85.223.179]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: wang6495) by mta-p3.oit.umn.edu (Postfix) with ESMTPSA id C4686191; Fri, 4 May 2018 02:18:22 -0500 (CDT) Received: by mail-io0-f179.google.com with SMTP id z4-v6so24580238iof.5; Fri, 04 May 2018 00:18:22 -0700 (PDT) X-Gm-Message-State: ALQs6tCkzlPNgTYzyXvoIFgcApDZ6JFsFxK0o1IVslG5dSn/GAZhuEcg NI7g/alJ1XL87KwQBURq9vA2O4AzP0vrJeQqn2M= X-Received: by 2002:a6b:200e:: with SMTP id g14-v6mr30160630iog.161.1525418302488; Fri, 04 May 2018 00:18:22 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:6f07:0:0:0:0:0 with HTTP; Fri, 4 May 2018 00:17:42 -0700 (PDT) In-Reply-To: References: <1525300581-27217-1-git-send-email-wang6495@umn.edu> <4390a69e-a297-313d-044d-abf846eff1d1@axentia.se> <82973b7b-b6ef-6d42-df5c-be3ea72212fa@axentia.se> From: Wenwen Wang Date: Fri, 4 May 2018 02:17:42 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] i2c: core-smbus: fix a potential uninitialization bug To: Peter Rosin Cc: Kangjie Lu , Wolfram Sang , "open list:I2C SUBSYSTEM" , open list , Wenwen Wang Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 4, 2018 at 1:49 AM, Peter Rosin wrote: > On 2018-05-04 07:28, Wenwen Wang wrote: >> On Fri, May 4, 2018 at 12:04 AM, Peter Rosin wrote: >>> On 2018-05-04 06:08, Wenwen Wang wrote: >>>> On Thu, May 3, 2018 at 3:34 PM, Peter Rosin wrote: >>>>> On 2018-05-03 00:36, Wenwen Wang wrote: >>>>>> In i2c_smbus_xfer_emulated(), there are two buffers: msgbuf0 and msgbuf1, >>>>>> which are used to save a series of messages, as mentioned in the comment. >>>>>> According to the value of the variable "size", msgbuf0 is initialized to >>>>>> various values. In contrast, msgbuf1 is left uninitialized until the >>>>>> function i2c_transfer() is invoked. However, mgsbuf1 is not always >>>>>> initialized on all possible execution paths (implementation) of >>>>>> i2c_transfer(). Thus, it is possible that mgsbuf1 may still not be >>>>> >>>>> double negation here >>>>> >>>>>> uninitialized even after the invocation of the function i2c_transfer(). In >>>>>> the following execution, the uninitialized msgbuf1 will be used, such as >>>>>> for security checks. Since uninitialized values can be random and >>>>>> arbitrary, this will cause undefined behaviors or even check bypass. For >>>>>> example, it is expected that if the value of "size" is >>>>>> I2C_SMBUS_BLOCK_PROC_CALL, the value of data->block[0] should not be larger >>>>>> than I2C_SMBUS_BLOCK_MAX. But, at the end of i2c_smbus_xfer_emulated(), the >>>>>> value read from msgbuf1 is assigned to data->block[0], which can >>>>>> potentially lead to invalid block write size, as demonstrated in the error >>>>>> message. >>>>>> >>>>>> This patch simply initializes the buffer msgbuf1 with 0 to avoid undefined >>>>>> behaviors or security issues. >>>>>> >>>>>> Signed-off-by: Wenwen Wang >>>>>> --- >>>>>> drivers/i2c/i2c-core-smbus.c | 2 +- >>>>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>>>> >>>>>> diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c >>>>>> index b5aec33..0fcca75 100644 >>>>>> --- a/drivers/i2c/i2c-core-smbus.c >>>>>> +++ b/drivers/i2c/i2c-core-smbus.c >>>>>> @@ -324,7 +324,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, >>>>>> * somewhat simpler. >>>>>> */ >>>>>> unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; >>>>>> - unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; >>>>>> + unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2] = {0}; >>>>> >>>>> I think this will result in the whole of msgbuf1 being filled with zeroes. >>>>> It might be cheaper to do this with code proper rather than with an >>>>> initializer? >>>> >>>> Thanks for your comment, Peter! How about using a memset() only when >>>> i2c_smbus_xfer_emulated() emulates reading commands, since msgbuf1 is >>>> used only in that case? >>> >>> I was thinking that an assignment of >>> >>> msgbuf1[0] = 0; >>> >>> would be enough in the I2C_SMBUS_BLOCK_DATA and I2C_SMBUS_BLOCK_PROC_CALL >>> cases before the i2c_transfer call. However, this will only kick in if >>> the call to kzalloc fails (and it most likely will not) in the call to the >>> i2c_smbus_try_get_dmabuf helper. So, this thing that you are trying to fix >>> seems like a non-issue to me. >>> >>> However, while looking I think the bigger problem with that function is that >>> it considers all non-negative return values from i2c_transfer as good. >>> IMHO, it should barf on any return values <> num. Or at the very least >>> describe why a partial result is considered OK... >>> >>> Cheers, >>> Peter >>> >>>>> >>>>> Cheers, >>>>> Peter >>>>> >>>>>> int num = read_write == I2C_SMBUS_READ ? 2 : 1; >>>>>> int i; >>>>>> u8 partial_pec = 0; >>>>>> >>>>> >>> >> >> Yes, it is a big issue if the return value from i2c_transfer() is not >> equal to num. I can add a check like this: >> >> if (status != num) >> return -EINVAL; >> > > Right, but make sure to add it *after* the existing "if (status < 0)" > check as we want to preserve any existing error. Also, -EIO is perhaps > more appropriate than -EINVAL which seems wrong for what is probably > a runtime incident. > Sure, I will place it after the existing check and replace -EINVAL with -EIO. >> Also, I wonder why msgbuf1 is necessary if it is replaced by kzalloc >> in i2c_smbus_try_get_dmabuf()? > > It is not always replaced. The stack buffer is probably retained as > the default mode of operation (and fallback) because kzalloc is > expensive and because kzalloc might fail? > That means the stack buffer is probably used if kzalloc is failed. Actually, the kzalloc failure would be possible if a user-space process maliciously causes the kernel to consume a large chunk of memory. In that case, the user can potentially exploit this problematic code. So it may be better to initialize the stack buffer. Thanks, Wenwen > Cheers, > Peter > >> Thanks, >> Wenwen >> >