From patchwork Mon Oct 9 17:59:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 13414184 X-Patchwork-Delegate: johannes@sipsolutions.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 193DECD611A for ; Mon, 9 Oct 2023 17:59:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377522AbjJIR7u (ORCPT ); Mon, 9 Oct 2023 13:59:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377375AbjJIR7s (ORCPT ); Mon, 9 Oct 2023 13:59:48 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8885EBA; Mon, 9 Oct 2023 10:59:47 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40B45C433C9; Mon, 9 Oct 2023 17:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696874387; bh=cb2RlVIw4noXeBlWpyiD4YYjxMG+5cCZ+39utZ6p6gM=; h=Date:From:To:Cc:Subject:From; b=Hj1WIpj0zzjAsMsi1dvVoxdhsRzyggk+NaQPbeJZVWQApNUzsElvGiepI5DBDgPdP B2Zifh4pmohGQKkl0aCwoOd9U+hWq2Ykynm6Y/Z+7j8hcOwXIaPoweQU0pRwicOtfI QCT+lKR/6/ad2somVn9EedoZEuUE3R5/orMp4Vy6lEeUf721HtK5Y1Ru0EXfDJBoke efwg9yl7C/PoVK35hMsgBB3+pqzjvIhJWA0s0T5lbsm0VyRcEVlM+29OPVSG1e37JC iem45Bx8weBz/dhHtOZlqinJvMsLxHqf5nRKO12XALOar/3Fyy0ydx8YMpTK6MTa5h sKmCrJRNObpkw== Date: Mon, 9 Oct 2023 11:59:41 -0600 From: "Gustavo A. R. Silva" To: Johannes Berg , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] wifi: mac80211: Add __counted_by for struct ieee802_11_elems and use struct_size() Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). While there, use struct_size() helper, instead of the open-coded version, to calculate the size for the allocation of the whole flexible structure including, of course, the flexible-array member. This code was found with the help of Coccinelle, and audited and fixed manually. Signed-off-by: Gustavo A. R. Silva Reviewed-by: Kees Cook --- net/mac80211/ieee80211_i.h | 2 +- net/mac80211/util.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h index e92eaf835ee0..0d3d386445c5 100644 --- a/net/mac80211/ieee80211_i.h +++ b/net/mac80211/ieee80211_i.h @@ -1746,7 +1746,7 @@ struct ieee802_11_elems { */ size_t scratch_len; u8 *scratch_pos; - u8 scratch[]; + u8 scratch[] __counted_by(scratch_len); }; static inline struct ieee80211_local *hw_to_local( diff --git a/net/mac80211/util.c b/net/mac80211/util.c index 98a3bffc6991..a91b0e7795a4 100644 --- a/net/mac80211/util.c +++ b/net/mac80211/util.c @@ -1612,7 +1612,7 @@ ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params) int nontransmitted_profile_len = 0; size_t scratch_len = 3 * params->len; - elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC); + elems = kzalloc(struct_size(elems, scratch, scratch_len), GFP_ATOMIC); if (!elems) return NULL; elems->ie_start = params->start;