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: 13431070 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 930CD1802E for ; Fri, 20 Oct 2023 19:39:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fymZahB4" Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCCEA1716 for ; Fri, 20 Oct 2023 12:39:08 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a824ef7a83so15826667b3.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=LaiaP2AB6q4ZG3LCp8IVn292O/r+b6tAAra1enHX8vTDfNegc3avBfNQ1Ow0IuexZJ mIaOScNhEOO+0pS4iOijQOxrh02HKC3mhJ3Sfi4iV2rd2Uy29emEJtknhy/r8aOxiqtS XrZ+BYoebrLnJ533K09xrl4OzArcciQM5Ns5SNEZJyy/SbV3N0PDLlDR/znYTpTOAP2g 7ZKw+w4GrLLgLbbkG+YfWHlXpU+yzWcM6Nmlit5VbvME0/ke3MIReCuH8a3Kibkp6o0Z 2hnW/LvFSLxiV/K1Y6a/aamXDRz8igVu/80Imv6JTUkAPocWufZGdmfVFpfP4MWyxEmu aWcw== X-Gm-Message-State: AOJu0Yxo6mz31WWIAPXDed6k7QzfU23eTxZONULG5pJAJk9mOzktspA5 DLw8f/vMA7re8VTjDUW20rdVldDi6o4nwRkk/w== 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 Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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;