Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3262695rdh; Thu, 28 Sep 2023 07:10:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJ/Wj/7bXB0n550quoN8PNPm9igpnQMJ9mb6AXLa+nCsjrqx+gigXn04BEUbJk0WaOFHw1 X-Received: by 2002:aa7:8d48:0:b0:68a:48e7:9deb with SMTP id s8-20020aa78d48000000b0068a48e79debmr2033985pfe.2.1695910243840; Thu, 28 Sep 2023 07:10:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695910243; cv=none; d=google.com; s=arc-20160816; b=Hjcok1c0aVj6gQSsdHa1z/Oj52BZY/pq12M1X10HnoEy/5Oj/XW4I9eWjwYYP/j/+y 9bI8414xjIiBj3LX5GGdL4SRCD1qj4vnbNlTuzlJy4IhitagCptt5R4ktja7TKPEkZyU 9byV8hRfL+vYs00UxtyjgIRi/nKAI3PgbN+skh0nGbJKcqfHLtkqzavJr511pXtU0IFt Dph18+ZpiSfd5jGsSaOsBWJ0U/g/w1PBNL0KBY7hXX8uHyz+1NtjF2RYGves8tKxa5wv cXkDtl5sLUSTLMZhrzJLZ84BrFkud/cqQzewfBqt7uFl+AXM0GQfGJfmr1lFidD1T2Y+ lrzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=aNbWuwqaif9wyxfe+6mf4OhKgEfNUWyl9wZQEOKArmo=; fh=LwyWnHnEEGYQzStCmGmKNiQ2f4MbYz+voD6+9l9dYrQ=; b=QHCaCpNTNPI9o2WWIqFP23JjXXbv7nYzETzUY738dI3WiioUGHvRdpDUUfYHlzL90R bkmcsqrFvq/e41cDPRJNemqV6JyWRPOfy+QtHZ8isUWmBQd3iJj9f9x/7jMHWQ06X0b1 dHjJ/oaEUA6Inf2FD3UU2tyxmFyc/1nupTVgqSPhZoNOix54kVtI1+QP5bCsOwtw7Ohs GgVK1tAQf4kFOEmjMOG5SAGv+8MdaSIpHlT6roZXLjHOdEd28LNPUXsrlZPKrFaEg5Yj KOACUMNEtckg5qUDs9IYqBbPNF3K6hzjnA6C4cwTm1rTlPfb1Nmt7M9f3DqQzwROHEkP CoFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ezyR/Fxm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id j71-20020a638b4a000000b00573fa8f2829si18939242pge.383.2023.09.28.07.10.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 07:10:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ezyR/Fxm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DC6FB82E7522; Thu, 28 Sep 2023 06:27:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231871AbjI1N1x (ORCPT + 99 others); Thu, 28 Sep 2023 09:27:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231243AbjI1N1w (ORCPT ); Thu, 28 Sep 2023 09:27:52 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FEC7180 for ; Thu, 28 Sep 2023 06:27:50 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7F3BC433C8; Thu, 28 Sep 2023 13:27:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695907670; bh=l2AzfVnQusW7bzHrFKQB4PAqb+m//Ig8AMRGcQRghPA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ezyR/FxmKsts3EHdMLgVbT0yHbZFg4bvvJ9WhGuWXLkquTvF9fZkDoItY6n6O9x3g 7HEN4870TbLrza3/zkzdGiJDY3SVLxl1D9oEshZa1hUps3DfKsW3AB7RqjoWiAzkej FC9UYKPVn0E+bp4WmMMyRknhpPC/I56PUc0U1batkkUxy26zxxWwOBhC4I2mBYH9d3 0VwsNDhPh7H9oprBjq9iwwJLmzj5iJBohSvw9WUy3RlF35GwS+9wFSqBo2GkpKnEos Gzkix9bLvTMdqOYqWToBDA1udMUTOVe1nmnoCutQBiIJtcmGZ5bpAQYn0GcW7eowec R8h/P3w6Y+6QQ== Date: Thu, 28 Sep 2023 15:27:37 +0200 From: Simon Horman To: "Gustavo A. R. Silva" Cc: Ariel Elior , Manish Chopra , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Michal Kalderon , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] qed/red_ll2: Fix undefined behavior bug in struct qed_ll2_info Message-ID: <20230928132737.GL24230@kernel.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 28 Sep 2023 06:27:56 -0700 (PDT) On Sat, Sep 23, 2023 at 07:15:59PM -0600, Gustavo A. R. Silva wrote: > The flexible structure (a structure that contains a flexible-array member > at the end) `qed_ll2_tx_packet` is nested within the second layer of > `struct qed_ll2_info`: > > struct qed_ll2_tx_packet { > ... > /* Flexible Array of bds_set determined by max_bds_per_packet */ > struct { > struct core_tx_bd *txq_bd; > dma_addr_t tx_frag; > u16 frag_len; > } bds_set[]; > }; > > struct qed_ll2_tx_queue { > ... > struct qed_ll2_tx_packet cur_completing_packet; > }; > > struct qed_ll2_info { > ... > struct qed_ll2_tx_queue tx_queue; > struct qed_ll2_cbs cbs; > }; > > The problem is that member `cbs` in `struct qed_ll2_info` is placed just > after an object of type `struct qed_ll2_tx_queue`, which is in itself > an implicit flexible structure, which by definition ends in a flexible > array member, in this case `bds_set`. This causes an undefined behavior > bug at run-time when dynamic memory is allocated for `bds_set`, which > could lead to a serious issue if `cbs` in `struct qed_ll2_info` is > overwritten by the contents of `bds_set`. Notice that the type of `cbs` > is a structure full of function pointers (and a cookie :) ): > > include/linux/qed/qed_ll2_if.h: > 107 typedef > 108 void (*qed_ll2_complete_rx_packet_cb)(void *cxt, > 109 struct qed_ll2_comp_rx_data *data); > 110 > 111 typedef > 112 void (*qed_ll2_release_rx_packet_cb)(void *cxt, > 113 u8 connection_handle, > 114 void *cookie, > 115 dma_addr_t rx_buf_addr, > 116 bool b_last_packet); > 117 > 118 typedef > 119 void (*qed_ll2_complete_tx_packet_cb)(void *cxt, > 120 u8 connection_handle, > 121 void *cookie, > 122 dma_addr_t first_frag_addr, > 123 bool b_last_fragment, > 124 bool b_last_packet); > 125 > 126 typedef > 127 void (*qed_ll2_release_tx_packet_cb)(void *cxt, > 128 u8 connection_handle, > 129 void *cookie, > 130 dma_addr_t first_frag_addr, > 131 bool b_last_fragment, bool b_last_packet); > 132 > 133 typedef > 134 void (*qed_ll2_slowpath_cb)(void *cxt, u8 connection_handle, > 135 u32 opaque_data_0, u32 opaque_data_1); > 136 > 137 struct qed_ll2_cbs { > 138 qed_ll2_complete_rx_packet_cb rx_comp_cb; > 139 qed_ll2_release_rx_packet_cb rx_release_cb; > 140 qed_ll2_complete_tx_packet_cb tx_comp_cb; > 141 qed_ll2_release_tx_packet_cb tx_release_cb; > 142 qed_ll2_slowpath_cb slowpath_cb; > 143 void *cookie; > 144 }; > > Fix this by moving the declaration of `cbs` to the middle of its > containing structure `qed_ll2_info`, preventing it from being > overwritten by the contents of `bds_set` at run-time. > > This bug was introduced in 2017, when `bds_set` was converted to a > one-element array, and started to be used as a Variable Length Object > (VLO) at run-time. > > Fixes: f5823fe6897c ("qed: Add ll2 option to limit the number of bds per packet") > Cc: stable@vger.kernel.org > Signed-off-by: Gustavo A. R. Silva Reviewed-by: Simon Horman