Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp4074971rdg; Wed, 18 Oct 2023 14:18:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFODt/5PV5uXaqugUF5xaCUb6b4tIw+15F9EnjkvbtjZ9fyoJBkbnIPEkTL+kYyLefuxvv7 X-Received: by 2002:a05:6a00:99e:b0:68a:6018:a66f with SMTP id u30-20020a056a00099e00b0068a6018a66fmr93688pfg.2.1697663936198; Wed, 18 Oct 2023 14:18:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697663936; cv=none; d=google.com; s=arc-20160816; b=S+MwEBX+dAV+hXhyQGA6u0hU7juQfFqM1iTy9Sshrjyn7eYvjl5ymmorcrtUG7j7en 0hWZeiwEbN9a89E7FtSgaE/t7UBtGPBruCx5RLgspnoCXayigFT9kvek6B1Za8ebwn9X X69jahJol0zAfrxhc9hOTS+JttH5He8fjfFF0IObFF0JwpX4IGtWVI2SJPCYzDHKPUfM /+4t8wSOy53HBP/o857HWUMKSLVkYr2WIaCLc4yZ289RFedOxHTxyLxSuL8L6XKLASgQ SYBEGe5wQz/R8Il8EANiRKt5LZm0sU1Yjmwgy3L6y3qS3kH0uEJoTyDCTLCVlqoQzNBY e6VA== 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=yN8Xmmf7uD1d77qxmyThi4rG4N+pXFvSzBoNB6ZmfFk=; fh=JWAO1fAe+Wf5O2DamUx86WOaLpB0s7CTfUExwjP/ICk=; b=tM/H7Yh0gD8QBPhDbJY1VRuiQrP+t/slWAFkaRvOfM7o65xbSuGjMRIDMXYdxy4qYf 6LoOCab3w7SSAKsLeoLXqbxpEgvCASUHc3WDx/9BW++aLWHe9D1PpxjI33UWLCmYmfRt qVJDbDM5ATSMCLNXD9Vo6Dl3g5Ke77d1r4oQxI6a6Wq4sodyyIj/2lAkGU92VoruDXg/ fIw2jMp95yhJjboVTM4G5C9yrxzteG5uGAqbDvkYY7HIyZhcK/8i8KOgzQlNCHqbGozA i7nPVCxQpwYgiqbL7OE/lgg9PxUrYK5ckS2w0iri1/w16VoQuoiVPxpYDGmLtZGG1au0 lHlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NpnU7Mfh; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id r29-20020aa7963d000000b006be3c302801si4552297pfg.397.2023.10.18.14.18.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 14:18:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NpnU7Mfh; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-wireless-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 fry.vger.email (Postfix) with ESMTP id 794298208F08; Wed, 18 Oct 2023 14:18:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232144AbjJRVSb (ORCPT + 52 others); Wed, 18 Oct 2023 17:18:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232041AbjJRVS3 (ORCPT ); Wed, 18 Oct 2023 17:18:29 -0400 Received: from mail-ot1-x349.google.com (mail-ot1-x349.google.com [IPv6:2607:f8b0:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21D10FE for ; Wed, 18 Oct 2023 14:18:27 -0700 (PDT) Received: by mail-ot1-x349.google.com with SMTP id 46e09a7af769-6c65a8aaa0dso9654687a34.2 for ; Wed, 18 Oct 2023 14:18:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697663906; x=1698268706; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yN8Xmmf7uD1d77qxmyThi4rG4N+pXFvSzBoNB6ZmfFk=; b=NpnU7MfhCS4XY98IISFshulZfv+fpsfPt3+qx1UQiseUqT/0mB3fvxaMw1g4bJlWvk SzDeF2w96iV2k7OI2gN8+R17ZGgxnZ7P+mU7rNz872yBAKntg/U4dV+csARd7tXyJNXq c2KmFiEO8gLl/AOxnbxVOW1yC20O/nLgJ6HFYzbjtwmVOubLORZ2SiTZ+Cnznda5nHpX IZkFVs4eKuCLlgpAjLiXWwcoR07rAU/zhgzLj5GomKsxSAohA8O/Kblg7IozDt9c0qyg rAT/81toXgxYCjBAd6JnoBrKTvHYHG82Fpwh7qLHM1vyg5KksyH5TmaXMBuDMZdlw1Ir UANQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697663906; x=1698268706; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yN8Xmmf7uD1d77qxmyThi4rG4N+pXFvSzBoNB6ZmfFk=; b=THXWN5g/7XFeeR9IZmxoBmwBJoiVuQoJZUTYKdOqiXAidHn6JpoUFb8wp9Hz5gJiI7 BAGgoepVERLmA+ttIPbH8yg3ugM82CthWovmCGfWXmVeImsEvGyPjX+jur7P9o6vsWH6 +WW36Og5+ZUdtTVjtIKJ8VF0rk4SD6tthn7Wzma4MwUVMUOHIKkfg1p3zdYsGlX5Apsw I1mjnBQ19xg0MixSHlx4k2qTAOvO9Iv6HowE1W0ZxhwWWltxe9OlW+KeDuzOma0Wv+lG YfF599EOzn39n8xoT/9lX28uG3VoX4UWvHyB4Xn5+fOuW6DAsMdGH/AWbdNBT2L0R3HS 3NNQ== X-Gm-Message-State: AOJu0YzXxu4mbjXzCdK0JoRD2y4/JyeQsJca9srSsszIRgOVkO9Z2AWU uEg9Ri8Pk8K9ZwNRbuhGf6+PgGt6vvcr7owc/Q== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a9d:4f08:0:b0:6c4:abb1:483f with SMTP id d8-20020a9d4f08000000b006c4abb1483fmr151782otl.2.1697663906391; Wed, 18 Oct 2023 14:18:26 -0700 (PDT) Date: Wed, 18 Oct 2023 21:18:24 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAJ9LMGUC/53NTQ6DIBCG4asY1p1GsP6kq96jcYE41UkUzEBQY 7x7qUfo8v0W33MIj0zoxTM7BGMkT86mULdMmFHbAYH61ELlqpC5rMEHtmbZoWeKyB4sBliJcUL vIRCsk2y2DWZNFgwUZaVzVatOdw+RPhfGD22X925Tj+SD4/3io/yt/0pRggTsy0IpNE1eNa/Bu WHCu3GzaM/z/AKAqSjd6wAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697663905; l=2897; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=lMVpXzAbthcxiCNfxcyNDG6TNfyPkqxWUDDeMmHC4Uw=; b=x86eQLBjWQhfaxdwh4ogz4PqhKCrv1sPzCQUF/5jtKH/6gtucs0svlqppMZ0fYjPZlWMlUBvW JKrY4BC107aBjmBPeGu+f1S+rMK+/2wLcsr58oG3dm4WiQ2tbTgNxRy X-Mailer: b4 0.12.3 Message-ID: <20231018-strncpy-drivers-net-wireless-ti-wl18xx-main-c-v2-1-ab828a491ce5@google.com> Subject: [PATCH v2] wifi: wl18xx: replace deprecated strncpy with strscpy From: Justin Stitt To: Kalle Valo Cc: linux-wireless@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,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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 18 Oct 2023 14:18:43 -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. wl->chip.phy_fw_ver_str is obviously intended to be NUL-terminated by the deliberate comment telling us as much. Furthermore, its only use is drivers/net/wireless/ti/wlcore/debugfs.c shows us it should be NUL-terminated since its used in scnprintf: 492 | DRIVER_STATE_PRINT_STR(chip.phy_fw_ver_str); which is defined as: | #define DRIVER_STATE_PRINT_STR(x) DRIVER_STATE_PRINT(x, "%s") ... | #define DRIVER_STATE_PRINT(x, fmt) \ | (res += scnprintf(buf + res, DRIVER_STATE_BUF_LEN - res,\ | #x " = " fmt "\n", wl->x)) We can also see that NUL-padding is not required. Considering the above, a suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on the destination buffer without unnecessarily NUL-padding. The very fact that a plain-english comment had to be made alongside a manual NUL-byte assignment for such a simple purpose shows why strncpy is faulty. It has non-obvious behavior that has to be clarified every time it is used (and if it isn't then the reader suffers). 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: - add wifi: to subject (thanks Jeff) - Link to v1: https://lore.kernel.org/r/20231017-strncpy-drivers-net-wireless-ti-wl18xx-main-c-v1-1-ed5322ec8068@google.com --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/net/wireless/ti/wl18xx/main.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/net/wireless/ti/wl18xx/main.c b/drivers/net/wireless/ti/wl18xx/main.c index 0b3cf8477c6c..1e4a430d1543 100644 --- a/drivers/net/wireless/ti/wl18xx/main.c +++ b/drivers/net/wireless/ti/wl18xx/main.c @@ -1516,12 +1516,9 @@ static int wl18xx_handle_static_data(struct wl1271 *wl, struct wl18xx_static_data_priv *static_data_priv = (struct wl18xx_static_data_priv *) static_data->priv; - strncpy(wl->chip.phy_fw_ver_str, static_data_priv->phy_version, + strscpy(wl->chip.phy_fw_ver_str, static_data_priv->phy_version, sizeof(wl->chip.phy_fw_ver_str)); - /* make sure the string is NULL-terminated */ - wl->chip.phy_fw_ver_str[sizeof(wl->chip.phy_fw_ver_str) - 1] = '\0'; - wl1271_info("PHY firmware version: %s", static_data_priv->phy_version); return 0; --- base-commit: 58720809f52779dc0f08e53e54b014209d13eebb change-id: 20231017-strncpy-drivers-net-wireless-ti-wl18xx-main-c-356a0272bab4 Best regards, -- Justin Stitt