reiserfs : fix improper free in reiserfs_get_block
PATCH 1/1 reiserfs : fix improper free in reiserfs_get_block
From: Tom Rix <[email protected]>
clang static analysis flags this error
inode.c:1083:5: warning: Argument to kfree() is the address of the
local variable 'unf_single', which is not memory allocated by malloc() [unix.Malloc]
kfree(un);
^~~~~~~~~Assignment of 'un'
/*
* We use this in case we need to allocate
* only one block which is a fastpath
*/
unp_t unf_single = 0;
...
if (blocks_needed == 1) {
un = &unf_single;
} else {
un = kcalloc(min(blocks_needed, max_to_insert),
UNFM_P_SIZE, GFP_NOFS);
if (!un) {
un = &unf_single;
blocks_needed = 1;
max_to_insert = 0;
}
}The logic to free 'un'
if (blocks_needed != 1) kfree(un);
Because the kcalloc failure falls back to using unf_single, the if-check for the free is wrong.
So improve the check.
--- fs/reiserfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/reiserfs/inode.c +1 -1
@@ -1079,7 +1079,7 @@ int reiserfs_get_block(struct inode *inode, sector_t block,UNFM_P_SIZE * blocks_needed); -if (blocks_needed !=1 ) +if (un != &unf_single)kfree (un);if (retval) {
Signature · 1 line
2.18.1
Quoted 31 lines · On Tue, Jul 14, 2020 at 06:05:09AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this error
inode.c:1083:5: warning: Argument to
kfree()is the address of the
local variable 'unf_single', which is not memory allocated bymalloc()[unix.Malloc]kfree(un); ^~~~~~~~~Assignment of 'un'
/* * We use this in case we need to allocate * only one block which is a fastpath */ unp_t unf_single = 0; ... if (blocks_needed == 1) { un = &unf_single; } else { un = kcalloc(min(blocks_needed, max_to_insert), UNFM_P_SIZE, GFP_NOFS); if (!un) { un = &unf_single; blocks_needed = 1; max_to_insert = 0; } }
Quoted 7 lines
The logic to free 'un'
if (blocks_needed != 1) kfree(un);Because the kcalloc failure falls back to using
unf_single, the if-check for the free is wrong.
I think you mean "Because clang's static analysis is limited, it warns incorrectly about this". There's no path to get to the kfree with blocks_needed != 1 and un being equal to &unf_single.
Quoted 12 lines
So improve the check.
Signed-off-by Tom Rix <[email protected]>--- fs/reiserfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/reiserfs/inode.c +0
-0
fs/reiserfs/inode.c int reiserfs_get_block(struct inode *inode, sector_t block, · line 1083
Quoted 6 lines
fs/reiserfs/inode.c +1
-1@@ -1079,7 +1079,7 @@ int reiserfs_get_block(struct inode *inode, sector_t block,UNFM_P_SIZE * blocks_needed); -if (blocks_needed !=1 ) +if (un != &unf_single)kfree (un);
I don't actually object to this patch, but your analysis of clang's analysis is wrong.
Quoted 34 lines · Matthew Wilcox
Quoted 33 lines · On Tue, Jul 14, 2020 at 06:05:09AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this error
inode.c:1083:5: warning: Argument to
kfree()is the address of the
local variable 'unf_single', which is not memory allocated bymalloc()[unix.Malloc]kfree(un); ^~~~~~~~~Assignment of 'un'
/* * We use this in case we need to allocate * only one block which is a fastpath */ unp_t unf_single = 0; ... if (blocks_needed == 1) { un = &unf_single; } else { un = kcalloc(min(blocks_needed, max_to_insert), UNFM_P_SIZE, GFP_NOFS); if (!un) { un = &unf_single; blocks_needed = 1; max_to_insert = 0; } }The logic to free 'un'
Quoted 8 lines
Quoted 5 lines
if (blocks_needed != 1) kfree(un);Because the kcalloc failure falls back to using
unf_single, the if-check for the free is wrong.I think you mean "Because clang's static analysis is limited, it warns incorrectly about this". There's no path to get to the kfree with
blocks_needed!= 1 and un being equal to&unf_single.
Ok.
Quoted 21 lines
Quoted 12 lines
So improve the check.
Signed-off-by Tom Rix <[email protected]>--- fs/reiserfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/reiserfs/inode.c +0
-0fs/reiserfs/inode.c int reiserfs_get_block(struct inode *inode, sector_t block, · line 1083
Quoted 6 lines
fs/reiserfs/inode.c +1
-1@@ -1079,7 +1079,7 @@ int reiserfs_get_block(struct inode *inode, sector_t block,UNFM_P_SIZE * blocks_needed); -if (blocks_needed !=1 ) +if (un != &unf_single)kfree (un);I don't actually object to this patch, but your analysis of clang's analysis is wrong.
Quoted 39 lines · Tom Rix
Quoted 37 lines · Matthew Wilcox
Quoted 36 lines · On Tue, Jul 14, 2020 at 06:05:09AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this error
inode.c:1083:5: warning: Argument to
kfree()is the address of the
local variable 'unf_single', which is not memory allocated bymalloc()[unix.Malloc]kfree(un); ^~~~~~~~~Assignment of 'un'
/* * We use this in case we need to allocate * only one block which is a fastpath */ unp_t unf_single = 0; ... if (blocks_needed == 1) { un = &unf_single; } else { un = kcalloc(min(blocks_needed, max_to_insert), UNFM_P_SIZE, GFP_NOFS); if (!un) { un = &unf_single; blocks_needed = 1; max_to_insert = 0; } }The logic to free 'un'
if (blocks_needed != 1) kfree(un);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Because the kcalloc failure falls back to using
unf_single, the if-check for the free is wrong.I think you mean "Because clang's static analysis is limited, it warns incorrectly about this". There's no path to get to the kfree with
blocks_needed!= 1 and un being equal to&unf_single.Ok.
I agree with Matthew the patch will make the code more obviously correct so it's a sensible cleanup. But the changelog needs to redone to reflect this is just a cleanup before the patch can be merged.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 44 lines · Jan Kara
Quoted 43 lines · Tom Rix
Quoted 42 lines · Matthew Wilcox
Quoted 31 lines · On Tue, Jul 14, 2020 at 06:05:09AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this error
inode.c:1083:5: warning: Argument to
kfree()is the address of the
local variable 'unf_single', which is not memory allocated bymalloc()[unix.Malloc]kfree(un); ^~~~~~~~~Assignment of 'un'
/* * We use this in case we need to allocate * only one block which is a fastpath */ unp_t unf_single = 0; ... if (blocks_needed == 1) { un = &unf_single; } else { un = kcalloc(min(blocks_needed, max_to_insert), UNFM_P_SIZE, GFP_NOFS); if (!un) { un = &unf_single; blocks_needed = 1; max_to_insert = 0; } }Quoted 7 lines
The logic to free 'un'
if (blocks_needed != 1) kfree(un);Because the kcalloc failure falls back to using
unf_single, the if-check for the free is wrong.I think you mean "Because clang's static analysis is limited, it warns incorrectly about this". There's no path to get to the kfree with
blocks_needed!= 1 and un being equal to&unf_single.
Quoted 6 lines
Quoted 1 line
Ok.
I agree with Matthew the patch will make the code more obviously correct so it's a sensible cleanup. But the changelog needs to redone to reflect this is just a cleanup before the patch can be merged.
Honza
I am going to look into the problem with the analyzer because that is where the fix should go.
If the problem isn't resolvable, i will loop back to this clean up.
Tom