Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Squashfs: remove deprecated strncpy by not copying the string

Squashfs copied the passed string (name) into a temporary buffer to ensure
it was NUL-terminated. This however is completely unnecessary as the
string is already NUL-terminated. So remove the deprecated strncpy() by
completely removing the string copy.

The background behind this unnecessary string copy is that it dates back
to the days when Squashfs was an out of kernel patch. The code
deliberately did not assume the string was NUL-terminated in case in
future this changed (due to kernel changes). This would mean the out of
tree patches would be broken but still compile OK.

Link: https://lkml.kernel.org/r/20240403183352.391308-1-phillip@squashfs.org.uk
Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Justin Stitt <justinstitt@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Phillip Lougher and committed by
Andrew Morton
040bf9a7 029c45bb

+4 -10
+4 -10
fs/squashfs/namei.c
··· 62 62 */ 63 63 static int get_dir_index_using_name(struct super_block *sb, 64 64 u64 *next_block, int *next_offset, u64 index_start, 65 - int index_offset, int i_count, const char *name, 66 - int len) 65 + int index_offset, int i_count, const char *name) 67 66 { 68 67 struct squashfs_sb_info *msblk = sb->s_fs_info; 69 68 int i, length = 0, err; 70 69 unsigned int size; 71 70 struct squashfs_dir_index *index; 72 - char *str; 73 71 74 72 TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count); 75 73 76 - index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN * 2 + 2, GFP_KERNEL); 74 + index = kmalloc(sizeof(*index) + SQUASHFS_NAME_LEN + 1, GFP_KERNEL); 77 75 if (index == NULL) { 78 76 ERROR("Failed to allocate squashfs_dir_index\n"); 79 77 goto out; 80 78 } 81 - 82 - str = &index->name[SQUASHFS_NAME_LEN + 1]; 83 - strncpy(str, name, len); 84 - str[len] = '\0'; 85 79 86 80 for (i = 0; i < i_count; i++) { 87 81 err = squashfs_read_metadata(sb, index, &index_start, ··· 95 101 96 102 index->name[size] = '\0'; 97 103 98 - if (strcmp(index->name, str) > 0) 104 + if (strcmp(index->name, name) > 0) 99 105 break; 100 106 101 107 length = le32_to_cpu(index->index); ··· 147 153 length = get_dir_index_using_name(dir->i_sb, &block, &offset, 148 154 squashfs_i(dir)->dir_idx_start, 149 155 squashfs_i(dir)->dir_idx_offset, 150 - squashfs_i(dir)->dir_idx_cnt, name, len); 156 + squashfs_i(dir)->dir_idx_cnt, name); 151 157 152 158 while (length < i_size_read(dir)) { 153 159 /*