Skip to content

Commit 336c650

Browse files
emiel-drkartben
authored andcommitted
fs: ext2: Fix ext2 read buffer overflow
Keep track of the amount of bytes read so no buffer overflow occurs. Signed-off-by: DEVER Emiel <[email protected]>
1 parent 535e347 commit 336c650

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

subsys/fs/ext2/ext2_impl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ ssize_t ext2_inode_read(struct ext2_inode *inode, void *buf, uint32_t offset, si
611611
int rc = 0;
612612
ssize_t read = 0;
613613
uint32_t block_size = inode->i_fs->block_size;
614+
size_t nbytes_to_read = nbytes;
614615

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

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

625626
uint32_t left_on_blk = block_size - block_off;
626627
uint32_t left_in_file = inode->i_size - offset;
627-
size_t to_read = MIN(nbytes, MIN(left_on_blk, left_in_file));
628+
size_t to_read = MIN(nbytes_to_read, MIN(left_on_blk, left_in_file));
628629

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

631632
read += to_read;
633+
nbytes_to_read -= read;
632634
offset += to_read;
633635
}
634636

0 commit comments

Comments
 (0)