Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp446501rdg; Tue, 10 Oct 2023 15:32:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+IzoKuwwMfOGMZ6n7xHmoau7KaLMlKmpN2GHB3nbn8aIe3+YysVss8JGCNGpUp2+xdD0M X-Received: by 2002:a05:6a00:1691:b0:693:3cbc:3d8e with SMTP id k17-20020a056a00169100b006933cbc3d8emr21745884pfc.0.1696977179559; Tue, 10 Oct 2023 15:32:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696977179; cv=none; d=google.com; s=arc-20160816; b=R97h04SwBUgmyQ83lt4cPvU83lBgWN52hB0lOnTo/plCTN/uvkNRyvki8TIapBzyYb zJn1oo7UXWMEMxQly9pACUO5Ov9ZszARstU0m75U06ohvL4QP5Q5TlLGIHEAT1tuSrZt 6fIdxooYMskNZMVfWFebFfBmgKjOd6zjyznemYU0tJs5WhT+pnKeRbsTVxLkjheiK424 tl6ubjIiSaf9S2s4kJe3GjBdDATGRYj/p+CHyhEhqtM9g2ODI+y0GSJEsq+V3a3T4lVq exaU5sfYr6gWYQT8lxlY59o7kujC/TOWWno07JnzxenLkWjWNl2cqtWoB5kpbCvygEkr 0zMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=QMExOtpQpyaeNVwjX7qYEE2Es2bm8MztU54xvZii8ok=; fh=+nR/KBYVMP75b/5EkORvVk3R3PpmbJKGYQ7gn75EMlo=; b=y8WLbJGwoiI42/V+e9nZrSFGokpIeQDVUrdfGrlM8UV9CXUTY/OCVShXNieEidCcvC VRm/gyWlcX6qB+zPf7os84ixFWej9VdIniXCniBmCptc/BpdRxrg/34/QnYuyEIX7yle YX2UpWPUbtr4Z2WrfnG0d1h2e8LF6QOoSieivcHU5BoWerfG9vK/8NabVWaDyGwJyVm0 tbfsmMoXvEXy5rJ76JirpvTr30n6ImEEEv7IYKk5rPu54x3UlYbOTsFRVydQqKfvK3QF cd6wAiRxy+3fep9m1iC9PbL88Ai7fpvlovlQdeL10dWNTRYAIXMAoamrPQ3/p0rchero xXYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aBu9xCCy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id f11-20020a056a0022cb00b00690de92ffe3si11198658pfj.309.2023.10.10.15.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 15:32:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aBu9xCCy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 218E080202CE; Tue, 10 Oct 2023 15:32:56 -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 S230347AbjJJWck (ORCPT + 99 others); Tue, 10 Oct 2023 18:32:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229577AbjJJWcj (ORCPT ); Tue, 10 Oct 2023 18:32:39 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A02C398 for ; Tue, 10 Oct 2023 15:32:37 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a397a7c1cso2598345276.2 for ; Tue, 10 Oct 2023 15:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696977157; x=1697581957; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=QMExOtpQpyaeNVwjX7qYEE2Es2bm8MztU54xvZii8ok=; b=aBu9xCCy0KGbLSmP0rx9JAA/1xA1BuzXUn1OjjKEfZ2TJWCWQN/3Wtc3dCnWVkapx+ gq1CMO7d+JhiJfNzfWglrzbWedKGYz4xj4lpG6QxePX2BYMo3H+53rAEIDqKbxcg4pio lD8FEoX7bwxXEe84cQzKSjOgvngAyKhUQU3f8k0QblEqh1gbrSuEQ32rC+MD4Fd4ckN8 USycg4Ly7eO7KTGfeUbawYsMxDQ5b+PVgR/R/6rBEKJ8S7s/q99iecyUwAMVAxDkhEh3 0GZRgaYdAZLoarGi7s/qC/8e6SkmyYjqOsMXpKuKWNTYepGB+uyq7m1fwmCzdLpxSATI 4jHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696977157; x=1697581957; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QMExOtpQpyaeNVwjX7qYEE2Es2bm8MztU54xvZii8ok=; b=P4lsyUJOLRK6MfiCGWKtQ/je54BIEAqR8eTGwk8U3nSQuwvtQDqtYjzuc1+Fx/r0vO yJkUDlqu9mSoTq8tR0W5M/9H/IAUZD5RUOeddWgqovvLKxlfUCG9kSV4O44+v43uCwd4 /+xKX7JOMQV6yFFX2yoabO5yGPfv14L5lQB9cZEluEwJ4hGk33yNlxo/eFYOhkQ/r+VR ibL1xgRe8j0OG5M1bphZD9+uSCNaMixo3JOchKAOT1l0b9Sh91oHY5UsW0ghABAB4/Sv XKK9KzfO4Lgy5jrMjeU+ms0BbsQLj3VTXhN4BMzDxaT0FGGF8a2YaBWBuGwWPCiAG/nl g+2A== X-Gm-Message-State: AOJu0Yz1P+vN3EuATPTEpNXkQJ1NZypESZYHw3R8Joef3w9FCBzh4tji Dcvk60FvperW/LtRzlGs6u/O783nPk5NocyHQQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:fc16:0:b0:d89:fc03:60ab with SMTP id v22-20020a25fc16000000b00d89fc0360abmr301988ybd.7.1696977156885; Tue, 10 Oct 2023 15:32:36 -0700 (PDT) Date: Tue, 10 Oct 2023 22:32:35 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAALRJWUC/53NQQ6CMBCF4auQrh3TFhPQlfcwLGo7wCTampmmg RDubuUILv+3eN+mBJlQ1K3ZFGMhoRRr2FOj/OzihEChtrLatkbrK0jm6D8rBKaCLBAxQxAHhTK KIBTxXbss4BMjeDB+DNp5G3rzVPX0wzjScoCPofZMkhOvh1/Mb/2bKgYMoL3YDnXfjS7cp5SmF 559eqth3/cv+vEqlO0AAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1696977155; l=3185; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=dy00Q/eHnNspNlp/8P9LCpOdW8TznhvIFS4XT0CmDbE=; b=GEGccPLv6B2Me/6Y16oiC/8KDaQ42fS2f4C7XzQ2ZVLmrSPSmM9qfBVvQPm1iAcLkoAUt2jEA Xi1fKm48WSZCCIjGLtjmQUqzy6dP2EKdn7i+Symuu7dCaKyKQZ6RR5j X-Mailer: b4 0.12.3 Message-ID: <20231010-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v2-1-ba4416a9ff23@google.com> Subject: [PATCH v2] net: dsa: vsc73xx: replace deprecated strncpy with ethtool_sprintf From: Justin Stitt To: Andrew Lunn , Florian Fainelli , Vladimir Oltean , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-4.8 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL 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 15:32:56 -0700 (PDT) `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. This change could result in misaligned strings when if(cnt) fails. To combat this, use ternary to place empty string in buffer and properly increment pointer to next string slot. 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 --- Changes in v2: - Prevent misaligned string assignments (thanks Vladimir) - Link to v1: https://lore.kernel.org/r/20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-v1-1-e2427e087fad@google.com --- Note: build-tested only. --- drivers/net/dsa/vitesse-vsc73xx-core.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/drivers/net/dsa/vitesse-vsc73xx-core.c b/drivers/net/dsa/vitesse-vsc73xx-core.c index 4f09e7438f3b..56dc3e7884fd 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"); /* Each port supports recording 3 RX counters and 3 TX counters, * figure out what counters we use in this set-up and return the @@ -961,23 +959,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 ? cnt->name : ""); } /* 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 ? cnt->name : ""); + } } --- base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 change-id: 20231009-strncpy-drivers-net-dsa-vitesse-vsc73xx-core-c-1cfd0ac2d81b Best regards, -- Justin Stitt