[PATCH] init, allow blacklisting of module_init functions

From: Prarit Bhargava
Date: Wed Jun 15 2016 - 11:31:02 EST


At some point I was 100% sure this worked. I do remember testing it against
just a loadable module and had positive testing results. I went back to
the time that it was commited (3.15-ish) and blacklisting a module init
function didn't work there either, so something went wrong somewhere. In
any case this is a trivial patch to add the functionality...

P.

---8<---

sprint_symbol_no_offset() returns the string "function_name [module_name]"
where [module_name] is not printed for built in kernel functions. This
means that the blacklisting code will fail when comparing module function
names with the extended string. This patch adds the functionality to
block a module's module_init() function by finding the space in the string
and truncating the comparison to that length.

Signed-off-by: Prarit Bhargava <prarit@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Yang Shi <yang.shi@xxxxxxxxxx>
Cc: Prarit Bhargava <prarit@xxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxx>
Cc: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Yaowei Bai <baiyaowei@xxxxxxxxxxxxxxxxxxxx>
Cc: Andrey Ryabinin <aryabinin@xxxxxxxxxxxxx>
---
init/main.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/init/main.c b/init/main.c
index 4c17fda5c2ff..730d6a846216 100644
--- a/init/main.c
+++ b/init/main.c
@@ -708,14 +708,25 @@ static bool __init_or_module initcall_blacklisted(initcall_t fn)
{
struct blacklist_entry *entry;
char fn_name[KSYM_SYMBOL_LEN];
+ char *space;
+ int length;

if (list_empty(&blacklisted_initcalls))
return false;

sprint_symbol_no_offset(fn_name, (unsigned long)fn);
+ /*
+ * fn will be "function_name [module_name]" where [module_name] is not
+ * displayed for built-in init functions. Strip off the [module_name].
+ */
+ space = strchrnul(fn_name, ' ');
+ if (!space)
+ length = strlen(fn_name);
+ else
+ length = space - fn_name;

list_for_each_entry(entry, &blacklisted_initcalls, next) {
- if (!strcmp(fn_name, entry->buf)) {
+ if (!strncmp(fn_name, entry->buf, length)) {
pr_debug("initcall %s blacklisted\n", fn_name);
return true;
}
--
1.7.9.3