ext4: replace deprecated strncpy with alternatives
authorJustin Stitt <justinstitt@google.com>
Thu, 21 Mar 2024 01:03:10 +0000 (01:03 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 3 May 2024 03:55:10 +0000 (23:55 -0400)
commit744a56389f7398f286231e062c2e63f0de01bcc6
tree845892d3d71e117f9b4161188ac04efa0798592e
parente19089dff547c9e1f09712acc3536d7b0aa9ce3d
ext4: replace deprecated strncpy with alternatives

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

in file.c:
s_last_mounted is marked as __nonstring meaning it does not need to be
NUL-terminated. Let's instead use strtomem_pad() to copy bytes from the
string source to the byte array destination -- while also ensuring to
pad with zeroes.

in ioctl.c:
We can drop the memset and size argument in favor of using the new
2-argument version of strscpy_pad() -- which was introduced with Commit
e6584c3964f2f ("string: Allow 2-argument strscpy()"). This guarantees
NUL-termination and NUL-padding on the destination buffer -- which seems
to be a requirement judging from this comment:

| static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label)
| {
| char label[EXT4_LABEL_MAX + 1];
|
| /*
|  * EXT4_LABEL_MAX must always be smaller than FSLABEL_MAX because
|  * FSLABEL_MAX must include terminating null byte, while s_volume_name
|  * does not have to.
|  */

in super.c:
s_first_error_func is marked as __nonstring meaning we can take the same
approach as in file.c; just use strtomem_pad()

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20240321-strncpy-fs-ext4-file-c-v1-1-36a6a09fef0c@google.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/file.c
fs/ext4/ioctl.c
fs/ext4/super.c