Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp58412rdg; Tue, 10 Oct 2023 04:21:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqTPy4YHIijQE4RbYGELbbLT+WqrwAwwj+uusVQljtmaj2beMaNsxhlb76YA/jLmy2wH+B X-Received: by 2002:a17:903:2689:b0:1c9:b1f7:ec6c with SMTP id jf9-20020a170903268900b001c9b1f7ec6cmr1198131plb.56.1696936895408; Tue, 10 Oct 2023 04:21:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696936895; cv=none; d=google.com; s=arc-20160816; b=rtq+lGFTLYQbtQd420aaL3NteI8m3wjARaOZKmv2hysb7yOPt5Wc8TEUIKGgk5yJCR Sy8AuB7oZl09wokt5+uXrT4se6lVdAKyt3K6hf6lE6acwHnfmX2nn/IT5kdEYyOzpS+v s1Y96q9tSzKZhMrNfyG3QbGpACyULSTqN7cmejYNBESA/ODw97zRkRMv6tOW6z9yVUz9 FhA58ZpfPi5klvIBDH0v097i3vagLkFwRXPsSxwQMJNIWf3NBq8grsSykM2+Mk8ntqEd vCQdFuPNVOY8N4tDxLrfHg1NFAWpnLdyP7+ZXEmItD+LZybKCqOMi6Vv0xQFRVwJxqrP UZEA== 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=sQsOgeceEIsXhp0lkWDt2Tm0RKONRcHVaKqhgdrpmWA=; fh=CRsDVyBDiJsCkJDPTKSlD3QTcHUA403BY5Rbgr8qUcU=; b=H2kdZCq9Jz+i1PHbeBGaIr0QrIbkn0s2B0JIOJxi802/LXFTpd/kq+DQqa3mtxXaQU QbHzJG8N2Pm1iqkGl9044BizbHTMLR+fetUxUFXq+5wTRxc7Fwbo42rTRIpM3vFMLtvN sEtj0SLt2EiTQ6VS9VAlL2YIKOsGWeINMQu7sF4nuvr9Cdv93/pFDcNORYB5BumFKVq0 VqCVgFvoehFmB9AQU/WZ7mVejQLl7fxCv67emMJmPLk9Efsyj+upfj3C883NNK0doAvN eTNdxHDQNAkw/86zz/2QjLXd7cMVq4Ok+LpiRoV1TJs7yoyL79oQAGFdsU0toV0+iMT1 TqFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SrwP2Dr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id u3-20020a170902e80300b001c1e1fe16cbsi12536429plg.255.2023.10.10.04.21.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 04:21:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SrwP2Dr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id C2976807C6F3; Tue, 10 Oct 2023 04:21:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231283AbjJJLU7 (ORCPT + 99 others); Tue, 10 Oct 2023 07:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230122AbjJJLU5 (ORCPT ); Tue, 10 Oct 2023 07:20:57 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 079A0A4; Tue, 10 Oct 2023 04:20:56 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-53b32dca0bfso8487959a12.0; Tue, 10 Oct 2023 04:20:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696936854; x=1697541654; 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=sQsOgeceEIsXhp0lkWDt2Tm0RKONRcHVaKqhgdrpmWA=; b=SrwP2Dr+nbYMbywpyMpPVXsAA1CiPs/F+/1HdiuOTSpyxrcnq1dI72tWpnTJ61QFrO BSOEG6k8hwpaE59h5fV7OGpU9BH5VUv+T+68zw8k0gySQJVDYA5Ed7IL1GCC+JIc4Q6P ZUwjAyxLA5kHO47CHNcO/a/6Y/KQg+RfH87AovJcIHxnRwp8zKGUz/sl7bPkRE00T+Py +EeOTRVFcYZtEnzcx/37cv+61E3PVWcJ4MYN8u0RWH7bxzHZtw6Ix/M9WpBTvzvkHho7 tMfCRtlUDmvwTwN38/qb670gS7Tf3rYd37GuUM1Omw1+CU3+XkoppZSTCE9IOocYbreU ta1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696936854; x=1697541654; 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=sQsOgeceEIsXhp0lkWDt2Tm0RKONRcHVaKqhgdrpmWA=; b=CGgmqooMh0q8XLTN+vsjQTY8gH3tT81HZcoYp2zps6qIZDqmQPwmXDhv5LX9vxHCPh a0td6doGrFJ401eppygBvzmVIx+VPut3peC/R+cfxli/Db8HdbxKGDDXay1hWEoDXSqp 6IaMLrJhRkcGI+BCK/OVzogjUtr/r01xgKGQ5/t+g8RV+RQQpvmWArKAcmf57XgRsKxX dwaMp7W9zhPv2kTnNJ+/xMYPxxW7hLz2lYHDB9T++D0FTACDidU8jgwZw3NrtdLu/rno AZbcXS1Sc9646IHSVh1xUPvwD+J8hk1jTaCJhIjQwpc+G9aDy9dcS3bqZ6moLA7Xfoja +IEg== X-Gm-Message-State: AOJu0YzHgtmh4iYOEG2rKNgTl8QVyCgGcY17nToS1FPGDyZ3cAZjrWLt HAvht7bAhkrS6c+1zNor8Ls= X-Received: by 2002:a05:6402:5190:b0:534:6b86:eda2 with SMTP id q16-20020a056402519000b005346b86eda2mr10776212edd.21.1696936854308; Tue, 10 Oct 2023 04:20:54 -0700 (PDT) Received: from skbuf ([188.26.57.160]) by smtp.gmail.com with ESMTPSA id r14-20020a05640251ce00b0053da777f7d1sm281611edd.10.2023.10.10.04.20.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 04:20:54 -0700 (PDT) Date: Tue, 10 Oct 2023 14:20:51 +0300 From: Vladimir Oltean To: Justin Stitt Cc: Andrew Lunn , Florian Fainelli , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Linus Walleij Subject: Re: [PATCH] net: dsa: vsc73xx: replace deprecated strncpy with ethtool_sprintf Message-ID: <20231010112051.zgefbx2c3tjneudz@skbuf> References: <20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v1-1-e2427e087fad@google.com> <20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v1-1-e2427e087fad@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v1-1-e2427e087fad@google.com> <20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v1-1-e2427e087fad@google.com> X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Tue, 10 Oct 2023 04:21:10 -0700 (PDT) X-Spam-Level: ** On Mon, Oct 09, 2023 at 10:54:37PM +0000, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > ethtool_sprintf() is designed specifically for get_strings() usage. > Let's replace strncpy in favor of this more robust and easier to > understand interface. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > Note: build-tested only. > --- > drivers/net/dsa/vitesse-vsc73xx-core.c | 20 ++++++-------------- > 1 file changed, 6 insertions(+), 14 deletions(-) > > diff --git a/drivers/net/dsa/vitesse-vsc73xx-core.c b/drivers/net/dsa/vitesse-vsc73xx-core.c > index 4f09e7438f3b..09955fdea2ff 100644 > --- a/drivers/net/dsa/vitesse-vsc73xx-core.c > +++ b/drivers/net/dsa/vitesse-vsc73xx-core.c > @@ -928,7 +928,8 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, > const struct vsc73xx_counter *cnt; > struct vsc73xx *vsc = ds->priv; > u8 indices[6]; > - int i, j; > + u8 *buf = data; > + int i; > u32 val; > int ret; > > @@ -948,10 +949,7 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, > indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */ > > /* The first counters is the RX octets */ > - j = 0; > - strncpy(data + j * ETH_GSTRING_LEN, > - "RxEtherStatsOctets", ETH_GSTRING_LEN); > - j++; > + ethtool_sprintf(&buf, "RxEtherStatsOctets"); Here you don't use "%s", but everywhere else you do. Can't you just pass the counter name everywhere, without "%s"? > > /* Each port supports recording 3 RX counters and 3 TX counters, > * figure out what counters we use in this set-up and return the > @@ -962,22 +960,16 @@ static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, > for (i = 0; i < 3; i++) { > cnt = vsc73xx_find_counter(vsc, indices[i], false); > if (cnt) > - strncpy(data + j * ETH_GSTRING_LEN, > - cnt->name, ETH_GSTRING_LEN); > - j++; > + ethtool_sprintf(&buf, "%s", cnt->name); The code conversion is not functionally identical, and I think it's a bit hard to make it identical. The VSC7395 has 45 port counters, but it seems that it can only monitor and display 8 of them at a time - 2 fixed and 6 configurable through some windows. vsc73xx_get_strings() detects which counter is each window configured for, based on the value of the CNT_CTRL_CFG hardware register (VSC73XX_C_CFG in the code). It displays a different string depending on the hardware value. The code must deal with the case where vsc73xx_find_counter() returns NULL, aka the hardware window is configured for a value that vsc73xx_tx_counters[] and vsc73xx_rx_counters[] don't know about. Currently, the way that this is treated is by skipping the strncpy() (and thus leaving an empty string), and incrementing j to get to the next ethtool counter, and next window. The order of the strings in vsc73xx_get_strings() needs to be strongly correlated to the order of the counters from vsc73xx_get_ethtool_stats(). So, the driver would still print counter values for the unknown windows, it will just not provide a string for them. In your proposal, the increment of j basically goes into the "if (cnt)" block because it's embedded within ethtool_sprintf(), which means that if a hardware counter is unknown, the total number of reported strings will be less than 8. Which is very problematic, because vsc73xx_get_sset_count() says that 8 strings are reported. Also, all the counter strings after the unknown one will be shifted to the left. I suggest that "if (!cnt)", you should call ethtool_sprintf() with an empty string, to preserve the original behavior. > } > > /* TX stats begins with the number of TX octets */ > - strncpy(data + j * ETH_GSTRING_LEN, > - "TxEtherStatsOctets", ETH_GSTRING_LEN); > - j++; > + ethtool_sprintf(&buf, "TxEtherStatsOctets"); > > for (i = 3; i < 6; i++) { > cnt = vsc73xx_find_counter(vsc, indices[i], true); > if (cnt) > - strncpy(data + j * ETH_GSTRING_LEN, > - cnt->name, ETH_GSTRING_LEN); > - j++; > + ethtool_sprintf(&buf, "%s", cnt->name); > } > } > > > --- > base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 > change-id: 20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-1cfd0ac2d81b > > Best regards, > -- > Justin Stitt >