[PATCH] Race between cat /proc/kallsyms and rmmod

From: Weilong Chen
Date: Sat Jan 30 2016 - 05:24:03 EST


Iterating code of /proc/kallsyms calls module_get_kallsym() which grabs
and drops module_mutex internally and returns "struct module *",
module is removed, aforementioned "struct module *" is used in non-trivial
way.

So, grab module_mutex for entire operation like /proc/modules does.

Steps to reproduce:
while true; do modprobe xfs; rmmod xfs; done
vs
while true; do cat /proc/kallsyms >/dev/null; done

ref:http://lkml.iu.edu/hypermail/linux/kernel/0703.1/2582.html
https://bugzilla.kernel.org/show_bug.cgi?id=111541

Signed-off-by: Weilong Chen <chenweilong@xxxxxxxxxx>
---
kernel/kallsyms.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index 3127ad5..aaecf19 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -515,6 +515,7 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos)

static void *s_start(struct seq_file *m, loff_t *pos)
{
+ mutex_lock(&module_mutex);
if (!update_iter(m->private, *pos))
return NULL;
return m->private;
@@ -522,6 +523,7 @@ static void *s_start(struct seq_file *m, loff_t *pos)

static void s_stop(struct seq_file *m, void *p)
{
+ mutex_unlock(&module_mutex);
}

static int s_show(struct seq_file *m, void *p)
--
1.8.3.1