LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Scott Branden <scott.branden@broadcom.com>
To: Luis Chamberlain <mcgrof@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
David Brown <david.brown@linaro.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Shuah Khan <shuah@kernel.org>,
bjorn.andersson@linaro.org,
Shuah Khan <skhan@linuxfoundation.org>,
Arnd Bergmann <arnd@arndb.de>
Cc: "Rafael J . Wysocki" <rafael@kernel.org>,
linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
linux-fsdevel@vger.kernel.org,
BCM Kernel Feedback <bcm-kernel-feedback-list@broadcom.com>,
Olof Johansson <olof@lixom.net>,
Andrew Morton <akpm@linux-foundation.org>,
Dan Carpenter <dan.carpenter@oracle.com>,
Colin Ian King <colin.king@canonical.com>,
Kees Cook <keescook@chromium.org>, Takashi Iwai <tiwai@suse.de>,
linux-kselftest@vger.kernel.org, Andy Gross <agross@kernel.org>,
Scott Branden <scott.branden@broadcom.com>
Subject: [PATCH v2 1/7] fs: introduce kernel_pread_file* support
Date: Wed, 19 Feb 2020 16:48:19 -0800 [thread overview]
Message-ID: <20200220004825.23372-2-scott.branden@broadcom.com> (raw)
In-Reply-To: <20200220004825.23372-1-scott.branden@broadcom.com>
Add kernel_pread_file* support to kernel to allow for partial read
of files with an offset into the file. Existing kernel_read_file
functions call new kernel_pread_file functions with offset=0 and
flags=KERNEL_PREAD_FLAG_WHOLE.
Signed-off-by: Scott Branden <scott.branden@broadcom.com>
---
fs/exec.c | 77 ++++++++++++++++++++++++++++++++++++----------
include/linux/fs.h | 15 +++++++++
2 files changed, 75 insertions(+), 17 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index db17be51b112..a38f8fb432fa 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -896,10 +896,14 @@ struct file *open_exec(const char *name)
}
EXPORT_SYMBOL(open_exec);
-int kernel_read_file(struct file *file, void **buf, loff_t *size,
- loff_t max_size, enum kernel_read_file_id id)
-{
- loff_t i_size, pos;
+int kernel_pread_file(struct file *file, void **buf, loff_t *size,
+ loff_t pos, loff_t max_size, unsigned int flags,
+ enum kernel_read_file_id id)
+{
+ loff_t alloc_size;
+ loff_t buf_pos;
+ loff_t read_end;
+ loff_t i_size;
ssize_t bytes = 0;
int ret;
@@ -919,21 +923,31 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
ret = -EINVAL;
goto out;
}
- if (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) {
+
+ /* Default read to end of file */
+ read_end = i_size;
+
+ /* Allow reading partial portion of file */
+ if ((flags & KERNEL_PREAD_FLAG_PART) &&
+ (i_size > (pos + max_size)))
+ read_end = pos + max_size;
+
+ alloc_size = read_end - pos;
+ if (i_size > SIZE_MAX || (max_size > 0 && alloc_size > max_size)) {
ret = -EFBIG;
goto out;
}
if (id != READING_FIRMWARE_PREALLOC_BUFFER)
- *buf = vmalloc(i_size);
+ *buf = vmalloc(alloc_size);
if (!*buf) {
ret = -ENOMEM;
goto out;
}
- pos = 0;
- while (pos < i_size) {
- bytes = kernel_read(file, *buf + pos, i_size - pos, &pos);
+ buf_pos = 0;
+ while (pos < read_end) {
+ bytes = kernel_read(file, *buf + buf_pos, read_end - pos, &pos);
if (bytes < 0) {
ret = bytes;
goto out_free;
@@ -941,14 +955,16 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
if (bytes == 0)
break;
+
+ buf_pos += bytes;
}
- if (pos != i_size) {
+ if (pos != read_end) {
ret = -EIO;
goto out_free;
}
- ret = security_kernel_post_read_file(file, *buf, i_size, id);
+ ret = security_kernel_post_read_file(file, *buf, alloc_size, id);
if (!ret)
*size = pos;
@@ -964,10 +980,20 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
allow_write_access(file);
return ret;
}
+EXPORT_SYMBOL_GPL(kernel_pread_file);
+
+int kernel_read_file(struct file *file, void **buf, loff_t *size,
+ loff_t max_size, enum kernel_read_file_id id)
+{
+ return kernel_pread_file(file, buf, size, 0, max_size,
+ KERNEL_PREAD_FLAG_WHOLE, id);
+}
EXPORT_SYMBOL_GPL(kernel_read_file);
-int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
- loff_t max_size, enum kernel_read_file_id id)
+int kernel_pread_file_from_path(const char *path, void **buf,
+ loff_t *size, loff_t pos,
+ loff_t max_size, unsigned int flags,
+ enum kernel_read_file_id id)
{
struct file *file;
int ret;
@@ -979,14 +1005,23 @@ int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
if (IS_ERR(file))
return PTR_ERR(file);
- ret = kernel_read_file(file, buf, size, max_size, id);
+ ret = kernel_pread_file(file, buf, size, pos, max_size, flags, id);
fput(file);
return ret;
}
+EXPORT_SYMBOL_GPL(kernel_pread_file_from_path);
+
+int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
+ loff_t max_size, enum kernel_read_file_id id)
+{
+ return kernel_pread_file_from_path(path, buf, size, 0, max_size,
+ KERNEL_PREAD_FLAG_WHOLE, id);
+}
EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
-int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
- enum kernel_read_file_id id)
+int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size, loff_t pos,
+ loff_t max_size, unsigned int flags,
+ enum kernel_read_file_id id)
{
struct fd f = fdget(fd);
int ret = -EBADF;
@@ -994,11 +1029,19 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
if (!f.file)
goto out;
- ret = kernel_read_file(f.file, buf, size, max_size, id);
+ ret = kernel_pread_file(f.file, buf, size, pos, max_size, flags, id);
out:
fdput(f);
return ret;
}
+EXPORT_SYMBOL_GPL(kernel_pread_file_from_fd);
+
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+ enum kernel_read_file_id id)
+{
+ return kernel_pread_file_from_fd(fd, buf, size, 0, max_size,
+ KERNEL_PREAD_FLAG_WHOLE, id);
+}
EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);
ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3cd4fe6b845e..8f39530cfcc2 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3008,10 +3008,25 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)
return kernel_read_file_str[id];
}
+/* Flags used by kernel_pread_file functions */
+#define KERNEL_PREAD_FLAG_WHOLE 0x0000 /* Only Allow reading of whole file */
+#define KERNEL_PREAD_FLAG_PART 0x0001 /* Allow reading part of file */
+
+extern int kernel_pread_file(struct file *file, void **buf, loff_t *size,
+ loff_t pos, loff_t max_size, unsigned int flags,
+ enum kernel_read_file_id id);
extern int kernel_read_file(struct file *, void **, loff_t *, loff_t,
enum kernel_read_file_id);
+extern int kernel_pread_file_from_path(const char *path, void **buf,
+ loff_t *size, loff_t pos,
+ loff_t max_size, unsigned int flags,
+ enum kernel_read_file_id id);
extern int kernel_read_file_from_path(const char *, void **, loff_t *, loff_t,
enum kernel_read_file_id);
+extern int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size,
+ loff_t pos, loff_t max_size,
+ unsigned int flags,
+ enum kernel_read_file_id id);
extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t,
enum kernel_read_file_id);
extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
--
2.17.1
next prev parent reply other threads:[~2020-02-20 0:48 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-20 0:48 [PATCH v2 0/7] firmware: add partial read support in request_firmware_into_buf Scott Branden
2020-02-20 0:48 ` Scott Branden [this message]
2020-02-20 0:48 ` [PATCH v2 2/7] firmware: add offset to request_firmware_into_buf Scott Branden
2020-02-20 1:22 ` Luis Chamberlain
2020-02-21 0:14 ` Scott Branden
2020-02-20 0:48 ` [PATCH v2 3/7] test_firmware: add partial read support for request_firmware_into_buf Scott Branden
2020-02-20 8:42 ` Dan Carpenter
2020-02-21 18:30 ` Scott Branden
2020-02-22 1:13 ` Scott Branden
2020-02-24 8:08 ` Dan Carpenter
2020-02-25 19:11 ` Luis Chamberlain
2020-02-20 0:48 ` [PATCH v2 4/7] firmware: test partial file reads of request_firmware_into_buf Scott Branden
2020-02-20 0:48 ` [PATCH v2 5/7] bcm-vk: add bcm_vk UAPI Scott Branden
2020-02-20 7:50 ` Greg Kroah-Hartman
2020-02-21 1:15 ` Scott Branden
2020-02-21 8:34 ` Arnd Bergmann
2020-02-21 18:27 ` Scott Branden
2020-02-21 9:22 ` Greg Kroah-Hartman
2020-02-20 0:48 ` [PATCH v2 6/7] misc: bcm-vk: add Broadcom VK driver Scott Branden
2020-02-20 1:04 ` Randy Dunlap
2020-02-21 0:06 ` Scott Branden
2020-02-21 0:21 ` Randy Dunlap
2020-02-20 7:47 ` Greg Kroah-Hartman
2020-02-21 18:19 ` Scott Branden
2020-02-22 8:02 ` Arnd Bergmann
2020-02-23 10:00 ` Greg Kroah-Hartman
2020-02-23 23:55 ` Olof Johansson
2020-02-25 22:37 ` Scott Branden
2020-02-20 10:43 ` Dan Carpenter
2020-02-21 18:29 ` Scott Branden
2020-04-17 21:49 ` Scott Branden
2020-04-18 11:45 ` Dan Carpenter
2020-04-18 11:47 ` Dan Carpenter
2020-04-18 17:25 ` Scott Branden
2020-02-22 16:44 ` kbuild test robot
2020-02-22 16:44 ` [RFC PATCH] misc: bcm-vk: image_tab[] can be static kbuild test robot
2020-02-20 0:48 ` [PATCH v2 7/7] MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver Scott Branden
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=20200220004825.23372-2-scott.branden@broadcom.com \
--to=scott.branden@broadcom.com \
--cc=agross@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=bjorn.andersson@linaro.org \
--cc=colin.king@canonical.com \
--cc=dan.carpenter@oracle.com \
--cc=david.brown@linaro.org \
--cc=gregkh@linuxfoundation.org \
--cc=keescook@chromium.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mcgrof@kernel.org \
--cc=olof@lixom.net \
--cc=rafael@kernel.org \
--cc=shuah@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tiwai@suse.de \
--cc=viro@zeniv.linux.org.uk \
--subject='Re: [PATCH v2 1/7] fs: introduce kernel_pread_file* support' \
/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
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).