ext4: fix invalid free tracking in ext4_xattr_move_to_block()
Theodore Ts'o
maintainerPATCH 1/1 ext4: fix invalid free tracking in ext4_xattr_move_to_block()
In ext4_xattr_move_to_block(), the value of the extended attribute which we need to move to an external block may be allocated by kvmalloc() if the value is stored in an external inode. So at the end of the function the code tried to check if this was the case by testing entry->e_value_inum.
However, at this point, the pointer to the xattr entry is no longer valid, because it was removed from the original location where it had been stored. So we could end up calling kvfree() on a pointer which was not allocated by kvmalloc(); or we could also potentially leak memory by not freeing the buffer when it should be freed. Fix this by storing whether it should be freed in a separate variable.
--- fs/ext4/xattr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ext4/xattr.c +3 -2
@@ -2615,6 +2615,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, .in_inode = !!entry->e_value_inum, };struct ext4_xattr_ibody_header *header =IHDR (inode, raw_inode); +int needs_kvfree =0 ;int error; is =kzalloc (sizeof (struct ext4_xattr_ibody_find ),GFP_NOFS );
@@ -2637,7 +2638,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, error = -ENOMEM ; goto out; } - + needs_kvfree =1 ; error =ext4_xattr_inode_get (inode, entry, buffer, value_size);if (error) goto out;
@@ -2676,7 +2677,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, out:kfree (b_entry_name); -if (entry->e_value_inum && buffer) +if (needs_kvfree && buffer)kvfree (buffer);if (is)brelse (is->iloc.bh);
Signature · 1 line
2.31.0
Quoted 44 lines · Theodore Ts'o
In
ext4_xattr_move_to_block(), the value of the extended attribute which we need to move to an external block may be allocated bykvmalloc()if the value is stored in an external inode. So at the end of the function the code tried to check if this was the case by testingentry->e_value_inum.However, at this point, the pointer to the xattr entry is no longer valid, because it was removed from the original location where it had been stored. So we could end up calling
kvfree()on a pointer which was not allocated bykvmalloc(); or we could also potentially leak memory by not freeing the buffer when it should be freed. Fix this by storing whether it should be freed in a separate variable.Reported-by [email protected]Reported-by [email protected]Signed-off-by Theodore Ts'o <[email protected]>--- fs/ext4/xattr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)fs/ext4/xattr.c +2
-1@@ -2615,6 +2615,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, .in_inode = !!entry->e_value_inum, };struct ext4_xattr_ibody_header *header =IHDR (inode, raw_inode); +int needs_kvfree =0 ;int error; is =kzalloc (sizeof (struct ext4_xattr_ibody_find ),GFP_NOFS );@@ -2637,7 +2638,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, error = -ENOMEM ; goto out; } - + needs_kvfree =1 ; error =ext4_xattr_inode_get (inode, entry, buffer, value_size);if (error) goto out;
fs/ext4/xattr.c static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, · line 2680
Quoted 5 lines
fs/ext4/xattr.c +1
-1@@ -2676,7 +2677,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, out:kfree (b_entry_name); -if (entry->e_value_inum && buffer) +if (needs_kvfree && buffer)
If buffer is null, no operation should be performed, so we may get rid of the buffer check.
We should also add the Fixes tag and Cc: [email protected], as the blamed commit fixes another bug and it was backported to 4.14+, thus this one needs to be backported as well.
Anyway:
Cheers,
ta
Quoted 3 lines
fs/ext4/xattr.c +0
-0kvfree (buffer);if (is)brelse (is->iloc.bh);
Quoted 48 lines · Tudor Ambarus
Quoted 45 lines · Theodore Ts'o
In
ext4_xattr_move_to_block(), the value of the extended attribute which we need to move to an external block may be allocated bykvmalloc()if the value is stored in an external inode. So at the end of the function the code tried to check if this was the case by testingentry->e_value_inum.However, at this point, the pointer to the xattr entry is no longer valid, because it was removed from the original location where it had been stored. So we could end up calling
kvfree()on a pointer which was not allocated bykvmalloc(); or we could also potentially leak memory by not freeing the buffer when it should be freed. Fix this by storing whether it should be freed in a separate variable.Reported-by [email protected]Reported-by [email protected]Signed-off-by Theodore Ts'o <[email protected]>--- fs/ext4/xattr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)fs/ext4/xattr.c +2
-1@@ -2615,6 +2615,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, .in_inode = !!entry->e_value_inum, };struct ext4_xattr_ibody_header *header =IHDR (inode, raw_inode); +int needs_kvfree =0 ;int error; is =kzalloc (sizeof (struct ext4_xattr_ibody_find ),GFP_NOFS );@@ -2637,7 +2638,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, error = -ENOMEM ; goto out; } - + needs_kvfree =1 ; error =ext4_xattr_inode_get (inode, entry, buffer, value_size);if (error) goto out;@@ -2676,7 +2677,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
Quoted 8 lines
fs/ext4/xattr.c static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, · line 2680
Quoted 4 lines
fs/ext4/xattr.c +1
-1out:kfree (b_entry_name); -if (entry->e_value_inum && buffer) +if (needs_kvfree && buffer)If buffer is null, no operation should be performed, so we may get rid of the buffer check.
I meant that if buffer is null no operation is performed for kvfree(buffer). Sent a patch on top of yours at:
https://lore.kernel.org/linux-ext4/[email protected]/T/#u
Cheers,
ta
Quoted 14 lines
We should also add the Fixes tag and Cc: [email protected], as the blamed commit fixes another bug and it was backported to 4.14+, thus this one needs to be backported as well.
Fixes 1e9d62d25281 ("ext4: optimize ea_inode block expansion")Anyway:
Reviewed-by Tudor Ambarus <[email protected]>Cheers,
taQuoted 3 lines
fs/ext4/xattr.c +0
-0kvfree (buffer);if (is)brelse (is->iloc.bh);
Quoted 13 lines · Theodore Ts'o
In
ext4_xattr_move_to_block(), the value of the extended attribute which we need to move to an external block may be allocated bykvmalloc()if the value is stored in an external inode. So at the end of the function the code tried to check if this was the case by testingentry->e_value_inum.However, at this point, the pointer to the xattr entry is no longer valid, because it was removed from the original location where it had been stored. So we could end up calling
kvfree()on a pointer which was not allocated bykvmalloc(); or we could also potentially leak memory by not freeing the buffer when it should be freed. Fix this by storing whether it should be freed in a separate variable.
Quoted 5 lines
Good spotting. Feel free to add:
Honza
Quoted 37 lines
--- fs/ext4/xattr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)fs/ext4/xattr.c +3
-2@@ -2615,6 +2615,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, .in_inode = !!entry->e_value_inum, };struct ext4_xattr_ibody_header *header =IHDR (inode, raw_inode); +int needs_kvfree =0 ;int error; is =kzalloc (sizeof (struct ext4_xattr_ibody_find ),GFP_NOFS );@@ -2637,7 +2638,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, error = -ENOMEM ; goto out; } - + needs_kvfree =1 ; error =ext4_xattr_inode_get (inode, entry, buffer, value_size);if (error) goto out;@@ -2676,7 +2677,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, out:kfree (b_entry_name); -if (entry->e_value_inum && buffer) +if (needs_kvfree && buffer)kvfree (buffer);if (is)brelse (is->iloc.bh);Signature · 1 line
2.31.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR