Skip to contentSkip to conversation

btrfs-progs: Improve error handling while loading log root

PATCH In review Nikolay Borisov 2 participants 3 messages Aug 12, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: Improve error handling while loading log root

read_tree_block can return an error due to a variety of reason, currently its return value is not being checked when loading the log root's node but is directly used in a call to extent_buffer_uptodate. This can lead to a crash if read_tree_block errored out, since the node won't be a pointer but an error value cast to a pointer.

Fix this by properly checking the return value of read_tree_block before utilising the value for anything else.

---
 kernel-shared/disk-io.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
kernel-shared/disk-io.c +4 -5
index cc635152c46d..4b5436254671 100644
@@ -629,15 +629,14 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root,
 
 	log_root->node = read_tree_block(fs_info, blocknr,
 				     btrfs_super_generation(disk_super) + 1);
-
-	fs_info->log_root_tree = log_root;
-
-	if (!extent_buffer_uptodate(log_root->node)) {
-		free_extent_buffer(log_root->node);
+	if (IS_ERR(log_root->node) || !extent_buffer_uptodate(log_root->node)) {
+		if (!IS_ERR(log_root->node))
+			free_extent_buffer(log_root->node);
 		free(log_root);
 		fs_info->log_root_tree = NULL;
 		return -EIO;
 	}
+	fs_info->log_root_tree = log_root;
 
 	return 0;
 }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 18 lines · Nikolay Borisov

read_tree_block can return an error due to a variety of reason, currently its return value is not being checked when loading the log root's node but is directly used in a call to extent_buffer_uptodate. This can lead to a crash if read_tree_block errored out, since the node won't be a pointer but an error value cast to a pointer.

Fix this by properly checking the return value of read_tree_block before utilising the value for anything else.

---
  kernel-shared/disk-io.c | 9 ++++-----
  1 file changed, 4 insertions(+), 5 deletions(-)
kernel-shared/disk-io.c +0 -0
index cc635152c46d..4b5436254671 100644

kernel-shared/disk-io.c static int find_and_setup_log_root(struct btrfs_root *tree_root, · line 632

Quoted 9 lines
kernel-shared/disk-io.c +1 -5
@@ -629,15 +629,14 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root,
 
  	log_root->node = read_tree_block(fs_info, blocknr,
  				     btrfs_super_generation(disk_super) + 1);
-
-	fs_info->log_root_tree = log_root;
-
-	if (!extent_buffer_uptodate(log_root->node)) {
-		free_extent_buffer(log_root->node);
+	if (IS_ERR(log_root->node) || !extent_buffer_uptodate(log_root->node)) {

extent_buffer_uptodate() already has check for IS_ERROR().

Thus the existing check is already good.

kernel-shared/disk-io.c static int find_and_setup_log_root(struct btrfs_root *tree_root, · line 634

Quoted 2 lines
kernel-shared/disk-io.c +2 -0
+		if (!IS_ERR(log_root->node))
+			free_extent_buffer(log_root->node);

The same for free_extent_buffer();

Thanks,
Qu

Quoted 9 lines
kernel-shared/disk-io.c +1 -0
  		free(log_root);
  		fs_info->log_root_tree = NULL;
  		return -EIO;
  	}
+	fs_info->log_root_tree = log_root;
 
  	return 0;
  }
#2 lore raw
Quoted 35 lines · Qu Wenruo
Quoted 19 lines · Nikolay Borisov

read_tree_block can return an error due to a variety of reason, currently its return value is not being checked when loading the log root's node but is directly used in a call to extent_buffer_uptodate. This can lead to a crash if read_tree_block errored out, since the node won't be a pointer but an error value cast to a pointer.

Fix this by properly checking the return value of read_tree_block before utilising the value for anything else.
---
  kernel-shared/disk-io.c | 9 ++++-----

  1 file changed, 4 insertions(+), 5 deletions(-)
kernel-shared/disk-io.c +0 -0
index cc635152c46d..4b5436254671 100644
@@ -629,15 +629,14 @@ static int find_and_setup_log_root(struct
 btrfs_root *tree_root,

kernel-shared/disk-io.c static int find_and_setup_log_root(struct · line 634

Quoted 10 lines
kernel-shared/disk-io.c +1 -5
 
       log_root->node = read_tree_block(fs_info, blocknr,
                        btrfs_super_generation(disk_super) + 1);
-
-    fs_info->log_root_tree = log_root;
-
-    if (!extent_buffer_uptodate(log_root->node)) {
-        free_extent_buffer(log_root->node);
+    if (IS_ERR(log_root->node) ||
 !extent_buffer_uptodate(log_root->node)) {

extent_buffer_uptodate() already has check for IS_ERROR().

Quoted 6 lines

Thus the existing check is already good.

kernel-shared/disk-io.c static int find_and_setup_log_root(struct btrfs_root *tree_root, · line 634

Quoted 2 lines
kernel-shared/disk-io.c +2 -0
+        if (!IS_ERR(log_root->node))
+            free_extent_buffer(log_root->node);

The same for free_extent_buffer();

You are right, so indeed it's not an issue but this is somewhat hidden. So this patch can be ignored.

Quoted 13 lines

Thanks,
Qu

Quoted 9 lines
kernel-shared/disk-io.c +1 -0
           free(log_root);
           fs_info->log_root_tree = NULL;
           return -EIO;
       }
+    fs_info->log_root_tree = log_root;
 

      return 0;
  }