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: 150249 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2034706vqo; Mon, 9 Oct 2023 10:59:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGh/wHGziRlNnAcs2TMHtvPwh6QvsA2oe73zSwRvFr8BMjMtXBY0kS9P7mzGJknsaz4kxij X-Received: by 2002:a05:6830:1d6c:b0:6b9:9f84:dc8b with SMTP id l12-20020a0568301d6c00b006b99f84dc8bmr15768881oti.19.1696874397473; Mon, 09 Oct 2023 10:59:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696874397; cv=none; d=google.com; s=arc-20160816; b=oW4mmqWoAeFSDY8G7e6VWVNx57wCBo6JcWDE4RtYl8YugUXMNOmh+13x7Cm7B5Le4V 5/XurQxjCd5uwyCZdhElh3A+Axa40c6QUvON51u3UyfzoyafxU9dE4CzMoQHCgOc4QFe X1KceGGplMZuhkgG6chZ1d2rHEKyns73PGn07SZ3pGjFbrKihGeK1+hAK+IIRFQbGQj0 jGMkkMWYWV+Ut8fLR3KS6sbTbn3T5Hxtivd5Jy6FNEZ6B9Z5ANc1I9HCeoBa8SkItg8A KuGURbsV3EkxMPA9itLyAW0BfDD6aUE0y0M+8s3JLsNf6/XfFSIJb4XE1XgoDw0u7/xQ xSdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=ABPg2X/ibqqxVXhnaI/PMbnFfNXOY4VmvDOVPQ5zCyI=; fh=ASuO/UN72Nvy/xfjrtvV15hm6tspssbFgAn5/HMVySs=; b=j811qEHScymElxC+VIsG2KgLvdTVb3R/ziF8H87tBF8qpT9H6q9XVQcUKAgciUaTv7 hyNkH26+gJgo/LEOB3DSQelVG1dvvlYLF2c5a5Y9qM5zBgkmk1H5hsXDp78aPFyb/uRm xY9JjsWxwwrBFLgJy/VOrZsYqcLLBzzz/IIKWD1v96WhF6iY6b/1VwFXHtD+rk2XN9fK 2pjZfejaVQlAx0y/qcNbgg54W4U5jBG4z7hRIrQmaybjl71M5RwLpDCb0mfN4mNM08iN jc0wZnvd4s5auU/2TUHTmS4h3jeloKFMHMT9NX9wm0jBeiJPXGY4gkDPAsnhLfEt6qfg ExpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hj1WIpj0; 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id k186-20020a633dc3000000b0057a32c18a47si6623617pga.47.2023.10.09.10.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 10:59:57 -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=Hj1WIpj0; 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 F31B6801BCDD; Mon, 9 Oct 2023 10:59: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 S1377964AbjJIR7v (ORCPT + 18 others); Mon, 9 Oct 2023 13:59:51 -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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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]); Mon, 09 Oct 2023 10:59:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779301767962167151 X-GMAIL-MSGID: 1779301767962167151 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;