From patchwork Fri Oct 20 19:39:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13431071 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 765BEC004C0 for ; Fri, 20 Oct 2023 19:39:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230320AbjJTTjd (ORCPT ); Fri, 20 Oct 2023 15:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230337AbjJTTjY (ORCPT ); Fri, 20 Oct 2023 15:39:24 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EC271713 for ; Fri, 20 Oct 2023 12:39:08 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a824ef7a83so15826657b3.0 for ; Fri, 20 Oct 2023 12:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697830746; x=1698435546; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ApgtBIwM/UT3SKqY+L+66VIK2kG5whPiTDfo769iKqE=; b=fymZahB42Jt7DJG2f/jHR8IofMHwz/ta/5lRINoFiRseWXr7Pr8dG5ADbn1Ht5M7NW eHTiu6zByuTlWMnSER7itpHo3wMFefGGeTg77XLxOxr/LGqqhUVlc2JJmbh4pAkSYMv6 cU19bMgVNtkHao5tny5U35qlnW38QKSX67XlFR0C23HGAgkAfwcTynMulRCQp0UiFKd2 tG589/4x0MN+/0bINH502AMfhROlGJ+liX6+Bg5Vy/DOXnQaRJed+2sCSTeglFyBa3Eh OX4BC74mayd3MYNkRt8xhSU9VkiOlZ7v2o7sd89LE0slvkQFrwXSxpJejQ15OLwzvMZO U7Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697830746; x=1698435546; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ApgtBIwM/UT3SKqY+L+66VIK2kG5whPiTDfo769iKqE=; b=wNJcxvCKuhT0x8UkanimcMt2XIti077hgAPqIP/WOHghiqvsIQ9rzY8iq3RPBgCAYs MlBO2OHktyZCwnQLLRD02tUM9l2JEC/fpMpbm4UXTJ/qFbR0AOvnK1sMqcyBqzcb4jWD om4uEOIZUgk0Q+D6k6j1RfZ7pd85s1J8m547uMZ0halsVeENXXjS8+AW9YKMeYEHmq2l ZpcOmaQdq+595s5ppL7yppiA8TjzgrAU3+e+pDvBNr2LQltTVId+U0n+f7R/dS7bmAnf NpRx2o2tdWx8RU9MbuETu9pOkfK3NqGoRWJes7/G2oEKd6zcuLHhYvd4dZC6Y/ljvAug 5AuA== X-Gm-Message-State: AOJu0YxQddcBatb4d4u1rIRNMQiOTNPozKQfFtx3FD/2JLVyzMQXJ6H/ +3lBA3xkcX9NE2paViBgrc4Emy+zQYySa0Q4ZA== X-Google-Smtp-Source: AGHT+IEMURRd+a5veV8jdamkf3IjKiUnn+FpVD4h6z8O/r3QLS9fPuSYUe8nhXkO+H6BxwqrGYE3SZBBG6MGCUhKZQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a81:6cc3:0:b0:5a7:de61:4d9b with SMTP id h186-20020a816cc3000000b005a7de614d9bmr72471ywc.3.1697830746201; Fri, 20 Oct 2023 12:39:06 -0700 (PDT) Date: Fri, 20 Oct 2023 19:39:02 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAFbXMmUC/52NQQ7CIBBFr9KwdgygrY0r72EaA3TaktRCBkRJ0 7uLPYKrn/cX760sIFkM7FqtjDDZYN1SQB4qZia1jAi2L8wklyfBJYcQaTE+Q082IQXw7o0E4eX 9nMvQoAw+tIoRKYMBzU171rxWRrasSD3hYD978N4VnmyIjvLeT+L3/p1KAgQYpRWqmmPTXG6jc +OMR+OerNu27QtczP977QAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697830745; l=2480; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=zP2JAHf39kTI56frJ1mycRlDlgI+x3V8U7lKpy4JwDA=; b=8lnCfGuxm7KfO7JWZvnLM0jkO3KfBDK4t51JJ/oInHUz0BAWOE3mpSG3NT20U8vZ+oT5gyPB3 3MX1Z5PX6faDN9OI7yXoH0OvrjC02jZhH08FqCPvJOVLwOuem7lHeCP X-Mailer: b4 0.12.3 Message-ID: <20231020-strncpy-drivers-power-supply-surface_battery-c-v2-1-29ed16b2caf1@google.com> Subject: [PATCH v2] power: supply: surface_battery: replace deprecated strncpy with strscpy From: Justin Stitt To: Maximilian Luz , Sebastian Reichel Cc: linux-pm@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Precedence: bulk List-ID: X-Mailing-List: linux-pm@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. We expect bat->name to be NUL-terminated based on its usage with strcmp(): power_supply_core.c: 445: return strcmp(psy->desc->name, name) == 0; ... and also by the manual `... - 1` for the length argument of the original strncpy() invocation. Furthermore, no NUL-padding is needed as bat is zero-allocated before calling spwr_battery_init(): 826: bat = devm_kzalloc(&sdev->dev, sizeof(*bat), GFP_KERNEL); 827: if (!bat) 828: return -ENOMEM; 829: 830: spwr_battery_init(bat, sdev, p->registry, p->name); ... this means any further NUL-byte assignments (like the ones that strncpy() does) are redundant. 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 opt to use the more idiomatic strscpy() usage of: (dest, src, sizeof(dest)). 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 Reviewed-by: Maximilian Luz Tested-by: Maximilian Luz --- Changes in v2: - fix subject line - Link to v1: https://lore.kernel.org/r/20231020-strncpy-drivers-power-supply-surface_battery-c-v1-1-cabaea50e667@google.com --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/power/supply/surface_battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- base-commit: bb55d7f7f7445abcc8db50e6a65d4315e79f75c7 change-id: 20231020-strncpy-drivers-power-supply-surface_battery-c-b0c84b05ac28 Best regards, -- Justin Stitt diff --git a/drivers/power/supply/surface_battery.c b/drivers/power/supply/surface_battery.c index 19d2f8834e56..196d290dc596 100644 --- a/drivers/power/supply/surface_battery.c +++ b/drivers/power/supply/surface_battery.c @@ -722,7 +722,7 @@ static void spwr_battery_init(struct spwr_battery_device *bat, struct ssam_devic struct ssam_event_registry registry, const char *name) { mutex_init(&bat->lock); - strncpy(bat->name, name, ARRAY_SIZE(bat->name) - 1); + strscpy(bat->name, name, sizeof(bat->name)); bat->sdev = sdev;