Skip to contentSkip to conversation

fs: btrfs: Fix a possible null-pointer dereference in insert_inline_extent()

PATCH In review Jia-Ju Bai 3 participants 5 messages Jul 24, 2019 – Jul 26, 2019 Btrfs file system

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

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
index 1af069a9a0c7..19182272fbd8 100644
@@ -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
#1 lore raw
Quoted 10 lines · Jia-Ju Bai

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 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.

---
 fs/btrfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/inode.c +1 -1
index 1af069a9a0c7..19182272fbd8 100644
@@ -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);
author
#2 lore raw
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 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];
Quoted 7 lines
Quoted 3 lines

Thus, a possible null-pointer dereference may occur.

To fix this possible bug, compressed_pages is 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 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

#3 lore raw
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 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 can only be hit with compressed_size > 0 and compressed_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 add ASSERT(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

#4 lore raw
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 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 can only be hit with compressed_size > 0 and compressed_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 add ASSERT(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.