From: Marek Vasut <marex@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] fs: ext4: Prevent erasing buffer past file size
Date: Mon, 23 Jul 2018 11:42:12 +0200 [thread overview]
Message-ID: <20180723094212.3031-1-marex@denx.de> (raw)
The variable 'n' represents the number of bytes to be read from a certain
offset in a file, to a certain offset in buffer 'buf'. The variable 'len'
represents the length of the entire file, clamped correctly to avoid any
overflows.
Therefore, comparing 'n' and 'len' to determine whether clearing 'n'
bytes of the buffer 'buf' at a certain offset would clear data past
buffer 'buf' cannot lead to a correct result, since the 'n' does not
contain the offset from the beginning of the file.
This patch keeps track of the amount of data read and checks for the
buffer overflow by comparing the 'n' to the remaining amount of data
to be read instead.
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Ian Ray <ian.ray@ge.com>
Cc: Martyn Welch <martyn.welch@collabora.co.uk>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Fixes: ecdfb4195b20 ("ext4: recover from filesystem corruption when reading")
---
fs/ext4/ext4fs.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index 2a28031d14..537aa05eff 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -60,6 +60,7 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
lbaint_t delayed_extent = 0;
lbaint_t delayed_skipfirst = 0;
lbaint_t delayed_next = 0;
+ lbaint_t read_total = 0;
char *delayed_buf = NULL;
short status;
@@ -140,13 +141,14 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
return -1;
previous_block_number = -1;
}
- /* Zero no more than `len' bytes. */
+ /* Zero no more than 'filesize' bytes. */
n = blocksize - skipfirst;
- if (n > len)
- n = len;
+ if (n > (len - read_total))
+ n = (len - read_total);
memset(buf, 0, n);
}
buf += blocksize - skipfirst;
+ read_total += blocksize - skipfirst;
}
if (previous_block_number != -1) {
/* spill */
--
2.16.2
next reply other threads:[~2018-07-23 9:42 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-23 9:42 Marek Vasut [this message]
2018-07-23 12:30 ` [U-Boot] [PATCH] fs: ext4: Prevent erasing buffer past file size Stefano Babic
2018-07-23 14:09 ` Tom Rini
2018-07-23 21:32 ` Marek Vasut
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=20180723094212.3031-1-marex@denx.de \
--to=marex@denx.de \
--cc=u-boot@lists.denx.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.