Skip to content

Commit 36cd2bd

Browse files
writeback: avoid use-after-free after removing device
jira VULN-6836 cve CVE-2024-0562 commit-author Khazhismel Kumykov <[email protected]> commit f87904c When a disk is removed, bdi_unregister gets called to stop further writeback and wait for associated delayed work to complete. However, wb_inode_writeback_end() may schedule bandwidth estimation dwork after this has completed, which can result in the timer attempting to access the just freed bdi_writeback. Fix this by checking if the bdi_writeback is alive, similar to when scheduling writeback work. Since this requires wb->work_lock, and wb_inode_writeback_end() may get called from interrupt, switch wb->work_lock to an irqsafe lock. Link: https://lkml.kernel.org/r/[email protected] Fixes: 45a2966 ("writeback: fix bandwidth estimate for spiky workload") Signed-off-by: Khazhismel Kumykov <[email protected]> Reviewed-by: Jan Kara <[email protected]> Cc: Michael Stapelberg <[email protected]> Cc: Wu Fengguang <[email protected]> Cc: Alexander Viro <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> (cherry picked from commit f87904c) Signed-off-by: Pratham Patel <[email protected]>
1 parent 5d5ceb8 commit 36cd2bd

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

fs/fs-writeback.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ static bool inode_io_list_move_locked(struct inode *inode,
133133

134134
static void wb_wakeup(struct bdi_writeback *wb)
135135
{
136-
spin_lock_bh(&wb->work_lock);
136+
spin_lock_irq(&wb->work_lock);
137137
if (test_bit(WB_registered, &wb->state))
138138
mod_delayed_work(bdi_wq, &wb->dwork, 0);
139-
spin_unlock_bh(&wb->work_lock);
139+
spin_unlock_irq(&wb->work_lock);
140140
}
141141

142142
static void finish_writeback_work(struct bdi_writeback *wb,
@@ -163,15 +163,15 @@ static void wb_queue_work(struct bdi_writeback *wb,
163163
if (work->done)
164164
atomic_inc(&work->done->cnt);
165165

166-
spin_lock_bh(&wb->work_lock);
166+
spin_lock_irq(&wb->work_lock);
167167

168168
if (test_bit(WB_registered, &wb->state)) {
169169
list_add_tail(&work->list, &wb->work_list);
170170
mod_delayed_work(bdi_wq, &wb->dwork, 0);
171171
} else
172172
finish_writeback_work(wb, work);
173173

174-
spin_unlock_bh(&wb->work_lock);
174+
spin_unlock_irq(&wb->work_lock);
175175
}
176176

177177
/**
@@ -2105,13 +2105,13 @@ static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
21052105
{
21062106
struct wb_writeback_work *work = NULL;
21072107

2108-
spin_lock_bh(&wb->work_lock);
2108+
spin_lock_irq(&wb->work_lock);
21092109
if (!list_empty(&wb->work_list)) {
21102110
work = list_entry(wb->work_list.next,
21112111
struct wb_writeback_work, list);
21122112
list_del_init(&work->list);
21132113
}
2114-
spin_unlock_bh(&wb->work_lock);
2114+
spin_unlock_irq(&wb->work_lock);
21152115
return work;
21162116
}
21172117

mm/backing-dev.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ void wb_wakeup_delayed(struct bdi_writeback *wb)
273273
unsigned long timeout;
274274

275275
timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
276-
spin_lock_bh(&wb->work_lock);
276+
spin_lock_irq(&wb->work_lock);
277277
if (test_bit(WB_registered, &wb->state))
278278
queue_delayed_work(bdi_wq, &wb->dwork, timeout);
279-
spin_unlock_bh(&wb->work_lock);
279+
spin_unlock_irq(&wb->work_lock);
280280
}
281281

282282
static void wb_update_bandwidth_workfn(struct work_struct *work)
@@ -361,12 +361,12 @@ static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
361361
static void wb_shutdown(struct bdi_writeback *wb)
362362
{
363363
/* Make sure nobody queues further work */
364-
spin_lock_bh(&wb->work_lock);
364+
spin_lock_irq(&wb->work_lock);
365365
if (!test_and_clear_bit(WB_registered, &wb->state)) {
366-
spin_unlock_bh(&wb->work_lock);
366+
spin_unlock_irq(&wb->work_lock);
367367
return;
368368
}
369-
spin_unlock_bh(&wb->work_lock);
369+
spin_unlock_irq(&wb->work_lock);
370370

371371
cgwb_remove_from_bdi_list(wb);
372372
/*

mm/page-writeback.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,7 @@ static void wb_inode_writeback_start(struct bdi_writeback *wb)
27692769

27702770
static void wb_inode_writeback_end(struct bdi_writeback *wb)
27712771
{
2772+
unsigned long flags;
27722773
atomic_dec(&wb->writeback_inodes);
27732774
/*
27742775
* Make sure estimate of writeback throughput gets updated after
@@ -2777,7 +2778,10 @@ static void wb_inode_writeback_end(struct bdi_writeback *wb)
27772778
* that if multiple inodes end writeback at a similar time, they get
27782779
* batched into one bandwidth update.
27792780
*/
2780-
queue_delayed_work(bdi_wq, &wb->_rh->bw_dwork, BANDWIDTH_INTERVAL);
2781+
spin_lock_irqsave(&wb->work_lock, flags);
2782+
if (test_bit(WB_registered, &wb->state))
2783+
queue_delayed_work(bdi_wq, &wb->_rh->bw_dwork, BANDWIDTH_INTERVAL);
2784+
spin_unlock_irqrestore(&wb->work_lock, flags);
27812785
}
27822786

27832787
int test_clear_page_writeback(struct page *page)

0 commit comments

Comments
 (0)