From: gouhao@uniontech.com
To: viro@zeniv.linux.org.uk, brauner@kernel.org
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] fs/kernel_read_file: set the correct 'buf_size' after allocating 'buf'
Date: Mon, 27 Mar 2023 14:47:46 +0800 [thread overview]
Message-ID: <20230327064746.20441-1-gouhao@uniontech.com> (raw)
From: Gou Hao <gouhao@uniontech.com>
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.
Signed-off-by: Gou Hao <gouhao@uniontech.com>
---
fs/kernel_read_file.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/fs/kernel_read_file.c b/fs/kernel_read_file.c
index 5d826274570c..77d400f5951d 100644
--- a/fs/kernel_read_file.c
+++ b/fs/kernel_read_file.c
@@ -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;
--
2.20.1
reply other threads:[~2023-03-27 6:48 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230327064746.20441-1-gouhao@uniontech.com \
--to=gouhao@uniontech.com \
--cc=brauner@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).