linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfs: Replace all non-returning strlcpy with strscpy
@ 2023-05-10 22:11 Azeem Shaikh
  2023-05-11 16:26 ` Kees Cook
  2023-05-15  7:50 ` Christian Brauner
  0 siblings, 2 replies; 4+ messages in thread
From: Azeem Shaikh @ 2023-05-10 22:11 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: linux-hardening, Azeem Shaikh, linux-fsdevel, linux-kernel

strlcpy() reads the entire source buffer first.
This read may exceed the destination size limit.
This is both inefficient and can lead to linear read
overflows if a source string is not NUL-terminated [1].
In an effort to remove strlcpy() completely [2], replace
strlcpy() here with strscpy().
No return values were used, so direct replacement is safe.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89

Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>
---
 fs/char_dev.c |    2 +-
 fs/super.c    |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/char_dev.c b/fs/char_dev.c
index 13deb45f1ec6..950b6919fb87 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -150,7 +150,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 	cd->major = major;
 	cd->baseminor = baseminor;
 	cd->minorct = minorct;
-	strlcpy(cd->name, name, sizeof(cd->name));
+	strscpy(cd->name, name, sizeof(cd->name));
 
 	if (!prev) {
 		cd->next = curr;
diff --git a/fs/super.c b/fs/super.c
index 34afe411cf2b..8d8d68799b34 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -595,7 +595,7 @@ struct super_block *sget_fc(struct fs_context *fc,
 	fc->s_fs_info = NULL;
 	s->s_type = fc->fs_type;
 	s->s_iflags |= fc->s_iflags;
-	strlcpy(s->s_id, s->s_type->name, sizeof(s->s_id));
+	strscpy(s->s_id, s->s_type->name, sizeof(s->s_id));
 	list_add_tail(&s->s_list, &super_blocks);
 	hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
 	spin_unlock(&sb_lock);
@@ -674,7 +674,7 @@ struct super_block *sget(struct file_system_type *type,
 		return ERR_PTR(err);
 	}
 	s->s_type = type;
-	strlcpy(s->s_id, type->name, sizeof(s->s_id));
+	strscpy(s->s_id, type->name, sizeof(s->s_id));
 	list_add_tail(&s->s_list, &super_blocks);
 	hlist_add_head(&s->s_instances, &type->fs_supers);
 	spin_unlock(&sb_lock);


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

* Re: [PATCH] vfs: Replace all non-returning strlcpy with strscpy
  2023-05-10 22:11 [PATCH] vfs: Replace all non-returning strlcpy with strscpy Azeem Shaikh
@ 2023-05-11 16:26 ` Kees Cook
  2023-05-15  7:50 ` Christian Brauner
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-05-11 16:26 UTC (permalink / raw)
  To: Azeem Shaikh
  Cc: Alexander Viro, Christian Brauner, linux-hardening,
	linux-fsdevel, linux-kernel

On Wed, May 10, 2023 at 10:11:19PM +0000, Azeem Shaikh wrote:
> strlcpy() reads the entire source buffer first.
> This read may exceed the destination size limit.
> This is both inefficient and can lead to linear read
> overflows if a source string is not NUL-terminated [1].
> In an effort to remove strlcpy() completely [2], replace
> strlcpy() here with strscpy().
> No return values were used, so direct replacement is safe.
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
> [2] https://github.com/KSPP/linux/issues/89
> 
> Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com>

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

-- 
Kees Cook

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

* Re: [PATCH] vfs: Replace all non-returning strlcpy with strscpy
  2023-05-10 22:11 [PATCH] vfs: Replace all non-returning strlcpy with strscpy Azeem Shaikh
  2023-05-11 16:26 ` Kees Cook
@ 2023-05-15  7:50 ` Christian Brauner
  2023-05-16 20:37   ` Kees Cook
  1 sibling, 1 reply; 4+ messages in thread
From: Christian Brauner @ 2023-05-15  7:50 UTC (permalink / raw)
  To: Azeem Shaikh
  Cc: Christian Brauner, linux-hardening, linux-fsdevel, linux-kernel,
	Alexander Viro

On Wed, 10 May 2023 22:11:19 +0000, Azeem Shaikh wrote:
> strlcpy() reads the entire source buffer first.
> This read may exceed the destination size limit.
> This is both inefficient and can lead to linear read
> overflows if a source string is not NUL-terminated [1].
> In an effort to remove strlcpy() completely [2], replace
> strlcpy() here with strscpy().
> No return values were used, so direct replacement is safe.
> 
> [...]

I sincerely hope we'll be done with swapping out various string
functions for one another at some point. Such patches always seems
benign and straightforward but the potential for subtle bugs is
feels rather high...

Applied to the vfs.misc branch of the vfs/vfs.git tree.
Patches in the vfs.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.misc

[1/1] vfs: Replace all non-returning strlcpy with strscpy
      https://git.kernel.org/vfs/vfs/c/3c5d4d803c60

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

* Re: [PATCH] vfs: Replace all non-returning strlcpy with strscpy
  2023-05-15  7:50 ` Christian Brauner
@ 2023-05-16 20:37   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-05-16 20:37 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Azeem Shaikh, linux-hardening, linux-fsdevel, linux-kernel,
	Alexander Viro

On Mon, May 15, 2023 at 09:50:25AM +0200, Christian Brauner wrote:
> On Wed, 10 May 2023 22:11:19 +0000, Azeem Shaikh wrote:
> > strlcpy() reads the entire source buffer first.
> > This read may exceed the destination size limit.
> > This is both inefficient and can lead to linear read
> > overflows if a source string is not NUL-terminated [1].
> > In an effort to remove strlcpy() completely [2], replace
> > strlcpy() here with strscpy().
> > No return values were used, so direct replacement is safe.
> > 
> > [...]
> 
> I sincerely hope we'll be done with swapping out various string
> functions for one another at some point. Such patches always seems
> benign and straightforward but the potential for subtle bugs is
> feels rather high...

Agreed. The long-term goal is to remove "strlcpy"[1] and "strncpy"[2]
completely from the kernel, leaving only "strscpy". From there, I hope
to do a treewide change to scrub the kernel of the pattern:
	strscpy(fixed-sized-dest, fixed-sized-source, sizeof(fixed-size-dest))
and replace it with a much stricter "strcpy" that refuses to work on
dynamically sized arguments. This will get us away from the pointless
exercise of duplicating sizeof() arguments when the compiler can very
happily do it itself.

But doing the return value transitions (and padding checks) for strlcpy
and strncpy need to happen first. It's a long road.

> Applied to the vfs.misc branch of the vfs/vfs.git tree.
> Patches in the vfs.misc branch should appear in linux-next soon.

Thanks!

-Kees

[1] https://github.com/KSPP/linux/issues/89
[2] https://github.com/KSPP/linux/issues/90

-- 
Kees Cook

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

end of thread, other threads:[~2023-05-16 20:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-10 22:11 [PATCH] vfs: Replace all non-returning strlcpy with strscpy Azeem Shaikh
2023-05-11 16:26 ` Kees Cook
2023-05-15  7:50 ` Christian Brauner
2023-05-16 20:37   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).