Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp141742rda; Sat, 21 Oct 2023 01:44:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqsonixqn94iPJyWoVOY7Jvj8b9TgrbZqibljBh4FJbjpqCeu3itIN+p+r+JN6RHw6fORH X-Received: by 2002:a54:4787:0:b0:3a7:1d15:28fe with SMTP id o7-20020a544787000000b003a71d1528femr3745560oic.56.1697877878232; Sat, 21 Oct 2023 01:44:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697877878; cv=none; d=google.com; s=arc-20160816; b=HibU/KrB/boHX4Bi4SIwGXxg2BN5wYt7ZmW0+ihmv33WV+gj0ogM7EH4yC2fxXSfqE NEfYjqi03a7vgs5Uh7sr8fbYlRU4xCs8mZL7L2x0ZWs1XuYX4F6BiGrgqcj0pl7KOQVE /nw6p3VgVxXkd3l/g7DtNKuPcofkT/cUwVou0iUgkP5rpnJeXbhr5TD9Fd7QIMQWCNqt AYK6msOfSXomvAyxYTxYRg+a2nAsdqUwCNgxgjAqWNVM041qo/c0mMou+DBoHciyFbWz IVv/nmsb0d3E+oMOebT3ATim6uEAQ8GjTFyvwAvcwBSLvN82STfSJP8O4Q1DwPTgCoY/ POGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=yDtjhJ1x9aGm8BrdvhZ66AGy0SmCLFMPb2JjWVfHFBk=; fh=/6Uzhd5JcG+1ixHf+I5iDV/HgIRkaxy18hVqiiS7YK4=; b=tpBd+44b94QQ+/Z4IL9xQdOQqJmvX6tNlfIma50uHBPkhOgWlMfRbD65cqZsNfijpF VbLYVNDe9LnhcsMvvEecrTfQuHTmRGA3kIApoIPlqBKubxU1ifhplCV0D17MxNyln66r CYcPwxI2PvoFvX4wpxRaJAck+WKczCPAuBEGgfUKkQ8pTVIHgRy1wXlAzeMHVbtympJT ldvJVKPBjX2LWv08dwsMCjRSGmXxY/W777Dwvyjbf2PrBzHgN9ty5X0HyMgjOuyTFP2+ g2taKvRrQDv2kVj0vIGKVE5tBEtLUmSm3TPiHPfLLMVcPCGsYk9+LHjslB5i5lOFFmvD WLVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="QbX7K/R5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id m188-20020a6326c5000000b0057c7341d568si3286342pgm.645.2023.10.21.01.44.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 01:44:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="QbX7K/R5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 4B54980990DA; Sat, 21 Oct 2023 01:44:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229686AbjJUIoT (ORCPT + 99 others); Sat, 21 Oct 2023 04:44:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbjJUIoS (ORCPT ); Sat, 21 Oct 2023 04:44:18 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43981D63; Sat, 21 Oct 2023 01:44:16 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-408382da7f0so12913455e9.0; Sat, 21 Oct 2023 01:44:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697877855; x=1698482655; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=yDtjhJ1x9aGm8BrdvhZ66AGy0SmCLFMPb2JjWVfHFBk=; b=QbX7K/R5kyYFPxYQ/dL4hYzL7PUjvp17peRPV+KZvWduaZBeZpRmmyMDoUXVMHQJR7 tKFqjGBG8JzQ5K9UiaScT2D4HexObZzAcjs+TbeCds7LZmF24wlnipm+CJLGp6pGld/Z jARhriOBO/+P7MMBERmolXDPmvVTdE1enEjq9Ew2lo6t73yGJygk4yO45zTAdrrkZpTJ d+LWEJF9ADkPmYbFef5L3stEAjrGgOcosuZzIyL90iILwe/lsI3xMnfmSHW8atM5LURs 0ybAshAEtjRd2M3h1PoUrM36zOb8BYQ1hng2if8LLcyNgko4EPM7w7ZDtdq9eYXGCHge XPDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697877855; x=1698482655; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yDtjhJ1x9aGm8BrdvhZ66AGy0SmCLFMPb2JjWVfHFBk=; b=SpVmcUpIm+NdlBA/1mY2hgxU5VwtYymRSAg2Mkajx5gtgxzHXhhZfAvzV9x5eXmVqj E9bg1LWQ9bQ8nuX3tIF98Ve4YodhV+VY8Rpgrngyv2Sv9p3zrYQGWkipc/6Rf6s4k5zF wMfVMuUPIFm9sZIeA6XgbcdINRug+acw4nRzkHcNFukcff0sUPiZ2YqpThOTtWizfbGI trU2jyLjN4mwaWvJ1lwYno0/JEK5UUSO1zsrdhkxmVJPctSQS9tTKpq4oIybEMh2hBXg H4MmEry3WEhBA4jWg5Ibp2iRmVtC9dtgW3jaOhQ67+WkVqRLzgYODgbSlQjxgye/Yu7X VOvw== X-Gm-Message-State: AOJu0YywJIh0FpBeLTFIcyK1UlevCsjW4Pi7sxHjBsrxn6Jvjn/XdUBy PxzhMN0jYdSFkx7ZZWdFgKKJAcSxmww= X-Received: by 2002:a05:600c:45c7:b0:408:4918:590e with SMTP id s7-20020a05600c45c700b004084918590emr2958392wmo.39.1697877854236; Sat, 21 Oct 2023 01:44:14 -0700 (PDT) Received: from ?IPV6:2a02:8071:b783:140:927c:82ba:d32d:99c1? ([2a02:8071:b783:140:927c:82ba:d32d:99c1]) by smtp.gmail.com with ESMTPSA id x17-20020a5d60d1000000b0032d8f075810sm3330391wrt.10.2023.10.21.01.44.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 21 Oct 2023 01:44:13 -0700 (PDT) Message-ID: <5b9cf91a-d726-44d0-99e4-9440c6bd3165@gmail.com> Date: Sat, 21 Oct 2023 10:44:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] power: supply: surface-charger: replace deprecated strncpy with strscpy To: Justin Stitt , Sebastian Reichel Cc: linux-pm@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20231020-strncpy-drivers-power-supply-surface_charger-c-v1-1-93ddbf668e10@google.com> Content-Language: en-US From: Maximilian Luz In-Reply-To: <20231020-strncpy-drivers-power-supply-surface_charger-c-v1-1-93ddbf668e10@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sat, 21 Oct 2023 01:44:35 -0700 (PDT) On 10/20/23 21:46, Justin Stitt wrote: > 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 ac->name to be NUL-terminated based on its usage with format > strings: > > surface_charger.c: > 190: ac->psy_desc.name = ac->name; > > ... > > power_supply_core.c: > 174: dev_dbg(&psy->dev, "%s: Found supply : %s\n", > 175: psy->desc->name, epsy->desc->name); > > Moreover, NUL-padding is not required as ac is already zero-allocated > before being passed to spwr_ac_init(): > > surface_charger.c: > 240: ac = devm_kzalloc(&sdev->dev, sizeof(*ac), GFP_KERNEL); > 241: if (!ac) > 242: return -ENOMEM; > 243: > 244: spwr_ac_init(ac, sdev, p->registry, p->name); > > ... this means any future 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 for 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 Thanks! Reviewed-by: Maximilian Luz Tested-by: Maximilian Luz > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > drivers/power/supply/surface_charger.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/power/supply/surface_charger.c b/drivers/power/supply/surface_charger.c > index cabdd8da12d0..7a6c62d6f883 100644 > --- a/drivers/power/supply/surface_charger.c > +++ b/drivers/power/supply/surface_charger.c > @@ -175,7 +175,7 @@ static void spwr_ac_init(struct spwr_ac_device *ac, struct ssam_device *sdev, > struct ssam_event_registry registry, const char *name) > { > mutex_init(&ac->lock); > - strncpy(ac->name, name, ARRAY_SIZE(ac->name) - 1); > + strscpy(ac->name, name, sizeof(ac->name)); > > ac->sdev = sdev; > > > --- > base-commit: bb55d7f7f7445abcc8db50e6a65d4315e79f75c7 > change-id: 20231020-strncpy-drivers-power-supply-surface_charger-c-466920fb1f48 > > Best regards, > -- > Justin Stitt >