Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp543227rdh; Sun, 24 Sep 2023 00:57:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEJA7NGro+H4RC+FbhzHfJ1C3hK9pBGgPJ3z7KobdN+jWeaRJcGS8FXy8fHM3qkRqLtqxfm X-Received: by 2002:a05:6808:1381:b0:3a3:9df9:1928 with SMTP id c1-20020a056808138100b003a39df91928mr5752426oiw.17.1695542249870; Sun, 24 Sep 2023 00:57:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695542249; cv=none; d=google.com; s=arc-20160816; b=hy9lz/qXcaKyowIJcfPEYIqAMz3jEhoLqIftIxTV2Vzk4ISlhBeQ7/XTmfdPprzRPK mhbRr4evtOvElEqtnHIV2NfYORbOa9pkxd9RHPtyCTPRapCbybvEP9IcAEBDsk4w370P Xp+jcJupcfFbNwxAAvAB+Zbl9UJEiuMe8Cfb03n1lwxV7h8TdwfWhArawiiTzRS4V+eh WteNyA/3qorei5ul2pRKQLKEQXY3ZxGeV95b7X99ORu81DOOKiY7Gj7zhSmUI5xKrYdE usfWN7ZR0HGMpT6H5Vfd7ql35urJiv3z2i9NikBtflFsMVnmR0q7zhUvCAsLiERr3elt m9Bw== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=seBNO35I6t27+aW9cJIbrOKykhwY8CBfF73TPyxAkzU=; fh=fkBteQeUvSBKhtac1mcGuj+ibyhXJ6VUujUCno3V/Ig=; b=tA4UlFDZfF8H5lwXR425iG1wXRqGLdHyNJJdoGY2AIba8TBHxc1l+2MB8UGpj8OMRN Xkz7lvXC27ZeTknQOqm26d7eMxLzahLI7RJwgagZIzsprnQcnehcIU7lATAOG41QCgA3 qTZQs9NnlNmwx2ZyDybVvDnIeuUqSvgQ6dbyG/vMuLDiwobgYjZaGIRTa9s7CvBYpC0e yeZrAxFB6hERpuUN4HsivPRvzKB5zrR5+68iepvWD/US82P1MV4fILMbLFbtHWaWw6l2 lQD3idQ6fUUGraXhbC/gsbOukYGwH2dx6EVxFCZJ54IHqCsjoeaaV6VyGBgCn+RIzxIt vMaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EDFXaiYk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id kt5-20020a170903088500b001bc63c38e17si7041625plb.244.2023.09.24.00.57.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 00:57:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EDFXaiYk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 866F9829F66C; Sat, 23 Sep 2023 20:43:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229556AbjIXDnS (ORCPT + 99 others); Sat, 23 Sep 2023 23:43:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjIXDnQ (ORCPT ); Sat, 23 Sep 2023 23:43:16 -0400 Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8932109 for ; Sat, 23 Sep 2023 20:43:09 -0700 (PDT) Received: by mail-ot1-x32b.google.com with SMTP id 46e09a7af769-6c4e38483d2so144120a34.1 for ; Sat, 23 Sep 2023 20:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695526989; x=1696131789; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=seBNO35I6t27+aW9cJIbrOKykhwY8CBfF73TPyxAkzU=; b=EDFXaiYkSd1bB8l0T5fa8bmSbpBIrk1zJeZoev6LOoy0s0jSOIXjiJtr+XPk517mCe vW8k3VAizHB85g+A8uuCFlJtOPhHO55Z3Ilsqw46eFFernrVTPXTQ42bEn6sS9HDganI 5ZJxuRwCRizbTpOZ/jBfOfHyaA86KqFaUoNYs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695526989; x=1696131789; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=seBNO35I6t27+aW9cJIbrOKykhwY8CBfF73TPyxAkzU=; b=QBiFsTtxbXQtGATmjoGeYmSYpfOztHVHpQMeZNzB0MUCearDKibYHatSHHNuxOCg1W pBWzQ46muF7QNb0HiaOoh/7D6TwDqiiWmv2honICyKo8/lMbUGFMIb+fdymdC7az8fXf C71w9LsX/vWAmu8R0uqugb81bKaq7FSwUnR9x+Pe0AJOILc1cAcHSExWHv06/opngfkP H1iE8yJ7lOYq7iOjin240fpQFsBmFS24pPW9sZbQMlTNZ6G2IlYvnMmTYeCwNtXZ7LbG AF/a5k4CIVSS9XsKfTooV9Sd8w/B+Lc44fceJizKEP8H/0Plk/Io1RCIsEwdYgXgcM3p nx1A== X-Gm-Message-State: AOJu0Yz/3LSTN7OxXyHAAx6PCVYbsXGPTQiasSTeOYoHzbPiNdaYimDM yiLpvkuiWeZIY4/L2IvgLQjQ3w== X-Received: by 2002:a05:6358:41a3:b0:143:9f18:d71a with SMTP id w35-20020a05635841a300b001439f18d71amr3868238rwc.14.1695526989027; Sat, 23 Sep 2023 20:43:09 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id 23-20020a17090a199700b002694fee879csm8008047pji.36.2023.09.23.20.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 20:43:08 -0700 (PDT) Date: Sat, 23 Sep 2023 20:43:07 -0700 From: Kees Cook To: Justin Stitt Cc: Pavel Machek , Lee Jones , 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: <202309232039.979F3B4@keescook> References: <20230922-strncpy-drivers-leds-leds-lp3952-c-v1-1-4941d6f60ca4@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230922-strncpy-drivers-leds-leds-lp3952-c-v1-1-4941d6f60ca4@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 lindbergh.monkeyblade.net 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 (howler.vger.email [0.0.0.0]); Sat, 23 Sep 2023 20:43:19 -0700 (PDT) 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 -Kees > return 0; > } > > > --- > base-commit: 2cf0f715623872823a72e451243bbf555d10d032 > change-id: 20230922-strncpy-drivers-leds-leds-lp3952-c-666fcfabeebd > > Best regards, > -- > Justin Stitt > -- Kees Cook