Skip to content

Commit 83d060c

Browse files
AstralBobagruenba
authored andcommitted
gfs2: fix use-after-free on transaction ail lists
Before this patch, transactions could be merged into the system transaction by function gfs2_merge_trans(), but the transaction ail lists were never merged. Because the ail flushing mechanism can run separately, bd elements can be attached to the transaction's buffer list during the transaction (trans_add_meta, etc) but quickly moved to its ail lists. Later, in function gfs2_trans_end, the transaction can be freed (by gfs2_trans_end) while it still has bd elements queued to its ail lists, which can cause it to either lose track of the bd elements altogether (memory leak) or worse, reference the bd elements after the parent transaction has been freed. Although I've not seen any serious consequences, the problem becomes apparent with the previous patch's addition of: gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); to function gfs2_trans_free(). This patch adds logic into gfs2_merge_trans() to move the merged transaction's ail lists to the sdp transaction. This prevents the use-after-free. To do this properly, we need to hold the ail lock, so we pass sdp into the function instead of the transaction itself. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
1 parent b839dad commit 83d060c

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎fs/gfs2/log.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,10 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
10181018
* @new: New transaction to be merged
10191019
*/
10201020

1021-
static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
1021+
static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
10221022
{
1023+
struct gfs2_trans *old = sdp->sd_log_tr;
1024+
10231025
WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
10241026

10251027
old->tr_num_buf_new += new->tr_num_buf_new;
@@ -1031,6 +1033,11 @@ static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
10311033

10321034
list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
10331035
list_splice_tail_init(&new->tr_buf, &old->tr_buf);
1036+
1037+
spin_lock(&sdp->sd_ail_lock);
1038+
list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
1039+
list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
1040+
spin_unlock(&sdp->sd_ail_lock);
10341041
}
10351042

10361043
static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
@@ -1042,7 +1049,7 @@ static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
10421049
gfs2_log_lock(sdp);
10431050

10441051
if (sdp->sd_log_tr) {
1045-
gfs2_merge_trans(sdp->sd_log_tr, tr);
1052+
gfs2_merge_trans(sdp, tr);
10461053
} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
10471054
gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
10481055
sdp->sd_log_tr = tr;

0 commit comments

Comments
 (0)