Linux kbuild/kconfig development
 help / color / mirror / Atom feed
* [PATCH 1/3] modpost: add array range check to sec_name()
@ 2022-07-30 17:36 Masahiro Yamada
  2022-07-30 17:36 ` [PATCH 2/3] modpost: use more reliable way to get fromsec in section_rel(a)() Masahiro Yamada
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Masahiro Yamada @ 2022-07-30 17:36 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Masahiro Yamada, Michal Marek, Nick Desaulniers, linux-kernel

The section index is always positive, so the argunent, secindex, should
be unsigned.

Also, inserted the array range check.

If sym->st_shndx is a special section index (between SHN_LORESERVE and
SHN_HIRESERVE), there is no corresponding section header.

For example, if a symbol specifies an absolute value, sym->st_shndx is
SHN_ABS (=0xfff1).

The current users do not cause the out-of-range access of
info->sechddrs[], but it is better to avoid such a pitfall.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 08411fff3e17..148b38699889 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -336,8 +336,16 @@ static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
 				      sechdr->sh_name);
 }
 
-static const char *sec_name(const struct elf_info *info, int secindex)
+static const char *sec_name(const struct elf_info *info, unsigned int secindex)
 {
+	/*
+	 * If sym->st_shndx is a special section index, there is no
+	 * corresponding section header.
+	 * Return "" if the index is out of range of info->sechdrs[] array.
+	 */
+	if (secindex >= info->num_sections)
+		return "";
+
 	return sech_name(info, &info->sechdrs[secindex]);
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-08-04  6:48 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-30 17:36 [PATCH 1/3] modpost: add array range check to sec_name() Masahiro Yamada
2022-07-30 17:36 ` [PATCH 2/3] modpost: use more reliable way to get fromsec in section_rel(a)() Masahiro Yamada
2022-08-02 17:58   ` Nick Desaulniers
2022-07-30 17:36 ` [PATCH 3/3] Revert "Kbuild, lto, workaround: Don't warn for initcall_reference in modpost" Masahiro Yamada
2022-08-02 18:04   ` Nick Desaulniers
2022-08-04  6:47     ` Jiri Slaby
2022-08-01 20:29 ` [PATCH 1/3] modpost: add array range check to sec_name() Jeff Johnson
2022-08-02 17:09   ` Masahiro Yamada
2022-08-02 17:55 ` Nick Desaulniers
2022-08-03 10:05   ` Masahiro Yamada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox