Skip to contentSkip to conversation

modpost: fix segfault with short symbol names

PATCH Applied Michal Marek 3 participants 8 messages Dec 12, 2009 – Dec 15, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 modpost: fix segfault with short symbol names

memcmp() is wrong here, the symbol name can be shorter than KSYMTAB_PFX or CRC_PFX.

---
 scripts/mod/modpost.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
scripts/mod/modpost.c +2 -2
index 801a16a..065ee0b 100644
@@ -515,7 +515,7 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 		break;
 	case SHN_ABS:
 		/* CRC'd symbol */
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
 			crc = (unsigned int) sym->st_value;
 			sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
 					export);
@@ -559,7 +559,7 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
 		break;
 	default:
 		/* All exported symbols */
-		if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
+		if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
 			sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
 					export);
 		}
Signature · 1 line
1.6.5.3
#1 lore raw

scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 518

Quoted 5 lines

memcmp() is wrong here, the symbol name can be shorter than KSYMTAB_PFX or CRC_PFX.

scripts/mod/modpost.c +1 -1
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {

This doesn't change anything. In both cases the function will return 0 only if all strlen(CRC_PFX) chars match, and in both cases it can access strlen(CRC_PFX) chars (strncmp() is allowed to access characters after the first \0 just fine).

Segher

#2 lore raw
Quoted 11 lines · Segher Boessenkool

scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 518

Quoted 6 lines

memcmp() is wrong here, the symbol name can be shorter than KSYMTAB_PFX
or CRC_PFX.

scripts/mod/modpost.c +1 -1
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {

This doesn't change anything. In both cases the function will return 0 only if all strlen(CRC_PFX) chars match, and in both cases it can access strlen(CRC_PFX) chars (strncmp() is allowed to access characters after the first \0 just fine).

str(n)cmp is not allowed to access characters past the NUL byte.

Michal

#3 lore raw
Quoted 12 lines
Quoted 11 lines

scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 518

Quoted 6 lines

memcmp() is wrong here, the symbol name can be shorter than KSYMTAB_PFX
or CRC_PFX.

scripts/mod/modpost.c +1 -1
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {

This doesn't change anything. In both cases the function will return 0 only if all strlen(CRC_PFX) chars match, and in both cases it can access strlen(CRC_PFX) chars (strncmp() is allowed to access characters after the first \0 just fine).

Quoted 1 line

str(n)cmp is not allowed to access characters past the NUL byte.

You are wrong. strncmp() _is_ allowed to do that. So this patch doesn't change anything.

Segher

#4 lore raw
In reply to Michal Marek
Quoted 13 lines · Segher Boessenkool
Quoted 11 lines

scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 518

Quoted 6 lines

memcmp() is wrong here, the symbol name can be shorter than KSYMTAB_PFX
or CRC_PFX.

scripts/mod/modpost.c +1 -1
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {

This doesn't change anything. In both cases the function will return 0 only if all strlen(CRC_PFX) chars match, and in both cases it can access strlen(CRC_PFX) chars (strncmp() is allowed to access characters after the first \0 just fine).

Quoted 1 line

str(n)cmp is not allowed to access characters past the NUL byte.

Let me apologise for the confusion.

strncmp() _can_ access characters past the first NUL byte; and many implementations do so (they do accesses per 32-bit word, for example). But strncmp() behaves as-if it didn't compare anything past the first null character, so for example causing a page fault if the string ends just before the end of a memory page isn't allowed; OTOH, the parameters to memcmp() are required to point to two objects, each at least "len" bytes big.

So the patch fixes a real problem. I'm sorry for being dense.

Segher

#5 lore raw
Quoted 3 lines · Segher Boessenkool

So the patch fixes a real problem. I'm sorry for being dense.

Thanks :). Rusty, I sent the patch to you because you have some other modpost stuff in the rr tree. But I can add it to the kbuild tree as well if you want.

Michal

#6 lore raw
In reply to Segher Boessenkool
Quoted 9 lines · Segher Boessenkool
Quoted 9 lines · Segher Boessenkool
Quoted 7 lines

scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 518

Quoted 2 lines
scripts/mod/modpost.c +1 -1
-		if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
+		if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {

This doesn't change anything. In both cases the function will return 0 only if all strlen(CRC_PFX) chars match, and in both cases it can access strlen(CRC_PFX) chars (strncmp() is allowed to access characters after the first \0 just fine).

Quoted 3 lines
Quoted 1 line

str(n)cmp is not allowed to access characters past the NUL byte.

Let me apologise for the confusion.

Mea culpas must be short, to avoid wasting even more time. And they must be at least as absolute as the incorrect assertion was. This helps us lurkers and archive readers flush the crap from their brains.

Your mails used _ for emphasis and "You are wrong.". Ouch. I suggest:

"I am *wrong*. Sorry Michal for the brain fart."

Also, please stop channeling Richard B. Johnson.
Rusty.