Skip to contentSkip to conversation

btrfs-progs: Don't crash when processing a clone request during received

PATCH In review Nikolay Borisov 3 participants 5 messages Jan 10, 2022 – Jan 11, 2022 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: Don't crash when processing a clone request during received

If subvol_uuid_search can't find the clone root then 'si' would either be NULL or contain an errno. The behavior of this function was changed as a result of commit
ac5954ee36a5 ("btrfs-progs: merge subvol_uuid_search helpers"). Before this commit subvol_uuid_search() was a wrapper around subvol_uuid_search2 and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label in process_clone.

Properly handle this new semantic by changing the simple null check to IS_ERR_OR_NULL which covers all possible return value of subvol_uuid_search.

---
 cmds/receive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmds/receive.c +1 -1
index b4099bc482b2..16b9b4a853be 100644
@@ -794,7 +794,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);
 	}

--
2.17.1

#1 lore raw
Quoted 8 lines · Nikolay Borisov

If subvol_uuid_search can't find the clone root then 'si' would either be NULL or contain an errno. The behavior of this function was changed as a result of commit
ac5954ee36a5 ("btrfs-progs: merge subvol_uuid_search helpers"). Before this commit subvol_uuid_search() was a wrapper around subvol_uuid_search2 and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label in process_clone.

Quoted 7 lines

Properly handle this new semantic by changing the simple null check to IS_ERR_OR_NULL which covers all possible return value of subvol_uuid_search.

Qu pointed me to
https://patchwork.kernel.org/project/linux-btrfs/patch/[email protected]/

which is essentially the same patch. So you can drop this.

#2 lore raw
Quoted 20 lines · Nikolay Borisov
Quoted 8 lines · Nikolay Borisov

If subvol_uuid_search can't find the clone root then 'si' would either be NULL or contain an errno. The behavior of this function was changed as a result of commit
ac5954ee36a5 ("btrfs-progs: merge subvol_uuid_search helpers"). Before this commit subvol_uuid_search() was a wrapper around subvol_uuid_search2 and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label in process_clone.

Quoted 7 lines

Properly handle this new semantic by changing the simple null check to IS_ERR_OR_NULL which covers all possible return value of subvol_uuid_search.

Quoted 4 lines

Qu pointed me to
https://patchwork.kernel.org/project/linux-btrfs/patch/[email protected]/

which is essentially the same patch. So you can drop this.

As suggested I've updated the changelog with yours as it's more descriptive, thanks.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Nikolay Borisov
Quoted 8 lines · Nikolay Borisov

If subvol_uuid_search can't find the clone root then 'si' would either be NULL or contain an errno. The behavior of this function was changed as a result of commit
ac5954ee36a5 ("btrfs-progs: merge subvol_uuid_search helpers"). Before this commit subvol_uuid_search() was a wrapper around subvol_uuid_search2 and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label in process_clone.

Quoted 7 lines

Properly handle this new semantic by changing the simple null check to IS_ERR_OR_NULL which covers all possible return value of subvol_uuid_search.

Just curious that, can we refactor the helper to remove the NULL pointer return case?

Thanks,
Qu

Quoted 20 lines
---
  cmds/receive.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cmds/receive.c +1 -2
index b4099bc482b2..16b9b4a853be 100644
@@ -794,7 +794,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);
  	}
--

2.17.1

#4 lore raw
Quoted 3 lines · Qu Wenruo

Just curious that, can we refactor the helper to remove the NULL pointer return case?

Yes, the change is simple enough:

common/send-utils.c +2 -4
index 4ed448b1f306..5002cfbbd0e8 100644
@@ -308,10 +308,8 @@ struct subvol_info *subvol_uuid_search(int mnt_fd,
                goto out;
 
        info = calloc(1, sizeof(*info));
-       if (!info) {
-               ret = -ENOMEM;
-               goto out;
-       }
+       if (!info)
+               return ERR_PTR(-ENOMEM);
        info->root_id = root_id;
        memcpy(info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
        memcpy(info->received_uuid, root_item.received_uuid,

BTRFS_UUID_SIZE);

However this only results in callers doing IS_ERR() rather than IS_ERR_OR_NULL imo not much of a win.