]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ftrace/module: Call clean up function when module init fails early
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>
Wed, 6 Jan 2016 01:32:47 +0000 (20:32 -0500)
committerLuis Henriques <luis.henriques@canonical.com>
Mon, 25 Jan 2016 10:44:04 +0000 (10:44 +0000)
commit 049fb9bd416077b3622d317a45796be4f2431df3 upstream.

If the module init code fails after calling ftrace_module_init() and before
calling do_init_module(), we can suffer from a memory leak. This is because
ftrace_module_init() allocates pages to store the locations that ftrace
hooks are placed in the module text. If do_init_module() fails, it still
calls the MODULE_GOING notifiers which will tell ftrace to do a clean up of
the pages it allocated for the module. But if load_module() fails before
then, the pages allocated by ftrace_module_init() will never be freed.

Call ftrace_release_mod() on the module if load_module() fails before
getting to do_init_module().

Link: http://lkml.kernel.org/r/567CEA31.1070507@intel.com
Reported-by: "Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Fixes: a949ae560a511 "ftrace/module: Hardcode ftrace_module_init() call into load_module()"
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
[ luis: backported to 3.16: adjusted context ]
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
include/linux/ftrace.h
kernel/module.c

index 721de254ba7a8af243a991f608a9b761806a62ba..c314fe2e0f46f3519a02c7bc35db83af2c6fe898 100644 (file)
@@ -541,6 +541,7 @@ extern int ftrace_arch_read_dyn_info(char *buf, int size);
 
 extern int skip_trace(unsigned long ip);
 extern void ftrace_module_init(struct module *mod);
+extern void ftrace_release_mod(struct module *mod);
 
 extern void ftrace_disable_daemon(void);
 extern void ftrace_enable_daemon(void);
index ed4d3b7ec58ade899e884c3550a334a90ef3aefe..365b0a9d87b39b4f96f38a41f0f145651d58853c 100644 (file)
@@ -3339,6 +3339,12 @@ static int load_module(struct load_info *info, const char __user *uargs,
        wake_up_all(&module_wq);
        mutex_unlock(&module_mutex);
  free_module:
+       /*
+        * Ftrace needs to clean up what it initialized.
+        * This does nothing if ftrace_module_init() wasn't called,
+        * but it must be called outside of module_mutex.
+        */
+       ftrace_release_mod(mod);
        module_deallocate(mod, info);
  free_copy:
        free_copy(info);