fs: btrfs: Fix a possible null-pointer dereference in insert_inline_extent()
PATCH 1/1 fs: btrfs: Fix a possible null-pointer dereference in insert_inline_extent()
In insert_inline_extent(), there is an if statement on line 181 to check whether compressed_pages is NULL:
if (compressed_size && compressed_pages)
When compressed_pages is NULL, compressed_pages is used on line 215:
cpage = compressed_pages[i];
Thus, a possible null-pointer dereference may occur.
To fix this possible bug, compressed_pages is checked on line 214.
This bug is found by a static analysis tool STCheck written by us.
--- fs/btrfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/inode.c +1 -1
@@ -211,7 +211,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,if (compress_type !=BTRFS_COMPRESS_NONE ) {struct page *cpage;int i =0 ; -while (compressed_size >0 ) { +while (compressed_size >0 && compressed_pages) { cpage = compressed_pages[i]; cur_size = min_t(unsigned long, compressed_size,PAGE_SIZE );
Signature · 1 line
2.17.0
Quoted 10 lines · Jia-Ju Bai
In
insert_inline_extent(), there is an if statement on line 181 to check whethercompressed_pagesis NULL:if (compressed_size && compressed_pages)When
compressed_pagesis NULL,compressed_pagesis used on line 215:cpage = compressed_pages[i];Thus, a possible null-pointer dereference may occur.
To fix this possible bug,
compressed_pagesis checked on line 214.
This can only be hit with compressed_size > 0 and compressed_pages != NULL.
It would be better to have an extra ASSERT() to warn developers about the impossible case.
Thanks,
Qu
Quoted 22 lines
This bug is found by a static analysis tool STCheck written by us.
Signed-off-by Jia-Ju Bai <[email protected]>--- fs/btrfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/inode.c +1
-1@@ -211,7 +211,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,if (compress_type !=BTRFS_COMPRESS_NONE ) {struct page *cpage;int i =0 ; -while (compressed_size >0 ) { +while (compressed_size >0 && compressed_pages) { cpage = compressed_pages[i]; cur_size = min_t(unsigned long, compressed_size,PAGE_SIZE );
Quoted 9 lines · Qu Wenruo
Quoted 7 lines · Jia-Ju Bai
In
insert_inline_extent(), there is an if statement on line 181 to check whethercompressed_pagesis NULL:if (compressed_size && compressed_pages)When
compressed_pagesis NULL,compressed_pagesis used on line 215:cpage = compressed_pages[i];
Quoted 7 lines
Quoted 3 lines
Thus, a possible null-pointer dereference may occur.
To fix this possible bug,
compressed_pagesis checked on line 214.This can only be hit with
compressed_size> 0 andcompressed_pages!= NULL.It would be better to have an extra
ASSERT()to warn developers about the impossible case.
Thanks for the reply :)
So I should add ASSERT(compressed_size > 0 & compressed_pages) at the beginning of the function, and remove "if (compressed_size && compressed_pages)"?
Best wishes,
Jia-Ju Bai
Quoted 18 lines · Jia-Ju Bai
Quoted 15 lines · Qu Wenruo
Quoted 10 lines · Jia-Ju Bai
In
insert_inline_extent(), there is an if statement on line 181 to check whethercompressed_pagesis NULL:
if (compressed_size&&compressed_pages)When
compressed_pagesis NULL,compressed_pagesis used on line 215:
cpage = compressed_pages[i];Thus, a possible null-pointer dereference may occur.
To fix this possible bug,
compressed_pagesis checked on line 214.This can only be hit with
compressed_size> 0 andcompressed_pages!= NULL.
Quoted 7 lines
Quoted 2 lines
It would be better to have an extra
ASSERT()to warn developers about the impossible case.Thanks for the reply :)
So I should addASSERT(compressed_size > 0 & compressed_pages)at the beginning of the function, and remove "if (compressed_size&&compressed_pages)"?
My suggestion is, ASSERT((compressed_size >0 && compressed_pages) || (compressed_size == 0 && !compressed_pages))
And keeps the original checks.
Anyway, just a suggestion.
Thanks,
Qu
Quoted 4 lines
Best wishes,
Jia-Ju Bai
Quoted 24 lines · Qu Wenruo
Quoted 15 lines · Jia-Ju Bai
Quoted 14 lines · Qu Wenruo
Quoted 10 lines · Jia-Ju Bai
In
insert_inline_extent(), there is an if statement on line 181 to check whethercompressed_pagesis NULL:
if (compressed_size&&compressed_pages)When
compressed_pagesis NULL,compressed_pagesis used on line 215:
cpage = compressed_pages[i];Thus, a possible null-pointer dereference may occur.
To fix this possible bug,
compressed_pagesis checked on line 214.This can only be hit with
compressed_size> 0 andcompressed_pages!= NULL.Quoted 7 lines
Quoted 2 lines
It would be better to have an extra
ASSERT()to warn developers about the impossible case.Thanks for the reply :)
So I should addASSERT(compressed_size > 0 & compressed_pages)at the beginning of the function, and remove "if (compressed_size&&compressed_pages)"?
Quoted 6 lines
My suggestion is,
ASSERT((compressed_size >0 && compressed_pages) || (compressed_size == 0 && !compressed_pages))And keeps the original checks.
Anyway, just a suggestion.
Agreed, the assertion would be good, covering both cases in one statement at the beginning of the function.