public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PM: hibernate: replace deprecated strncpy with strscpy
@ 2024-04-29 20:50 Justin Stitt
  2024-04-29 21:58 ` Kees Cook
  2024-04-30  9:36 ` Dhruva Gole
  0 siblings, 2 replies; 4+ messages in thread
From: Justin Stitt @ 2024-04-29 20:50 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, Pavel Machek
  Cc: linux-pm, linux-kernel, linux-hardening, 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.

This kernel config option is simply assigned with the resume_file
buffer. It should be NUL-terminated but not necessarily NUL-padded as
per its further usage with other string apis:
|	static int __init find_resume_device(void)
|	{
|		if (!strlen(resume_file))
|			return -ENOENT;
|
|		pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);

Use strscpy [2] as it guarantees NUL-termination on the destination
buffer. Specifically, use the new 2-argument version of strscpy()
introduced in Commit e6584c3964f2f ("string: Allow 2-argument
strscpy()").

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.

Found with: $ rg "strncpy\("
---
 kernel/power/hibernate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index 43b1a82e800c..0a213f69a9e4 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -1361,7 +1361,7 @@ static int __init resume_setup(char *str)
 	if (noresume)
 		return 1;
 
-	strncpy(resume_file, str, 255);
+	strscpy(resume_file, str);
 	return 1;
 }
 

---
base-commit: d7ad0581567927c433918bb5f06f3d29f89807d3
change-id: 20240412-strncpy-kernel-power-hibernate-c-77985696443c

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] PM: hibernate: replace deprecated strncpy with strscpy
  2024-04-29 20:50 [PATCH] PM: hibernate: replace deprecated strncpy with strscpy Justin Stitt
@ 2024-04-29 21:58 ` Kees Cook
  2024-04-30  9:36 ` Dhruva Gole
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2024-04-29 21:58 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, linux-pm,
	linux-kernel, linux-hardening

On Mon, Apr 29, 2024 at 08:50:30PM +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.
> 
> This kernel config option is simply assigned with the resume_file
> buffer. It should be NUL-terminated but not necessarily NUL-padded as
> per its further usage with other string apis:
> |	static int __init find_resume_device(void)
> |	{
> |		if (!strlen(resume_file))
> |			return -ENOENT;
> |
> |		pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
> 
> Use strscpy [2] as it guarantees NUL-termination on the destination
> buffer. Specifically, use the new 2-argument version of strscpy()
> introduced in Commit e6584c3964f2f ("string: Allow 2-argument
> strscpy()").
> 
> 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.
> 
> Found with: $ rg "strncpy\("
> ---
>  kernel/power/hibernate.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
> index 43b1a82e800c..0a213f69a9e4 100644
> --- a/kernel/power/hibernate.c
> +++ b/kernel/power/hibernate.c
> @@ -1361,7 +1361,7 @@ static int __init resume_setup(char *str)
>  	if (noresume)
>  		return 1;
>  
> -	strncpy(resume_file, str, 255);
> +	strscpy(resume_file, str);
>  	return 1;
>  }
>  

Yup, this looks correct to me. resume_file is:

static char resume_file[256] = CONFIG_PM_STD_PARTITION;

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PM: hibernate: replace deprecated strncpy with strscpy
  2024-04-29 20:50 [PATCH] PM: hibernate: replace deprecated strncpy with strscpy Justin Stitt
  2024-04-29 21:58 ` Kees Cook
@ 2024-04-30  9:36 ` Dhruva Gole
  2024-04-30 11:01   ` Rafael J. Wysocki
  1 sibling, 1 reply; 4+ messages in thread
From: Dhruva Gole @ 2024-04-30  9:36 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, linux-pm,
	linux-kernel, linux-hardening

On Apr 29, 2024 at 20:50:30 +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.
> 
> This kernel config option is simply assigned with the resume_file
> buffer. It should be NUL-terminated but not necessarily NUL-padded as
> per its further usage with other string apis:
> |	static int __init find_resume_device(void)
> |	{
> |		if (!strlen(resume_file))
> |			return -ENOENT;
> |
> |		pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
> 
> Use strscpy [2] as it guarantees NUL-termination on the destination
> buffer. Specifically, use the new 2-argument version of strscpy()
> introduced in Commit e6584c3964f2f ("string: Allow 2-argument
> strscpy()").
> 
> 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>
> ---
[...]

Reviewed-by: Dhruva Gole <d-gole@ti.com>

-- 
Best regards,
Dhruva Gole <d-gole@ti.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] PM: hibernate: replace deprecated strncpy with strscpy
  2024-04-30  9:36 ` Dhruva Gole
@ 2024-04-30 11:01   ` Rafael J. Wysocki
  0 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2024-04-30 11:01 UTC (permalink / raw)
  To: Dhruva Gole, Justin Stitt
  Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, linux-pm,
	linux-kernel, linux-hardening, Kees Cook

On Tue, Apr 30, 2024 at 11:36 AM Dhruva Gole <d-gole@ti.com> wrote:
>
> On Apr 29, 2024 at 20:50:30 +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.
> >
> > This kernel config option is simply assigned with the resume_file
> > buffer. It should be NUL-terminated but not necessarily NUL-padded as
> > per its further usage with other string apis:
> > |     static int __init find_resume_device(void)
> > |     {
> > |             if (!strlen(resume_file))
> > |                     return -ENOENT;
> > |
> > |             pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
> >
> > Use strscpy [2] as it guarantees NUL-termination on the destination
> > buffer. Specifically, use the new 2-argument version of strscpy()
> > introduced in Commit e6584c3964f2f ("string: Allow 2-argument
> > strscpy()").
> >
> > 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>
> > ---
> [...]
>
> Reviewed-by: Dhruva Gole <d-gole@ti.com>

Applied as 6.10 material, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-04-30 11:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-29 20:50 [PATCH] PM: hibernate: replace deprecated strncpy with strscpy Justin Stitt
2024-04-29 21:58 ` Kees Cook
2024-04-30  9:36 ` Dhruva Gole
2024-04-30 11:01   ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox