Skip to contentSkip to conversation

scripts/mod/modpost: fix null pointer dereference

PATCH In review Max Kellermann 2 participants 5 messages Feb 15, 2024 – Mar 18, 2024 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/mod/modpost: fix null pointer dereference

If the find_fromsym() call fails and returns NULL, the warn() call will dereference this NULL pointer and cause the program to crash.

This happened when I tried to build with "test_user_copy" module. With this fix, it prints lots of warnings like this:

WARNING: modpost: lib/test_user_copy: section mismatch in reference: (unknown)+0x4 (section: .text.fixup) -> (unknown) (section: .init.text)

---
 scripts/mod/modpost.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/mod/modpost.c +3 -1
index bf7c4b4b5ff4..6b37039c9e92 100644
@@ -1050,7 +1050,9 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,
 	sec_mismatch_count++;
 
 	warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n",
-	     modname, fromsym, (unsigned int)(faddr - from->st_value), fromsec, tosym, tosec);
+	     modname, fromsym,
+	     (unsigned int)(faddr - (from ? from->st_value : 0)),
+	     fromsec, tosym, tosec);
 
 	if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
 		if (match(tosec, mismatch->bad_tosec))
Signature · 1 line
2.39.2
#1 lore raw
Quoted 5 lines · Max Kellermann

If the find_fromsym() call fails and returns NULL, the warn() call will dereference this NULL pointer and cause the program to crash.

This happened when I tried to build with "test_user_copy" module.

Can you describe the steps to reproduce it?

I set CONFIG_TEST_USER_COPY=m, but I could not
reproduce it.

Quoted 27 lines

With this fix, it prints lots of warnings like this:

WARNING: modpost: lib/test_user_copy: section mismatch in reference: (unknown)+0x4 (section: .text.fixup) -> (unknown) (section: .init.text)

---
 scripts/mod/modpost.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/mod/modpost.c +3 -2
index bf7c4b4b5ff4..6b37039c9e92 100644
@@ -1050,7 +1050,9 @@ static void default_mismatch_handler(const char *modname, struct elf_info *elf,
        sec_mismatch_count++;
 
        warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s (section: %s)\n",
-            modname, fromsym, (unsigned int)(faddr - from->st_value), fromsec, tosym, tosec);
+            modname, fromsym,
+            (unsigned int)(faddr - (from ? from->st_value : 0)),
+            fromsec, tosym, tosec);
 
        if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
                if (match(tosec, mismatch->bad_tosec))
--

2.39.2

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw
Quoted 1 line · Masahiro Yamada

Can you describe the steps to reproduce it?

This crash occurred while I was testing an (unrelated) patch set with "randconfig" in a loop. Unfortunately, I don't have that config anymore, sorry.

#3 lore raw
In reply to Masahiro Yamada
Quoted 4 lines · Masahiro Yamada

Can you describe the steps to reproduce it?

I set CONFIG_TEST_USER_COPY=m, but I could not
reproduce it.

I have just reproduced (and logged) it - with "ARCH=arm randconfig KCONFIG_SEED=0xDD5A884F". Config file attached.

#4 lore raw
Quoted 9 lines · Max Kellermann
Quoted 4 lines · Masahiro Yamada

Can you describe the steps to reproduce it?

I set CONFIG_TEST_USER_COPY=m, but I could not
reproduce it.

I have just reproduced (and logged) it - with "ARCH=arm randconfig KCONFIG_SEED=0xDD5A884F". Config file attached.

Thanks for the test case. I confirmed that.

Applied to linux-kbuild.

Signature · 2 lines
Best Regards
Masahiro Yamada