[PATCH 1/5] debugfs: Prevent NULL dereference reading from string property

From: Richard Fitzgerald
Date: Tue May 16 2023 - 12:09:39 EST


Check in debugfs_read_file_str() if the string pointer is NULL.

It is perfectly reasonable that a driver may wish to export a string
to debugfs that can have the value NULL to indicate empty/unused/ignore.

Signed-off-by: Richard Fitzgerald <rf@xxxxxxxxxxxxxxxxxxxxx>
---
fs/debugfs/file.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 1f971c880dde..2c085ab4e800 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -878,6 +878,9 @@ ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
return ret;

str = *(char **)file->private_data;
+ if (!str)
+ return simple_read_from_buffer(user_buf, count, ppos, "\n", 1);
+
len = strlen(str) + 1;
copy = kmalloc(len, GFP_KERNEL);
if (!copy) {
--
2.30.2