Message ID | 20230922-strncpy-drivers-leds-leds-lp3952-c-v1-1-4941d6f60ca4@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5762045vqi; Fri, 22 Sep 2023 10:46:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGibJIphJDJi1pCu1Te7dtnOMkg5av7TkMPcJ5+wzCo3mR1JkjVEjPD5ROZothTOHUKe+GX X-Received: by 2002:a17:902:ce8d:b0:1c3:b268:ecba with SMTP id f13-20020a170902ce8d00b001c3b268ecbamr4223451plg.18.1695404784680; Fri, 22 Sep 2023 10:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695404784; cv=none; d=google.com; s=arc-20160816; b=jDyG1xv2fVtzLTdqxRNXj+JJYEYcv3J2kbUKoTEJKQEQXvfc0Y4N0QjYK0Ywg5zUqp ZRi/hZC3iFWjrgJyLK/Nh11dg94GzXUcnrqR5hi2PezGxRz0YJHuqqpVoLK7FnDvFb02 LnXLzNUvzNY2QBes8baYFJcAn51Bg328qv/9oORmu5ei/LE+Sw6jMVtCi6yXqGppazmp 50mUGyAtM0mYrH89NH2Q0Yo/bXkJp/v+cQVjcBEqpB30Ao7oDgDYWy5aBeO2wbriX4n/ 0I7KWFlWmi6mpp4ja0C8fIhx830a59wUADjUMleb1ribKBIw7cQepExAPNxfdBwjiMKs 0U4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=Yx7UXzctOd2GGBJ46MDq+/W8zRb8/97PbfKpOmbNRf0=; fh=+V1FaC8T+SREOUC8smzd9hkgXZ4SxlZd5uLMRESueMA=; b=md057uaNFejDVzJPn7iSrUNz135LPMPQro2V75jpZnwf0t/NlQIgEq2LzlVh5hpX2j r8QZAPtWuQWlskO1oyf58QNXgb1hq1g5er4HT8ZZYCDfTZEZRSNaO+GOBS4CoW0DrhgS 233IwwiBQnVUEH6DbiClysYsVhRPT8Pq0lN5A1ZtZ+Q27v7frNP5earUgHFIlsLCuP1P UQthElXn+aS3NrDTIk/QsklEhss/W5Q3Ab000LG1kexhus9tysm8OD7QZaiSqeUu8hyb APQUy0DVcO9qUie363dZ2/9n48pfgBQ3P4SkA9C8ENbxPT10GOuqwuAfYUZ4agJag/tk PxQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="sb12I//8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id cm3-20020a056a020a0300b00578f7063ad6si4159072pgb.823.2023.09.22.10.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 10:46:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="sb12I//8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E4B3D839E0DF; Fri, 22 Sep 2023 08:27:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjIVP10 (ORCPT <rfc822;chrisfriedt@gmail.com> + 29 others); Fri, 22 Sep 2023 11:27:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229849AbjIVP1Z (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 22 Sep 2023 11:27:25 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E26A8199 for <linux-kernel@vger.kernel.org>; Fri, 22 Sep 2023 08:27:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d7fd4c23315so2811650276.2 for <linux-kernel@vger.kernel.org>; Fri, 22 Sep 2023 08:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695396438; x=1696001238; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Yx7UXzctOd2GGBJ46MDq+/W8zRb8/97PbfKpOmbNRf0=; b=sb12I//8gdjw5oaEDIJtIciZtGFg0h9rcFpt5ZUmbzOb0slqLwdSPbNjbOAlJUcd2x CbKSnO05ZR4pfumMbNWdNgWogZ9gNECI0chhNnghiowRoETc6YORJfgwyNNImS0y4EtL gI5oxqau9LFVbb1v9DJu2wZqmz/GJAk/uG6AN+NFHshZo+W1xPCl+zxcD8iQjpam0ju6 lVC3GyDAr+1Jr0pOds9kSiwG85Jyexf15LEBBilg6rZXSRN54kX2gHjwIjtKSnHN/4WI 5IatoDDedspua/oaYEsOx0XsUDZs9nsen/k3p0MNBaMGUkNhUXELFHiRtPQZXYNe2MfK NDMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695396438; x=1696001238; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Yx7UXzctOd2GGBJ46MDq+/W8zRb8/97PbfKpOmbNRf0=; b=rT4CFAbOzsCbQdlf/p17MOqRsAZzOzf1JX+j2DTPrg5jWotTuzuFHYgYWxQ2CIkQOk qgqh4O3nDS/pBmiGUjYklIdBXyGYsRClAq2UslWTrwgjEsQWRNGuMAhOXsOfK6stUf6Z vnIRKOe2L4fnQs5j6mv44n2zUnHW/N7F1TcCC1MJWDRpS7Stz4QhJ2VfAX8TacW6wlFj s5DvJA/7uX1OCIbqfw9zbyO9rYJoML2a9UrB1X9xCPZGn8Q7HVpF2UUnaj1uVVXsK4qB clwGCV5MnDG7Ymgkfv6Gp96H1MHO+NHX40u8W0mx8i7l9Gk7DNXJgeCE9YQVTxri2jWG F8dQ== X-Gm-Message-State: AOJu0YzMwTq4IQnKUbZQDVILUmlclHCquDBKiZpaD/D02TEshdtIuoBv hgcDO31d384f/bQD8PGbEEWPhhHsLvUJ3PkdXQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:562:b0:d85:ae1e:f696 with SMTP id a2-20020a056902056200b00d85ae1ef696mr116487ybt.0.1695396438208; Fri, 22 Sep 2023 08:27:18 -0700 (PDT) Date: Fri, 22 Sep 2023 15:27:17 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAFSyDWUC/zWNwQrCMBAFf6Xs2YW6pYH6K+KhTV50QWLYlaKU/ rtB6GVgLjMbOUzhdOk2Mqzq+ipNzqeO4mMud7Cm5iS9DP0kwv62EuuXk+kKc34iHajDNApHDiH kmOcFWBK1UDVk/fwn19u+/wBIt94ddAAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1695396437; l=1995; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=aH/Nw0wgXmPofMn8+msmli+1RTvUcNQ+NVlfyvVDvCQ=; b=ZWRxzHHGGdhrY8rSZAUGiKrWyLXM756BR/zKBvIX99dEWy6+GTHX00h/IgVVA6bNhrmDfH8cn 7IfSxl4KYjvBBbUfHKpbcjv9tL4vZ5rcDPVYRGc6Nfp33Bo/GSGKYLe X-Mailer: b4 0.12.3 Message-ID: <20230922-strncpy-drivers-leds-leds-lp3952-c-v1-1-4941d6f60ca4@google.com> Subject: [PATCH] leds: lp3952: replace deprecated strncpy with strscpy From: Justin Stitt <justinstitt@google.com> To: Pavel Machek <pavel@ucw.cz>, Lee Jones <lee@kernel.org> Cc: linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt <justinstitt@google.com> Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 22 Sep 2023 08:27:36 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777760767921450516 X-GMAIL-MSGID: 1777760767921450516 |
Series |
leds: lp3952: replace deprecated strncpy with strscpy
|
|
Commit Message
Justin Stitt
Sept. 22, 2023, 3:27 p.m. UTC
`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 <justinstitt@google.com>
---
Note: build-tested only.
---
drivers/leds/leds-lp3952.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
---
base-commit: 2cf0f715623872823a72e451243bbf555d10d032
change-id: 20230922-strncpy-drivers-leds-leds-lp3952-c-666fcfabeebd
Best regards,
--
Justin Stitt <justinstitt@google.com>
Comments
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 <justinstitt@google.com> > --- > 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 <keescook@chromium.org> -Kees > return 0; > } > > > --- > base-commit: 2cf0f715623872823a72e451243bbf555d10d032 > change-id: 20230922-strncpy-drivers-leds-leds-lp3952-c-666fcfabeebd > > Best regards, > -- > Justin Stitt <justinstitt@google.com> >
On Fri, 22 Sep 2023 15:27:17 +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); > > [...] Applied, thanks! [1/1] leds: lp3952: replace deprecated strncpy with strscpy commit: 821d3ff4b4e2c689576a623348555114e3f2f1c2 -- Lee Jones [李琼斯]
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 <justinstitt@google.com> > > --- > > 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 <keescook@chromium.org> I've applied this as-is, but please consider Kees' proposal.
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); return 0; }