Skip to contentSkip to conversation

fs/kernel_read_file: set the correct 'buf_size' after allocating 'buf'

PATCH New [email protected] 1 participant 1 message Mar 27, 2023 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/kernel_read_file: set the correct 'buf_size' after allocating 'buf'

From: Gou Hao <[email protected]>

According to the comments of the kernel_read_file():
'if @buf is NULL, a buffer will be allocated, and @buf_size will be ignored'.

But if i pass 'buf=NULL, buf_size=0' to kernel_read_file(), 0 is returned, it means that has not read the content.

The root cause is that 'buf_size' is not set correctly after allocating memory, which does not match 'copied < buf_size', so 0 is returned.

So we should set 'buf_size' to 'i_size' after allocating memory.

---
 fs/kernel_read_file.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/kernel_read_file.c +8 -3
index 5d826274570c..77d400f5951d 100644
@@ -63,12 +63,14 @@ ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
 		goto out;
 	}
 	/* The entire file cannot be read in one buffer. */
-	if (!file_size && offset == 0 && i_size > buf_size) {
+	if (!file_size && offset == 0 &&
+		(buf_size && i_size > buf_size)) {
 		ret = -EFBIG;
 		goto out;
 	}
 
-	whole_file = (offset == 0 && i_size <= buf_size);
+	whole_file = (offset == 0 &&
+		(!buf_size || i_size <= buf_size));
 	ret = security_kernel_read_file(file, id, whole_file);
 	if (ret)
 		goto out;
@@ -76,8 +78,11 @@ ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
 	if (file_size)
 		*file_size = i_size;
 
-	if (!*buf)
+	if (!*buf) {
 		*buf = allocated = vmalloc(i_size);
+		buf_size = i_size;
+	}
+
 	if (!*buf) {
 		ret = -ENOMEM;
 		goto out;
Signature · 1 line
2.20.1