Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1960470rdb; Thu, 7 Dec 2023 13:54:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IH/ywRhZlVOyI+R4J/Z/baczJ7reC4iEzgqWInD8ZmrBJZI1gMP4EO12deETTtxelt6U/Hq X-Received: by 2002:a05:6870:5ba3:b0:1fa:2ddb:6b73 with SMTP id em35-20020a0568705ba300b001fa2ddb6b73mr3250285oab.20.1701986090563; Thu, 07 Dec 2023 13:54:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701986090; cv=none; d=google.com; s=arc-20160816; b=qb0qy635luQhHnbOebeD63qOkKlTS0tQAxO00ObKO/HSrj4NUvIyz4QtpGBMnTACFp zgX1HvVOwuY/sLeC/U7AzV9Zrobjwd8dpgOlSAFy1XMeZ+l7cgsSLP41bePR0QKLGQVu 12oF6L8VYuKHbkTdNJT9K2yvxHhKWctHZ6xYE+zkO4eLvHOdxhzfg9VFbEN6SuFMhcuO 49P1GQIRC9Z+n2dgnqIXHmpU9/PUoBi1pChYuGsroSP6XySoMrbWJE7cF4idHlCwGYJq 9a0DOHt6bo8o/ZVeTQvE0PqDujjk4IFSHTxccP7p746rTAdPA6jKIa/uXfGkuU4ZRdHb QXOQ== 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 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id e4-20020a63e004000000b005b902b61cbcsi339455pgh.723.2023.12.07.13.54.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 13:54:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; 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 2620:137:e000::3:1 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 E8BD38313D00; Thu, 7 Dec 2023 13:54:47 -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 + 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 ); 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 ; Thu, 7 Dec 2023 13:54:37 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ddd64f83a4so12946617b3.0 for ; 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 To: Andrew Lunn , Heiner Kallweit , Russell King , "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=-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: 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:48 -0800 (PST) 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 --- 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(-) 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; --- base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 change-id: 20231012-strncpy-drivers-net-mdio-mdio-gpio-c-bddd9ed0c630 Best regards, -- Justin Stitt