History

genksyms: fix memory leak when the same symbol is read from *.symref file

When a symbol that is already registered is read again from *.symref file, __add_symbol() removes the previous one from the hash table without freeing it. [Test Case] $ cat foo.c #include <linux/export.h> void foo(void); void foo(void) {} EXPORT_SYMBOL(foo); $ cat foo.symref foo void foo ( void ) foo void foo ( void ) When a symbol is removed from the hash table, it must be freed along with its ->name and ->defn members. However, sym->name cannot be freed because it is sometimes shared with node->string, but not always. If sym->name and node->string share the same memory, free(sym->name) could lead to a double-free bug. To resolve this issue, always assign a strdup'ed string to sym->name.

Masahiro Yamada committed 1 year ago

Commit be2fa441 parent 45c9c413 files · +9 −5Browse files

Showing the changes to scripts (3 of 3 files). Show all

Unified · Split

3 files
  1. scripts/genksyms/genksyms.c +6 −2
  2. scripts/genksyms/genksyms.h +1 −1
  3. scripts/genksyms/parse.y +2 −2
scripts/genksyms/genksyms.c+6−2
@@ -272,11 +272,15 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, · 271 unmodified lines
272272 break;
273273 }
274274 }
275+
276+ free_list(sym->defn, NULL);
277+ free(sym->name);
278+ free(sym);
275279 --nsyms;
276280 }
277281
278282 sym = xmalloc(sizeof(*sym));
279− sym->name = name;
283+ sym->name = xstrdup(name);
280284 sym->type = type;
281285 sym->defn = defn;
282286 sym->expansion_trail = NULL;
@@ -483,7 +487,7 @@ static void read_reference(FILE *f) · 200 unmodified lines
483487 defn = def;
484488 def = read_node(f);
485489 }
486− subsym = add_reference_symbol(xstrdup(sym->string), sym->tag,
490+ subsym = add_reference_symbol(sym->string, sym->tag,
487491 defn, is_extern);
488492 subsym->is_override = is_override;
489493 free_node(sym);
scripts/genksyms/genksyms.h+1−1
@@ -32,7 +32,7 @@ struct string_list { · 31 unmodified lines
3232
3333 struct symbol {
3434 struct symbol *hash_next;
35− const char *name;
35+ char *name;
3636 enum symbol_type type;
3737 struct string_list *defn;
3838 struct symbol *expansion_trail;
scripts/genksyms/parse.y+2−2
@@ -482,12 +482,12 @@ enumerator_list: · 481 unmodified lines
482482 enumerator:
483483 IDENT
484484 {
485− const char *name = strdup((*$1)->string);
485+ const char *name = (*$1)->string;
486486 add_symbol(name, SYM_ENUM_CONST, NULL, 0);
487487 }
488488 | IDENT '=' EXPRESSION_PHRASE
489489 {
490− const char *name = strdup((*$1)->string);
490+ const char *name = (*$1)->string;
491491 struct string_list *expr = copy_list_range(*$3, *$2);
492492 add_symbol(name, SYM_ENUM_CONST, expr, 0);
493493 }