Skip to content

Commit

Permalink
fs: ext2: Fix ext2 read buffer overflow
Browse files Browse the repository at this point in the history
Keep track of the amount of bytes read so no buffer overflow occurs.

Signed-off-by: DEVER Emiel <[email protected]>
(cherry picked from commit 336c650)
  • Loading branch information
emiel-dr authored and github-actions[bot] committed Dec 16, 2024
1 parent c52903c commit 5a0c1e5
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion subsys/fs/ext2/ext2_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ ssize_t ext2_inode_read(struct ext2_inode *inode, void *buf, uint32_t offset, si
int rc = 0;
ssize_t read = 0;
uint32_t block_size = inode->i_fs->block_size;
size_t nbytes_to_read = nbytes;

while (read < nbytes && offset < inode->i_size) {

Expand All @@ -624,11 +625,12 @@ ssize_t ext2_inode_read(struct ext2_inode *inode, void *buf, uint32_t offset, si

uint32_t left_on_blk = block_size - block_off;
uint32_t left_in_file = inode->i_size - offset;
size_t to_read = MIN(nbytes, MIN(left_on_blk, left_in_file));
size_t to_read = MIN(nbytes_to_read, MIN(left_on_blk, left_in_file));

memcpy((uint8_t *)buf + read, inode_current_block_mem(inode) + block_off, to_read);

read += to_read;
nbytes_to_read -= read;
offset += to_read;
}

Expand Down

0 comments on commit 5a0c1e5

Please sign in to comment.