Received: by 10.192.165.148 with SMTP id m20csp697926imm; Fri, 4 May 2018 18:44:29 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrJ5VIMlIZbSuLgwLt3xbUie3z5HVFtMDQxeOkPpwj3C/eY2l849Vy9qaFDEYON1oVg93A5 X-Received: by 2002:a17:902:268:: with SMTP id 95-v6mr4831870plc.386.1525484669727; Fri, 04 May 2018 18:44:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525484669; cv=none; d=google.com; s=arc-20160816; b=eKftws51dWf4G8Ex8sjvysDEL5a7tbMLVrFO1Gu1CtVbqTuVLXmV6bAvPpKUIOZEQ6 zfY2MBdpV6aquW05Xox9LflAlaOGXeAYwZcmZLmnqqjyJX9aS2bNm5N6GsPriBteH/6G ZcEsp4xPfHot10ITbw99cCDfqoz7J81Duf8Q8gO+wVwU3WYP5eVk+E/Qgf+9wDcbgECM nEbJH8IfyYRIUa9pyiAeq95pVip4yZzQmXtLjqbJuCizSWEDKWjsOaq75Rv2l3jev7jw 3ljYBtie5qBA379oEcRpFzKBv88usiXwTe3wWoGKpdnYPKwdWUegNiEpKzI4BN6e7L5O FTeQ== 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=LJau+ji0qCbCdVHE1nnZRZh4nyIBipmH43DS0z8oXsc=; b=e1FBKVruKvsuW4rFXFW2kNDeCR+9z0PrzI5ChCZJb1dFrmscysueXUuCm4nKhOjv35 O/kJrXAUePFGxxapdiV5dm8NkxNctmWgKWkqCQT6VhxyRK0NKsVyBznOOeqEiti+Aos7 fJ8ZStDeCPov/hcycFjy0eHlC4vhNoxrY3T0ub0tuoTskUPfeuOMGEWv3nKr6hdJBMM5 KAKox3ykWF7TwElxeJcWnyMDO1/AAASAT7YyGLVJESNfzZDXrtSaw2pX8Y+ZUm63+k7b 098mzK8MnBjBAMJJaeewI7bOkHXfQELqL4AvEOy5XHzANqg+umjRLSSGh0E3XAbXaQZC 0Sqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umn.edu header.s=20160920 header.b=xyqNLL1W; 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 p13-v6si17197025pll.416.2018.05.04.18.43.41; Fri, 04 May 2018 18:44:29 -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=xyqNLL1W; 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 S1751903AbeEEB3T (ORCPT + 99 others); Fri, 4 May 2018 21:29:19 -0400 Received: from mta-p3.oit.umn.edu ([134.84.196.203]:33916 "EHLO mta-p3.oit.umn.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751684AbeEEB3R (ORCPT ); Fri, 4 May 2018 21:29:17 -0400 Received: from localhost (localhost [127.0.0.1]) by mta-p3.oit.umn.edu (Postfix) with ESMTP id 7867172E; Sat, 5 May 2018 01:29:16 +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=1525483756; x=1527298157; bh=R 7KhaDM40ZExRnhunnVxT3a1Qc0pPzTalD1nGZNeKvQ=; b=xyqNLL1WHinFEhL+2 n9rbv4TrkEPN0VwsAuOQTksqkCoR8KY4y6TMntT8aWOLBE8PES5IkC17vZrTq1do AL80c7SN6Db06+tyf/CgYIjanVSO+AqeJkqZkwHdiH58hqrF+zOaCiL0OPAsjYs1 not3HSfzdWMR4j7oqkfBKup93U7FVQzZkpWTEBCkxe3J07wJkK/bUNXu6e0BBpH2 Wf7jxZApI0CL5E1Em2yDZE/FVdRk0NZOAmvdqKkeFu/HoSGSNPMhLPVtFb/4sTuA Y2VB5qE4y63QfUJ+ZN5ztYH3kA8ku1mQxX8KHUsXDg0RupH3WuyfklOIspaqJsQ0 s+wVA== 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 c2oqAnYO4I5q; Fri, 4 May 2018 20:29:16 -0500 (CDT) Received: from mail-io0-f170.google.com (mail-io0-f170.google.com [209.85.223.170]) (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 3E1DE728; Fri, 4 May 2018 20:29:16 -0500 (CDT) Received: by mail-io0-f170.google.com with SMTP id g1-v6so18325971iob.2; Fri, 04 May 2018 18:29:16 -0700 (PDT) X-Gm-Message-State: ALQs6tD+x9eUqKO+O+wAEaL4gs+DYIFAoFQqmH2WBNZGuL9QjCJ/e3Mv U/N6NDqDoJfv5EAijKSaoF6PodDoNhnKsmkqoHQ= X-Received: by 2002:a6b:21cb:: with SMTP id h194-v6mr29757069ioh.181.1525483755807; Fri, 04 May 2018 18:29:15 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:6f07:0:0:0:0:0 with HTTP; Fri, 4 May 2018 18:28:35 -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> <8100306b-1aab-718e-258c-f69e6ab51446@axentia.se> From: Wenwen Wang Date: Fri, 4 May 2018 20:28:35 -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 10:38 AM, Peter Rosin wrote: > On 2018-05-04 16:59, Wenwen Wang wrote: >> On Fri, May 4, 2018 at 2:27 AM, Peter Rosin wrote: >>> On 2018-05-04 09:17, Wenwen Wang wrote: >>>> 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. >>> >>> Yes, but I see little reason to initialize more than the first byte. >>> >>> You hinted in the commit message that there were execution paths (or >>> implementations) where the second buffer wasn't initialized. Can you >>> give an example where this matters when the more extensive check on >>> the i2c_transfer return value is in place? That seems like a bugs >>> that should *also* be fixed in the affected i2c bus drivers... >> >> One possible execution path is as follows: >> >> i2c_transfer -> __i2c_transfer -> pca_xfer (which is one of the >> master_xfer handlers) >> >> In pca_xfer(), it reads the status of the i2c_adapter and then >> performs different actions according to different statuses. >> >> It seems probable that the buffer is not filled with the wanted data >> if the status is not as expected. > > Ah, so you're talking about hardware malfunction without any actual > real-life incident. In other words, pure speculation. I'm sure the > kernel is full of problems if every potential HW misbehavior is > considered, and I'm not so sure this particular problem is going > to matter all that much... Thanks for your comments, Peter! I will submit a new patch :) Wenwen