Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3246814rdh; Thu, 28 Sep 2023 06:49:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEMTBcQGHervHjX0FNqEZWrYjV5oARV4JlRoS/fVW9b2F7V2+Kd8zivbirO7UiDbKLZblLg X-Received: by 2002:a17:903:54d:b0:1c6:1a26:ef56 with SMTP id jo13-20020a170903054d00b001c61a26ef56mr1058567plb.48.1695908951498; Thu, 28 Sep 2023 06:49:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695908951; cv=none; d=google.com; s=arc-20160816; b=t7U3MBKYaCcCgk+lCymhxNvMtQn8n0QkbB8tHdiQosbdM8WS3J3xnHpSc+t2GovSIj 8fRkdGv6CUfbIwbH/HRg7OTAWVurG2vKcUR1S6BkJ+cZDIoAsLFNs2k/ddWwjNd6vxwl Rmt8BlrCaCpa5REBLWzLXkSd4jBqpwFhSTHZcn87m/ec1JrMMAVwAop+m+9ESj8gOuyk 4APDl1zhbyEpLVnITufHVYmhnvuFQQR5HJpTRbS75qyTRZoXK8LGJpVe7+Zln05PqxES v+SnsjVjGXAfEcwco5dGFxbwlMNt7d2TWr7urswXnueXLbVdHEyTnuiZrbb3kYQWxucN nKXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=o4iCKJTmpxlsPLlzWZWJSxYV6EddPcqyj1ktPAARiIc=; fh=nDScngRnUaXasYBlVuattRmEHV3KVgT2B4EUzED/BXQ=; b=Jvs9Zv2m+OlFu/SAtdCXC4QsfuckTl6L4CbzMqrb74DGBmlY4VwEOHc/hRnrbZr49n 9fVbXkdM+uOmvhfRuFSRpjgGtL4AZE6Lj6Txf0Z8ZGVF5WZLdq9bHuSwKDZvXUhyv9rI Vs6oUeh+qvV5HiMFZa8WTxn0dof3wauRALbtX87dVxRkLfFHQTkOMYWTNOR73IdYG0tX JXU0mssR8jXH0RrG3tlUJb0tdRmdUaHZHMR/2ERGxSwrqXogdtx+Q6/KFBnK197PD+0P dm7YZVZ9hy/Al/4U59los29NowKq5s051iiXPNqd7CiJghwVr3F7ImXHyvvaLYeYLCuX /qlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EdCBwGo9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id b17-20020a170903229100b001c61ad51779si11683213plh.623.2023.09.28.06.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 06:49:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EdCBwGo9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 45AD3804484B; Thu, 28 Sep 2023 06:42:16 -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 S232251AbjI1NmH (ORCPT + 99 others); Thu, 28 Sep 2023 09:42:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232031AbjI1NmF (ORCPT ); Thu, 28 Sep 2023 09:42:05 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53461193; Thu, 28 Sep 2023 06:42:04 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87A42C433C8; Thu, 28 Sep 2023 13:42:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695908523; bh=7Y2PniOfB3kAjyIQoXZvNLWZUM33zoeeBRwcWGGreJE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=EdCBwGo9bet5FaWMwSzUMysVdQ1j2UDHxdKtNQQNmov93Gt1hWyFARuBLcAT4KTji GF3NtTvyR8v11b9egen8g+/6/5gS/7F1M3z7fqcsRQQR1na/P+bW1rvTo+GagHtls3 B4sE3Wuf29K5Ml9/eCNrCTDn1o6xQMlHDu0WuuG05WsweybtEna+YLleDJ2y6Yl865 ii6JEkd1sRhRT2k5HZjY7bpLrnoxw36rPq6vcRbeeFRVsUsvvg1oR9bvmKPSDcDI4C UZyMC7yPpqDbfYH7+fkoXSMPH1GiPCXHadDL2VVYs/RrkGe31FCY5RRSliOP7XQ7km SWTHDg4xHR8Wg== Date: Thu, 28 Sep 2023 14:41:59 +0100 From: Lee Jones To: Kees Cook Cc: Justin Stitt , Pavel Machek , linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] leds: lp3952: replace deprecated strncpy with strscpy Message-ID: <20230928134159.GJ9999@google.com> References: <20230922-strncpy-drivers-leds-leds-lp3952-c-v1-1-4941d6f60ca4@google.com> <202309232039.979F3B4@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202309232039.979F3B4@keescook> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 pete.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 (pete.vger.email [0.0.0.0]); Thu, 28 Sep 2023 06:42:16 -0700 (PDT) On Sat, 23 Sep 2023, Kees Cook wrote: > On Fri, Sep 22, 2023 at 03:27:17PM +0000, 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 `dest` to be NUL-terminated due to its use with dev_err. > > > > lp3952_get_label()'s dest argument is priv->leds[i].name: > > | acpi_ret = lp3952_get_label(&priv->client->dev, led_name_hdl[i], > > | priv->leds[i].name); > > ... which is then assigned to: > > | priv->leds[i].cdev.name = priv->leds[i].name; > > ... which is used with a format string > > | dev_err(&priv->client->dev, > > | "couldn't register LED %s\n", > > | priv->leds[i].cdev.name); > > > > There is no indication that NUL-padding is required but if it is let's > > opt for strscpy_pad. > > > > 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. > > > > 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. > > --- > > drivers/leds/leds-lp3952.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/drivers/leds/leds-lp3952.c b/drivers/leds/leds-lp3952.c > > index 3bd55652a706..62ade3f05a87 100644 > > --- a/drivers/leds/leds-lp3952.c > > +++ b/drivers/leds/leds-lp3952.c > > @@ -101,7 +101,7 @@ static int lp3952_get_label(struct device *dev, const char *label, char *dest) > > if (ret) > > return ret; > > > > - strncpy(dest, str, LP3952_LABEL_MAX_LEN); > > + strscpy(dest, str, LP3952_LABEL_MAX_LEN); > > Given my desire to use sizeof(dest) for these things, I wonder if it'd > be nicer to pass more context here for the compiler as the only user of > this function is the immediately next function. Instead of passing in > "char *dest", it could pass "struct lp3952_led_array *priv", and > suddenly sizeof() would be possible. > > But, since it's technically correct as-is: > > struct lp3952_ctrl_hdl { > struct led_classdev cdev; > char name[LP3952_LABEL_MAX_LEN]; > > There's no pressing need to actually do the priv refactor. It's just a > comment on the coding style of the original code. :) > > Reviewed-by: Kees Cook I've applied this as-is, but please consider Kees' proposal. -- Lee Jones [李琼斯]