Skip to content

Commit 94755a0

Browse files
JustinStittjankara
authored andcommitted
udf: replace deprecated strncpy/strcpy with strscpy
strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. Also replace an instance of strcpy() which is also deprecated. s_volume_ident is a NUL-terminated string which is evident from its usage in udf_debug: | udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident); s_volume_ident should also be NUL-padded as it is copied out to userspace: | if (copy_to_user((char __user *)arg, | UDF_SB(inode->i_sb)->s_volume_ident, 32)) | return -EFAULT; Considering the above, a suitable replacement is `strscpy_pad` [2] due to the fact that it guarantees both NUL-termination and NUL-padding on the destination buffer. To simplify the code, let's use the new 2-argument version of strscpy_pad() introduced in Commit e6584c3 ("string: Allow 2-argument strscpy()"). Also zero-allocate @outstr so we can safely use a non-@ret length argument. This is just in case udf_dstrCS0toChar() doesn't include the NUL-byte in its return length, we won't truncate @outstr or write garbage bytes either. 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: KSPP#90 Cc: [email protected] Signed-off-by: Justin Stitt <[email protected]> Signed-off-by: Jan Kara <[email protected]> Message-Id: <[email protected]>
1 parent 8777446 commit 94755a0

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

fs/udf/super.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
895895
int ret;
896896
struct timestamp *ts;
897897

898-
outstr = kmalloc(128, GFP_KERNEL);
898+
outstr = kzalloc(128, GFP_KERNEL);
899899
if (!outstr)
900900
return -ENOMEM;
901901

@@ -921,11 +921,11 @@ static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
921921

922922
ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
923923
if (ret < 0) {
924-
strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName");
924+
strscpy_pad(UDF_SB(sb)->s_volume_ident, "InvalidName");
925925
pr_warn("incorrect volume identification, setting to "
926926
"'InvalidName'\n");
927927
} else {
928-
strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
928+
strscpy_pad(UDF_SB(sb)->s_volume_ident, outstr);
929929
}
930930
udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
931931

0 commit comments

Comments
 (0)