]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
genksyms: fix memory leak when the same symbol is read from *.symref file
authorMasahiro Yamada <masahiroy@kernel.org>
Fri, 3 Jan 2025 07:30:39 +0000 (16:30 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 13 Mar 2025 11:50:13 +0000 (12:50 +0100)
commitc48b8fc7f0a869e686eacd87088ff23f9b61b172
tree034045202fad0d461413ad9f71466779b4c3f777
parentbb28d02726ab2aeb246c76586d7a5b9c7898ca83
genksyms: fix memory leak when the same symbol is read from *.symref file

[ Upstream commit be2fa44b5180a1f021efb40c55fdf63c249c3209 ]

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.

Fixes: 64e6c1e12372 ("genksyms: track symbol checksum changes")
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
scripts/genksyms/genksyms.c
scripts/genksyms/genksyms.h
scripts/genksyms/parse.y