Received: by 10.192.165.148 with SMTP id m20csp367144imm; Thu, 3 May 2018 22:30:00 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpEkfrVcsW8/XF5UGsSU0BE8zRULBItqeUGGC/k6ehh2LwXHnNW/lxByDkCXPMJdLmaDTMD X-Received: by 2002:a63:9741:: with SMTP id d1-v6mr21684620pgo.447.1525411800561; Thu, 03 May 2018 22:30:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525411800; cv=none; d=google.com; s=arc-20160816; b=oV5zo9X907S0iT2cLh0g+cCV9Mao2iW/nzUsJ+TJ9J+02ONq0qvjzo1rY9SxOUCDz7 tv5dgqYO+02n6ZRgHwKsvmrm3OH4iUNtJEJoboWhFzRcT7y2F2UOVdPCxNmtc4Zh4njv UpWfW9FtsMbP/Yp5BrDXlrNvywQehKjTd9Tbh8QkzO2uUToiuBjfrIGNtfFCX4e+l1yx UwmldCtBfCDwn+m6Y/Y9ZG1n3VVDw/FdFKiX8oPbj1MZLgm3BE5yiE9h7F3sgjXV/47F 492dyCU8p22Nwg9aoszHMgBCc4HftigfYkaCNHN79bJMoV5rIUyfTkya3n3+gzlKp2BZ e4uA== 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=tciNzFZXPxT7VnB0PeFsS383eZWA45hc12HURZYKAvA=; b=lLbP1/e9JIRJu4P6MCwxCFyAIQxv/8jQ1fnPCYutVr8RM+O+yMbbkKWGk0Urc+c5Ta nZ2KE/LVMvq2UKRPZdOE6NxpwgRrzyaOQ9Kbq1Yekt1sgUJo8R3lgWpVvSc55incK9ou fxos0CKoBsuf9cnw7Vj7pPqjgQNnwwHmZQVVwbsJwflyUzVrUnWJMRk2KB5Ntl8tjIyC EXu7g0UxwdruvuCgd6pmbi4//9e5GQ0Ln8CV+YOfXUI2ACjatZo3TvRUxfY90cVSpK27 mpg0xBEfCt3NT9gqgjRq/ZIpAkGz3oDM/JNtASBNBBItdwQEmi4bUMnAd9DusRFQkJTA 7MIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umn.edu header.s=20160920 header.b=oYNR458S; 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 u71-v6si12665204pgb.332.2018.05.03.22.29.33; Thu, 03 May 2018 22:30:00 -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=oYNR458S; 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 S1751291AbeEDF26 (ORCPT + 99 others); Fri, 4 May 2018 01:28:58 -0400 Received: from mta-p3.oit.umn.edu ([134.84.196.203]:57024 "EHLO mta-p3.oit.umn.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751239AbeEDF24 (ORCPT ); Fri, 4 May 2018 01:28:56 -0400 Received: from localhost (localhost [127.0.0.1]) by mta-p3.oit.umn.edu (Postfix) with ESMTP id C9AB357D; Fri, 4 May 2018 05:28:55 +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=1525411735; x=1527226136; bh=P TQbxKoJ5ne5WpKLmYZKmLCJNlT/vsTajJ541y9vWUA=; b=oYNR458SNszdkbXys Pyhw7tJbQZb4esuAm9xAr/qZjUMBZxKojqMxEBzxRQRP3kgz76+QP/DN/5UJuUy6 h7tbpbbTnFNNc4J45ShBF0oFA1MaLVxJ5VKtuuH9hwYKNCmoYU2ringkAuwQufAY uWI3dBQqrkIaDLc3j3FasnD82j1OG1SS4Qei1eTLxBf5Y/bnOajJwgcm7DLsgojf wdOYJVgAuK87XttFDFDSL9cVbvJ4r60ZntUPUk2AvaGlujUnZrbaqaJHSSc9RRLv 5BDgpYqBVpOvXwmWowhSttkiwGa/DZBiqO26tLMK+NqgFyTdTQa0Bj5olC40jy2j mE65w== 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 U-JjBb4uS9yv; Fri, 4 May 2018 00:28:55 -0500 (CDT) Received: from mail-it0-f44.google.com (mail-it0-f44.google.com [209.85.214.44]) (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 87773290; Fri, 4 May 2018 00:28:55 -0500 (CDT) Received: by mail-it0-f44.google.com with SMTP id f65-v6so1973675itd.3; Thu, 03 May 2018 22:28:55 -0700 (PDT) X-Gm-Message-State: ALQs6tCjWb8oPQUAFDWVPcbTeCeX8po8xa7IQJSHgv/ObqyGkSeF3JXY UpQnFrOoHeZpauMAwcG0r/SOHQpPpjISMRD0HFY= X-Received: by 2002:a24:25d0:: with SMTP id g199-v6mr8981335itg.26.1525411735265; Thu, 03 May 2018 22:28:55 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:6f07:0:0:0:0:0 with HTTP; Thu, 3 May 2018 22:28:14 -0700 (PDT) In-Reply-To: <82973b7b-b6ef-6d42-df5c-be3ea72212fa@axentia.se> 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 00:28:14 -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 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; Also, I wonder why msgbuf1 is necessary if it is replaced by kzalloc in i2c_smbus_try_get_dmabuf()? Thanks, Wenwen