Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp30797rdb; Wed, 18 Oct 2023 17:03:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZBhBrt4nvyJkhsAUZP86VbHSD5oMliiBAXdKXbVFpikLKvnDHSuiiPKGgMyI5hmKID7rT X-Received: by 2002:a05:6358:cb27:b0:166:d4ec:f616 with SMTP id gr39-20020a056358cb2700b00166d4ecf616mr430198rwb.4.1697673809835; Wed, 18 Oct 2023 17:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697673809; cv=none; d=google.com; s=arc-20160816; b=eMPJqqxf5ac8ylj60ZCEcnwSnGJoojSqtaf6/Is3lu6LbqYT7hzF2LQ4uCVEyl8SgU 3eggmCL6ja7rQiED/iGJlb/iWsrDyshVkmrQraajXzQHLy25stIUBGPp0V8Li4GR54lX E4FlC0fr0mZEeokl6m8dPl+6mvrLfH5FU70XOjZk4mdLhbpZkqTki+r8IO8eoqasJgog imskiLwR20sTFg3dSwPyHzwPyqnCpnRmbHp7wu+o/ebV44pZUfNcvQ83hSrHed3248BR qZ+VC5EBFqLhCaB8rGFIrLfP2YJQWDJgwRBWCONPdIMRYEboUIkpWf1C11+V63w7Tn5d oKcg== 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=T/Zi+O5EOF8MxmYTpNQNZzDcOGwyvihj+XdDWSOvn6s=; fh=wJ5QCLCtilVt+38cfrPLO49L0KeR4LRtovl1gE1SzKA=; b=eyoOwDat1TDBaKGyDi+Ce6OBvMYSUowbMXs+rj6UN+MM2NCHuvIHM8FpbGSiBkbDVB icGyAkaZDHjqQsWYD+dxDKTcoJhliGbX7EFmaxybOY4Oxwx83/lOUxN/qLORcU0FHcMK PAR7mkCE82x5GX/hfeMq7IcBlax6jAK0YsZV2vTKXywBmJmAz9lrUex/YxwPtj5N1ct0 qa6fKEVKjvHBOCfMILPBcJovDGPpLI9u7Yv+rMXsTc1ZzAcFeOWO+DXq5FPOQP3bgWzJ l8dQ3CoMLmV675YM3y9K+aA0ypkOtyWvd+giZU7io/LD9AjvBjW1DkykU5Hpqwb36vGA tvEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=iTY8joTj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id w65-20020a636244000000b00563d9ff5158si3212701pgb.350.2023.10.18.17.03.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 17:03:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=iTY8joTj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 5F73680B288B; Wed, 18 Oct 2023 17:03:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230233AbjJSADI (ORCPT + 99 others); Wed, 18 Oct 2023 20:03:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbjJSADG (ORCPT ); Wed, 18 Oct 2023 20:03:06 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C26CD106 for ; Wed, 18 Oct 2023 17:03:04 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-5b5354da665so2505563a12.2 for ; Wed, 18 Oct 2023 17:03:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697673784; x=1698278584; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=T/Zi+O5EOF8MxmYTpNQNZzDcOGwyvihj+XdDWSOvn6s=; b=iTY8joTjEef/ERLwWT1SitzkN02oqldiSAsUv38Bn7nIXqZFzrW+D9erwAxe29Dk7V O5rSFaI9v6q1Ndk/Dh3RgW0ZXHyiQxwvJH801k5CfMuHe5aHLwsQ9LlMz0GPxdsBGiRU AkK+jzaPEqxxikeP6WT23RtUwlDnlMTqCvLKI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697673784; x=1698278584; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=T/Zi+O5EOF8MxmYTpNQNZzDcOGwyvihj+XdDWSOvn6s=; b=vGX368tviB/N9P+SVivYUez1TROEsTKj21pWYVn5gRTK7d+luOugH7CzbyyIzooEp/ zjQd8jiNTn6ZuK/ypTH6Ik8CkdwGVrhGUhnSdb654yjAZJKXOfhI6izu5AKHaxXNCEkO +DpwlBdfY+QxmKOH0lhQpsNG+jEqafAk3yRitxNdOG+rjoUv2k5NvYjDAIte+TmPnrmh ySUzCYs+DjHCBr2juO0nyiGi8w8Av7lyascZgQHLKw8yGbJ/1mgRbk34jpxhQF6HSDgq m48k/vj67rQ0GwVBNlEoo7v2mDxVAI+qND3Ns93qXT3gmFdvV1ItOWE1lPRN9O3nrUB2 upuw== X-Gm-Message-State: AOJu0YzWeLo5y6wareMHn6x4kNsUF09abKdMrjFWOv7DT/B6wTjD1NG4 B7/sBnJsTqetaPCdo7fIh8VyqA== X-Received: by 2002:a05:6a20:12d5:b0:16b:c62d:876 with SMTP id v21-20020a056a2012d500b0016bc62d0876mr866390pzg.23.1697673784108; Wed, 18 Oct 2023 17:03:04 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id d92-20020a17090a6f6500b0027654d389casm467204pjk.54.2023.10.18.17.03.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 17:03:03 -0700 (PDT) Date: Wed, 18 Oct 2023 17:03:02 -0700 From: Kees Cook To: Justin Stitt Cc: Arend van Spriel , Franky Lin , Hante Meuleman , Kalle Valo , linux-wireless@vger.kernel.org, brcm80211-dev-list.pdl@broadcom.com, SHA-cyfmac-dev-list@infineon.com, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v3 2/2] wifi: brcmsmac: replace deprecated strncpy with memcpy Message-ID: <202310181654.E47A7709@keescook> References: <20231017-strncpy-drivers-net-wireless-broadcom-brcm80211-brcmfmac-cfg80211-c-v3-0-af780d74ae38@google.com> <20231017-strncpy-drivers-net-wireless-broadcom-brcm80211-brcmfmac-cfg80211-c-v3-2-af780d74ae38@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231017-strncpy-drivers-net-wireless-broadcom-brcm80211-brcmfmac-cfg80211-c-v3-2-af780d74ae38@google.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Wed, 18 Oct 2023 17:03:25 -0700 (PDT) On Tue, Oct 17, 2023 at 08:11:29PM +0000, Justin Stitt wrote: > Let's move away from using strncpy and instead use the more obvious > interface for this context. > > For wlc->pub->srom_ccode, we're just copying two bytes from ccode into > wlc->pub->srom_ccode with no expectation that srom_ccode be > NUL-terminated: > wlc->pub->srom_ccode is only used in regulatory_hint(): > 1193 | if (wl->pub->srom_ccode[0] && > 1194 | regulatory_hint(wl->wiphy, wl->pub->srom_ccode)) > 1195 | wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__); > > We can see that only index 0 and index 1 are accessed. > 3307 | int regulatory_hint(struct wiphy *wiphy, const char *alpha2) > 3308 | { > ... | ... > 3322 | request->alpha2[0] = alpha2[0]; > 3323 | request->alpha2[1] = alpha2[1]; > ... | ... > 3332 | } > > Since this is just a simple byte copy with correct lengths, let's use > memcpy(). There should be no functional change. > > In a similar boat, both wlc->country_default and > wlc->autocountry_default are just simple byte copies so let's use > memcpy. However, FWICT they aren't used anywhere. (they should be > used or removed -- not in scope of my patch, though). > > Signed-off-by: Justin Stitt > --- > drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c b/drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c > index 5a6d9c86552a..f6962e558d7c 100644 > --- a/drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c > +++ b/drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c > @@ -341,7 +341,7 @@ struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc) > /* store the country code for passing up as a regulatory hint */ > wlc_cm->world_regd = brcms_world_regd(ccode, ccode_len); > if (brcms_c_country_valid(ccode)) > - strncpy(wlc->pub->srom_ccode, ccode, ccode_len); > + memcpy(wlc->pub->srom_ccode, ccode, ccode_len); const char *ccode = sprom->alpha2; int ccode_len = sizeof(sprom->alpha2); struct ssb_sprom { ... char alpha2[2]; /* Country Code as two chars like EU or US */ This should be marked __nonstring, IMO. struct brcms_pub { ... char srom_ccode[BRCM_CNTRY_BUF_SZ]; /* Country Code in SROM */ #define BRCM_CNTRY_BUF_SZ 4 /* Country string is 3 bytes + NUL */ This, however, is shown as explicitly %NUL terminated. The old strncpy wasn't %NUL terminating wlc->pub->srom_ccode, though, so the memcpy is the same result, but is that actually _correct_ here? > > /* > * If no custom world domain is found in the SROM, use the > @@ -354,10 +354,10 @@ struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc) > } > > /* save default country for exiting 11d regulatory mode */ > - strncpy(wlc->country_default, ccode, ccode_len); > + memcpy(wlc->country_default, ccode, ccode_len); > > /* initialize autocountry_default to driver default */ > - strncpy(wlc->autocountry_default, ccode, ccode_len); > + memcpy(wlc->autocountry_default, ccode, ccode_len); struct brcms_c_info { ... char country_default[BRCM_CNTRY_BUF_SZ]; char autocountry_default[BRCM_CNTRY_BUF_SZ]; These are similar... So, this change results in the same behavior, but is it right? -Kees -- Kees Cook