Received: by 10.192.165.148 with SMTP id m20csp144383imm; Fri, 4 May 2018 08:02:05 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpT7gaQUXTaqtB81u1+62raUa1sr324wZmAkwGNxyhoq0EpwfnRXUGhhHDB7nSAQC/PRep0 X-Received: by 10.98.18.212 with SMTP id 81mr27418281pfs.243.1525446125256; Fri, 04 May 2018 08:02:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525446125; cv=none; d=google.com; s=arc-20160816; b=PC69U8Bfb6Iiro+m3Uuq240Qx4hls5Pn/1PQDewFK14S7FC25AG9v//Lg/o4lw9GUw 4rftFCDPd5mB375Chu/0CFppX9wrh4sIC/D0ONLwCpAUA+b8Lw0ziqN7qB1lfgwR2SJw IsvoJVwunRFAfGHMaIxpEH3vG2SM8dtJ8CNLgXdhq05zePfMdOxkzn7IuoecD1pTw4Q3 NYnMf/DJELIlKYM7VLLR966vmPF4T42gilnXPdkjlFbimciKGn0ElMo0AvNeYG4q0E3/ eKsgPGtvxgn44XpNm74KrHYSwsvxnxY+++ZmuUoIBSa2zBN/9QAPzPtIx1kRbyYfuu+P Vs4A== 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=g6hCm1E1HezZHueK3u28D3Rzqzvh8zbsSgq3XYYre/w=; b=xVlxl4vf30XV7QRtEMBuoJVR/diK1/AtYKNDbaVU5gMYfFAxxUuq0iggPu8Wn26xs7 dZkizBs9waId6qv0h6bDK/ly+eNLWXKYSbtXaqJsvjWXtKFYHuN1URZfjJtio1CeSNSy zYeRJRpG3yBUDNfGKdqfpE18shnjbAL9X3KNQTnpH++JEU1WW49SxKWwPux1cXY2IwUl jLlO4zeafdx1pri3APGJ6h9D2p2+sGCutV2hoiKDk8dzPkdKNqYCZTpGhk8naiWw2N1D Nb/5OoxnQT3mtMjUjY8ZtTkLdyFfSrn8gi07D9Eh4SxZnzGejbXMuDZU8i66J1dVopll 1lSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umn.edu header.s=20160920 header.b=xsg9F4QL; 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 68-v6si15959331pla.452.2018.05.04.08.01.50; Fri, 04 May 2018 08:02:05 -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=xsg9F4QL; 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 S1751404AbeEDPAR (ORCPT + 99 others); Fri, 4 May 2018 11:00:17 -0400 Received: from mta-p1.oit.umn.edu ([134.84.196.201]:54496 "EHLO mta-p1.oit.umn.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751189AbeEDPAP (ORCPT ); Fri, 4 May 2018 11:00:15 -0400 Received: from localhost (localhost [127.0.0.1]) by mta-p1.oit.umn.edu (Postfix) with ESMTP id 5FEF358B; Fri, 4 May 2018 15:00:14 +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=1525446014; x=1527260415; bh=5 18lLp+G4rM2CPckxTYE668v1ZkGn63xqtrlECs4QYk=; b=xsg9F4QLY8TBvP4De qbiWWy2cMGXjW1h+WwwpQTlispoNQOy+rHI94GY7dyOaUj9DW7A/dMtd6RLu79fK er2eWVMM+qwEaaZ5k4LrerOY618Ncx3c7JTCP2RGz9KVGPlWr0o81wEFqcz60yx0 zFajTPO54u/43jwMTaleOZfd/I4/W4f+rUaC3QD0p/CTx6ycd8LLMBDSuB+iv3Sh KgAo81IhLhOvTkBa4T7Ihp8D3DjB6vnoVB780AynZRADR4HrStIF9ANMkj3jNrmN 3obUQwmApqHOYccVsJTVWQWu9by7Y99ER7vltsOAWDPK/I6i3DFGNO7TPsAMCOAl 6OS0g== X-Virus-Scanned: amavisd-new at umn.edu Received: from mta-p1.oit.umn.edu ([127.0.0.1]) by localhost (mta-p1.oit.umn.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id C6MMZ0FjpJcR; Fri, 4 May 2018 10:00:14 -0500 (CDT) Received: from mail-io0-f176.google.com (mail-io0-f176.google.com [209.85.223.176]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: wang6495) by mta-p1.oit.umn.edu (Postfix) with ESMTPSA id 29EC8284; Fri, 4 May 2018 10:00:14 -0500 (CDT) Received: by mail-io0-f176.google.com with SMTP id a10-v6so25965013ioc.9; Fri, 04 May 2018 08:00:14 -0700 (PDT) X-Gm-Message-State: ALQs6tBD+PDRRjhQWs12sYtGTC42N9J8ufM+C26MxyMIGQPC1OAeQJbN WroOOrgIe00A4eljbeescXWAUKiTDH7F0+mT7eM= X-Received: by 2002:a6b:5010:: with SMTP id e16-v6mr29155206iob.274.1525446013800; Fri, 04 May 2018 08:00:13 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a4f:6f07:0:0:0:0:0 with HTTP; Fri, 4 May 2018 07:59:33 -0700 (PDT) In-Reply-To: <8100306b-1aab-718e-258c-f69e6ab51446@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> <8100306b-1aab-718e-258c-f69e6ab51446@axentia.se> From: Wenwen Wang Date: Fri, 4 May 2018 09:59:33 -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 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. Wenwen