Skip to content

Commit 792e047

Browse files
keesgregkh
authored andcommitted
kernfs: Convert kernfs_walk_ns() from strlcpy() to 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]. Additionally, it returns the size of the source string, not the resulting size of the destination string. In an effort to remove strlcpy() completely[2], replace strlcpy() here with strscpy(). Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1] Link: KSPP#89 [2] Cc: Greg Kroah-Hartman <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Azeem Shaikh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2678fd2 commit 792e047

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

fs/kernfs/dir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,16 +862,16 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
862862
const unsigned char *path,
863863
const void *ns)
864864
{
865-
size_t len;
865+
ssize_t len;
866866
char *p, *name;
867867

868868
lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
869869

870870
spin_lock_irq(&kernfs_pr_cont_lock);
871871

872-
len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
872+
len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
873873

874-
if (len >= sizeof(kernfs_pr_cont_buf)) {
874+
if (len < 0) {
875875
spin_unlock_irq(&kernfs_pr_cont_lock);
876876
return NULL;
877877
}

0 commit comments

Comments
 (0)