From patchwork Mon Oct 16 20:22:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 735019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDFD7CDB465 for ; Mon, 16 Oct 2023 20:22:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233280AbjJPUWt (ORCPT ); Mon, 16 Oct 2023 16:22:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbjJPUWs (ORCPT ); Mon, 16 Oct 2023 16:22:48 -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 B675BE8 for ; Mon, 16 Oct 2023 13:22:46 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d8486b5e780so6131436276.0 for ; Mon, 16 Oct 2023 13:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697487766; x=1698092566; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=XsgTw1CJMHDMphse0HPVrCXoGi6YQMkmWaeP25eKZWc=; b=o8+sNLy08emDvAq4aklEG6lmCDYopFWxnU4+TK1JRj10qWhxodXmgheUSa6MsScsjf +ImxPMh3noRohUmbScTUrnNOzHBQdlAdHi0wdAwU2PiLgBDqV/k/RbZsfdoI9iVJBHDb 8d9lfoWXsNOtFLqSEo0NHu9JgJkWgQ8c8X0Q9NKQ3tLDHPpmC/G7Agnv4GlhkNqPAICy CLzmDQP4tp8sTJ73GgGBevE9i1TlE3QcJ5bnl0AIjZXyB3koG/MtYrWERl22y1TuSXUl fas+Pt/LjtjtpqOxLE98ETH7AtsrUt5Doi3XvhzTHAgM8nyEHq0467GWqQ7ynRhw4YiO 5/Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697487766; x=1698092566; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XsgTw1CJMHDMphse0HPVrCXoGi6YQMkmWaeP25eKZWc=; b=uS2c5tN/mr+z6BP+EbyGqKsSax7nDYP5Dou2GvwT9EFyShDjWfQsqwc+8fxppHutQg EqtwCrRj1Tl3UC09rUoSn8axE+c/ELiIDb4EU9YvQQcqEFPHMGjwHmRxApa+r80XmYNb NgnaWvUhgH6kVW+FxMWdla5FG2XzpfbtQajgfRpy+I0q8NSrtNMHIkn09KR2pHeefztc nnllCTbJOXLNxc/BJjxnn35FGdZkfYB1ADl4jTCf6ImUR+8yA8OGCtURu2IJ1fL5cGRz 45Gw57AlKaAcrUBIbNXRG+p9vq67ws8KI8lgo95KsrPYrS3yduZa/KYANiArLwVYwTjA CVPw== X-Gm-Message-State: AOJu0YxwGIzLQL6dSGX8enIC1qjIpInhXCdBRePPY7fjA2NDWjq7rPxJ Bm0tgGiRuezsGC9M2tpS2Mt1CEPVxav9lL+YSg== X-Google-Smtp-Source: AGHT+IHQ5wdWI2ysVEyvwpliX52H0WBhxPQy/yEdkoI8j0clA4li6U5CriNyDxKoH6VQ/TdPRKg1iyGE3HtskTtPgA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:8a8b:0:b0:d9a:cc6b:cf1a with SMTP id h11-20020a258a8b000000b00d9acc6bcf1amr2518ybl.8.1697487765639; Mon, 16 Oct 2023 13:22:45 -0700 (PDT) Date: Mon, 16 Oct 2023 20:22:45 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAJSbLWUC/y2NwQrCQAwFf6XkbKBbqUV/RTws6VsN1LUkS1VK/ 72LeBmYy8xKDlM4XZqVDIu6vnKVcGhIHjHfwTpWp67tjqENJ/ZiWeYvj6YLzDmj8FsNE9w5lie mP4UHiec+SBokRarB2ZD085tdb9u2A/j+Z9J8AAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697487764; l=2957; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=1+nNVSjnJRHuqWkXwql7HcWOFbNymzy4VcgSRCyCDGA=; b=55/2gEWyZRObYYt9U34T3z95iBbqHlCwUtlamFUm66VwEpc8DS5kSyJPPs7za29bOe/oMzu+s SQnU4HCklRvCzbBqjqBR0DvcqPrXnOhw6GF3Vg/DHoA169r0wl1nUkI X-Mailer: b4 0.12.3 Message-ID: <20231016-strncpy-drivers-net-wireless-atmel-atmel-c-v1-1-038d0e73c402@google.com> Subject: [PATCH] atmel: replace deprecated strncpy/strcpy 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 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. strcpy() is also deprecated [2] and shouldn't be used. 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 --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/net/wireless/atmel/atmel.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 change-id: 20231016-strncpy-drivers-net-wireless-atmel-atmel-c-7ca951cf7cfa Best regards, -- Justin Stitt diff --git a/drivers/net/wireless/atmel/atmel.c b/drivers/net/wireless/atmel/atmel.c index 7c2d1c588156..199b9144ef63 100644 --- a/drivers/net/wireless/atmel/atmel.c +++ b/drivers/net/wireless/atmel/atmel.c @@ -2011,7 +2011,7 @@ static int atmel_get_name(struct net_device *dev, union iwreq_data *wrqu, char *extra) { - strcpy(wrqu->name, "IEEE 802.11-DS"); + strscpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name)); return 0; } @@ -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: @@ -3889,7 +3888,8 @@ static int reset_atmel_card(struct net_device *dev) printk(KERN_INFO "%s: if not, use the firmware= module parameter.\n", dev->name); - strcpy(priv->firmware_id, "atmel_at76c502.bin"); + strscpy(priv->firmware_id, "atmel_at76c502.bin", + sizeof(priv->firmware_id)); } err = request_firmware(&fw_entry, priv->firmware_id, priv->sys_dev); if (err != 0) {