mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-10-26 10:57:38 +08:00
try_add_tainted_module() allocates the tracking record without zeroing
it and initializes the fields in this order:
mod_taint = kmalloc_obj(*mod_taint);
strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN);
mod_taint->taints = mod->taints;
list_add_rcu(&mod_taint->list, &unloaded_tainted_modules);
mod_taint->count = 1;
The record is published by list_add_rcu() before the count field is
written. The readers, unloaded_tainted_modules_seq_show() (debugfs,
under rcu_read_lock()) and print_unloaded_tainted_modules() (panic
path, under guard(rcu)), traverse the list under RCU and read
mod_taint->count. module_mutex, which the writer holds, does not
exclude RCU readers.
A reader that observes the new node between the list_add_rcu() and
the count store reads an uninitialized u64 left over from previous
slab use. This is a data race and leaks kernel heap residue through
the debugfs file and the panic message.
The race window is two adjacent instructions; it is reachable when a
tainted module is unloaded (rmmod) concurrently with a read of
/sys/kernel/debug/module/unloaded_tainted, or with the module list
printout of an oops on another CPU.
Fix this by initializing count before the record is published.
Found during a static-analysis-assisted audit: candidate sites were
surfaced by a Coccinelle rule, and the reader/writer paths were
confirmed by LLM-assisted (GLM-5.3) code inspection. The issue was
not observed at runtime; it is identified by code inspection only.
Fixes: 99bd995655 ("module: Introduce module unload taint tracking")
Signed-off-by: Binbin Deng <18983559317@163.com>
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
128 lines
3.1 KiB
C
128 lines
3.1 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Module taint unload tracking support
|
|
*
|
|
* Copyright (C) 2022 Aaron Tomlin
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/string.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/list.h>
|
|
#include <linux/debugfs.h>
|
|
#include <linux/rculist.h>
|
|
#include "internal.h"
|
|
|
|
static LIST_HEAD(unloaded_tainted_modules);
|
|
extern struct dentry *mod_debugfs_root;
|
|
|
|
int try_add_tainted_module(struct module *mod)
|
|
{
|
|
struct mod_unload_taint *mod_taint;
|
|
|
|
if (!mod->taints)
|
|
goto out;
|
|
|
|
list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, list,
|
|
lockdep_is_held(&module_mutex)) {
|
|
if (!strcmp(mod_taint->name, mod->name) &&
|
|
mod_taint->taints & mod->taints) {
|
|
mod_taint->count++;
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
mod_taint = kmalloc_obj(*mod_taint);
|
|
if (unlikely(!mod_taint))
|
|
return -ENOMEM;
|
|
strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN);
|
|
mod_taint->taints = mod->taints;
|
|
mod_taint->count = 1;
|
|
list_add_rcu(&mod_taint->list, &unloaded_tainted_modules);
|
|
out:
|
|
return 0;
|
|
}
|
|
|
|
void print_unloaded_tainted_modules(void)
|
|
{
|
|
struct mod_unload_taint *mod_taint;
|
|
char buf[MODULE_FLAGS_BUF_SIZE];
|
|
|
|
if (!list_empty(&unloaded_tainted_modules)) {
|
|
printk(KERN_DEFAULT "Unloaded tainted modules:");
|
|
list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules,
|
|
list) {
|
|
size_t l;
|
|
|
|
l = module_flags_taint(mod_taint->taints, buf);
|
|
buf[l++] = '\0';
|
|
pr_cont(" %s(%s):%llu", mod_taint->name, buf,
|
|
mod_taint->count);
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
static void *unloaded_tainted_modules_seq_start(struct seq_file *m, loff_t *pos)
|
|
__acquires(rcu)
|
|
{
|
|
rcu_read_lock();
|
|
return seq_list_start_rcu(&unloaded_tainted_modules, *pos);
|
|
}
|
|
|
|
static void *unloaded_tainted_modules_seq_next(struct seq_file *m, void *p, loff_t *pos)
|
|
{
|
|
return seq_list_next_rcu(p, &unloaded_tainted_modules, pos);
|
|
}
|
|
|
|
static void unloaded_tainted_modules_seq_stop(struct seq_file *m, void *p)
|
|
__releases(rcu)
|
|
{
|
|
rcu_read_unlock();
|
|
}
|
|
|
|
static int unloaded_tainted_modules_seq_show(struct seq_file *m, void *p)
|
|
{
|
|
struct mod_unload_taint *mod_taint;
|
|
char buf[MODULE_FLAGS_BUF_SIZE];
|
|
size_t l;
|
|
|
|
mod_taint = list_entry(p, struct mod_unload_taint, list);
|
|
l = module_flags_taint(mod_taint->taints, buf);
|
|
buf[l++] = '\0';
|
|
|
|
seq_printf(m, "%s (%s) %llu", mod_taint->name, buf, mod_taint->count);
|
|
seq_puts(m, "\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct seq_operations unloaded_tainted_modules_seq_ops = {
|
|
.start = unloaded_tainted_modules_seq_start,
|
|
.next = unloaded_tainted_modules_seq_next,
|
|
.stop = unloaded_tainted_modules_seq_stop,
|
|
.show = unloaded_tainted_modules_seq_show,
|
|
};
|
|
|
|
static int unloaded_tainted_modules_open(struct inode *inode, struct file *file)
|
|
{
|
|
return seq_open(file, &unloaded_tainted_modules_seq_ops);
|
|
}
|
|
|
|
static const struct file_operations unloaded_tainted_modules_fops = {
|
|
.open = unloaded_tainted_modules_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = seq_release,
|
|
};
|
|
|
|
static int __init unloaded_tainted_modules_init(void)
|
|
{
|
|
debugfs_create_file("unloaded_tainted", 0444, mod_debugfs_root, NULL,
|
|
&unloaded_tainted_modules_fops);
|
|
return 0;
|
|
}
|
|
module_init(unloaded_tainted_modules_init);
|
|
#endif /* CONFIG_DEBUG_FS */
|