Skip to contentSkip to conversation

scripts/kallsyms: fix memory corruption caused by write over-run

PATCH Applied Masahiro Yamada 3 participants 5 messages Feb 10, 2020 – Feb 12, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/kallsyms: fix memory corruption caused by write over-run

scripts/kallsyms crashes because memcpy() writes one more byte than allocated.

Fixes 8d60526999aa ("scripts/kallsyms: change table to store (strcut sym_entry *)")

---

 scripts/kallsyms.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/kallsyms.c +2 -2
index a566d8201b56..0133dfaaf352 100644
@@ -210,7 +210,7 @@ static struct sym_entry *read_symbol(FILE *in)
 
 	len = strlen(name) + 1;
 
-	sym = malloc(sizeof(*sym) + len);
+	sym = malloc(sizeof(*sym) + len + 1);
 	if (!sym) {
 		fprintf(stderr, "kallsyms failure: "
 			"unable to allocate required amount of memory\n");
@@ -219,7 +219,7 @@ static struct sym_entry *read_symbol(FILE *in)
 	sym->addr = addr;
 	sym->len = len;
 	sym->sym[0] = type;
-	memcpy(sym_name(sym), name, len);
+	strcpy(sym_name(sym), name);
 	sym->percpu_absolute = 0;
 
 	return sym;
Signature · 1 line
2.17.1
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Masahiro Yamada

scripts/kallsyms crashes because memcpy() writes one more byte than allocated.

Fixes 8d60526999aa ("scripts/kallsyms: change table to store (strcut sym_entry *)")

Thanks!
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#2 lore raw
In reply to Masahiro Yamada

Looks like len is already +1, maybe it shouldn't be?

scripts/kallsyms.c static struct sym_entry *read_symbol(FILE *in) · line 213

Quoted 4 lines
        len = strlen(name) + 1;
scripts/kallsyms.c +1 -1
-       sym = malloc(sizeof(*sym) + len);
+       sym = malloc(sizeof(*sym) + len + 1);

Maybe strlcpy or if len wasn't incremented?

Quoted 4 lines
scripts/kallsyms.c +1 -1
-       memcpy(sym_name(sym), name, len);
+       strcpy(sym_name(sym), name);
#3 lore raw

Hi.

Quoted 2 lines · Justin Capella

Looks like len is already +1, maybe it shouldn't be?

This increment is for storing one more character, 'type'.

        sym->sym[0] = type;
Quoted 4 lines

scripts/kallsyms.c static struct sym_entry *read_symbol(FILE *in) · line 213

Quoted 4 lines
        len = strlen(name) + 1;
scripts/kallsyms.c +1 -1
-       sym = malloc(sizeof(*sym) + len);
+       sym = malloc(sizeof(*sym) + len + 1);

This increment is for the '\0' termination.

So, malloc() needs to allocate:
sizeof(*sym) + strlen(name) + 2.

Quoted 8 lines

Maybe strlcpy or if len wasn't incremented?

Quoted 4 lines
scripts/kallsyms.c +1 -1
-       memcpy(sym_name(sym), name, len);
+       strcpy(sym_name(sym), name);
Signature · 2 lines
Best Regards
Masahiro Yamada
author Applied
#4 lore raw
In reply to Masahiro Yamada
Quoted 9 lines · Masahiro Yamada

scripts/kallsyms crashes because memcpy() writes one more byte than allocated.

Fixes 8d60526999aa ("scripts/kallsyms: change table to store (strcut sym_entry *)")

---

Applied.
I will send a pull request shortly
because many people are tripping over this bug.

Quoted 28 lines
 scripts/kallsyms.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/kallsyms.c +2 -3
index a566d8201b56..0133dfaaf352 100644
@@ -210,7 +210,7 @@ static struct sym_entry *read_symbol(FILE *in)
 
        len = strlen(name) + 1;
 
-       sym = malloc(sizeof(*sym) + len);
+       sym = malloc(sizeof(*sym) + len + 1);
        if (!sym) {
                fprintf(stderr, "kallsyms failure: "
                        "unable to allocate required amount of memory\n");
@@ -219,7 +219,7 @@ static struct sym_entry *read_symbol(FILE *in)
        sym->addr = addr;
        sym->len = len;
        sym->sym[0] = type;
-       memcpy(sym_name(sym), name, len);
+       strcpy(sym_name(sym), name);
        sym->percpu_absolute = 0;
 
        return sym;
--

2.17.1

Signature · 2 lines
Best Regards
Masahiro Yamada