Commit e180a6b7 authored by Rusty Russell's avatar Rusty Russell

param: fix charp parameters set via sysfs

Impact: fix crash on reading from /sys/module/.../ieee80211_default_rc_algo

The module_param type "charp" simply sets a char * pointer in the
module to the parameter in the commandline string: this is why we keep
the (mangled) module command line around.  But when set via sysfs (as
about 11 charp parameters can be) this memory is freed on the way
out of the write().  Future reads hit random mem.

So we kstrdup instead: we have to check we're not in early commandline
parsing, and we have to note when we've used it so we can reliably
kfree the parameter when it's next overwritten, and also on module
unload.

(Thanks to Randy Dunlap for CONFIG_SYSFS=n fixes)
Reported-by: default avatarSitsofe Wheeler <sitsofe@yahoo.com>
Diagnosed-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Tested-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Tested-by: default avatarChristof Schmitt <christof.schmitt@de.ibm.com>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 15f7176e
...@@ -248,6 +248,10 @@ struct module ...@@ -248,6 +248,10 @@ struct module
const unsigned long *crcs; const unsigned long *crcs;
unsigned int num_syms; unsigned int num_syms;
/* Kernel parameters. */
struct kernel_param *kp;
unsigned int num_kp;
/* GPL-only exported symbols. */ /* GPL-only exported symbols. */
unsigned int num_gpl_syms; unsigned int num_gpl_syms;
const struct kernel_symbol *gpl_syms; const struct kernel_symbol *gpl_syms;
......
...@@ -138,6 +138,16 @@ extern int parse_args(const char *name, ...@@ -138,6 +138,16 @@ extern int parse_args(const char *name,
unsigned num, unsigned num,
int (*unknown)(char *param, char *val)); int (*unknown)(char *param, char *val));
/* Called by module remove. */
#ifdef CONFIG_SYSFS
extern void destroy_params(const struct kernel_param *params, unsigned num);
#else
static inline void destroy_params(const struct kernel_param *params,
unsigned num)
{
}
#endif /* !CONFIG_SYSFS */
/* All the helper functions */ /* All the helper functions */
/* The macros to do compile-time type checking stolen from Jakub /* The macros to do compile-time type checking stolen from Jakub
Jelinek, who IIRC came up with this idea for the 2.4 module init code. */ Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
......
...@@ -1491,6 +1491,9 @@ static void free_module(struct module *mod) ...@@ -1491,6 +1491,9 @@ static void free_module(struct module *mod)
/* Module unload stuff */ /* Module unload stuff */
module_unload_free(mod); module_unload_free(mod);
/* Free any allocated parameters. */
destroy_params(mod->kp, mod->num_kp);
/* release any pointers to mcount in this module */ /* release any pointers to mcount in this module */
ftrace_release(mod->module_core, mod->core_size); ftrace_release(mod->module_core, mod->core_size);
...@@ -1898,8 +1901,7 @@ static noinline struct module *load_module(void __user *umod, ...@@ -1898,8 +1901,7 @@ static noinline struct module *load_module(void __user *umod,
unsigned int symindex = 0; unsigned int symindex = 0;
unsigned int strindex = 0; unsigned int strindex = 0;
unsigned int modindex, versindex, infoindex, pcpuindex; unsigned int modindex, versindex, infoindex, pcpuindex;
unsigned int num_kp, num_mcount; unsigned int num_mcount;
struct kernel_param *kp;
struct module *mod; struct module *mod;
long err = 0; long err = 0;
void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
...@@ -2144,8 +2146,8 @@ static noinline struct module *load_module(void __user *umod, ...@@ -2144,8 +2146,8 @@ static noinline struct module *load_module(void __user *umod,
/* Now we've got everything in the final locations, we can /* Now we've got everything in the final locations, we can
* find optional sections. */ * find optional sections. */
kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp), mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
&num_kp); sizeof(*mod->kp), &mod->num_kp);
mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
sizeof(*mod->syms), &mod->num_syms); sizeof(*mod->syms), &mod->num_syms);
mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
...@@ -2291,11 +2293,11 @@ static noinline struct module *load_module(void __user *umod, ...@@ -2291,11 +2293,11 @@ static noinline struct module *load_module(void __user *umod,
*/ */
list_add_rcu(&mod->list, &modules); list_add_rcu(&mod->list, &modules);
err = parse_args(mod->name, mod->args, kp, num_kp, NULL); err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
if (err < 0) if (err < 0)
goto unlink; goto unlink;
err = mod_sysfs_setup(mod, kp, num_kp); err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
if (err < 0) if (err < 0)
goto unlink; goto unlink;
add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/slab.h> #include <linux/slab.h>
/* We abuse the high bits of "perm" to record whether we kmalloc'ed. */
#define KPARAM_KMALLOCED 0x80000000
#if 0 #if 0
#define DEBUGP printk #define DEBUGP printk
#else #else
...@@ -217,7 +220,19 @@ int param_set_charp(const char *val, struct kernel_param *kp) ...@@ -217,7 +220,19 @@ int param_set_charp(const char *val, struct kernel_param *kp)
return -ENOSPC; return -ENOSPC;
} }
*(char **)kp->arg = (char *)val; if (kp->perm & KPARAM_KMALLOCED)
kfree(*(char **)kp->arg);
/* This is a hack. We can't need to strdup in early boot, and we
* don't need to; this mangled commandline is preserved. */
if (slab_is_available()) {
kp->perm |= KPARAM_KMALLOCED;
*(char **)kp->arg = kstrdup(val, GFP_KERNEL);
if (!kp->arg)
return -ENOMEM;
} else
*(const char **)kp->arg = val;
return 0; return 0;
} }
...@@ -571,6 +586,15 @@ void module_param_sysfs_remove(struct module *mod) ...@@ -571,6 +586,15 @@ void module_param_sysfs_remove(struct module *mod)
} }
#endif #endif
void destroy_params(const struct kernel_param *params, unsigned num)
{
unsigned int i;
for (i = 0; i < num; i++)
if (params[i].perm & KPARAM_KMALLOCED)
kfree(*(char **)params[i].arg);
}
static void __init kernel_add_sysfs_param(const char *name, static void __init kernel_add_sysfs_param(const char *name,
struct kernel_param *kparam, struct kernel_param *kparam,
unsigned int name_skip) unsigned int name_skip)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment