mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2025-01-01 10:42:11 +00:00
39e21403c9
In kunit_debugfs_create_suite(), if alloc_string_stream() fails in the
kunit_suite_for_each_test_case() loop, the "suite->log = stream"
has assigned before, and the error path only free the suite->log's stream
memory but not set it to NULL, so the later string_stream_clear() of
suite->log in kunit_init_suite() will cause below UAF bug.
Set stream pointer to NULL after free to fix it.
Unable to handle kernel paging request at virtual address 006440150000030d
Mem abort info:
ESR = 0x0000000096000004
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x04: level 0 translation fault
Data abort info:
ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
[006440150000030d] address between user and kernel address ranges
Internal error: Oops: 0000000096000004 [#1] PREEMPT SMP
Dumping ftrace buffer:
(ftrace buffer empty)
Modules linked in: iio_test_gts industrialio_gts_helper cfg80211 rfkill ipv6 [last unloaded: iio_test_gts]
CPU: 5 UID: 0 PID: 6253 Comm: modprobe Tainted: G B W N 6.12.0-rc4+ #458
Tainted: [B]=BAD_PAGE, [W]=WARN, [N]=TEST
Hardware name: linux,dummy-virt (DT)
pstate: 40000005 (nZcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : string_stream_clear+0x54/0x1ac
lr : string_stream_clear+0x1a8/0x1ac
sp : ffffffc080b47410
x29: ffffffc080b47410 x28: 006440550000030d x27: ffffff80c96b5e98
x26: ffffff80c96b5e80 x25: ffffffe461b3f6c0 x24: 0000000000000003
x23: ffffff80c96b5e88 x22: 1ffffff019cdf4fc x21: dfffffc000000000
x20: ffffff80ce6fa7e0 x19: 032202a80000186d x18: 0000000000001840
x17: 0000000000000000 x16: 0000000000000000 x15: ffffffe45c355cb4
x14: ffffffe45c35589c x13: ffffffe45c03da78 x12: ffffffb810168e75
x11: 1ffffff810168e74 x10: ffffffb810168e74 x9 : dfffffc000000000
x8 : 0000000000000004 x7 : 0000000000000003 x6 : 0000000000000001
x5 : ffffffc080b473a0 x4 : 0000000000000000 x3 : 0000000000000000
x2 : 0000000000000001 x1 : ffffffe462fbf620 x0 : dfffffc000000000
Call trace:
string_stream_clear+0x54/0x1ac
__kunit_test_suites_init+0x108/0x1d8
kunit_exec_run_tests+0xb8/0x100
kunit_module_notify+0x400/0x55c
notifier_call_chain+0xfc/0x3b4
blocking_notifier_call_chain+0x68/0x9c
do_init_module+0x24c/0x5c8
load_module+0x4acc/0x4e90
init_module_from_file+0xd4/0x128
idempotent_init_module+0x2d4/0x57c
__arm64_sys_finit_module+0xac/0x100
invoke_syscall+0x6c/0x258
el0_svc_common.constprop.0+0x160/0x22c
do_el0_svc+0x44/0x5c
el0_svc+0x48/0xb8
el0t_64_sync_handler+0x13c/0x158
el0t_64_sync+0x190/0x194
Code: f9400753 d2dff800 f2fbffe0 d343fe7c (38e06b80)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
Link: https://lore.kernel.org/r/20241112080314.407966-1-ruanjinjie@huawei.com
Cc: stable@vger.kernel.org
Fixes: a3fdf78478
("kunit: string-stream: Decouple string_stream from kunit")
Suggested-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
231 lines
5.9 KiB
C
231 lines
5.9 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (c) 2020, Oracle and/or its affiliates.
|
|
* Author: Alan Maguire <alan.maguire@oracle.com>
|
|
*/
|
|
|
|
#include <linux/debugfs.h>
|
|
#include <linux/module.h>
|
|
|
|
#include <kunit/test.h>
|
|
#include <kunit/test-bug.h>
|
|
|
|
#include "string-stream.h"
|
|
#include "debugfs.h"
|
|
|
|
#define KUNIT_DEBUGFS_ROOT "kunit"
|
|
#define KUNIT_DEBUGFS_RESULTS "results"
|
|
#define KUNIT_DEBUGFS_RUN "run"
|
|
|
|
/*
|
|
* Create a debugfs representation of test suites:
|
|
*
|
|
* Path Semantics
|
|
* /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
|
|
* testsuite
|
|
* /sys/kernel/debug/kunit/<testsuite>/run Write to this file to trigger
|
|
* testsuite to run
|
|
*
|
|
*/
|
|
|
|
static struct dentry *debugfs_rootdir;
|
|
|
|
void kunit_debugfs_cleanup(void)
|
|
{
|
|
debugfs_remove_recursive(debugfs_rootdir);
|
|
}
|
|
|
|
void kunit_debugfs_init(void)
|
|
{
|
|
if (!debugfs_rootdir)
|
|
debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
|
|
}
|
|
|
|
static void debugfs_print_result(struct seq_file *seq, struct string_stream *log)
|
|
{
|
|
struct string_stream_fragment *frag_container;
|
|
|
|
if (!log)
|
|
return;
|
|
|
|
/*
|
|
* Walk the fragments so we don't need to allocate a temporary
|
|
* buffer to hold the entire string.
|
|
*/
|
|
spin_lock(&log->lock);
|
|
list_for_each_entry(frag_container, &log->fragments, node)
|
|
seq_printf(seq, "%s", frag_container->fragment);
|
|
spin_unlock(&log->lock);
|
|
}
|
|
|
|
/*
|
|
* /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
|
|
*/
|
|
static int debugfs_print_results(struct seq_file *seq, void *v)
|
|
{
|
|
struct kunit_suite *suite = (struct kunit_suite *)seq->private;
|
|
enum kunit_status success;
|
|
struct kunit_case *test_case;
|
|
|
|
if (!suite)
|
|
return 0;
|
|
|
|
success = kunit_suite_has_succeeded(suite);
|
|
|
|
/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
|
|
seq_puts(seq, "KTAP version 1\n");
|
|
seq_puts(seq, "1..1\n");
|
|
|
|
/* Print suite header because it is not stored in the test logs. */
|
|
seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
|
|
seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
|
|
seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
|
|
|
|
kunit_suite_for_each_test_case(suite, test_case)
|
|
debugfs_print_result(seq, test_case->log);
|
|
|
|
debugfs_print_result(seq, suite->log);
|
|
|
|
seq_printf(seq, "%s %d %s\n",
|
|
kunit_status_to_ok_not_ok(success), 1, suite->name);
|
|
return 0;
|
|
}
|
|
|
|
static int debugfs_release(struct inode *inode, struct file *file)
|
|
{
|
|
return single_release(inode, file);
|
|
}
|
|
|
|
static int debugfs_results_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct kunit_suite *suite;
|
|
|
|
suite = (struct kunit_suite *)inode->i_private;
|
|
|
|
return single_open(file, debugfs_print_results, suite);
|
|
}
|
|
|
|
/*
|
|
* Print a usage message to the debugfs "run" file
|
|
* (/sys/kernel/debug/kunit/<testsuite>/run) if opened.
|
|
*/
|
|
static int debugfs_print_run(struct seq_file *seq, void *v)
|
|
{
|
|
struct kunit_suite *suite = (struct kunit_suite *)seq->private;
|
|
|
|
seq_puts(seq, "Write to this file to trigger the test suite to run.\n");
|
|
seq_printf(seq, "usage: echo \"any string\" > /sys/kernel/debugfs/kunit/%s/run\n",
|
|
suite->name);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* The debugfs "run" file (/sys/kernel/debug/kunit/<testsuite>/run)
|
|
* contains no information. Write to the file to trigger the test suite
|
|
* to run.
|
|
*/
|
|
static int debugfs_run_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct kunit_suite *suite;
|
|
|
|
suite = (struct kunit_suite *)inode->i_private;
|
|
|
|
return single_open(file, debugfs_print_run, suite);
|
|
}
|
|
|
|
/*
|
|
* Trigger a test suite to run by writing to the suite's "run" debugfs
|
|
* file found at: /sys/kernel/debug/kunit/<testsuite>/run
|
|
*
|
|
* Note: what is written to this file will not be saved.
|
|
*/
|
|
static ssize_t debugfs_run(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct inode *f_inode = file->f_inode;
|
|
struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private;
|
|
|
|
__kunit_test_suites_init(&suite, 1);
|
|
|
|
return count;
|
|
}
|
|
|
|
static const struct file_operations debugfs_results_fops = {
|
|
.open = debugfs_results_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = debugfs_release,
|
|
};
|
|
|
|
static const struct file_operations debugfs_run_fops = {
|
|
.open = debugfs_run_open,
|
|
.read = seq_read,
|
|
.write = debugfs_run,
|
|
.llseek = seq_lseek,
|
|
.release = debugfs_release,
|
|
};
|
|
|
|
void kunit_debugfs_create_suite(struct kunit_suite *suite)
|
|
{
|
|
struct kunit_case *test_case;
|
|
struct string_stream *stream;
|
|
|
|
/* If suite log already allocated, do not create new debugfs files. */
|
|
if (suite->log)
|
|
return;
|
|
|
|
/*
|
|
* Allocate logs before creating debugfs representation.
|
|
* The suite->log and test_case->log pointer are expected to be NULL
|
|
* if there isn't a log, so only set it if the log stream was created
|
|
* successfully.
|
|
*/
|
|
stream = alloc_string_stream(GFP_KERNEL);
|
|
if (IS_ERR_OR_NULL(stream))
|
|
return;
|
|
|
|
string_stream_set_append_newlines(stream, true);
|
|
suite->log = stream;
|
|
|
|
kunit_suite_for_each_test_case(suite, test_case) {
|
|
stream = alloc_string_stream(GFP_KERNEL);
|
|
if (IS_ERR_OR_NULL(stream))
|
|
goto err;
|
|
|
|
string_stream_set_append_newlines(stream, true);
|
|
test_case->log = stream;
|
|
}
|
|
|
|
suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
|
|
|
|
debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
|
|
suite->debugfs,
|
|
suite, &debugfs_results_fops);
|
|
|
|
/* Do not create file to re-run test if test runs on init */
|
|
if (!suite->is_init) {
|
|
debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644,
|
|
suite->debugfs,
|
|
suite, &debugfs_run_fops);
|
|
}
|
|
return;
|
|
|
|
err:
|
|
string_stream_destroy(suite->log);
|
|
suite->log = NULL;
|
|
kunit_suite_for_each_test_case(suite, test_case) {
|
|
string_stream_destroy(test_case->log);
|
|
test_case->log = NULL;
|
|
}
|
|
}
|
|
|
|
void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
|
|
{
|
|
struct kunit_case *test_case;
|
|
|
|
debugfs_remove_recursive(suite->debugfs);
|
|
string_stream_destroy(suite->log);
|
|
kunit_suite_for_each_test_case(suite, test_case)
|
|
string_stream_destroy(test_case->log);
|
|
}
|