Skip to contentSkip to conversation

btrfs-progs: receive: fix a segfault that free() an err value

PATCH Superseded Wang Yugui 3 participants 4 messages Sep 1, 2022 – Sep 9, 2022 Btrfs file system

lore
Share & tools
lore

Wang Yugui

author
#0 lore raw

PATCH 1/1 btrfs-progs: receive: fix a segfault that free() an err value

I noticed a segfault of 'btrfs receive'.

$ gdb
 #0  process_clone (path=0x23829d0 "after.s1.txt", offset=0, len=2097152, clone_uuid=<optimized out>,
    clone_ctransid=<optimized out>, clone_path=0x2382920 "after.s1.txt", clone_offset=0, user=0x7ffe21985ba0)
    at cmds/receive.c:793
793                     free(si->path);
(gdb) p si
$1 = (struct subvol_info *) 0xfffffffffffffffe

'si' was a ERR value here. so add the check of 'IS_ERR()' before 'free()'.

---
 cmds/receive.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cmds/receive.c +2 -2
index d106e554..cada6343 100644
@@ -789,8 +789,8 @@ static int process_clone(const char *path, u64 offset, u64 len,
 	}
 
 out:
-	if (si) {
-		free(si->path);
+	if (si && !IS_ERR(si)) {
+		if(si->path) free(si->path);
 		free(si);
 	}
 	if (clone_fd != -1)
Signature · 1 line
2.36.2
#1 lore raw
Quoted 10 lines · Wang Yugui

I noticed a segfault of 'btrfs receive'.

$ gdb
  #0  process_clone (path=0x23829d0 "after.s1.txt", offset=0, len=2097152, clone_uuid=<optimized out>,
     clone_ctransid=<optimized out>, clone_path=0x2382920 "after.s1.txt", clone_offset=0, user=0x7ffe21985ba0)
     at cmds/receive.c:793
793                     free(si->path);
(gdb) p si
$1 = (struct subvol_info *) 0xfffffffffffffffe

'si' was a ERR value here. so add the check of 'IS_ERR()' before 'free()'.

The reason looks good to me, but the code doesn't follow our standard.

Quoted 11 lines
---
  cmds/receive.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
cmds/receive.c +0 -0
index d106e554..cada6343 100644

cmds/receive.c static int process_clone(const char *path, u64 offset, u64 len, · line 793

Quoted 7 lines
cmds/receive.c +2 -2
@@ -789,8 +789,8 @@ static int process_clone(const char *path, u64 offset, u64 len,
  	}
 
  out:
-	if (si) {
-		free(si->path);
+	if (si && !IS_ERR(si)) {
+		if(si->path) free(si->path);

Such "if (condition) do_something();" is definitely against the common practice.

Another thing is, that happens for the search failure for "si = subvol_uuid_search();" line.

That's the only way @si can be a error pointer.

What about resetting @si to NULL in the else branch?

Some like this:

si = subvol_uuid_search();

if (IS_ERROR_OR_NULL(si)) {
	if (!si) {
		ret = -ENOENT;
	} else {
		ret = PTR_ERR(si);
		si = NULL;
	}
}

This removes the need to bother if @si is an error pointer or NULL at out tag.

Thanks,
Qu

Quoted 3 lines
cmds/receive.c +0 -0
  		free(si);
  	}
  	if (clone_fd != -1)
author
#2 lore raw

PATCH v2 1/1 btrfs-progs: receive: fix a segfault that free() an err value

In reply to Wang Yugui

I noticed a segfault of 'btrfs receive'.

$ gdb
 #0  process_clone (path=0x23829d0 "after.s1.txt", offset=0, len=2097152, clone_uuid=<optimized out>,
    clone_ctransid=<optimized out>, clone_path=0x2382920 "after.s1.txt", clone_offset=0, user=0x7ffe21985ba0)
    at cmds/receive.c:793
793                     free(si->path);
(gdb) p si
$1 = (struct subvol_info *) 0xfffffffffffffffe

'si' was an ERR value. so add the check of '!IS_ERR_OR_NULL()' before 'free()' just similar to process_snapshot().

---
changes since v1:
let the check similar to process_snapshot().

 cmds/receive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmds/receive.c +1 -1
index aec32458..bf476387 100644
@@ -811,7 +811,7 @@ static int process_clone(const char *path, u64 offset, u64 len,
 	}
 
 out:
-	if (si) {
+	if (!IS_ERR_OR_NULL(si)) {
 		free(si->path);
 		free(si);
 	}
Signature · 1 line
2.36.2
#3 lore raw
Quoted 9 lines · Wang Yugui

I noticed a segfault of 'btrfs receive'.

$ gdb
 #0  process_clone (path=0x23829d0 "after.s1.txt", offset=0, len=2097152, clone_uuid=<optimized out>,
    clone_ctransid=<optimized out>, clone_path=0x2382920 "after.s1.txt", clone_offset=0, user=0x7ffe21985ba0)
    at cmds/receive.c:793
793                     free(si->path);
(gdb) p si
$1 = (struct subvol_info *) 0xfffffffffffffffe
Quoted 4 lines

'si' was an ERR value. so add the check of '!IS_ERR_OR_NULL()' before 'free()' just similar to process_snapshot().

Added to devel, thanks.