Skip to contentSkip to conversation

module: Add module name to modinfo

PATCH Superseded Kees Cook 2 participants 5 messages Apr 12, 2017 – Apr 21, 2017 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

cover module: Add module name to modinfo

The mod structure is accessed for the "name" field prior to validating sanity in check_modstruct_version(). This becomes very obvious once struct layout randomization is happening, so instead add the module name to modinfo and use that until the mod struct has been sanity-checked.

-Kees

author
#1 lore raw

PATCH 1/2 module: Pass struct load_info into symbol checks

Since we're already using values from struct load_info, just pass this pointer in directly and use what's needed as we need it. This allows us to access future fields in struct load_info too.

---
 kernel/module.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
kernel/module.c +10 -12
index 7eba6dea4f41..ed6cf2367217 100644
@@ -1257,12 +1257,13 @@ static u32 resolve_rel_crc(const s32 *crc)
 	return *(u32 *)((void *)crc + *crc);
 }
 
-static int check_version(Elf_Shdr *sechdrs,
-			 unsigned int versindex,
+static int check_version(const struct load_info *info,
 			 const char *symname,
 			 struct module *mod,
 			 const s32 *crc)
 {
+	Elf_Shdr *sechdrs = info->sechdrs;
+	unsigned int versindex = info->index.vers;
 	unsigned int i, num_versions;
 	struct modversion_info *versions;
 
@@ -1305,8 +1306,7 @@ static int check_version(Elf_Shdr *sechdrs,
 	return 0;
 }
 
-static inline int check_modstruct_version(Elf_Shdr *sechdrs,
-					  unsigned int versindex,
+static inline int check_modstruct_version(const struct load_info *info,
 					  struct module *mod)
 {
 	const s32 *crc;
@@ -1322,8 +1322,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
 		BUG();
 	}
 	preempt_enable();
-	return check_version(sechdrs, versindex,
-			     VMLINUX_SYMBOL_STR(module_layout), mod, crc);
+	return check_version(info, VMLINUX_SYMBOL_STR(module_layout),
+			     mod, crc);
 }
 
 /* First part is kernel version, which we ignore if module has crcs. */
@@ -1337,8 +1337,7 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 	return strcmp(amagic, bmagic) == 0;
 }
 #else
-static inline int check_version(Elf_Shdr *sechdrs,
-				unsigned int versindex,
+static inline int check_version(const struct load_info *info,
 				const char *symname,
 				struct module *mod,
 				const s32 *crc)
@@ -1346,8 +1345,7 @@ static inline int check_version(Elf_Shdr *sechdrs,
 	return 1;
 }
 
-static inline int check_modstruct_version(Elf_Shdr *sechdrs,
-					  unsigned int versindex,
+static inline int check_modstruct_version(const struct load_info *info,
 					  struct module *mod)
 {
 	return 1;
@@ -1383,7 +1381,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod,
 	if (!sym)
 		goto unlock;
 
-	if (!check_version(info->sechdrs, info->index.vers, name, mod, crc)) {
+	if (!check_version(info, name, mod, crc)) {
 		sym = ERR_PTR(-EINVAL);
 		goto getname;
 	}
@@ -2950,7 +2948,7 @@ static struct module *setup_load_info(struct load_info *info, int flags)
 	info->index.pcpu = find_pcpusec(info);
 
 	/* Check module struct version now, before we try to use module. */
-	if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
+	if (!check_modstruct_version(info, mod))
 		return ERR_PTR(-ENOEXEC);
 
 	return mod;
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH 2/2 module: Add module name to modinfo

In reply to Kees Cook

Accessing the mod structure (e.g. for mod->name) prior to having completed check_modstruct_version() can result in writing garbage to the error logs if the layout of the mod structure loaded from disk doesn't match the running kernel's mod structure layout. This kind of mismatch will become much more likely if a kernel is built with different randomization seed for the struct layout randomization plugin.

Instead, add and use a new modinfo string for logging the module name.

---
 kernel/module.c       | 21 ++++++++++++++-------
 scripts/mod/modpost.c |  1 +
 2 files changed, 15 insertions(+), 7 deletions(-)
kernel/module.c +14 -7
index ed6cf2367217..ed4a399174ba 100644
@@ -299,6 +299,7 @@ int unregister_module_notifier(struct notifier_block *nb)
 EXPORT_SYMBOL(unregister_module_notifier);
 
 struct load_info {
+	char *name;
 	Elf_Ehdr *hdr;
 	unsigned long len;
 	Elf_Shdr *sechdrs;
@@ -1297,12 +1298,12 @@ static int check_version(const struct load_info *info,
 	}
 
 	/* Broken toolchain. Warn once, then let it go.. */
-	pr_warn_once("%s: no symbol version for %s\n", mod->name, symname);
+	pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
 	return 1;
 
 bad_version:
 	pr_warn("%s: disagrees about version of symbol %s\n",
-	       mod->name, symname);
+	       info->name, symname);
 	return 0;
 }
 
@@ -2892,9 +2893,15 @@ static int rewrite_section_headers(struct load_info *info, int flags)
 		info->index.vers = 0; /* Pretend no __versions section! */
 	else
 		info->index.vers = find_sec(info, "__versions");
+	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+
 	info->index.info = find_sec(info, ".modinfo");
+	if (!info->index.info)
+		info->name = "unknown";
+	else
+		info->name = get_modinfo(info, "name");
 	info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
-	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+
 	return 0;
 }
 
@@ -2934,14 +2941,14 @@ static struct module *setup_load_info(struct load_info *info, int flags)
 
 	info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
 	if (!info->index.mod) {
-		pr_warn("No module found in object\n");
+		pr_warn("%s: No module found in object\n", info->name);
 		return ERR_PTR(-ENOEXEC);
 	}
 	/* This is temporary: point mod into copy of data. */
 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
 
 	if (info->index.sym == 0) {
-		pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
+		pr_warn("%s: module has no symbols (stripped?)\n", info->name);
 		return ERR_PTR(-ENOEXEC);
 	}
 
@@ -2969,7 +2976,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
 			return err;
 	} else if (!same_magic(modmagic, vermagic, info->index.vers)) {
 		pr_err("%s: version magic '%s' should be '%s'\n",
-		       mod->name, modmagic, vermagic);
+		       info->name, modmagic, vermagic);
 		return -ENOEXEC;
 	}
 
@@ -3622,7 +3629,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	if (!mod->sig_ok) {
 		pr_notice_once("%s: module verification failed: signature "
 			       "and/or required key missing - tainting "
-			       "kernel\n", mod->name);
+			       "kernel\n", info->name);
 		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
 	}
 #endif
scripts/mod/modpost.c +1 -0
index 30d752a4a6a6..48397feb08fb 100644
@@ -2126,6 +2126,7 @@ static void add_header(struct buffer *b, struct module *mod)
 	buf_printf(b, "#include <linux/compiler.h>\n");
 	buf_printf(b, "\n");
 	buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
+	buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
 	buf_printf(b, "\n");
 	buf_printf(b, "__visible struct module __this_module\n");
 	buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Signature · 1 line
2.7.4
#3 lore raw

+++ Kees Cook [12/04/17 16:16 -0700]:

Quoted 43 lines

Accessing the mod structure (e.g. for mod->name) prior to having completed check_modstruct_version() can result in writing garbage to the error logs if the layout of the mod structure loaded from disk doesn't match the running kernel's mod structure layout. This kind of mismatch will become much more likely if a kernel is built with different randomization seed for the struct layout randomization plugin.

Instead, add and use a new modinfo string for logging the module name.

---
kernel/module.c | 21 ++++++++++++++-------
scripts/mod/modpost.c | 1 +

2 files changed, 15 insertions(+), 7 deletions(-)
kernel/module.c +2 -1
index ed6cf2367217..ed4a399174ba 100644
@@ -299,6 +299,7 @@ int unregister_module_notifier(struct notifier_block *nb)
 EXPORT_SYMBOL(unregister_module_notifier);
 
 struct load_info {
+	char *name;
 	Elf_Ehdr *hdr;
 	unsigned long len;
 	Elf_Shdr *sechdrs;
@@ -1297,12 +1298,12 @@ static int check_version(const struct load_info *info,
 	}
 
 	/* Broken toolchain. Warn once, then let it go.. */
-	pr_warn_once("%s: no symbol version for %s\n", mod->name, symname);
+	pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
 	return 1;
 

bad_version:
pr_warn("%s: disagrees about version of symbol %s\n",

kernel/module.c +1 -1
-	       mod->name, symname);
+	       info->name, symname);
	return 0;
}

kernel/module.c static int rewrite_section_headers(struct load_info *info, int flags) · line 2902

Quoted 10 lines
kernel/module.c +6 -0
@@ -2892,9 +2893,15 @@ static int rewrite_section_headers(struct load_info *info, int flags)
 		info->index.vers = 0; /* Pretend no __versions section! */
 	else
 		info->index.vers = find_sec(info, "__versions");
+	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+
 	info->index.info = find_sec(info, ".modinfo");
+	if (!info->index.info)
+		info->name = "unknown";
+	else
+		info->name = get_modinfo(info, "name");

Hm, if we attempt to load an old module without the new name field (i.e. all modules currently), we'll end up printing "(null)" for all the prints that use info->name. Maybe we can fall back to mod->name if the name field isn't there and the kernel wasn't compiled with the randstruct plugin?

Quoted 24 lines
kernel/module.c +3 -3
 	info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
-	info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+
 	return 0;
 }
 
@@ -2934,14 +2941,14 @@ static struct module *setup_load_info(struct load_info *info, int flags)
 
 	info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
 	if (!info->index.mod) {
-		pr_warn("No module found in object\n");
+		pr_warn("%s: No module found in object\n", info->name);
 		return ERR_PTR(-ENOEXEC);
 	}
 	/* This is temporary: point mod into copy of data. */
 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
 
 	if (info->index.sym == 0) {
-		pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
+		pr_warn("%s: module has no symbols (stripped?)\n", info->name);
 		return ERR_PTR(-ENOEXEC);
 	}
 

kernel/module.c static int check_modinfo(struct module *mod, struct load_info *info, int flags) · line 2982

Quoted 8 lines
kernel/module.c +1 -1
@@ -2969,7 +2976,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
 			return err;
 	} else if (!same_magic(modmagic, vermagic, info->index.vers)) {
 		pr_err("%s: version magic '%s' should be '%s'\n",
-		       mod->name, modmagic, vermagic);
+		       info->name, modmagic, vermagic);
 		return -ENOEXEC;
 	}
 

Maybe we want to use info->name for the blacklisted() check as well (see layout_and_allocate()).

kernel/module.c static int load_module(struct load_info *info, const char __user *uargs, · line 3634

Quoted 8 lines
kernel/module.c +1 -1
@@ -3622,7 +3629,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	if (!mod->sig_ok) {
 		pr_notice_once("%s: module verification failed: signature "
 			       "and/or required key missing - tainting "
-			       "kernel\n", mod->name);
+			       "kernel\n", info->name);
 		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
 	}

Do we still need to use info->name here? At this point we're done with layout_and_allocate(), plus the modstruct check and vermagic check, and mod should be pointing to its new place in memory.

Quoted 16 lines
kernel/module.c +0 -0
 #endif
scripts/mod/modpost.c +1 -0
index 30d752a4a6a6..48397feb08fb 100644
@@ -2126,6 +2126,7 @@ static void add_header(struct buffer *b, struct module *mod)
 	buf_printf(b, "#include <linux/compiler.h>\n");
 	buf_printf(b, "\n");
 	buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
+	buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
 	buf_printf(b, "\n");
 	buf_printf(b, "__visible struct module __this_module\n");
 	buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Signature · 1 line
2.7.4
author
#4 lore raw
Quoted 60 lines · Jessica Yu

+++ Kees Cook [12/04/17 16:16 -0700]:

Quoted 58 lines

Accessing the mod structure (e.g. for mod->name) prior to having completed check_modstruct_version() can result in writing garbage to the error logs if the layout of the mod structure loaded from disk doesn't match the running kernel's mod structure layout. This kind of mismatch will become much more likely if a kernel is built with different randomization seed for the struct layout randomization plugin.

Instead, add and use a new modinfo string for logging the module name.

---
kernel/module.c | 21 ++++++++++++++-------
scripts/mod/modpost.c | 1 +

2 files changed, 15 insertions(+), 7 deletions(-)
kernel/module.c +1 -0
index ed6cf2367217..ed4a399174ba 100644
@@ -299,6 +299,7 @@ int unregister_module_notifier(struct notifier_block
 *nb)
 EXPORT_SYMBOL(unregister_module_notifier);
 
 struct load_info {
+       char *name;
        Elf_Ehdr *hdr;
        unsigned long len;
        Elf_Shdr *sechdrs;
kernel/module.c +1 -1
@@ -1297,12 +1298,12 @@ static int check_version(const struct load_info
 *info,
        }
 
        /* Broken toolchain. Warn once, then let it go.. */
-       pr_warn_once("%s: no symbol version for %s\n", mod->name,
 symname);
+       pr_warn_once("%s: no symbol version for %s\n", info->name,
 symname);
        return 1;
 

bad_version:
pr_warn("%s: disagrees about version of symbol %s\n",

kernel/module.c +1 -1
-              mod->name, symname);
+              info->name, symname);
        return 0;

}

kernel/module.c +5 -0
@@ -2892,9 +2893,15 @@ static int rewrite_section_headers(struct load_info
 *info, int flags)
                info->index.vers = 0; /* Pretend no __versions section! */
        else
                info->index.vers = find_sec(info, "__versions");
+       info->sechdrs[info->index.vers].sh_flags &= ~(unsigned
 long)SHF_ALLOC;
+
        info->index.info = find_sec(info, ".modinfo");
+       if (!info->index.info)
+               info->name = "unknown";
+       else
Quoted 8 lines

kernel/module.c static int rewrite_section_headers(struct load_info *info, int flags) · line 2902

Quoted 1 line
kernel/module.c +1 -0
+               info->name = get_modinfo(info, "name");

Hm, if we attempt to load an old module without the new name field (i.e. all modules currently), we'll end up printing "(null)" for all the prints that use info->name. Maybe we can fall back to mod->name if the name field isn't there and the kernel wasn't compiled with the randstruct plugin?

Ah yes, excellent point. I'll fix this.

Quoted 29 lines
Quoted 29 lines
kernel/module.c +0 -1
        info->sechdrs[info->index.info].sh_flags &= ~(unsigned
 long)SHF_ALLOC;
-       info->sechdrs[info->index.vers].sh_flags &= ~(unsigned

long)SHF_ALLOC;
+

        return 0;
}
kernel/module.c +2 -2
@@ -2934,14 +2941,14 @@ static struct module *setup_load_info(struct
 load_info *info, int flags)
 
        info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
        if (!info->index.mod) {
-               pr_warn("No module found in object\n");
+               pr_warn("%s: No module found in object\n", info->name);
                return ERR_PTR(-ENOEXEC);
        }
        /* This is temporary: point mod into copy of data. */
        mod = (void *)info->sechdrs[info->index.mod].sh_addr;
 
        if (info->index.sym == 0) {
-               pr_warn("%s: module has no symbols (stripped?)\n",
 mod->name);
+               pr_warn("%s: module has no symbols (stripped?)\n",
 info->name);
                return ERR_PTR(-ENOEXEC);
        }
Quoted 13 lines

kernel/module.c static int check_modinfo(struct module *mod, struct · line 2982

Quoted 10 lines
kernel/module.c +1 -1
@@ -2969,7 +2976,7 @@ static int check_modinfo(struct module *mod, struct
 load_info *info, int flags)
                        return err;
        } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
                pr_err("%s: version magic '%s' should be '%s'\n",
-                      mod->name, modmagic, vermagic);
+                      info->name, modmagic, vermagic);
                return -ENOEXEC;
        }

Maybe we want to use info->name for the blacklisted() check as well (see layout_and_allocate()).

Eek! Yes, good catch.

Quoted 17 lines
Quoted 10 lines
kernel/module.c +0 -0
@@ -3622,7 +3629,7 @@ static int load_module(struct load_info *info, const
 char __user *uargs,
        if (!mod->sig_ok) {
                pr_notice_once("%s: module verification failed: signature

"
"and/or required key missing - tainting "

kernel/module.c +1 -1
-                              "kernel\n", mod->name);
+                              "kernel\n", info->name);
                add_taint_module(mod, TAINT_UNSIGNED_MODULE,
Quoted 2 lines
LOCKDEP_STILL_OK);
        }

Do we still need to use info->name here? At this point we're done with layout_and_allocate(), plus the modstruct check and vermagic check, and mod should be pointing to its new place in memory.

Ah, you're right. I'll drop this.

Quoted 12 lines
Quoted 12 lines

#endif

scripts/mod/modpost.c +0 -0
index 30d752a4a6a6..48397feb08fb 100644
@@ -2126,6 +2126,7 @@ static void add_header(struct buffer *b, struct
 module *mod)
        buf_printf(b, "#include <linux/compiler.h>\n");
        buf_printf(b, "\n");
        buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Quoted 9 lines
Quoted 8 lines
scripts/mod/modpost.c +1 -0
+       buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
        buf_printf(b, "\n");
        buf_printf(b, "__visible struct module __this_module\n");

buf_printf(b,
"__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); --
2.7.4

Thanks for the review!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security