Skip to content

Commit 4e8d213

Browse files
jryutytso
authored andcommitted
ext4: fix use-after-free in ext4_mb_new_blocks
ext4_mb_put_pa should hold pa->pa_lock before accessing pa->pa_count. While ext4_mb_use_preallocated checks pa->pa_deleted first and then increments pa->count later, ext4_mb_put_pa decrements pa->pa_count before holding pa->pa_lock and then sets pa->pa_deleted. * Free sequence ext4_mb_put_pa (1): atomic_dec_and_test pa->pa_count ext4_mb_put_pa (2): lock pa->pa_lock ext4_mb_put_pa (3): check pa->pa_deleted ext4_mb_put_pa (4): set pa->pa_deleted=1 ext4_mb_put_pa (5): unlock pa->pa_lock ext4_mb_put_pa (6): remove pa from a list ext4_mb_pa_callback: free pa * Use sequence ext4_mb_use_preallocated (1): iterate over preallocation ext4_mb_use_preallocated (2): lock pa->pa_lock ext4_mb_use_preallocated (3): check pa->pa_deleted ext4_mb_use_preallocated (4): increase pa->pa_count ext4_mb_use_preallocated (5): unlock pa->pa_lock ext4_mb_release_context: access pa * Use-after-free sequence [initial status] <pa->pa_deleted = 0, pa_count = 1> ext4_mb_use_preallocated (1): iterate over preallocation ext4_mb_use_preallocated (2): lock pa->pa_lock ext4_mb_use_preallocated (3): check pa->pa_deleted ext4_mb_put_pa (1): atomic_dec_and_test pa->pa_count [pa_count decremented] <pa->pa_deleted = 0, pa_count = 0> ext4_mb_use_preallocated (4): increase pa->pa_count [pa_count incremented] <pa->pa_deleted = 0, pa_count = 1> ext4_mb_use_preallocated (5): unlock pa->pa_lock ext4_mb_put_pa (2): lock pa->pa_lock ext4_mb_put_pa (3): check pa->pa_deleted ext4_mb_put_pa (4): set pa->pa_deleted=1 [race condition!] <pa->pa_deleted = 1, pa_count = 1> ext4_mb_put_pa (5): unlock pa->pa_lock ext4_mb_put_pa (6): remove pa from a list ext4_mb_pa_callback: free pa ext4_mb_release_context: access pa AddressSanitizer has detected use-after-free in ext4_mb_new_blocks Bug report: http://goo.gl/rG1On3 Signed-off-by: Junho Ryu <jayr@google.com> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: stable@vger.kernel.org
1 parent ae1495b commit 4e8d213

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎fs/ext4/mballoc.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,6 +3442,9 @@ static void ext4_mb_pa_callback(struct rcu_head *head)
34423442
{
34433443
struct ext4_prealloc_space *pa;
34443444
pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3445+
3446+
BUG_ON(atomic_read(&pa->pa_count));
3447+
BUG_ON(pa->pa_deleted == 0);
34453448
kmem_cache_free(ext4_pspace_cachep, pa);
34463449
}
34473450

@@ -3455,11 +3458,13 @@ static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
34553458
ext4_group_t grp;
34563459
ext4_fsblk_t grp_blk;
34573460

3458-
if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3459-
return;
3460-
34613461
/* in this short window concurrent discard can set pa_deleted */
34623462
spin_lock(&pa->pa_lock);
3463+
if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3464+
spin_unlock(&pa->pa_lock);
3465+
return;
3466+
}
3467+
34633468
if (pa->pa_deleted == 1) {
34643469
spin_unlock(&pa->pa_lock);
34653470
return;

0 commit comments

Comments
 (0)