Message ID | 20231207-strncpy-drivers-net-mdio-mdio-gpio-c-v2-1-c28d52dd3dfe@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5085616vqy; Thu, 7 Dec 2023 13:54:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFnY7fncRySgVxBWdAYrGO/8Wy26WA5dkkImpzUjwr0NMQ0BkQdEtckMonldAAzSdRxmajU X-Received: by 2002:aa7:9e8e:0:b0:6ce:351e:b0d0 with SMTP id p14-20020aa79e8e000000b006ce351eb0d0mr3361548pfq.67.1701986086543; Thu, 07 Dec 2023 13:54:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701986086; cv=none; d=google.com; s=arc-20160816; b=iQpmTcQ0G7V+KUF9PPst/0xbvvrvwNfpczmfTkMBVFh9rhw9YARLHI0vWFWJiFKwnq k3DTBcv/FfvpbF/DLjXy/A+eyyNL0lzwUir59DXIgV0qj6imUM/ghP5LqARKr+b0LwrH Cmo/xPAxvPcWrYDGn3Et8mqz1epI4HRcq++jlNcw9W4wlaftiApnYD6kQ61zR/ftjk23 FsoSsnO/Q5ayIu6T8IDcaloCC6kB0LTOYNcDNGfAuICKG+ERvUUrR1dyuSKAT+Qww/k2 xMOMjGr4zGEfzOBrU7C3sV4arUIJFNxwp8w6cU/yZNS7IkKJsrwugzCR3EyJb/XX/xNd Uq+w== 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=Noxi+OqGUk9/20RL0r0Ovx99qYSoefYFwzxukbeHOP4=; fh=jNFzfs0LVcNPJYpz3MO9rP5uq1TU3TrcBg7mkF9EWrg=; b=sMelrT8bbP53xQBfSYzQheXbHdN059Spf3Un0s8QoWopDXsS08VbtnSWamz7SxftNc utYR52B/IPmDYwI5TKmmEMaFWVvef4MvelQOZPbHzrUbWNyHRhWFV+9VuRu+OKdoAnyA ib95/YBQZfgoDfwXxVv9y63KTMfUtaT/RqV6Ds4RdYDbJ+B0CHlOmgcSoDms2uIzysVb FxNw1Sab8Pd2iIR+dJccztjfCMhF2lxFnsQ31qWrFUirMMRMxNgsfarWZ5EyKKtkxwOC ApaK9Pa7koJYTPUodHhtnwHgyGhxBAB6bU4+t70y1vhvMJQ+1SKXR8lxTCNa+7i3wd7y 9IhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qdaoTNUf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id u11-20020a62d44b000000b006ce2efa4422si316502pfl.405.2023.12.07.13.54.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 13:54:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qdaoTNUf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 49FC88313D00; Thu, 7 Dec 2023 13:54:42 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231664AbjLGVyd (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Thu, 7 Dec 2023 16:54:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229671AbjLGVyb (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 7 Dec 2023 16:54:31 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E68412D for <linux-kernel@vger.kernel.org>; Thu, 7 Dec 2023 13:54:37 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ddd64f83a4so12946617b3.0 for <linux-kernel@vger.kernel.org>; Thu, 07 Dec 2023 13:54:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701986076; x=1702590876; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Noxi+OqGUk9/20RL0r0Ovx99qYSoefYFwzxukbeHOP4=; b=qdaoTNUf8vq8NB7lgw9jI1u1kGNQui1LEmVg7IzJNYj5lXpuph2wxEL8aAe6GUiQAn meiq5L4RDWZ9QVZD/o94qChXDw3fS7tkzz9hZK+IeCU0+KCh1qs9L+R//ZXnPAMZkeSA 9cTY655w/8KEoPsrJd75F05J3l8kv2nEEpfbpyGLHbjQNHAyyRxigG5icl9HkBbNK7YE UiO18s5YEHFh+gZDhxAF4WmUm/V/e0CII1iEN1WLwysKtdaYgsjEXIlm2VHvFi1C7WDA Jai4sh7v5ZC/jYw+nHhe3IF9agW5MNyJonqb9BbglX2ky+Mm+AoCnMJWCO5jwhwClJPh 6GMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701986076; x=1702590876; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Noxi+OqGUk9/20RL0r0Ovx99qYSoefYFwzxukbeHOP4=; b=TxWqDNT9+z04JgobrPZdAO6aShPTmJi+qOEXH5SRX6diM2Pzcp/98s+Mx8fSVPy8gO MZhERX5f+9eAm9c37wimXTduwtyno/YuQxU/VupqmrPjvsuNJrMkFwG02lp8HbQ07TCj s6YxH/JaGMoHxq49mb2SLxwS8vNFbj6dJ353ybIzLd22e8CiTRVw7IJeuaWcBTLDYVtn n74Wz5DHJKeQtgb6IfLU6rYBPPgfvAeHp+v0uzeSm4O5/855EfpdKoHH1oyoz4Qwpbxg p3XFyvQ0TB1IX2HX4VskXWuDfSxOmkQ7CHDUrTc+j/7YDnBzITPKD3R7jol96YFCsOJb YBug== X-Gm-Message-State: AOJu0YxBuzwL7mNOY8kN4w6dPr1TFYJP3Dlyo91FIF6kiHN3RENVTpPQ Czut/OhptDdIAzPbVy4cw4hdGP5clfFZy6WFLw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:dac7:0:b0:da0:3117:f35 with SMTP id n190-20020a25dac7000000b00da031170f35mr50538ybf.3.1701986076410; Thu, 07 Dec 2023 13:54:36 -0800 (PST) Date: Thu, 07 Dec 2023 21:54:31 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIABY/cmUC/5WNQQqDMBBFr1Ky7pQkgmBXvUdxYWbGOFATSSRUx Ls3tSfo5sH7i/92lTkJZ3W/7CpxkSwxVLHXi8JpCJ5BqLqy2jZGGwt5TQGXDShJ4ZQh8AozSfz BLxUIjog6Jo1to1W9WhKP8j4zz776JHmNaTurxXzXPwPFgIHBdU63OCIN7uFj9C++YZxVfxzHB 6DmPazZAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1701986075; l=2506; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=zIrhzyHJNpJ40QKMRusYTExmdzhkSwgmZkrSxmSyxvs=; b=RRGltwx9Y0ze6MjA6QcpAPfN/r19x0syHfol/YE9t1zYphYLUnvX5TOrPEtZsMYZsJsdKTs+v gSjruECAkTlAYO1P+Ow+E5USv9FTXuiPVpyLCFA7mN9EigO70KzLKKJ X-Mailer: b4 0.12.3 Message-ID: <20231207-strncpy-drivers-net-mdio-mdio-gpio-c-v2-1-c28d52dd3dfe@google.com> Subject: [PATCH v2] net: mdio-gpio: replace deprecated strncpy with strscpy From: Justin Stitt <justinstitt@google.com> To: Andrew Lunn <andrew@lunn.ch>, Heiner Kallweit <hkallweit1@gmail.com>, Russell King <linux@armlinux.org.uk>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com> Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt <justinstitt@google.com> Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 07 Dec 2023 13:54:42 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784661762744334081 X-GMAIL-MSGID: 1784661762744334081 |
Series |
[v2] net: mdio-gpio: replace deprecated strncpy with strscpy
|
|
Commit Message
Justin Stitt
Dec. 7, 2023, 9:54 p.m. UTC
strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.
We expect new_bus->id to be NUL-terminated but not NUL-padded based on
its prior assignment through snprintf:
| snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
We can also use sizeof() instead of a length macro as this more closely
ties the maximum buffer size to the destination buffer.
Due to this, a suitable replacement is `strscpy` [2] due to the fact
that it guarantees NUL-termination on the destination buffer without
unnecessarily NUL-padding.
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 <justinstitt@google.com>
---
Changes in v2:
- change subject line as it was causing problems in patchwork with
"superseded" label being improperly applied.
- update commit msg with rationale around sizeof() (thanks Kees)
- Link to v1 (lore): https://lore.kernel.org/r/20231012-strncpy-drivers-net-mdio-mdio-gpio-c-v1-1-ab9b06cfcdab@google.com
- Link to v1 (patchwork): https://patchwork.kernel.org/project/netdevbpf/patch/20231012-strncpy-drivers-net-mdio-mdio-gpio-c-v1-1-ab9b06cfcdab@google.com/
- Link to other patch with same subject message: https://patchwork.kernel.org/project/netdevbpf/patch/20231012-strncpy-drivers-net-phy-mdio_bus-c-v1-1-15242e6f9ec4@google.com/
---
Note: build-tested only.
Found with: $ rg "strncpy\("
---
drivers/net/mdio/mdio-gpio.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
---
base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2
change-id: 20231012-strncpy-drivers-net-mdio-mdio-gpio-c-bddd9ed0c630
Best regards,
--
Justin Stitt <justinstitt@google.com>
Comments
On Thu, Dec 07, 2023 at 09:54:31PM +0000, Justin Stitt wrote: > We expect new_bus->id to be NUL-terminated but not NUL-padded based on > its prior assignment through snprintf: > | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); > > We can also use sizeof() instead of a length macro as this more closely > ties the maximum buffer size to the destination buffer. Honestly, this looks machine generated and unreviewed by the submitter, because... > if (bus_id != -1) > snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); > else > - strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); > + strscpy(new_bus->id, "gpio", sizeof(new_bus->id)); If there is an argument for not using MII_BUS_ID_SIZE in one place, then the very same argument applies to snprintf(). If one place changes the other also needs to be changed.
On Thu, Dec 7, 2023 at 2:57 PM Russell King (Oracle) <linux@armlinux.org.uk> wrote: > > On Thu, Dec 07, 2023 at 09:54:31PM +0000, Justin Stitt wrote: > > We expect new_bus->id to be NUL-terminated but not NUL-padded based on > > its prior assignment through snprintf: > > | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); > > > > We can also use sizeof() instead of a length macro as this more closely > > ties the maximum buffer size to the destination buffer. > > Honestly, this looks machine generated and unreviewed by the submitter, > because... > Not machine generated. Was just trying to keep my change as small as possible towards the goal of replacing strncpy. However, you're right. It's literally the line right above it and now it looks inconsistent . > > if (bus_id != -1) > > snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); > > else > > - strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); > > + strscpy(new_bus->id, "gpio", sizeof(new_bus->id)); > > If there is an argument for not using MII_BUS_ID_SIZE in one place, > then the very same argument applies to snprintf(). If one place > changes the other also needs to be changed. > Gotcha, I've sent a [v3]. > -- > RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ > FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last! [v3]: https://lore.kernel.org/all/20231211-strncpy-drivers-net-mdio-mdio-gpio-c-v3-1-76dea53a1a52@google.com/ Thanks Justin
diff --git a/drivers/net/mdio/mdio-gpio.c b/drivers/net/mdio/mdio-gpio.c index 0fb3c2de0845..a1718d646504 100644 --- a/drivers/net/mdio/mdio-gpio.c +++ b/drivers/net/mdio/mdio-gpio.c @@ -125,7 +125,7 @@ static struct mii_bus *mdio_gpio_bus_init(struct device *dev, if (bus_id != -1) snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); else - strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); + strscpy(new_bus->id, "gpio", sizeof(new_bus->id)); if (pdata) { new_bus->phy_mask = pdata->phy_mask;