vfs: Replace all non-returning strlcpy with strscpy
PATCH 1/1 vfs: Replace all non-returning strlcpy with strscpy
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
--- fs/char_dev.c | 2 +- fs/super.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
fs/char_dev.c +1 -1
@@ -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;
fs/super.c +2 -2
@@ -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);
Quoted 8 lines · Azeem Shaikh
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().
No return values were used, so direct replacement is safe.
Quoted 4 lines
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] https://github.com/KSPP/linux/issues/89Signed-off-by Azeem Shaikh <[email protected]>
Signature · 1 line
Kees Cook
Quoted 9 lines · Azeem Shaikh
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().
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
Quoted 9 lines · Christian Brauner
Quoted 8 lines · Azeem Shaikh
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 removestrlcpy()completely [2], replacestrlcpy()here withstrscpy().
No return values were used, so direct replacement is safe.
Quoted 6 lines
Quoted 1 line
[...]
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.
Quoted 2 lines
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
Signature · 1 line
Kees Cook