From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04DFEC433DB for ; Wed, 17 Mar 2021 21:14:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A822364EEC for ; Wed, 17 Mar 2021 21:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231346AbhCQVOQ (ORCPT ); Wed, 17 Mar 2021 17:14:16 -0400 Received: from gateway23.websitewelcome.com ([192.185.48.71]:46058 "EHLO gateway23.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbhCQVOI (ORCPT ); Wed, 17 Mar 2021 17:14:08 -0400 X-Greylist: delayed 1406 seconds by postgrey-1.27 at vger.kernel.org; Wed, 17 Mar 2021 17:14:07 EDT Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway23.websitewelcome.com (Postfix) with ESMTP id C1A3D524C for ; Wed, 17 Mar 2021 15:50:37 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id Md7ll16dlmJLsMd7lloaY4; Wed, 17 Mar 2021 15:50:37 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=DnIZtQW0bcbbdaS/T+teivlKQ5XVsr7Zh+LxMgxl77A=; b=PWVvR0sU3N0KIRxK9Uzfz04Xsx x5ifWNrxZ5YeNj5aKhcigVBl1Af3heKDijEko6iutE3pt2q+TMGFpi4UFna7mkCbThJeU+XTRUplv GTvLmLiYT/K6OZ8/zwGQuy2KK+gk4HwjUah7q6aUay3sA0BkB0e6cXx1UhYCueWls+PcAgf65E62R lQKt1ZOVST5kOiEwNYGN05wrqrYnq9kFPOTvSNQ5DS83Ymw6UQcw+q28c0iMmLyF74C1fSvsHumeY 94U2ZGoJwNL6lXxqx7XS09tdYwbW6wL8nsAhQQrmV0eOU8YlDsTKjO6wfjBU2KTDz+CZkobnfRhZG G471hFwQ==; Received: from 187-162-31-110.static.axtel.net ([187.162.31.110]:33348 helo=[192.168.15.8]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lMd7l-003qlO-9q; Wed, 17 Mar 2021 15:50:37 -0500 Subject: Re: [Intel-wired-lan] [PATCH][next] ixgbe: Fix out-of-bounds warning in ixgbe_host_interface_command() To: Jann Horn Cc: "Gustavo A. R. Silva" , Network Development , kernel list , intel-wired-lan@lists.osuosl.org, linux-hardening@vger.kernel.org, Jakub Kicinski , "David S. Miller" References: <20210317064148.GA55123@embeddedor> <3bd8d009-2ad2-c24d-5c34-5970c52502de@embeddedor.com> <03c013b8-4ddb-8e9f-af86-3c43cd746dbb@embeddedor.com> From: "Gustavo A. R. Silva" Message-ID: <944f3971-544f-49b4-1351-2eb3ee20588b@embeddedor.com> Date: Wed, 17 Mar 2021 14:50:27 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.162.31.110 X-Source-L: No X-Exim-ID: 1lMd7l-003qlO-9q X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187-162-31-110.static.axtel.net ([192.168.15.8]) [187.162.31.110]:33348 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 12 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/17/21 15:10, Jann Horn wrote: > On Wed, Mar 17, 2021 at 9:04 PM Gustavo A. R. Silva > wrote: >> On 3/17/21 13:57, Jann Horn wrote: >>>>>> diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> index 62ddb452f862..bff3dc1af702 100644 >>>>>> --- a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> +++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> @@ -3679,7 +3679,7 @@ s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, void *buffer, >>>>>> u32 hdr_size = sizeof(struct ixgbe_hic_hdr); >>>>>> union { >>>>>> struct ixgbe_hic_hdr hdr; >>>>>> - u32 u32arr[1]; >>>>>> + u32 *u32arr; >>>>>> } *bp = buffer; >>>>>> u16 buf_len, dword_len; >>>>>> s32 status; >>>>> >>>>> This looks bogus. An array is inline, a pointer points elsewhere - >>>>> they're not interchangeable. >>>> >>>> Yep; but in this case these are the only places in the code where _u32arr_ is >>>> being used: >>>> >>>> 3707 /* first pull in the header so we know the buffer length */ >>>> 3708 for (bi = 0; bi < dword_len; bi++) { >>>> 3709 bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); >>>> 3710 le32_to_cpus(&bp->u32arr[bi]); >>>> 3711 } >>> >>> So now line 3709 means: Read a pointer from bp->u32arr (the value >>> being read from there is not actually a valid pointer), and write to >>> that pointer at offset `bi`. I don't see how that line could execute >>> without crashing. >> >> Yeah; you're right. I see my confusion now. Apparently, there is no escape >> from allocating heap memory to fix this issue, as I was proposing in my >> last email. > > Why? Can't you do something like this? Yep; it seems you're right. I was thinking in terms of a flexible array. Also, I think I needed more coffee in my system this morning and I need to stop working after midnight. :) I'll send a proper patch for this, shortly. I'll add your Proposed-by and Co-developed-by tags to the changelog text. Thanks a lot for the feedback. I really appreciate it. :) -- Gustavo > > diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > index 62ddb452f862..768fa124105b 100644 > --- a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > +++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > @@ -3677,10 +3677,8 @@ s32 ixgbe_host_interface_command(struct > ixgbe_hw *hw, void *buffer, > bool return_data) > { > u32 hdr_size = sizeof(struct ixgbe_hic_hdr); > - union { > - struct ixgbe_hic_hdr hdr; > - u32 u32arr[1]; > - } *bp = buffer; > + u32 *bp = buffer; > + struct ixgbe_hic_hdr hdr; > u16 buf_len, dword_len; > s32 status; > u32 bi; > @@ -3706,12 +3704,13 @@ s32 ixgbe_host_interface_command(struct > ixgbe_hw *hw, void *buffer, > > /* first pull in the header so we know the buffer length */ > for (bi = 0; bi < dword_len; bi++) { > - bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > - le32_to_cpus(&bp->u32arr[bi]); > + bp[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > + le32_to_cpus(&bp[bi]); > } > > /* If there is any thing in data position pull it in */ > - buf_len = bp->hdr.buf_len; > + memcpy(&hdr, bp, sizeof(hdr)); > + buf_len = hdr.buf_len; > if (!buf_len) > goto rel_out; > > @@ -3726,8 +3725,8 @@ s32 ixgbe_host_interface_command(struct ixgbe_hw > *hw, void *buffer, > > /* Pull in the rest of the buffer (bi is where we left off) */ > for (; bi <= dword_len; bi++) { > - bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > - le32_to_cpus(&bp->u32arr[bi]); > + bp[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > + le32_to_cpus(&bp[bi]); > } > > rel_out: > From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gustavo A. R. Silva Date: Wed, 17 Mar 2021 14:50:27 -0500 Subject: [Intel-wired-lan] [PATCH][next] ixgbe: Fix out-of-bounds warning in ixgbe_host_interface_command() In-Reply-To: References: <20210317064148.GA55123@embeddedor> <3bd8d009-2ad2-c24d-5c34-5970c52502de@embeddedor.com> <03c013b8-4ddb-8e9f-af86-3c43cd746dbb@embeddedor.com> Message-ID: <944f3971-544f-49b4-1351-2eb3ee20588b@embeddedor.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: intel-wired-lan@osuosl.org List-ID: On 3/17/21 15:10, Jann Horn wrote: > On Wed, Mar 17, 2021 at 9:04 PM Gustavo A. R. Silva > wrote: >> On 3/17/21 13:57, Jann Horn wrote: >>>>>> diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> index 62ddb452f862..bff3dc1af702 100644 >>>>>> --- a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> +++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c >>>>>> @@ -3679,7 +3679,7 @@ s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, void *buffer, >>>>>> u32 hdr_size = sizeof(struct ixgbe_hic_hdr); >>>>>> union { >>>>>> struct ixgbe_hic_hdr hdr; >>>>>> - u32 u32arr[1]; >>>>>> + u32 *u32arr; >>>>>> } *bp = buffer; >>>>>> u16 buf_len, dword_len; >>>>>> s32 status; >>>>> >>>>> This looks bogus. An array is inline, a pointer points elsewhere - >>>>> they're not interchangeable. >>>> >>>> Yep; but in this case these are the only places in the code where _u32arr_ is >>>> being used: >>>> >>>> 3707 /* first pull in the header so we know the buffer length */ >>>> 3708 for (bi = 0; bi < dword_len; bi++) { >>>> 3709 bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); >>>> 3710 le32_to_cpus(&bp->u32arr[bi]); >>>> 3711 } >>> >>> So now line 3709 means: Read a pointer from bp->u32arr (the value >>> being read from there is not actually a valid pointer), and write to >>> that pointer at offset `bi`. I don't see how that line could execute >>> without crashing. >> >> Yeah; you're right. I see my confusion now. Apparently, there is no escape >> from allocating heap memory to fix this issue, as I was proposing in my >> last email. > > Why? Can't you do something like this? Yep; it seems you're right. I was thinking in terms of a flexible array. Also, I think I needed more coffee in my system this morning and I need to stop working after midnight. :) I'll send a proper patch for this, shortly. I'll add your Proposed-by and Co-developed-by tags to the changelog text. Thanks a lot for the feedback. I really appreciate it. :) -- Gustavo > > diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > index 62ddb452f862..768fa124105b 100644 > --- a/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > +++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_common.c > @@ -3677,10 +3677,8 @@ s32 ixgbe_host_interface_command(struct > ixgbe_hw *hw, void *buffer, > bool return_data) > { > u32 hdr_size = sizeof(struct ixgbe_hic_hdr); > - union { > - struct ixgbe_hic_hdr hdr; > - u32 u32arr[1]; > - } *bp = buffer; > + u32 *bp = buffer; > + struct ixgbe_hic_hdr hdr; > u16 buf_len, dword_len; > s32 status; > u32 bi; > @@ -3706,12 +3704,13 @@ s32 ixgbe_host_interface_command(struct > ixgbe_hw *hw, void *buffer, > > /* first pull in the header so we know the buffer length */ > for (bi = 0; bi < dword_len; bi++) { > - bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > - le32_to_cpus(&bp->u32arr[bi]); > + bp[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > + le32_to_cpus(&bp[bi]); > } > > /* If there is any thing in data position pull it in */ > - buf_len = bp->hdr.buf_len; > + memcpy(&hdr, bp, sizeof(hdr)); > + buf_len = hdr.buf_len; > if (!buf_len) > goto rel_out; > > @@ -3726,8 +3725,8 @@ s32 ixgbe_host_interface_command(struct ixgbe_hw > *hw, void *buffer, > > /* Pull in the rest of the buffer (bi is where we left off) */ > for (; bi <= dword_len; bi++) { > - bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > - le32_to_cpus(&bp->u32arr[bi]); > + bp[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi); > + le32_to_cpus(&bp[bi]); > } > > rel_out: >