Skip to content

Commit 9d65cb4

Browse files
Alexey DobriyanLinus Torvalds
authored andcommitted
Fix race between cat /proc/*/wchan and rmmod et al
kallsyms_lookup() can go iterating over modules list unprotected which is OK for emergency situations (oops), but not OK for regular stuff like /proc/*/wchan. Introduce lookup_symbol_name()/lookup_module_symbol_name() which copy symbol name into caller-supplied buffer or return -ERANGE. All copying is done with module_mutex held, so... Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru> Cc: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ffb4512 commit 9d65cb4

7 files changed

Lines changed: 66 additions & 19 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,15 @@ static int proc_pid_auxv(struct task_struct *task, char *buffer)
278278
*/
279279
static int proc_pid_wchan(struct task_struct *task, char *buffer)
280280
{
281-
const char *sym_name;
282281
unsigned long wchan;
283-
char namebuf[KSYM_NAME_LEN+1];
282+
char symname[KSYM_NAME_LEN+1];
284283

285284
wchan = get_wchan(task);
286285

287-
sym_name = kallsyms_lookup(wchan, NULL, NULL, NULL, namebuf);
288-
if (sym_name)
289-
return sprintf(buffer, "%s", sym_name);
290-
return sprintf(buffer, "%lu", wchan);
286+
if (lookup_symbol_name(wchan, symname) < 0)
287+
return sprintf(buffer, "%lu", wchan);
288+
else
289+
return sprintf(buffer, "%s", symname);
291290
}
292291
#endif /* CONFIG_KALLSYMS */
293292

‎include/linux/kallsyms.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ extern int sprint_symbol(char *buffer, unsigned long address);
3030
/* Look up a kernel symbol and print it to the kernel messages. */
3131
extern void __print_symbol(const char *fmt, unsigned long address);
3232

33+
int lookup_symbol_name(unsigned long addr, char *symname);
34+
3335
#else /* !CONFIG_KALLSYMS */
3436

3537
static inline unsigned long kallsyms_lookup_name(const char *name)
@@ -58,6 +60,11 @@ static inline int sprint_symbol(char *buffer, unsigned long addr)
5860
return 0;
5961
}
6062

63+
static inline int lookup_symbol_name(unsigned long addr, char *symname)
64+
{
65+
return -ERANGE;
66+
}
67+
6168
/* Stupid that this does nothing, but I didn't create this mess. */
6269
#define __print_symbol(fmt, addr)
6370
#endif /*CONFIG_KALLSYMS*/

‎include/linux/module.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ const char *module_address_lookup(unsigned long addr,
454454
unsigned long *symbolsize,
455455
unsigned long *offset,
456456
char **modname);
457+
int lookup_module_symbol_name(unsigned long addr, char *symname);
457458

458459
/* For extable.c to search modules' exception tables. */
459460
const struct exception_table_entry *search_module_extables(unsigned long addr);
@@ -525,6 +526,11 @@ static inline const char *module_address_lookup(unsigned long addr,
525526
return NULL;
526527
}
527528

529+
static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
530+
{
531+
return -ERANGE;
532+
}
533+
528534
static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
529535
char *type, char *name,
530536
char *module_name, int *exported)

‎kernel/kallsyms.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,23 @@ const char *kallsyms_lookup(unsigned long addr,
269269
return NULL;
270270
}
271271

272+
int lookup_symbol_name(unsigned long addr, char *symname)
273+
{
274+
symname[0] = '\0';
275+
symname[KSYM_NAME_LEN] = '\0';
276+
277+
if (is_ksym_addr(addr)) {
278+
unsigned long pos;
279+
280+
pos = get_symbol_pos(addr, NULL, NULL);
281+
/* Grab name */
282+
kallsyms_expand_symbol(get_symbol_offset(pos), symname);
283+
return 0;
284+
}
285+
/* see if it's in a module */
286+
return lookup_module_symbol_name(addr, symname);
287+
}
288+
272289
/* Look up a kernel symbol and return it in a text buffer. */
273290
int sprint_symbol(char *buffer, unsigned long address)
274291
{

‎kernel/module.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,29 @@ const char *module_address_lookup(unsigned long addr,
21262126
return NULL;
21272127
}
21282128

2129+
int lookup_module_symbol_name(unsigned long addr, char *symname)
2130+
{
2131+
struct module *mod;
2132+
2133+
mutex_lock(&module_mutex);
2134+
list_for_each_entry(mod, &modules, list) {
2135+
if (within(addr, mod->module_init, mod->init_size) ||
2136+
within(addr, mod->module_core, mod->core_size)) {
2137+
const char *sym;
2138+
2139+
sym = get_ksymbol(mod, addr, NULL, NULL);
2140+
if (!sym)
2141+
goto out;
2142+
strlcpy(symname, sym, KSYM_NAME_LEN + 1);
2143+
mutex_unlock(&module_mutex);
2144+
return 0;
2145+
}
2146+
}
2147+
out:
2148+
mutex_unlock(&module_mutex);
2149+
return -ERANGE;
2150+
}
2151+
21292152
int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
21302153
char *name, char *module_name, int *exported)
21312154
{

‎kernel/time/timer_list.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
3838

3939
static void print_name_offset(struct seq_file *m, void *sym)
4040
{
41-
unsigned long addr = (unsigned long)sym;
42-
char namebuf[KSYM_NAME_LEN+1];
43-
const char *sym_name;
41+
char symname[KSYM_NAME_LEN+1];
4442

45-
sym_name = kallsyms_lookup(addr, NULL, NULL, NULL, namebuf);
46-
if (sym_name)
47-
SEQ_printf(m, "%s", sym_name);
48-
else
43+
if (lookup_symbol_name((unsigned long)sym, symname) < 0)
4944
SEQ_printf(m, "<%p>", sym);
45+
else
46+
SEQ_printf(m, "%s", symname);
5047
}
5148

5249
static void

‎kernel/time/timer_stats.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,12 @@ void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
257257

258258
static void print_name_offset(struct seq_file *m, unsigned long addr)
259259
{
260-
char namebuf[KSYM_NAME_LEN+1];
261-
const char *sym_name;
260+
char symname[KSYM_NAME_LEN+1];
262261

263-
sym_name = kallsyms_lookup(addr, NULL, NULL, NULL, namebuf);
264-
if (sym_name)
265-
seq_printf(m, "%s", sym_name);
266-
else
262+
if (lookup_symbol_name(addr, symname) < 0)
267263
seq_printf(m, "<%p>", (void *)addr);
264+
else
265+
seq_printf(m, "%s", symname);
268266
}
269267

270268
static int tstats_show(struct seq_file *m, void *v)

0 commit comments

Comments
 (0)