btrfs-progs: Don't crash when processing a clone request during received
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 commitac5954ee36a5 ("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
@@ -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
Quoted 8 lines · Nikolay Borisov
If
subvol_uuid_searchcan'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 commitac5954ee36a5("btrfs-progs: mergesubvol_uuid_searchhelpers"). Before this commitsubvol_uuid_search()was a wrapper aroundsubvol_uuid_search2and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label inprocess_clone.
Quoted 7 lines
Properly handle this new semantic by changing the simple null check to
IS_ERR_OR_NULLwhich covers all possible return value ofsubvol_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.
Quoted 20 lines · Nikolay Borisov
Quoted 8 lines · Nikolay Borisov
If
subvol_uuid_searchcan'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 commitac5954ee36a5("btrfs-progs: mergesubvol_uuid_searchhelpers"). Before this commitsubvol_uuid_search()was a wrapper aroundsubvol_uuid_search2and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label inprocess_clone.Quoted 7 lines
Properly handle this new semantic by changing the simple null check to
IS_ERR_OR_NULLwhich covers all possible return value ofsubvol_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.
Quoted 8 lines · Nikolay Borisov
If
subvol_uuid_searchcan'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 commitac5954ee36a5("btrfs-progs: mergesubvol_uuid_searchhelpers"). Before this commitsubvol_uuid_search()was a wrapper aroundsubvol_uuid_search2and it guaranteed to either return well-fromed 'si' or NULL. This was sufficient for the check after the out label inprocess_clone.
Quoted 7 lines
Properly handle this new semantic by changing the simple null check to
IS_ERR_OR_NULLwhich covers all possible return value ofsubvol_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@@ -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
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
@@ -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.