Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3463128rdg; Tue, 17 Oct 2023 16:13:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwYwBXd0dFLzCW5uAlbJFON+9QJDxk+3vkIMyMTlkmDxFNfYnfDr6U4ONlKcOeK+DYCYzd X-Received: by 2002:a17:90b:118:b0:27d:3c14:afec with SMTP id p24-20020a17090b011800b0027d3c14afecmr3256732pjz.48.1697584379775; Tue, 17 Oct 2023 16:12:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697584379; cv=none; d=google.com; s=arc-20160816; b=oUwM+cT3BEipwmciXvhiWqFZ8YyaTYtlFDLIW9e5EU7bEWK5yRPbiL9kbDXQq66ey9 YZRzhQq+XNhE4EXhvt04tD38HtQwD2anBV1AYJzTp4T7CUse9n8cGfr76b3TQDwWPjqP Wvf3GGaonf8MNsG5Wa18UvA2dexPykgNDiFD055itTLuHkPPVSauFiRVHI9FrBVGsQ18 lYSoBSd4P8rDSCs3zXwvcKRKdWr0bEYinrnZ2dxlU4q0Wm63WkLnm1CH9Kd069srmaQ3 0z/DA7dlEH2IO9xG2gsC9usNgcFnFwFemGV2A7aqR96OfqbGJiEifqlyxGmMB3b2+6r3 eXEA== 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=qJPL6XQWTu91QMPy+qd0o+1YjlCp6zhTXPWOlC7y3Hk=; fh=JWAO1fAe+Wf5O2DamUx86WOaLpB0s7CTfUExwjP/ICk=; b=s3MeoCoq+UpHFGb0Q8L6ItKqAn26NnpCByNdwSqK/I+/cnJ4Nyf3R0i2L1fto1o+3o ALLTLrimD/RRRbV0zoPmTiNe4K6eX2VXVNCyiPo+phHh9DdsmNF9QAjypkY7VoBIBpyc 8M/DHyERQwsUPUkoyu481iYhorugEobkB9jyoMkI8knRpWbpn4RQwbzQ9/zt4cbxaX2x gUPv9ApYtTJ/gmN22qathhMYXzAEmXiUBy2RVaTrpRk5G6AFyJX/uF0O2zbqqJJmX7np ZpRM2A6rqc061T2i1R/TPnRck+b9y+giZbk47vd09mDmDhSEs+9jjIbFhmmHlTY4+gwI jNiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PXOEaHY9; 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 t19-20020a17090aba9300b00273ce975beesi168979pjr.114.2023.10.17.16.12.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 16:12:59 -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=PXOEaHY9; 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 04514802843F; Tue, 17 Oct 2023 16:12:52 -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 S233591AbjJQXMw (ORCPT + 52 others); Tue, 17 Oct 2023 19:12:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230219AbjJQXMv (ORCPT ); Tue, 17 Oct 2023 19:12:51 -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 013D3C4 for ; Tue, 17 Oct 2023 16:12:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9ace796374so7640508276.0 for ; Tue, 17 Oct 2023 16:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697584369; x=1698189169; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=qJPL6XQWTu91QMPy+qd0o+1YjlCp6zhTXPWOlC7y3Hk=; b=PXOEaHY92YXHkMvzlTiFsTgUrOrNK3mMmB/H3/fsEf7rHaom1eDW7UzhtVDL0S4zqH ISj5Wv+0YlDZjlAc8mkGnUV3b6al6FRGdEkP9YmnJUVrJR0PbEIBNXGZ8SRzqXYI2Mp+ zasQZeaCF6qv0JL0iHra5uPblmqUpCnJrRymWByIIRj17mQFHmVP201UMeeJ/YWKNbYS Y9swA1ZnlthbydwA7Jlg6scn0eTFGRjFnZ4UyLR9nUnhP5U+VVMoejHKTGm8KGYEcCth p0DJGpiTYUcdA+/D4/HY6t5gPIstjY0//oxkxW89gf3mtvXAZ4P15XUMK9L6fU/eUgRo v81g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697584369; x=1698189169; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=qJPL6XQWTu91QMPy+qd0o+1YjlCp6zhTXPWOlC7y3Hk=; b=lQDiXq1ruRJDQac1PsUpkt8be2HZdhMK1IOGDc4UKS20sa/RiSeyOTu4onZILeyN/b WTPy0xEqlCoRykA9ov+7A7ep1iJcIxIBxWpdm9zHRwQEbAU8gx240NuRw+I3ZG5BLN6C UxKGrk8ERiLC971s0+61gXmf32KurbSkkxtHsh+RazIeH46D7dghVVh8/K6Lye5N5wUr OBBSDeQXfP3lF1zGKvU5mgRl/3RuJmLLZ1LHVDQ5BYIKuk2rOmux20Tao9v3tqFqUko6 hMvGpNPRoXiMJohPRsS0mU1KxiRqhXy4tLQvqACbLroccH/O7nF7UoQHNRwDJYkfGEqv C/ZQ== X-Gm-Message-State: AOJu0YxZKe7usWBo7kVxqfkLddNin2sM/hX8Dv7ecHVkaMjmMqH5N7NJ PWCDTaiQtzihSR68vhtqQg8sJDyEwiLbAsqNtw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:b09:0:b0:d9a:6855:6d31 with SMTP id 9-20020a250b09000000b00d9a68556d31mr73485ybl.3.1697584369270; Tue, 17 Oct 2023 16:12:49 -0700 (PDT) Date: Tue, 17 Oct 2023 23:12:48 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAO8UL2UC/x3NTQqDMBBA4avIrDvgxKjQq5Qu0mTaDkiUmeAP4 t0buvw2751grMIG9+YE5VVM5lxBtwbiN+QPo6RqcK3rqKURrWiOy4FJZWU1zFxwE+WJzbAIbhO 5njDEHSP6kV9xCMn7roeaXJTfsv93j+d1/QBnMkHwfgAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697584368; l=2538; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=5D83orUb3gyhDUlDeVDVW1PaTTVcafALjffwJNnrV0I=; b=Zoa1dQfYP30omS9KZsLyHDYiXZj1atVVZDTjnUxa+sFDfk1rhxQUTD2gr/YxwotX/S6ojeeIz 0NdGav3EIN5AGD+/Ha1lrhBT/HzBatDdLilGTbXA40x8NeuySGmwCYK X-Mailer: b4 0.12.3 Message-ID: <20231017-strncpy-drivers-net-wireless-ti-wl1251-acx-c-v1-1-a99584e82f65@google.com> Subject: [PATCH] wl1251: 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]); Tue, 17 Oct 2023 16:12:53 -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. There was a lot of effort done here to ensure buf is NUL-terminated. We now have access to better string apis -- `buf` should be NUL-terminated and doesn't seem to require NUL-padding as its only use is with format strings in wl1251/main.c 403 | wl1251_info("firmware booted (%s)", wl->fw_ver); Therefore, a suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on the destination buffer without unnecessarily NUL-padding. Do note that there is only one caller of wl1251_acx_fw_version() in drivers/net/wireless/ti/wl1251/boot.c: 264 | wl1251_acx_fw_version(wl, wl->fw_ver, sizeof(wl->fw_ver)); ... which passes wl->fw_ver and sizeof(wl->fw_ver) wherein fw_ver is defined as having size 21 in wl1251.h: 383 | char fw_ver[21]; ... and since fw_version has a size of 20 in acx.h: 66 | char fw_version[20]; ... there is no overflow or truncation because sizeof(dest) > sizeof(src). 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 --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/net/wireless/ti/wl1251/acx.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/drivers/net/wireless/ti/wl1251/acx.c b/drivers/net/wireless/ti/wl1251/acx.c index f78fc3880423..ed5733e0dd5e 100644 --- a/drivers/net/wireless/ti/wl1251/acx.c +++ b/drivers/net/wireless/ti/wl1251/acx.c @@ -149,15 +149,7 @@ int wl1251_acx_fw_version(struct wl1251 *wl, char *buf, size_t len) goto out; } - /* be careful with the buffer sizes */ - strncpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version))); - - /* - * if the firmware version string is exactly - * sizeof(rev->fw_version) long or fw_len is less than - * sizeof(rev->fw_version) it won't be null terminated - */ - buf[min(len, sizeof(rev->fw_version)) - 1] = '\0'; + strscpy(buf, rev->fw_version, len); out: kfree(rev); --- base-commit: 58720809f52779dc0f08e53e54b014209d13eebb change-id: 20231017-strncpy-drivers-net-wireless-ti-wl1251-acx-c-47ebc6ad4435 Best regards, -- Justin Stitt