Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp511244rdb; Thu, 19 Oct 2023 10:29:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExHRJxdbFpxkkBCpYUGjz+bmsq5yFeXTjkH1EuerNFZo/dWRkidhutS6dHDq7wNWLqaQDY X-Received: by 2002:a17:902:e28b:b0:1c5:f7e2:c5e0 with SMTP id o11-20020a170902e28b00b001c5f7e2c5e0mr2463181plc.51.1697736554976; Thu, 19 Oct 2023 10:29:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697736554; cv=none; d=google.com; s=arc-20160816; b=L1PidBhnIqwr80ekleXLXegGV2wTbJGXRgpt15Bf9GZVBBN1OTeOFFmrTasqBnq1Xb 073PimaWYTyOkYxuhAQP/zw6x7D9dcNCOPwz+Z+ch+GER5sttEK6eEyVNRrssbRR1sC5 lTE9xeeWuWxTBeSOxkJwmrnDZeuZFMDNjBKP1HOcPP95n7TRMEGN2BMBRdJ0TJVeWDQ+ H5RLzOMW88WPKU6wx9TFV4o3bTV9g6N21g1iOtaYRbKqvHLtkZjchWApIr1+FVPV64SO wmaXt7zLqou7fvCIaAyJ7k7UHJRCUGPyM5hasPQ8SGMraVH1Xbels+ngHMcT91+7+Dh+ Qz+g== 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=2dM38oFA9Pe/oU12qt1pqIA50gOKDfSgpU7CFprv1yc=; fh=JWAO1fAe+Wf5O2DamUx86WOaLpB0s7CTfUExwjP/ICk=; b=NBbnwKm3taiSIWgXhmZ1ChVcFCH62Ppgqe9NfY+gMKucu8HbV5MoI7u8ySu/O+6DWi Tfw1F5NeuSepCaRZeQ3rDIkYFzY3dAUh2QfeB/5JreY3Q5rm5P8M0ixCKegsiBeu5VE7 16FpOw1M2u3OG5pu2dbU3NQzyl3nRbVfy42Nz/UMLVNJ3dLdTeQdmNscpPJ/d56wJhUo PO+1SxAOtA+1hNNxnOUPWpqGpmb579MDj4bhYuWdddDIzUuTwIWGSB3fVqpWonyrjYyR 8nquX+4P6Jwh0KpxG6BrtV47zqAfboB+ksjEN4PUroeOidN7sTZLTGlWRXsdcyxR1SgT ru0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cHWdbIg0; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id j1-20020a170902690100b001c6223e5675si2451017plk.188.2023.10.19.10.29.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 10:29:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-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=cHWdbIg0; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id EF02681CE84A; Thu, 19 Oct 2023 10:29: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 S235490AbjJSR3J (ORCPT + 52 others); Thu, 19 Oct 2023 13:29:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233272AbjJSR3I (ORCPT ); Thu, 19 Oct 2023 13:29:08 -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 EF293112 for ; Thu, 19 Oct 2023 10:29:06 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d81e9981ff4so11150538276.3 for ; Thu, 19 Oct 2023 10:29:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697736546; x=1698341346; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=2dM38oFA9Pe/oU12qt1pqIA50gOKDfSgpU7CFprv1yc=; b=cHWdbIg0jyNGVyVEhDpxNzd/63un5tiuklbwUfrz2KzIOUWXh2jO82q85+9CcBe5U0 sgydZEpiY/wZRkzrXt4xvcXdWMpEKw8u0elwoFUMYZWL0hi8wVFUUMj8gKm7liIsO7mF D1S4j+F6r3+5yOyyUovTPr9LIfRgrRt7Wt4m2xz5rHIBC7u8BLS0cJuzkED03NtbrKnH O+1rJhBdt0YFmnmJxFGBfwCjTuiIO5rz0cXvsmiJFfD3M1STA0DTgurSQ4VE7RmCGppe HANmIARbxkaNF6UpdavJkoWMS/4HnnQxpk5u24CC57NgyXhd2QylQOn6t3C1Y9mEclAD 1gqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697736546; x=1698341346; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=2dM38oFA9Pe/oU12qt1pqIA50gOKDfSgpU7CFprv1yc=; b=ZAsNzS3Ei4SOWKzbKH6MF2xsa3C0BMYvH8TKCykq82Byf+oQChwz1mafVSP3CwHEF3 FDjfLV3jlPwEXatHTnKIRPsyUaGzbfDgz8EZtWdJZdDs5Z1I3ZSKiJfxfIdttOAktdpL c6BVQdw3/+8ZpydcJvMFQxcIpv0PgIzYbFcwdhJNjSpZ3hsb94bl8/+UlX6ZVdmB+K/3 XcEDRBBzlUMLtIP2pLKwoaRQMoZIbYI50u5WmpdEZWZty2tFoquBLcb+BZ5w4W3aDnwW dobzh53hLbodrQN3C17hXG4F29QOpJd+Pk5KHQDi8sK4THeZJfUFOOydRkmFG7xPtSji huog== X-Gm-Message-State: AOJu0YxR6VnyRG/mfLSuuvf3bjUAkAetu1VYM66WRE9uMp/btrFzeAO2 i1QLTQGOpRkgrHqlogiDDtAcUaRf51n5sre0aQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a5b:584:0:b0:c78:c530:6345 with SMTP id l4-20020a5b0584000000b00c78c5306345mr62074ybp.7.1697736546059; Thu, 19 Oct 2023 10:29:06 -0700 (PDT) Date: Thu, 19 Oct 2023 17:29:01 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAFxnMWUC/52NQQ6CMBBFr0K6dkxbVNSV9zAsmmGAJtCSGVIlh LtbiSdw85P3F++tSog9iboXq2JKXnwMGeyhUNi70BH4JrOy2pZGmwvIzAGnBRr2iVgg0AwvzzS QCLh5pOG3CBW629lgW2HrVBZOTK1/77Fnnbn3Mkde9nYy3/evTDJgQJfXRlNV4knbRxdjN9AR4 6jqbds+HuzT1uUAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697736545; l=2387; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=XowZOHRhqMCWq2fZ6yqvbWeRdNj7KFUM/jYuUGt1iRs=; b=I3H8ns0MlDbP5d8HN0mCYlz9v6sQJLyJQnj1UZVv0uRXoG+50xHyRKxV+C0qMYGOmECPx9Ey5 8hoyNmvhpxYC4k6QRDMvh7xD/gPJGHhPeCc3sOaKgXZJlWxe7qv5Yt4 X-Mailer: b4 0.12.3 Message-ID: <20231019-strncpy-drivers-net-wireless-atmel-atmel-c-v2-1-52e46fbea083@google.com> Subject: [PATCH v2] atmel: 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 pete.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 (pete.vger.email [0.0.0.0]); Thu, 19 Oct 2023 10:29:10 -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. We expect priv->firmware_id to be NUL-terminated based on its usage with seq_printf() and strlen() in atmel.c: 1420 | seq_printf(m, "%s loaded by host\n", priv->firmware_id); ... 3884 | if (strlen(priv->firmware_id) == 0) { NUL-padding is not required, which is evident by the usage of a plain strcpy(): 3891 | strcpy(priv->firmware_id, "atmel_at76c502.bin"); 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. Let's also replace hard-coded lengths to be `sizeof(...)` for buffers that the compiler can detect the size for as this is less error prone. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [2] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [3] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- Changes in v2: - leave strcpy() usage as is (thanks Kees) - Link to v1: https://lore.kernel.org/r/20231016-strncpy-drivers-net-wireless-atmel-atmel-c-v1-1-038d0e73c402@google.com --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/net/wireless/atmel/atmel.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/net/wireless/atmel/atmel.c b/drivers/net/wireless/atmel/atmel.c index 7c2d1c588156..99920430e8b4 100644 --- a/drivers/net/wireless/atmel/atmel.c +++ b/drivers/net/wireless/atmel/atmel.c @@ -2651,8 +2651,7 @@ static int atmel_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) priv->firmware = new_firmware; priv->firmware_length = com.len; - strncpy(priv->firmware_id, com.id, 31); - priv->firmware_id[31] = '\0'; + strscpy(priv->firmware_id, com.id, sizeof(priv->firmware_id)); break; case ATMELRD: --- base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 change-id: 20231016-strncpy-drivers-net-wireless-atmel-atmel-c-7ca951cf7cfa Best regards, -- Justin Stitt