From patchwork Fri Oct 20 19:21:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13431058 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 852C61DDC8 for ; Fri, 20 Oct 2023 19:21:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="avyZOTCx" 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 8D321C0 for ; Fri, 20 Oct 2023 12:21:50 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a90d6ab944so5408027b3.2 for ; Fri, 20 Oct 2023 12:21:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697829710; x=1698434510; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=qALOrG6vepevyfdTV65I5X2dgTb+XRjcAA0K1wR5V78=; b=avyZOTCxSheiio0cQy8we9ioz7bsEXKOaiDle56ZEEWD3VeroRzhHAwYc4FvxMOp12 6CrHrDFoZI556Xdrb+S2T5Ipy7xAkWN04RAjc+wKelNnx4Na7l02arP7YLwqEOlWkcLu ufExZn8FipZ9QPhE6+UsYRwJOzotnuX2LtpRfueFfT7c+RsLaTToycOE76C2GsF/xJEl tfvQOgfzs64n6rTLv5nYP0b59jcO8nmPxEhTxl24l20Ip3H8LyAD4Ep0H6ZSorrfw7MD fKicyMxWnxHFtvh1B2OEIGKDclqoQG7f/L2eLd/m7g8/v2/0IzLiZ2W7N6nckd6Ot7ao Le2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697829710; x=1698434510; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=qALOrG6vepevyfdTV65I5X2dgTb+XRjcAA0K1wR5V78=; b=Y215d3u4VwgWHvpCoi3H5h3QXDn/8HXU9DrVY9P6hS/Kt/N5nHlacsIUq2gwjzF2bI Aiml4Cfo1pBTehtarjmv4JyES3o6uU87UKieJx2wQYKsdoP4eCiCL04cHPe2KsMnWxvW dTJL5rOD1DNaRP3qlCHf7VYwjwwSzMZ/gZbUH1lhVFycQXokLg8AwE/GrgbwtDqzNKS0 a8nbkGVMmdlHocfqso/KfNVDRVOOWaY5qwPLCIK4pM3KlS0oj7tqRXMje7NXk1QnvSIk +USl6Nmy7WWzUzEvuBylrMwzHBMLxF3sXcsfTYaJu2eCr5VKJ0LDYeykA4yP+7puSKGS PClQ== X-Gm-Message-State: AOJu0YzgRpsOitHE0WUs3Kh+yeRwx9RRNMffegwy0L9AAW5MG5dgE9f5 RkFAttk5+sWVPbrDmjb1xYxAI6CJvkgkEZPnuw== X-Google-Smtp-Source: AGHT+IF0gbKlBuVYWFSDhK1cHQbvO0vJ+f+8x3DRXak9PN8AwNgsgu3tNhmxk4ZIHpRQ+61kd2848nUx67nBfQ58iQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:d110:0:b0:d9c:c9a8:8c27 with SMTP id i16-20020a25d110000000b00d9cc9a88c27mr57393ybg.13.1697829709856; Fri, 20 Oct 2023 12:21:49 -0700 (PDT) Date: Fri, 20 Oct 2023 19:21:46 +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=H4sIAErTMmUC/x3NwQqDQAwE0F+RnBtYIyj2V0oPyxptoF2XpLUV8 d+NPQ3vMDMbGKuwwbXaQHkRkzk76ksF6RHzxCiDGyhQUwcKaG/Nqaw4qCyshmX+sqJ9Snmu6BW dnK+Y45kJqWm7tuOxpz6BjxblUX7/w9t93w+Y0Zw+gAAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697829708; l=2442; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=Mdqt94cfpfyPw2z8vNiZUw0jKlb6Y4rhrl6QkPkNsjg=; b=eGMemd00Wx/G5iPloPDERokDgu99pfw0hb8wRp5tP7bzTbL5R7kAwXrl89cUd53z1hIPdIRS8 PsuJh/QEPMuD+eAqiCuQ/YtrDg+fE0fCywNIJFMoI5Bm2NEFlO3Dq6u X-Mailer: b4 0.12.3 Message-ID: <20231020-strncpy-drivers-power-supply-charger-manager-c-v1-1-698f73bcad2a@google.com> Subject: [PATCH] power: supply: charger-manager: replace deprecated strncpy with strscpy From: Justin Stitt To: Sebastian Reichel Cc: linux-pm@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 cm->psy_name_buf to be NUL-terminated based on its usage with format strings: 1522: cm->charger_psy_desc.name = cm->psy_name_buf; ... 1587: dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n", 1587: cm->charger_psy_desc.name); Moreover, NUL-padding is not required as `cm` is already zero-allocated and thus any future NUL-byte assignments (like what strncpy() will do) are redundant: 1437: cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL); 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 for the more idiomatic strscpy() usage of: strscpy(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 --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/power/supply/charger-manager.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- base-commit: bb55d7f7f7445abcc8db50e6a65d4315e79f75c7 change-id: 20231020-strncpy-drivers-power-supply-charger-manager-c-236767ef929c Best regards, -- Justin Stitt diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c index 5fa6ba7f41e1..085d5277bc19 100644 --- a/drivers/power/supply/charger-manager.c +++ b/drivers/power/supply/charger-manager.c @@ -1516,9 +1516,11 @@ static int charger_manager_probe(struct platform_device *pdev) memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default)); if (!desc->psy_name) - strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); + strscpy(cm->psy_name_buf, psy_default.name, + sizeof(cm->psy_name_buf)); else - strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); + strscpy(cm->psy_name_buf, desc->psy_name, + sizeof(cm->psy_name_buf)); cm->charger_psy_desc.name = cm->psy_name_buf; /* Allocate for psy properties because they may vary */