Skip to contentSkip to conversation

inherit TAINT_PROPRIETARY_MODULE

PATCH In review Christoph Hellwig 4 participants 15 messages Jul 29, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi Jessica,

we've had a bug in our resolution of _GPL modules since day one, that is a module can claim to be GPL licensed and use _GPL exports, while it also depends on symbols from non-GPL modules. This is used as a circumvention of the _GPL exports by using a small shim module using the _GPL exports and the other functionality. A recent example can be found here:

    https://lore.kernel.org/netdev/[email protected]/T/#md514322fdfa212afe9f1d3eb4e5f7eaefece36eb

Diffstat:

 include/linux/module.h |   24 ++----------------------
 kernel/module.c        |   42 +++++++++++++++++++++++++++---------------
 2 files changed, 29 insertions(+), 37 deletions(-)
#1 lore raw

PATCH 1/7 modules: mark ref_module static

ref_module isn't used anywhere outside of module.c.

---
 include/linux/module.h | 1 -
 kernel/module.c        | 6 ++----
 2 files changed, 2 insertions(+), 5 deletions(-)
include/linux/module.h +0 -1
index 2e6670860d275f..f1fdbeef2153a8 100644
@@ -657,7 +657,6 @@ static inline void __module_get(struct module *module)
 #define symbol_put_addr(p) do { } while (0)
 
 #endif /* CONFIG_MODULE_UNLOAD */
-int ref_module(struct module *a, struct module *b);
 
 /* This is a #define so the string doesn't get put in every .o file */
 #define module_name(mod)			\
kernel/module.c +2 -4
index aa183c9ac0a256..17d64dae756c80 100644
@@ -869,7 +869,7 @@ static int add_module_usage(struct module *a, struct module *b)
 }
 
 /* Module a uses b: caller needs module_mutex() */
-int ref_module(struct module *a, struct module *b)
+static int ref_module(struct module *a, struct module *b)
 {
 	int err;
 
@@ -888,7 +888,6 @@ int ref_module(struct module *a, struct module *b)
 	}
 	return 0;
 }
-EXPORT_SYMBOL_GPL(ref_module);
 
 /* Clear the unload stuff of the module. */
 static void module_unload_free(struct module *mod)
@@ -1169,11 +1168,10 @@ static inline void module_unload_free(struct module *mod)
 {
 }
 
-int ref_module(struct module *a, struct module *b)
+static int ref_module(struct module *a, struct module *b)
 {
 	return strong_try_module_get(b);
 }
-EXPORT_SYMBOL_GPL(ref_module);
 
 static inline int module_unload_init(struct module *mod)
 {
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/7 modules: mark find_symbol static

In reply to Christoph Hellwig

find_symbol is only used in module.c.

---
 include/linux/module.h | 11 -----------
 kernel/module.c        |  3 +--
 2 files changed, 1 insertion(+), 13 deletions(-)
include/linux/module.h +0 -11
index f1fdbeef2153a8..90bdc362be3681 100644
@@ -590,17 +590,6 @@ struct symsearch {
 	bool unused;
 };
 
-/*
- * Search for an exported symbol by name.
- *
- * Must be called with module_mutex held or preemption disabled.
- */
-const struct kernel_symbol *find_symbol(const char *name,
-					struct module **owner,
-					const s32 **crc,
-					bool gplok,
-					bool warn);
-
 /*
  * Walk the exported symbol table
  *
kernel/module.c +1 -2
index 17d64dae756c80..84da96a6d8241c 100644
@@ -585,7 +585,7 @@ static bool find_exported_symbol_in_section(const struct symsearch *syms,
 
 /* Find an exported symbol and return it, along with, (optional) crc and
  * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
-const struct kernel_symbol *find_symbol(const char *name,
+static const struct kernel_symbol *find_symbol(const char *name,
 					struct module **owner,
 					const s32 **crc,
 					bool gplok,
@@ -608,7 +608,6 @@ const struct kernel_symbol *find_symbol(const char *name,
 	pr_debug("Failed to find symbol %s\n", name);
 	return NULL;
 }
-EXPORT_SYMBOL_GPL(find_symbol);
 
 /*
  * Search for module by name: must hold module_mutex (or preempt disabled
Signature · 1 line
2.27.0
#3 lore raw

Re: [PATCH 2/7] modules: mark find_symbol static

+++ Christoph Hellwig [29/07/20 08:27 +0200]:

Quoted 3 lines

find_symbol is only used in module.c.

CCing the livepatching ML, as this may or may not impact its users.

AFAIK, the out-of-tree kpatch module had used find_symbol() in the past, I am not sure what its current status is. I suspect all of its functionality has been migrated to upstream livepatch already.

Quoted 51 lines

---
include/linux/module.h | 11 -----------
kernel/module.c | 3 +--

2 files changed, 1 insertion(+), 13 deletions(-)
include/linux/module.h +0 -11
index f1fdbeef2153a8..90bdc362be3681 100644
@@ -590,17 +590,6 @@ struct symsearch {
 	bool unused;
 };
 
-/*
- * Search for an exported symbol by name.
- *
- * Must be called with module_mutex held or preemption disabled.
- */
-const struct kernel_symbol *find_symbol(const char *name,
-					struct module **owner,
-					const s32 **crc,
-					bool gplok,
-					bool warn);
-
 /*
 * Walk the exported symbol table
 *
kernel/module.c +1 -2
index 17d64dae756c80..84da96a6d8241c 100644
@@ -585,7 +585,7 @@ static bool find_exported_symbol_in_section(const struct symsearch *syms,
 
 /* Find an exported symbol and return it, along with, (optional) crc and
 * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
-const struct kernel_symbol *find_symbol(const char *name,
+static const struct kernel_symbol *find_symbol(const char *name,
 					struct module **owner,
 					const s32 **crc,
 					bool gplok,
@@ -608,7 +608,6 @@ const struct kernel_symbol *find_symbol(const char *name,
 	pr_debug("Failed to find symbol %s\n", name);
 	return NULL;
 }
-EXPORT_SYMBOL_GPL(find_symbol);
 
 /*
 * Search for module by name: must hold module_mutex (or preempt disabled
Signature · 1 line
2.27.0
#4 lore raw

Re: [PATCH 2/7] modules: mark find_symbol static

Quoted 10 lines · Jessica Yu

+++ Christoph Hellwig [29/07/20 08:27 +0200]:

Quoted 3 lines

find_symbol is only used in module.c.

CCing the livepatching ML, as this may or may not impact its users.

AFAIK, the out-of-tree kpatch module had used find_symbol() in the past, I am not sure what its current status is. I suspect all of its functionality has been migrated to upstream livepatch already.

We still have symbol_get(), which is what I thought they were using.

If only we could get rid of that export one day...

thanks,

greg k-h

#5 lore raw

Re: [PATCH 2/7] modules: mark find_symbol static

Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines · Jessica Yu

+++ Christoph Hellwig [29/07/20 08:27 +0200]:

Quoted 3 lines

find_symbol is only used in module.c.

Quoted 7 lines
Quoted 5 lines

CCing the livepatching ML, as this may or may not impact its users.

AFAIK, the out-of-tree kpatch module had used find_symbol() in the past, I am not sure what its current status is. I suspect all of its functionality has been migrated to upstream livepatch already.

We still have symbol_get(), which is what I thought they were using.

And even if it didn't out of tree modules really don't matter.

maintainer
#6 lore raw

Re: [PATCH 2/7] modules: mark find_symbol static

In reply to Greg Kroah-Hartman
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines · Jessica Yu

+++ Christoph Hellwig [29/07/20 08:27 +0200]:

Quoted 3 lines

find_symbol is only used in module.c.

Quoted 8 lines
Quoted 5 lines

CCing the livepatching ML, as this may or may not impact its users.

AFAIK, the out-of-tree kpatch module had used find_symbol() in the past, I am not sure what its current status is. I suspect all of its functionality has been migrated to upstream livepatch already.

We still have symbol_get(), which is what I thought they were using.

The deprecated (though still in the repo) kpatch.ko still references find_symbol(), but that module is no longer officially supported by the project.

Jessica is correct that the functionality has been migrated upstream.

I don't see any references to symbol_get() either, so we're good on that front, too.

Thanks,

-- Joe

#7 lore raw

PATCH 3/7 modules: mark each_symbol_section static

In reply to Christoph Hellwig

each_symbol_section is only used inside of module.c.

---
 include/linux/module.h | 9 ---------
 kernel/module.c        | 3 +--
 2 files changed, 1 insertion(+), 11 deletions(-)
include/linux/module.h +0 -9
index 90bdc362be3681..b79219eed83c56 100644
@@ -590,15 +590,6 @@ struct symsearch {
 	bool unused;
 };
 
-/*
- * Walk the exported symbol table
- *
- * Must be called with module_mutex held or preemption disabled.
- */
-bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
-				    struct module *owner,
-				    void *data), void *data);
-
 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
    symnum out of range. */
 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
kernel/module.c +1 -2
index 84da96a6d8241c..feeaa9629eb179 100644
@@ -422,7 +422,7 @@ static bool each_symbol_in_section(const struct symsearch *arr,
 }
 
 /* Returns true as soon as fn returns true, otherwise false. */
-bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
+static bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
 				    struct module *owner,
 				    void *data),
 			 void *data)
@@ -484,7 +484,6 @@ bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
 	}
 	return false;
 }
-EXPORT_SYMBOL_GPL(each_symbol_section);
 
 struct find_symbol_arg {
 	/* Input */
Signature · 1 line
2.27.0
#8 lore raw

PATCH 4/7 modules: unexport __module_text_address

In reply to Christoph Hellwig

__module_text_address is only used by built-in code.

---
 kernel/module.c | 1 -
 1 file changed, 1 deletion(-)
kernel/module.c +0 -1
index feeaa9629eb179..d241866f9d4a2b 100644
@@ -4508,7 +4508,6 @@ struct module *__module_text_address(unsigned long addr)
 	}
 	return mod;
 }
-EXPORT_SYMBOL_GPL(__module_text_address);
 
 /* Don't grab lock, we're oopsing. */
 void print_modules(void)
Signature · 1 line
2.27.0
#9 lore raw

PATCH 5/7 modules: unexport __module_address

In reply to Christoph Hellwig

__module_address is only used by built-in code.

---
 kernel/module.c | 1 -
 1 file changed, 1 deletion(-)
kernel/module.c +0 -1
index d241866f9d4a2b..54e853c7212f72 100644
@@ -4469,7 +4469,6 @@ struct module *__module_address(unsigned long addr)
 	}
 	return mod;
 }
-EXPORT_SYMBOL_GPL(__module_address);
 
 /*
  * is_module_text_address - is this address inside module code?
Signature · 1 line
2.27.0
#10 lore raw

PATCH 6/7 modules: return licensing information from find_symbol

In reply to Christoph Hellwig

Report the GPLONLY status through a new argument.

---
 include/linux/module.h |  2 +-
 kernel/module.c        | 16 +++++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)
include/linux/module.h +1 -1
index b79219eed83c56..c9bc3412ae4465 100644
@@ -582,7 +582,7 @@ struct module *find_module(const char *name);
 struct symsearch {
 	const struct kernel_symbol *start, *stop;
 	const s32 *crcs;
-	enum {
+	enum mod_licence {
 		NOT_GPL_ONLY,
 		GPL_ONLY,
 		WILL_BE_GPL_ONLY,
kernel/module.c +11 -5
index 54e853c7212f72..a907bc57d343f9 100644
@@ -495,6 +495,7 @@ struct find_symbol_arg {
 	struct module *owner;
 	const s32 *crc;
 	const struct kernel_symbol *sym;
+	enum mod_licence licence;
 };
 
 static bool check_exported_symbol(const struct symsearch *syms,
@@ -528,6 +529,7 @@ static bool check_exported_symbol(const struct symsearch *syms,
 	fsa->owner = owner;
 	fsa->crc = symversion(syms->crcs, symnum);
 	fsa->sym = &syms->start[symnum];
+	fsa->licence = syms->licence;
 	return true;
 }
 
@@ -587,6 +589,7 @@ static bool find_exported_symbol_in_section(const struct symsearch *syms,
 static const struct kernel_symbol *find_symbol(const char *name,
 					struct module **owner,
 					const s32 **crc,
+					enum mod_licence *licence,
 					bool gplok,
 					bool warn)
 {
@@ -601,6 +604,8 @@ static const struct kernel_symbol *find_symbol(const char *name,
 			*owner = fsa.owner;
 		if (crc)
 			*crc = fsa.crc;
+		if (licence)
+			*licence = fsa.licence;
 		return fsa.sym;
 	}
 
@@ -1074,7 +1079,7 @@ void __symbol_put(const char *symbol)
 	struct module *owner;
 
 	preempt_disable();
-	if (!find_symbol(symbol, &owner, NULL, true, false))
+	if (!find_symbol(symbol, &owner, NULL, NULL, true, false))
 		BUG();
 	module_put(owner);
 	preempt_enable();
@@ -1352,7 +1357,7 @@ static inline int check_modstruct_version(const struct load_info *info,
 	 * locking is necessary -- use preempt_disable() to placate lockdep.
 	 */
 	preempt_disable();
-	if (!find_symbol("module_layout", NULL, &crc, true, false)) {
+	if (!find_symbol("module_layout", NULL, &crc, NULL, true, false)) {
 		preempt_enable();
 		BUG();
 	}
@@ -1436,6 +1441,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 	struct module *owner;
 	const struct kernel_symbol *sym;
 	const s32 *crc;
+	enum mod_licence licence;
 	int err;
 
 	/*
@@ -1445,7 +1451,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 	 */
 	sched_annotate_sleep();
 	mutex_lock(&module_mutex);
-	sym = find_symbol(name, &owner, &crc,
+	sym = find_symbol(name, &owner, &crc, &licence,
 			  !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
 	if (!sym)
 		goto unlock;
@@ -2216,7 +2222,7 @@ void *__symbol_get(const char *symbol)
 	const struct kernel_symbol *sym;
 
 	preempt_disable();
-	sym = find_symbol(symbol, &owner, NULL, true, true);
+	sym = find_symbol(symbol, &owner, NULL, NULL, true, true);
 	if (sym && strong_try_module_get(owner))
 		sym = NULL;
 	preempt_enable();
@@ -2252,7 +2258,7 @@ static int verify_exported_symbols(struct module *mod)
 	for (i = 0; i < ARRAY_SIZE(arr); i++) {
 		for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
 			if (find_symbol(kernel_symbol_name(s), &owner, NULL,
-					true, false)) {
+					NULL, true, false)) {
 				pr_err("%s: exports duplicate symbol %s"
 				       " (owned by %s)\n",
 				       mod->name, kernel_symbol_name(s),
Signature · 1 line
2.27.0
#11 lore raw

Re: [PATCH 6/7] modules: return licensing information from find_symbol

+++ Christoph Hellwig [29/07/20 08:27 +0200]:

Quoted 26 lines

Report the GPLONLY status through a new argument.

---
include/linux/module.h | 2 +-
kernel/module.c | 16 +++++++++++-----

2 files changed, 12 insertions(+), 6 deletions(-)
include/linux/module.h +1 -1
index b79219eed83c56..c9bc3412ae4465 100644
@@ -582,7 +582,7 @@ struct module *find_module(const char *name);
 struct symsearch {
 	const struct kernel_symbol *start, *stop;
 	const s32 *crcs;
-	enum {
+	enum mod_licence {
 		NOT_GPL_ONLY,
 		GPL_ONLY,
 		WILL_BE_GPL_ONLY,
kernel/module.c +0 -0
index 54e853c7212f72..a907bc57d343f9 100644

kernel/module.c struct find_symbol_arg { · line 498

Quoted 4 lines
kernel/module.c +1 -0
@@ -495,6 +495,7 @@ struct find_symbol_arg {
 	struct module *owner;
 	const s32 *crc;
 	const struct kernel_symbol *sym;
+	enum mod_licence licence;

Just a small nit. Most of module.c uses license rather than licence - could we unify the spelling to remain consistent? Sigh, American vs. British English.. :)

#12 lore raw

Re: [PATCH 6/7] modules: return licensing information from find_symbol

Quoted 3 lines · Jessica Yu

Just a small nit. Most of module.c uses license rather than licence - could we unify the spelling to remain consistent? Sigh, American vs. British English.. :)

Sure, I can fix that up.

#13 lore raw

PATCH 7/7 modules: inherit TAINT_PROPRIETARY_MODULE

In reply to Christoph Hellwig

If a TAINT_PROPRIETARY_MODULE exports symbol, inherit the tain flag for all modules importing these symbols, and don't allow loading symbols from TAINT_PROPRIETARY_MODULE modules if the module previously imported gplonly symbols. Add a anti-circumvention devices so people don't accidentally get themselves into trouble this way.

---
 include/linux/module.h |  1 +
 kernel/module.c        | 12 ++++++++++++
 2 files changed, 13 insertions(+)
include/linux/module.h +1 -0
index c9bc3412ae4465..d49e033563dadf 100644
@@ -389,6 +389,7 @@ struct module {
 	unsigned int num_gpl_syms;
 	const struct kernel_symbol *gpl_syms;
 	const s32 *gpl_crcs;
+	bool using_gplonly_symbols;
 
 #ifdef CONFIG_UNUSED_SYMBOLS
 	/* unused exported symbols. */
kernel/module.c +12 -0
index a907bc57d343f9..eb6ab2f4b81aaa 100644
@@ -1456,6 +1456,18 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 	if (!sym)
 		goto unlock;
 
+	if (licence == GPL_ONLY)
+		mod->using_gplonly_symbols = true;
+
+	if (owner && test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints)) {
+		if (mod->using_gplonly_symbols) {
+			sym = NULL;
+			goto getname;
+		}
+		add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
+				 LOCKDEP_NOW_UNRELIABLE);
+	}
+
 	if (!check_version(info, name, mod, crc)) {
 		sym = ERR_PTR(-EINVAL);
 		goto getname;
Signature · 1 line
2.27.0
Reviewed-by on v1 7/7
#14 lore raw

Re: [PATCH 7/7] modules: inherit TAINT_PROPRIETARY_MODULE

Quoted 1 line · Christoph Hellwig

If a TAINT_PROPRIETARY_MODULE exports symbol, inherit the tain flag

s/tain/taint/

Quoted 6 lines

for all modules importing these symbols, and don't allow loading symbols from TAINT_PROPRIETARY_MODULE modules if the module previously imported gplonly symbols. Add a anti-circumvention devices so people don't accidentally get themselves into trouble this way.

Ah, the proven-to-be-illegal "GPL Condom" defense :)

Nice, work, thanks for doing this: