Skip to content

Commit 0b4ffbe

Browse files
Tengda Wurostedt
authored andcommitted
tracing: Correct the refcount if the hist/hist_debug file fails to open
The function event_{hist,hist_debug}_open() maintains the refcount of 'file->tr' and 'file' through tracing_open_file_tr(). However, it does not roll back these counts on subsequent failure paths, resulting in a refcount leak. A very obvious case is that if the hist/hist_debug file belongs to a specific instance, the refcount leak will prevent the deletion of that instance, as it relies on the condition 'tr->ref == 1' within __remove_instance(). Fix this by calling tracing_release_file_tr() on all failure paths in event_{hist,hist_debug}_open() to correct the refcount. Cc: stable@vger.kernel.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Zheng Yejian <zhengyejian1@huawei.com> Link: https://lore.kernel.org/20250314065335.1202817-1-wutengda@huaweicloud.com Fixes: 1cc111b ("tracing: Fix uaf issue when open the hist or hist_debug file") Signed-off-by: Tengda Wu <wutengda@huaweicloud.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 7eb1721 commit 0b4ffbe

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

kernel/trace/trace_events_hist.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5689,22 +5689,31 @@ static int event_hist_open(struct inode *inode, struct file *file)
56895689
guard(mutex)(&event_mutex);
56905690

56915691
event_file = event_file_data(file);
5692-
if (!event_file)
5693-
return -ENODEV;
5692+
if (!event_file) {
5693+
ret = -ENODEV;
5694+
goto err;
5695+
}
56945696

56955697
hist_file = kzalloc(sizeof(*hist_file), GFP_KERNEL);
5696-
if (!hist_file)
5697-
return -ENOMEM;
5698+
if (!hist_file) {
5699+
ret = -ENOMEM;
5700+
goto err;
5701+
}
56985702

56995703
hist_file->file = file;
57005704
hist_file->last_act = get_hist_hit_count(event_file);
57015705

57025706
/* Clear private_data to avoid warning in single_open() */
57035707
file->private_data = NULL;
57045708
ret = single_open(file, hist_show, hist_file);
5705-
if (ret)
5709+
if (ret) {
57065710
kfree(hist_file);
5711+
goto err;
5712+
}
57075713

5714+
return 0;
5715+
err:
5716+
tracing_release_file_tr(inode, file);
57085717
return ret;
57095718
}
57105719

@@ -5979,7 +5988,10 @@ static int event_hist_debug_open(struct inode *inode, struct file *file)
59795988

59805989
/* Clear private_data to avoid warning in single_open() */
59815990
file->private_data = NULL;
5982-
return single_open(file, hist_debug_show, file);
5991+
ret = single_open(file, hist_debug_show, file);
5992+
if (ret)
5993+
tracing_release_file_tr(inode, file);
5994+
return ret;
59835995
}
59845996

59855997
const struct file_operations event_hist_debug_fops = {

0 commit comments

Comments
 (0)