fix possible memleak in '__blk_trace_remove'
Subsystems (2)
cover fix possible memleak in '__blk_trace_remove'
Ye Bin (3):
blktrace: introduce 'blk_trace_swicth_state' helper
blktrace: fix possible memleak in '__blk_trace_remove'
blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
kernel/trace/blktrace.c | 84 ++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 43 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/3 blktrace: introduce 'blk_trace_swicth_state' helper
Introduce 'blk_trace_swicth_state' helper. No functional changed.
--- kernel/trace/blktrace.c | 74 ++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 38 deletions(-)
kernel/trace/blktrace.c +36 -38
@@ -353,6 +353,39 @@ static void blk_trace_cleanup(struct request_queue *q, struct blk_trace *bt)put_probe_ref (); } +static int blk_trace_switch_state (struct blk_trace *bt,int start) +{ +/* +* For starting a trace, we can transition from a setup or stopped +* trace. For stopping a trace, the state must be running +*/ +if (start) { +if (bt->trace_state == Blktrace_setup || + bt->trace_state == Blktrace_stopped) { + blktrace_seq++; +smp_mb (); + bt->trace_state = Blktrace_running; +raw_spin_lock_irq (&running_trace_lock); +list_add (&bt->running_list, &running_trace_list); +raw_spin_unlock_irq (&running_trace_lock); + +trace_note_time (bt); +return 0 ; + } + }else { +if (bt->trace_state == Blktrace_running) { + bt->trace_state = Blktrace_stopped; +raw_spin_lock_irq (&running_trace_lock); +list_del_init (&bt->running_list); +raw_spin_unlock_irq (&running_trace_lock); +relay_flush (bt->rchan); +return 0 ; + } + } + +return -EINVAL ; +} +static int __blk_trace_remove (struct request_queue *q) {struct blk_trace *bt;
@@ -658,7 +691,6 @@ static int compat_blk_trace_setup(struct request_queue *q, char *name,static int __blk_trace_startstop (struct request_queue *q,int start) { -int ret;struct blk_trace *bt; bt = rcu_dereference_protected(q->blk_trace,
@@ -666,36 +698,7 @@ static int __blk_trace_startstop(struct request_queue *q, int start)if (bt ==NULL )return -EINVAL ; -/* -* For starting a trace, we can transition from a setup or stopped -* trace. For stopping a trace, the state must be running -*/ - ret = -EINVAL ; -if (start) { -if (bt->trace_state == Blktrace_setup || - bt->trace_state == Blktrace_stopped) { - blktrace_seq++; -smp_mb (); - bt->trace_state = Blktrace_running; -raw_spin_lock_irq (&running_trace_lock); -list_add (&bt->running_list, &running_trace_list); -raw_spin_unlock_irq (&running_trace_lock); - -trace_note_time (bt); - ret =0 ; - } - }else { -if (bt->trace_state == Blktrace_running) { - bt->trace_state = Blktrace_stopped; -raw_spin_lock_irq (&running_trace_lock); -list_del_init (&bt->running_list); -raw_spin_unlock_irq (&running_trace_lock); -relay_flush (bt->rchan); - ret =0 ; - } - } - -return ret; +return blk_trace_switch_state (bt, start); }int blk_trace_startstop (struct request_queue *q,int start)
@@ -1614,13 +1617,8 @@ static int blk_trace_remove_queue(struct request_queue *q)if (bt ==NULL )return -EINVAL ; -if (bt->trace_state == Blktrace_running) { - bt->trace_state = Blktrace_stopped; -raw_spin_lock_irq (&running_trace_lock); -list_del_init (&bt->running_list); -raw_spin_unlock_irq (&running_trace_lock); -relay_flush (bt->rchan); - } +if (bt->trace_state == Blktrace_running) +blk_trace_switch_state (bt,0 );put_probe_ref ();synchronize_rcu ();
Signature · 1 line
2.31.1
Re: [PATCH -next 1/3] blktrace: introduce 'blk_trace_swicth_state' helper
Quoted 1 line · Ye Bin
+static int
blk_trace_switch_state(struct blk_trace *bt, int start)
This is a very confusing helper. Please split it into two helpers (e.g. blk_trace_{start,stop}) instead of the awakward int used as boolean argument that makes it do two entirely unrelated things.
PATCH 2/3 blktrace: fix possible memleak in '__blk_trace_remove'
In reply to Ye BinWhen test as follows:
step1: ioctl(sda, BLKTRACESETUP, &arg)
step2: ioctl(sda, BLKTRACESTART, NULL)
step3: ioctl(sda, BLKTRACETEARDOWN, NULL)
step4: ioctl(sda, BLKTRACESETUP, &arg)
Got issue as follows:
debugfs: File 'dropped' in directory 'sda' already present!
debugfs: File 'msg' in directory 'sda' already present!
debugfs: File 'trace0' in directory 'sda' already present!
And also find syzkaller report issue like "KASAN: use-after-free Read in relay_switch_subbuf" "https://syzkaller.appspot.com/bug?id=13849f0d9b1b818b087341691be6cc3ac6a6bfb7"
If remove block trace without stop(BLKTRACESTOP) block trace, '__blk_trace_remove' will just set 'q->blk_trace' with NULL. However, debugfs file isn't removed, so will report file already present when call BLKTRACESETUP.
static int __blk_trace_remove(struct request_queue *q)
{
struct blk_trace *bt;
bt = rcu_replace_pointer(q->blk_trace, NULL,
lockdep_is_held(&q->debugfs_mutex));
if (!bt)
return -EINVAL;
if (bt->trace_state != Blktrace_running)
blk_trace_cleanup(q, bt);
return 0;
}If do test as follows:
step1: ioctl(sda, BLKTRACESETUP, &arg)
step2: ioctl(sda, BLKTRACESTART, NULL)
step3: ioctl(sda, BLKTRACETEARDOWN, NULL)
step4: remove sda
There will remove debugfs directory which will remove recursively all file under directory.
Quoted 2 lines
Quoted 2 lines
blk_release_queuedebugfs_remove_recursive(q->debugfs_dir)
So all files which created in 'do_blk_trace_setup' are removed, and 'dentry->d_inode' is NULL. But 'q->blk_trace' is still in 'running_trace_lock', 'trace_note_tsk' will traverse 'running_trace_lock' all nodes.
Quoted 5 lines
Quoted 5 lines
trace_note_tsk
trace_note
relay_reserverelay_switch_subbuf d_inode(buf->dentry)->i_size
To solve above issues, reference commit '5afedf670caf', first stop block trace when block trace state is 'Blktrace_running' in '__blk_trace_remove'.
--- kernel/trace/blktrace.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/trace/blktrace.c +4 -2
@@ -395,8 +395,10 @@ static int __blk_trace_remove(struct request_queue *q)if (!bt)return -EINVAL ; -if (bt->trace_state != Blktrace_running) -blk_trace_cleanup (q, bt); +if (bt->trace_state == Blktrace_running) +blk_trace_switch_state (bt,0 ); + +blk_trace_cleanup (q, bt);return 0 ; }
Signature · 1 line
2.31.1
Re: [PATCH -next 2/3] blktrace: fix possible memleak in '__blk_trace_remove'
Hi,
Quoted 2 lines · Ye Bin
To solve above issues, reference commit '
5afedf670caf', first stop block trace when block trace state is 'Blktrace_running' in '__blk_trace_remove'.
Will it be much simpler if we just return -EBUSY in blk_trace_remove if state is still running?
And similar checking in blk_trace_setup.
Thanks,
Kuai
Re: [PATCH -next 2/3] blktrace: fix possible memleak in '__blk_trace_remove'
In reply to Ye Binkernel/trace/blktrace.c static int __blk_trace_remove(struct request_queue *q) · line 399
Quoted 2 lines · Ye Bin
kernel/trace/blktrace.c +2
-0+if (bt->trace_state == Blktrace_running) +blk_trace_switch_state (bt,0 );
AFAICS blk_trace_switch_state already has that state check, so there should be no need to duplicate it here.
I think having this call in blk_trace_cleanup itself might be a little more obvious, too.
PATCH 3/3 blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
In reply to Ye BinAs previous commit, '__blk_trace_remove' will stop block trace if block trace's state is 'Blktrace_running'.
So remove unnessary stop block trace in 'blk_trace_shutdown'.
--- kernel/trace/blktrace.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/blktrace.c +1 -3
@@ -777,10 +777,8 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)void blk_trace_shutdown (struct request_queue *q) {if (rcu_dereference_protected (q->blk_trace, -lockdep_is_held (&q->debugfs_mutex))) { -__blk_trace_startstop (q,0 ); +lockdep_is_held (&q->debugfs_mutex)))__blk_trace_remove (q); - } }#ifdef CONFIG_BLK_CGROUP
Signature · 1 line
2.31.1