Skip to content

Commit 9d6c7f8

Browse files
mssolakdave
authored andcommitted
btrfs: fix double free of qgroup record after failure to add delayed ref head
In the previous code it was possible to incur into a double kfree() scenario when calling add_delayed_ref_head(). This could happen if the record was reported to already exist in the btrfs_qgroup_trace_extent_nolock() call, but then there was an error later on add_delayed_ref_head(). In this case, since add_delayed_ref_head() returned an error, the caller went to free the record. Since add_delayed_ref_head() couldn't set this kfree'd pointer to NULL, then kfree() would have acted on a non-NULL 'record' object which was pointing to memory already freed by the callee. The problem comes from the fact that the responsibility to kfree the object is on both the caller and the callee at the same time. Hence, the fix for this is to shift the ownership of the 'qrecord' object out of the add_delayed_ref_head(). That is, we will never attempt to kfree() the given object inside of this function, and will expect the caller to act on the 'qrecord' object on its own. The only exception where the 'qrecord' object cannot be kfree'd is if it was inserted into the tracing logic, for which we already have the 'qrecord_inserted_ret' boolean to account for this. Hence, the caller has to kfree the object only if add_delayed_ref_head() reports not to have inserted it on the tracing logic. As a side-effect of the above, we must guarantee that 'qrecord_inserted_ret' is properly initialized at the start of the function, not at the end, and then set when an actual insert happens. This way we avoid 'qrecord_inserted_ret' having an invalid value on an early exit. The documentation from the add_delayed_ref_head() has also been updated to reflect on the exact ownership of the 'qrecord' object. Fixes: 6ef8fbc ("btrfs: fix missing error handling when adding delayed ref with qgroups enabled") Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Miquel Sabaté Solà <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 38e17c3 commit 9d6c7f8

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

fs/btrfs/delayed-ref.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,13 @@ static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
798798
}
799799

800800
/*
801-
* helper function to actually insert a head node into the rbtree.
802-
* this does all the dirty work in terms of maintaining the correct
803-
* overall modification count.
801+
* Helper function to actually insert a head node into the xarray. This does all
802+
* the dirty work in terms of maintaining the correct overall modification
803+
* count.
804+
*
805+
* The caller is responsible for calling kfree() on @qrecord. More specifically,
806+
* if this function reports that it did not insert it as noted in
807+
* @qrecord_inserted_ret, then it's safe to call kfree() on it.
804808
*
805809
* Returns an error pointer in case of an error.
806810
*/
@@ -814,7 +818,14 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans,
814818
struct btrfs_delayed_ref_head *existing;
815819
struct btrfs_delayed_ref_root *delayed_refs;
816820
const unsigned long index = (head_ref->bytenr >> fs_info->sectorsize_bits);
817-
bool qrecord_inserted = false;
821+
822+
/*
823+
* If 'qrecord_inserted_ret' is provided, then the first thing we need
824+
* to do is to initialize it to false just in case we have an exit
825+
* before trying to insert the record.
826+
*/
827+
if (qrecord_inserted_ret)
828+
*qrecord_inserted_ret = false;
818829

819830
delayed_refs = &trans->transaction->delayed_refs;
820831
lockdep_assert_held(&delayed_refs->lock);
@@ -833,19 +844,23 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans,
833844

834845
/* Record qgroup extent info if provided */
835846
if (qrecord) {
847+
/*
848+
* Setting 'qrecord' but not 'qrecord_inserted_ret' will likely
849+
* result in a memory leakage.
850+
*/
851+
ASSERT(qrecord_inserted_ret != NULL);
852+
836853
int ret;
837854

838855
ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, qrecord,
839856
head_ref->bytenr);
840857
if (ret) {
841858
/* Clean up if insertion fails or item exists. */
842859
xa_release(&delayed_refs->dirty_extents, index);
843-
/* Caller responsible for freeing qrecord on error. */
844860
if (ret < 0)
845861
return ERR_PTR(ret);
846-
kfree(qrecord);
847-
} else {
848-
qrecord_inserted = true;
862+
} else if (qrecord_inserted_ret) {
863+
*qrecord_inserted_ret = true;
849864
}
850865
}
851866

@@ -888,8 +903,6 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans,
888903
delayed_refs->num_heads++;
889904
delayed_refs->num_heads_ready++;
890905
}
891-
if (qrecord_inserted_ret)
892-
*qrecord_inserted_ret = qrecord_inserted;
893906

894907
return head_ref;
895908
}
@@ -1049,6 +1062,14 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
10491062
xa_release(&delayed_refs->head_refs, index);
10501063
spin_unlock(&delayed_refs->lock);
10511064
ret = PTR_ERR(new_head_ref);
1065+
1066+
/*
1067+
* It's only safe to call kfree() on 'qrecord' if
1068+
* add_delayed_ref_head() has _not_ inserted it for
1069+
* tracing. Otherwise we need to handle this here.
1070+
*/
1071+
if (!qrecord_reserved || qrecord_inserted)
1072+
goto free_head_ref;
10521073
goto free_record;
10531074
}
10541075
head_ref = new_head_ref;
@@ -1071,6 +1092,8 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
10711092

10721093
if (qrecord_inserted)
10731094
return btrfs_qgroup_trace_extent_post(trans, record, generic_ref->bytenr);
1095+
1096+
kfree(record);
10741097
return 0;
10751098

10761099
free_record:

0 commit comments

Comments
 (0)