]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
fs/orangefs: use snprintf() instead of sprintf()
authorAmir Mohammad Jahangirzad <a.jahangirzad@gmail.com>
Sun, 8 Jun 2025 16:35:59 +0000 (20:05 +0330)
committerMike Marshall <hubcap@omnibond.com>
Mon, 21 Jul 2025 15:12:33 +0000 (11:12 -0400)
sprintf() is discouraged for use with bounded destination buffers
as it does not prevent buffer overflows when the formatted output
exceeds the destination buffer size. snprintf() is a safer
alternative as it limits the number of bytes written and ensures
NUL-termination.

Replace sprintf() with snprintf() for copying the debug string
into a temporary buffer, using ORANGEFS_MAX_DEBUG_STRING_LEN as
the maximum size to ensure safe formatting and prevent memory
corruption in edge cases.

EDIT: After this patch sat on linux-next for a few days, Dan
Carpenter saw it and suggested that I use scnprintf instead of
snprintf. I made the change and retested.

Signed-off-by: Amir Mohammad Jahangirzad <a.jahangirzad@gmail.com>
Signed-off-by: Mike Marshall <hubcap@omnibond.com>
fs/orangefs/orangefs-debugfs.c

index f7095c91660c34fc13b5bab7510fb25e4e5726e7..e463d3c73533cd6d3e34fe14e245f7a1e25261f0 100644 (file)
@@ -396,7 +396,7 @@ static ssize_t orangefs_debug_read(struct file *file,
                goto out;
 
        mutex_lock(&orangefs_debug_lock);
-       sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
+       sprintf_ret = scnprintf(buf, ORANGEFS_MAX_DEBUG_STRING_LEN, "%s", (char *)file->private_data);
        mutex_unlock(&orangefs_debug_lock);
 
        read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);