Skip to contentSkip to conversation

module: Move extra signature support out of core code

PATCH Superseded Aaron Tomlin 4 participants 13 messages Feb 9, 2022 – Feb 11, 2022

Subsystems (2)

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 7/13 module: Move extra signature support out of core code

No functional change.

This patch migrates additional module signature check code from core module code into kernel/module/signing.c.

---
 include/linux/module.h   |  1 +
 kernel/module/internal.h |  9 +++++
 kernel/module/main.c     | 87 ----------------------------------------
 kernel/module/signing.c  | 75 ++++++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+), 87 deletions(-)
include/linux/module.h +1 -0
index fd6161d78127..aea0ffd94a41 100644
@@ -863,6 +863,7 @@ static inline bool module_sig_ok(struct module *module)
 {
 	return true;
 }
+#define sig_enforce false
 #endif	/* CONFIG_MODULE_SIG */
 
 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
kernel/module/internal.h +9 -0
index 99204447ce86..6d5891cc8421 100644
@@ -162,3 +162,12 @@ static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
 	return 0;
 }
 #endif /* CONFIG_STRICT_MODULE_RWX */
+
+#ifdef CONFIG_MODULE_SIG
+int module_sig_check(struct load_info *info, int flags);
+#else /* !CONFIG_MODULE_SIG */
+static inline int module_sig_check(struct load_info *info, int flags)
+{
+	return 0;
+}
+#endif /* !CONFIG_MODULE_SIG */
kernel/module/main.c +0 -87

105 lines not on this page · raw message

kernel/module/signing.c +75 -0

87 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 8/13 module: Move kmemleak support to a separate file

No functional change.

This patch migrates kmemleak code out of core module code into kernel/module/debug_kmemleak.c

---
 kernel/module/Makefile         |  1 +
 kernel/module/debug_kmemleak.c | 30 ++++++++++++++++++++++++++++++
 kernel/module/internal.h       |  7 +++++++
 kernel/module/main.c           | 27 ---------------------------
 4 files changed, 38 insertions(+), 27 deletions(-)
 create mode 100644 kernel/module/debug_kmemleak.c
kernel/module/Makefile +1 -0
index 8f2857d9ba1e..62c9fc91d411 100644
@@ -11,4 +11,5 @@ ifdef CONFIG_MODULES
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
+obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
 endif
kernel/module/debug_kmemleak.c new file +30 -0
new file mode 100644
index 000000000000..e896c2268011
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Module kmemleak support
+ *
+ * Copyright (C) 2009 Catalin Marinas
+ */
+
+#include <linux/module.h>
+#include <linux/kmemleak.h>
+#include "internal.h"
+
+void kmemleak_load_module(const struct module *mod,
+				 const struct load_info *info)
+{
+	unsigned int i;
+
+	/* only scan the sections containing data */
+	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
+
+	for (i = 1; i < info->hdr->e_shnum; i++) {
+		/* Scan all writable sections that's not executable */
+		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
+		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
+		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
+			continue;
+
+		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
+				   info->sechdrs[i].sh_size, GFP_KERNEL);
+	}
+}
kernel/module/internal.h +7 -0
index 6d5891cc8421..33d7befd0602 100644
@@ -171,3 +171,10 @@ static inline int module_sig_check(struct load_info *info, int flags)
 	return 0;
 }
 #endif /* !CONFIG_MODULE_SIG */
+
+#ifdef CONFIG_DEBUG_KMEMLEAK
+void kmemleak_load_module(const struct module *mod, const struct load_info *info);
+#else /* !CONFIG_DEBUG_KMEMLEAK */
+static inline void __maybe_unused kmemleak_load_module(const struct module *mod,
+						       const struct load_info *info) { }
+#endif /* CONFIG_DEBUG_KMEMLEAK */
kernel/module/main.c +0 -27
index 403f2aacb3f6..c9931479e2eb 100644
@@ -2515,33 +2515,6 @@ bool __weak module_exit_section(const char *name)
 	return strstarts(name, ".exit");
 }
 
-#ifdef CONFIG_DEBUG_KMEMLEAK
-static void kmemleak_load_module(const struct module *mod,
-				 const struct load_info *info)
-{
-	unsigned int i;
-
-	/* only scan the sections containing data */
-	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
-
-	for (i = 1; i < info->hdr->e_shnum; i++) {
-		/* Scan all writable sections that's not executable */
-		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
-		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
-		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
-			continue;
-
-		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
-				   info->sechdrs[i].sh_size, GFP_KERNEL);
-	}
-}
-#else
-static inline void kmemleak_load_module(const struct module *mod,
-					const struct load_info *info)
-{
-}
-#endif
-
 static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
 {
 #if defined(CONFIG_64BIT)
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v5 08/13] module: Move kmemleak support to a separate file

Le 09/02/2022 à 18:08, Aaron Tomlin a écrit :

Quoted 33 lines

No functional change.

This patch migrates kmemleak code out of core module code into kernel/module/debug_kmemleak.c

---
  kernel/module/Makefile         |  1 +
  kernel/module/debug_kmemleak.c | 30 ++++++++++++++++++++++++++++++
  kernel/module/internal.h       |  7 +++++++
  kernel/module/main.c           | 27 ---------------------------
  4 files changed, 38 insertions(+), 27 deletions(-)

create mode 100644 kernel/module/debug_kmemleak.c

kernel/module/Makefile +1 -0
index 8f2857d9ba1e..62c9fc91d411 100644
@@ -11,4 +11,5 @@ ifdef CONFIG_MODULES
  obj-$(CONFIG_LIVEPATCH) += livepatch.o
  obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
  obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
+obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
  endif
kernel/module/debug_kmemleak.c new file +3 -0
new file mode 100644
index 000000000000..e896c2268011
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Module kmemleak support

kernel/module/debug_kmemleak.c line 13

Quoted 10 lines
kernel/module/debug_kmemleak.c new file +10 -0
+ *
+ * Copyright (C) 2009 Catalin Marinas
+ */
+
+#include <linux/module.h>
+#include <linux/kmemleak.h>
+#include "internal.h"
+
+void kmemleak_load_module(const struct module *mod,
+				 const struct load_info *info)

CHECK: Alignment should match open parenthesis #48: FILE: kernel/module/debug_kmemleak.c:13:
+void kmemleak_load_module(const struct module *mod,
+ const struct load_info *info)

Quoted 22 lines
kernel/module/debug_kmemleak.c new file +17 -0
+{
+	unsigned int i;
+
+	/* only scan the sections containing data */
+	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
+
+	for (i = 1; i < info->hdr->e_shnum; i++) {
+		/* Scan all writable sections that's not executable */
+		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
+		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
+		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
+			continue;
+
+		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
+				   info->sechdrs[i].sh_size, GFP_KERNEL);
+	}
+}
kernel/module/internal.h +0 -0
index 6d5891cc8421..33d7befd0602 100644

kernel/module/internal.h static inline int module_sig_check(struct load_info *info, int flags) · line 179

Quoted 9 lines
kernel/module/internal.h +6 -0
@@ -171,3 +171,10 @@ static inline int module_sig_check(struct load_info *info, int flags)
  	return 0;
  }
  #endif /* !CONFIG_MODULE_SIG */
+
+#ifdef CONFIG_DEBUG_KMEMLEAK
+void kmemleak_load_module(const struct module *mod, const struct load_info *info);
+#else /* !CONFIG_DEBUG_KMEMLEAK */
+static inline void __maybe_unused kmemleak_load_module(const struct module *mod,
+						       const struct load_info *info) { }

Remove __maybe_unused, not needed for a 'static inline' in the .h

Quoted 39 lines
kernel/module/internal.h +1 -0
+#endif /* CONFIG_DEBUG_KMEMLEAK */
kernel/module/main.c +0 -27
index 403f2aacb3f6..c9931479e2eb 100644
@@ -2515,33 +2515,6 @@ bool __weak module_exit_section(const char *name)
  	return strstarts(name, ".exit");
  }
 
-#ifdef CONFIG_DEBUG_KMEMLEAK
-static void kmemleak_load_module(const struct module *mod,
-				 const struct load_info *info)
-{
-	unsigned int i;
-
-	/* only scan the sections containing data */
-	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
-
-	for (i = 1; i < info->hdr->e_shnum; i++) {
-		/* Scan all writable sections that's not executable */
-		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
-		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
-		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
-			continue;
-
-		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
-				   info->sechdrs[i].sh_size, GFP_KERNEL);
-	}
-}
-#else
-static inline void kmemleak_load_module(const struct module *mod,
-					const struct load_info *info)
-{
-}
-#endif
-
  static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
  {
  #if defined(CONFIG_64BIT)
reviewer
#3 lore raw

Re: [PATCH v5 08/13] module: Move kmemleak support to a separate file

kernel/module/debug_kmemleak.c line 13

Quoted 4 lines · Christophe Leroy

CHECK: Alignment should match open parenthesis
#48: FILE: kernel/module/debug_kmemleak.c:13:

kernel/module/debug_kmemleak.c new file +2 -0
+void kmemleak_load_module(const struct module *mod,
+				 const struct load_info *info)

Ok.

Quoted 4 lines

kernel/module/internal.h static inline int module_sig_check(struct load_info *info, int flags) · line 179

Quoted 2 lines
kernel/module/internal.h +2 -0
+static inline void __maybe_unused kmemleak_load_module(const struct module *mod,
+						       const struct load_info *info) { }

Remove __maybe_unused, not needed for a 'static inline' in the .h

Agreed.

Thanks for your feedback Christophe.

Kind regards,

Signature · 1 line
Aaron Tomlin
#4 lore raw

PATCH v5 9/13 module: Move kallsyms support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates kallsyms code out of core module code kernel/module/kallsyms.c

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |  27 ++
 kernel/module/kallsyms.c | 502 +++++++++++++++++++++++++++++++++++++
 kernel/module/main.c     | 518 +--------------------------------------
 4 files changed, 534 insertions(+), 514 deletions(-)
 create mode 100644 kernel/module/kallsyms.c
kernel/module/Makefile +1 -0
index 62c9fc91d411..868b13c06920 100644
@@ -12,4 +12,5 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
+obj-$(CONFIG_KALLSYMS) += kallsyms.o
 endif
kernel/module/internal.h +27 -0
index 33d7befd0602..7973666452c3 100644
@@ -69,6 +69,11 @@ struct load_info {
 };
 
 int mod_verify_sig(const void *mod, struct load_info *info);
+struct module *find_module_all(const char *name, size_t len, bool even_unformed);
+unsigned long kernel_symbol_value(const struct kernel_symbol *sym);
+int cmp_name(const void *name, const void *sym);
+long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
+		       unsigned int section);
 
 #ifdef CONFIG_LIVEPATCH
 int copy_module_elf(struct module *mod, struct load_info *info);
@@ -178,3 +183,25 @@ void kmemleak_load_module(const struct module *mod, const struct load_info *info
 static inline void __maybe_unused kmemleak_load_module(const struct module *mod,
 						       const struct load_info *info) { }
 #endif /* CONFIG_DEBUG_KMEMLEAK */
+
+#ifdef CONFIG_KALLSYMS
+#ifdef CONFIG_STACKTRACE_BUILD_ID
+void init_build_id(struct module *mod, const struct load_info *info);
+#else /* !CONFIG_STACKTRACE_BUILD_ID */
+static inline void init_build_id(struct module *mod, const struct load_info *info) { }
+
+#endif
+void layout_symtab(struct module *mod, struct load_info *info);
+void add_kallsyms(struct module *mod, const struct load_info *info);
+bool sect_empty(const Elf_Shdr *sect);
+const char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+					unsigned long *size, unsigned long *offset);
+#else /* !CONFIG_KALLSYMS */
+static inline void layout_symtab(struct module *mod, struct load_info *info) { }
+static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
+static inline char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+					 unsigned long *size, unsigned long *offset)
+{
+	return NULL;
+}
+#endif /* CONFIG_KALLSYMS */
kernel/module/kallsyms.c new file +502 -0

502 lines not on this page · raw message

kernel/module/main.c +4 -514

560 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v5 09/13] module: Move kallsyms support into a separate file

Le 09/02/2022 à 18:08, Aaron Tomlin a écrit :

Quoted 5 lines

No functional change.

This patch migrates kallsyms code out of core module code kernel/module/kallsyms.c

Quoted 8 lines
---
  kernel/module/Makefile   |   1 +
  kernel/module/internal.h |  27 ++
  kernel/module/kallsyms.c | 502 +++++++++++++++++++++++++++++++++++++
  kernel/module/main.c     | 518 +--------------------------------------
  4 files changed, 534 insertions(+), 514 deletions(-)

create mode 100644 kernel/module/kallsyms.c

Checkpatch reports:

total: 3 errors, 1 warnings, 26 checks, 1103 lines checked

Sparse reports the following:

CHECK kernel/module/kallsyms.c
kernel/module/kallsyms.c:174:23: warning: incorrect type in assignment (different address spaces)
kernel/module/kallsyms.c:174:23: expected struct mod_kallsyms
[noderef] __rcu *kallsyms
kernel/module/kallsyms.c:174:23: got void *
kernel/module/kallsyms.c:176:12: warning: dereference of noderef expression kernel/module/kallsyms.c:177:12: warning: dereference of noderef expression kernel/module/kallsyms.c:179:12: warning: dereference of noderef expression kernel/module/kallsyms.c:180:12: warning: dereference of noderef expression kernel/module/kallsyms.c:189:18: warning: dereference of noderef expression kernel/module/kallsyms.c:190:35: warning: dereference of noderef expression kernel/module/kallsyms.c:191:20: warning: dereference of noderef expression kernel/module/kallsyms.c:196:32: warning: dereference of noderef expression kernel/module/kallsyms.c:199:45: warning: dereference of noderef expression

Quoted 16 lines
kernel/module/Makefile +1 -0
index 62c9fc91d411..868b13c06920 100644
@@ -12,4 +12,5 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o
  obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
  obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
  obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
+obj-$(CONFIG_KALLSYMS) += kallsyms.o
  endif
kernel/module/internal.h +0 -0
index 33d7befd0602..7973666452c3 100644

kernel/module/internal.h struct load_info { · line 73

Quoted 5 lines
kernel/module/internal.h +2 -0
@@ -69,6 +69,11 @@ struct load_info {
  };
 
  int mod_verify_sig(const void *mod, struct load_info *info);
+struct module *find_module_all(const char *name, size_t len, bool even_unformed);
+unsigned long kernel_symbol_value(const struct kernel_symbol *sym);

This function is small enought to be a 'static inline' in internal.h

kernel/module/internal.h struct load_info { · line 76

Quoted 3 lines
kernel/module/internal.h +3 -0
+int cmp_name(const void *name, const void *sym);
+long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
+		       unsigned int section);

Having a non static function called get_offset() seems dangerous.

There are already several get_offset() functions in the kernel allthough they are all static.

It takes a struct module as an argument so it could be called module_get_offset()

Quoted 8 lines
kernel/module/internal.h +1 -0
  #ifdef CONFIG_LIVEPATCH
  int copy_module_elf(struct module *mod, struct load_info *info);
@@ -178,3 +183,25 @@ void kmemleak_load_module(const struct module *mod, const struct load_info *info
  static inline void __maybe_unused kmemleak_load_module(const struct module *mod,
  						       const struct load_info *info) { }
  #endif /* CONFIG_DEBUG_KMEMLEAK */
+

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 196

Quoted 10 lines
kernel/module/internal.h +10 -0
+#ifdef CONFIG_KALLSYMS
+#ifdef CONFIG_STACKTRACE_BUILD_ID
+void init_build_id(struct module *mod, const struct load_info *info);
+#else /* !CONFIG_STACKTRACE_BUILD_ID */
+static inline void init_build_id(struct module *mod, const struct load_info *info) { }
+
+#endif
+void layout_symtab(struct module *mod, struct load_info *info);
+void add_kallsyms(struct module *mod, const struct load_info *info);
+bool sect_empty(const Elf_Shdr *sect);

sect_empty() is small enough to remain a static inline.

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 198

Quoted 2 lines
kernel/module/internal.h +2 -0
+const char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+					unsigned long *size, unsigned long *offset);

This is not used outside kallsyms.c, no need to have it in internal.h

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 203

Quoted 5 lines
kernel/module/internal.h +5 -0
+#else /* !CONFIG_KALLSYMS */
+static inline void layout_symtab(struct module *mod, struct load_info *info) { }
+static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
+static inline char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+					 unsigned long *size, unsigned long *offset)

This is not used outside kallsyms.c, no need to have it when !CONFIG_KALLSYMS

Quoted 10 lines
kernel/module/internal.h +4 -0
+{
+	return NULL;
+}
+#endif /* CONFIG_KALLSYMS */
kernel/module/kallsyms.c new file +0 -0
new file mode 100644
index 000000000000..ed28f6310701
@@ -0,0 +1,502 @@

...

kernel/module/kallsyms.c line 431

Quoted 3 lines
kernel/module/kallsyms.c new file +3 -0
+
+/* Given a module and name of symbol, find and return the symbol's value */
+static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)

This function is called from main.c, it can't be static and must be defined in internal.h

Quoted 14 lines
kernel/module/kallsyms.c new file +14 -0
+{
+	unsigned int i;
+	struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
+
+	for (i = 0; i < kallsyms->num_symtab; i++) {
+		const Elf_Sym *sym = &kallsyms->symtab[i];
+
+		if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
+		    sym->st_shndx != SHN_UNDEF)
+			return kallsyms_symbol_value(sym);
+	}
+	return 0;
+}
+

...

kernel/module/main.c static bool check_exported_symbol(const struct symsearch *syms, · line 288

Quoted 10 lines
kernel/module/main.c +1 -1
index c9931479e2eb..378dd7fd1b6a 100644
@@ -285,7 +285,7 @@ static bool check_exported_symbol(const struct symsearch *syms,
  	return true;
  }
 
-static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
+unsigned long kernel_symbol_value(const struct kernel_symbol *sym)

This function is small enought to become a 'static inline' in internal.h

kernel/module/main.c static const char *kernel_symbol_namespace(const struct kernel_symbol *sym) · line 317

Quoted 9 lines
kernel/module/main.c +1 -1
  {
  #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  	return (unsigned long)offset_to_ptr(&sym->value_offset);
@@ -314,7 +314,7 @@ static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
  #endif
  }
 
-static int cmp_name(const void *name, const void *sym)
+int cmp_name(const void *name, const void *sym)

This function is small enought to become a 'static inline' in internal.h

Quoted 39 lines
kernel/module/main.c +2 -9
  {
  	return strcmp(name, kernel_symbol_name(sym));
  }
@@ -384,7 +384,7 @@ static bool find_symbol(struct find_symbol_arg *fsa)
   * Search for module by name: must hold module_mutex (or preempt disabled
   * for read-only access).
   */
-static struct module *find_module_all(const char *name, size_t len,
+struct module *find_module_all(const char *name, size_t len,
  				      bool even_unformed)
  {
  	struct module *mod;
@@ -1291,13 +1291,6 @@ resolve_symbol_wait(struct module *mod,
  	return ksym;
  }
 
-#ifdef CONFIG_KALLSYMS
-static inline bool sect_empty(const Elf_Shdr *sect)
-{
-	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
-}
-#endif
-
  /*
   * /sys/module/foo/sections stuff
   * J. Corbet <[email protected]>
@@ -2061,7 +2054,7 @@ unsigned int __weak arch_mod_section_prepend(struct module *mod,
  }
 
  /* Update size with this section: return offset. */
-static long get_offset(struct module *mod, unsigned int *size,
+long get_offset(struct module *mod, unsigned int *size,
  		       Elf_Shdr *sechdr, unsigned int section)
  {
  	long ret;
@@ -2263,228 +2256,6 @@ static void free_modinfo(struct module *mod)
  	}
  }
 

...

#6 lore raw

Re: [PATCH v5 09/13] module: Move kallsyms support into a separate file

Quoted 3 lines · Christophe Leroy

Checkpatch reports:

total: 3 errors, 1 warnings, 26 checks, 1103 lines checked

Christophe,

Quoted 8 lines

Sparse reports the following:

CHECK kernel/module/kallsyms.c
kernel/module/kallsyms.c:174:23: warning: incorrect type in assignment (different address spaces)
kernel/module/kallsyms.c:174:23: expected struct mod_kallsyms
[noderef] __rcu *kallsyms
kernel/module/kallsyms.c:174:23: got void *

Thanks once again for your review and feedback!

Indeed I can see the same via 'make C=2 kernel/module/'. Looking at struct 'module' declaration we see that field namely "kallsyms" has the __rcu marker. So, If I understand correctly, perhaps this can be resolved as follows, to be more explicit:

@@ -171,7 +171,7 @@ void add_kallsyms(struct module *mod, const struct load_info *info)

        Elf_Shdr *symsec = &info->sechdrs[info->index.sym];

        /* Set up to point into init section. */

- mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
+ mod->kallsyms = (struct mod_kallsyms __rcu
*)mod->init_layout.base + info->mod_kallsyms_init_off;

Quoted 9 lines

kernel/module/kallsyms.c:176:12: warning: dereference of noderef expression kernel/module/kallsyms.c:177:12: warning: dereference of noderef expression kernel/module/kallsyms.c:179:12: warning: dereference of noderef expression kernel/module/kallsyms.c:180:12: warning: dereference of noderef expression kernel/module/kallsyms.c:189:18: warning: dereference of noderef expression kernel/module/kallsyms.c:190:35: warning: dereference of noderef expression kernel/module/kallsyms.c:191:20: warning: dereference of noderef expression kernel/module/kallsyms.c:196:32: warning: dereference of noderef expression kernel/module/kallsyms.c:199:45: warning: dereference of noderef expression

I will use rcu_dereference*() for the above since the pointer should not be accessed directly.

Quoted 18 lines
Quoted 18 lines
kernel/module/Makefile +1 -0
index 62c9fc91d411..868b13c06920 100644
@@ -12,4 +12,5 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o
  obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
  obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
  obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
+obj-$(CONFIG_KALLSYMS) += kallsyms.o
  endif
kernel/module/internal.h +0 -0
index 33d7befd0602..7973666452c3 100644
@@ -69,6 +69,11 @@ struct load_info {
  };
 
Quoted 5 lines

kernel/module/internal.h struct load_info { · line 73

Quoted 3 lines
kernel/module/internal.h +2 -0
  int mod_verify_sig(const void *mod, struct load_info *info);
+struct module *find_module_all(const char *name, size_t len, bool even_unformed);
+unsigned long kernel_symbol_value(const struct kernel_symbol *sym);

This function is small enought to be a 'static inline' in internal.h

Fair enough.

Quoted 11 lines

kernel/module/internal.h struct load_info { · line 76

Quoted 3 lines
kernel/module/internal.h +3 -0
+int cmp_name(const void *name, const void *sym);
+long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
+               unsigned int section);

Having a non static function called get_offset() seems dangerous.

There are already several get_offset() functions in the kernel allthough they are all static.

It takes a struct module as an argument so it could be called module_get_offset()

The rename is a good idea.

Quoted 3 lines

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 196

Quoted 1 line
kernel/module/internal.h +1 -0
+bool sect_empty(const Elf_Shdr *sect);

sect_empty() is small enough to remain a static inline.

Yes and moved to kernel/module/internal.h.

Quoted 5 lines

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 198

Quoted 3 lines
kernel/module/internal.h +2 -0
+const char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+                    unsigned long *size, unsigned long *offset);

This is not used outside kallsyms.c, no need to have it in internal.h

Agreed.

Quoted 9 lines

kernel/module/internal.h void kmemleak_load_module(const struct module *mod, const struct load_info *info · line 203

Quoted 6 lines
kernel/module/internal.h +5 -0
+#else /* !CONFIG_KALLSYMS */
+static inline void layout_symtab(struct module *mod, struct load_info *info) { }
+static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
+static inline char *find_kallsyms_symbol(struct module *mod, unsigned long addr,
+                     unsigned long *size, unsigned long *offset)

This is not used outside kallsyms.c, no need to have it when !CONFIG_KALLSYMS

Agreed.

Quoted 12 lines
Quoted 11 lines
kernel/module/internal.h +4 -0
+{
+    return NULL;
+}
+#endif /* CONFIG_KALLSYMS */
kernel/module/kallsyms.c new file +0 -0
new file mode 100644
index 000000000000..ed28f6310701
@@ -0,0 +1,502 @@

...

Quoted 6 lines

kernel/module/kallsyms.c line 431

Quoted 3 lines
kernel/module/kallsyms.c new file +3 -0
+
+/* Given a module and name of symbol, find and return the symbol's value */
+static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)

This function is called from main.c, it can't be static and must be defined in internal.h

Agreed. This was an unfortunate oversight.

Quoted 4 lines

kernel/module/kallsyms.c

Quoted 2 lines
kernel/module/kallsyms.c new file +1 -1
-static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
+unsigned long kernel_symbol_value(const struct kernel_symbol *sym)

This function is small enought to become a 'static inline' in internal.h

Agreed.

Quoted 3 lines
Quoted 1 line

+int cmp_name(const void *name, const void *sym)

This function is small enought to become a 'static inline' in internal.h

Agreed.

Kind regards,

Signature · 1 line
Aaron Tomlin
#7 lore raw

PATCH v5 10/13 module: Move procfs support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates code that allows one to generate a list of loaded/or linked modules via /proc when procfs support is enabled into kernel/module/procfs.c.

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |   1 +
 kernel/module/main.c     | 131 +-----------------------------------
 kernel/module/procfs.c   | 142 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 130 deletions(-)
 create mode 100644 kernel/module/procfs.c
kernel/module/Makefile +1 -0
index 868b13c06920..c6be08060252 100644
@@ -13,4 +13,5 @@ obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
+obj-$(CONFIG_PROC_FS) += procfs.o
 endif
kernel/module/internal.h +1 -0
index 7973666452c3..b67ce836746a 100644
@@ -74,6 +74,7 @@ unsigned long kernel_symbol_value(const struct kernel_symbol *sym);
 int cmp_name(const void *name, const void *sym);
 long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
 		       unsigned int section);
+char *module_flags(struct module *mod, char *buf);
 
 #ifdef CONFIG_LIVEPATCH
 int copy_module_elf(struct module *mod, struct load_info *info);
kernel/module/main.c +1 -130

161 lines not on this page · raw message

kernel/module/procfs.c new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v5 10/13] module: Move procfs support into a separate file

Le 09/02/2022 à 18:08, Aaron Tomlin a écrit :

Quoted 6 lines

No functional change.

This patch migrates code that allows one to generate a list of loaded/or linked modules via /proc when procfs support is enabled into kernel/module/procfs.c.

Quoted 8 lines
---
  kernel/module/Makefile   |   1 +
  kernel/module/internal.h |   1 +
  kernel/module/main.c     | 131 +-----------------------------------
  kernel/module/procfs.c   | 142 +++++++++++++++++++++++++++++++++++++++
  4 files changed, 145 insertions(+), 130 deletions(-)

create mode 100644 kernel/module/procfs.c

Checkpatch:

total: 0 errors, 2 warnings, 2 checks, 315 lines checked

#9 lore raw
In reply to Aaron Tomlin

Hello,

Quoted 1 line · Aaron Tomlin

No functional change.

There is functional change.

Quoted 8 lines
kernel/module/main.c +0 -4
@@ -2565,70 +2542,6 @@ static inline void kmemleak_load_module(const struct module *mod,
 }
 #endif
 
-#ifdef CONFIG_MODULE_SIG
-static int module_sig_check(struct load_info *info, int flags)
-{
-	int err = -ENODATA;

kernel/module/main.c static inline void kmemleak_load_module(const struct module *mod, · line 2581

Quoted 10 lines
kernel/module/main.c +0 -10
-	const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
-	const char *reason;
-	const void *mod = info->hdr;
-	bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS |
-				       MODULE_INIT_IGNORE_VERMAGIC);
-	/*
-	 * Do not allow mangled modules as a module with version information
-	 * removed is no longer the module that was signed.
-	 */
-	if (!mangled_module &&
             ^^^^^^^^^^^^^
Quoted 23 lines
kernel/module/main.c +0 -10
-	    info->len > markerlen &&
-	    memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
-		/* We truncate the module to discard the signature */
-		info->len -= markerlen;
-		err = mod_verify_sig(mod, info);
-		if (!err) {
-			info->sig_ok = true;
-			return 0;
-		}
-	}
 
kernel/module/signing.c +3 -0
index 8aeb6d2ee94b..ff41541e982a 100644
@@ -43,3 +62,59 @@ int mod_verify_sig(const void *mod, struct load_info *info)
 				      VERIFYING_MODULE_SIGNATURE,
 				      NULL, NULL);
 }
+
+int module_sig_check(struct load_info *info, int flags)
+{

kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 77

Quoted 10 lines
kernel/module/signing.c +10 -0
+	int err = -ENODATA;
+	const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
+	const char *reason;
+	const void *mod = info->hdr;
+
+	/*
+	 * Require flags == 0, as a module with version information
+	 * removed is no longer the module that was signed
+	 */
+	if (flags == 0 &&
            ^^^^^^

This reverts a97ac8cb24a3c3ad74794adb83717ef1605d1b47

Please re-apply.

Thanks

Michal

Quoted 10 lines
kernel/module/signing.c +10 -0
+	    info->len > markerlen &&
+	    memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
+		/* We truncate the module to discard the signature */
+		info->len -= markerlen;
+		err = mod_verify_sig(mod, info);
+		if (!err) {
+			info->sig_ok = true;
+			return 0;
+		}
+	}
#10 lore raw
Quoted 1 line · Michal Suchánek

This reverts a97ac8cb24a3c3ad74794adb83717ef1605d1b47

Hi Michal,

Oops! I'll address this.

Thanks,

Signature · 1 line
Aaron Tomlin
#11 lore raw
In reply to Aaron Tomlin

Why do patches 7 to 13 have a Reply-to:
[email protected] and not patches 1 to 6 ?

Le 09/02/2022 à 18:08, Aaron Tomlin a écrit :

Quoted 18 lines

No functional change.

This patch migrates additional module signature check code from core module code into kernel/module/signing.c.

---
  include/linux/module.h   |  1 +
  kernel/module/internal.h |  9 +++++
  kernel/module/main.c     | 87 ----------------------------------------
  kernel/module/signing.c  | 75 ++++++++++++++++++++++++++++++++++
  4 files changed, 85 insertions(+), 87 deletions(-)
include/linux/module.h +0 -0
index fd6161d78127..aea0ffd94a41 100644

include/linux/module.h static inline bool module_sig_ok(struct module *module) · line 866

Quoted 4 lines
include/linux/module.h +1 -0
@@ -863,6 +863,7 @@ static inline bool module_sig_ok(struct module *module)
  {
  	return true;
  }
+#define sig_enforce false

Having that is module.h it may redefine some existing symbol, like in security/integrity/ima/ima_main.c

sig_enforce is used only in signing.c so it should be defined there exclusively. This #define shouldn't be needed at all.

And checkpatch is not happy:

CHECK: Please use a blank line after function/struct/union/enum declarations #27: FILE: include/linux/module.h:866:
}
+#define sig_enforce false

Quoted 3 lines
include/linux/module.h +0 -0
  #endif	/* CONFIG_MODULE_SIG */
 
  int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
#12 lore raw
Quoted 2 lines · Christophe Leroy

Why do patches 7 to 13 have a Reply-to:
[email protected] and not patches 1 to 6 ?

Christophe,

Please disregard this mishap. Unfortunately, at the time I hit the relay quota.

Quoted 11 lines

include/linux/module.h static inline bool module_sig_ok(struct module *module) · line 866

Quoted 9 lines
include/linux/module.h +1 -0
index fd6161d78127..aea0ffd94a41 100644
@@ -863,6 +863,7 @@ static inline bool module_sig_ok(struct module *module)
  {
      return true;
  }
+#define sig_enforce false

sig_enforce is used only in signing.c so it should be defined there exclusively.

Agreed.

Quoted 6 lines

And checkpatch is not happy:

CHECK: Please use a blank line after function/struct/union/enum declarations #27: FILE: include/linux/module.h:866:
}
+#define sig_enforce false

Ok.

Kind regards,

Signature · 1 line
Aaron Tomlin