Skip to contentSkip to conversation

x86, relocs: Ignore relocations in .notes section

PATCH Superseded Kees Cook 3 participants 4 messages Feb 22, 2024 – Feb 27, 2024 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 x86, relocs: Ignore relocations in .notes section

When building with CONFIG_XEN_PV=y, .text symbols are emitted into the .notes section so that Xen can find the "startup_xen" entry point. This information is used prior to booting the kernel, so relocations are not useful. In fact, performing relocations against the .notes section means that the KASLR base is exposed since /sys/kernel/notes is world-readable.

To avoid leaking the KASLR base without breaking unprivileged tools that are expecting to read /sys/kernel/notes, skip performing relocations in the .notes section. The values readable in .notes are then identical to those found in System.map.

Fixes 5ead97c84fa7 ("xen: Core Xen implementation")
Fixes da1a679cde9b ("Add /sys/kernel/notes")

---
Cc: Borislav Petkov <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>
Cc: "Peter Zijlstra (Intel)" <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Tony Luck <[email protected]>
Cc: Kristen Carlson Accardi <[email protected]>
Cc: "Jürgen Groß" <[email protected]>
Cc: Boris Ostrovsky <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>
Cc: Guixiong Wei <[email protected]>
Cc: Jann Horn <[email protected]>

---
 arch/x86/tools/relocs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/tools/relocs.c +10 -0
index a3bae2b24626..0811fff23b9c 100644
@@ -733,6 +733,16 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
 			continue;
 		}
+
+		/*
+		 * Do not perform relocations in .notes section; any
+		 * values there are meant for pre-boot consumption (e.g.
+		 * startup_xen).
+		 */
+		if (strcmp(sec_name(sec->shdr.sh_info), ".notes") == 0) {
+			continue;
+		}
+
 		sec_symtab  = sec->link;
 		sec_applies = &secs[sec->shdr.sh_info];
 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · Kees Cook

When building with CONFIG_XEN_PV=y, .text symbols are emitted into the .notes section so that Xen can find the "startup_xen" entry point. This information is used prior to booting the kernel, so relocations are not useful. In fact, performing relocations against the .notes section means that the KASLR base is exposed since /sys/kernel/notes is world-readable.

To avoid leaking the KASLR base without breaking unprivileged tools that are expecting to read /sys/kernel/notes, skip performing relocations in the .notes section. The values readable in .notes are then identical to those found in System.map.

Fixes 5ead97c84fa7 ("xen: Core Xen implementation")
Fixes da1a679cde9b ("Add /sys/kernel/notes")

---
Cc: Borislav Petkov <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>
Cc: "Peter Zijlstra (Intel)" <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Tony Luck <[email protected]>
Cc: Kristen Carlson Accardi <[email protected]>
Cc: "Jürgen Groß" <[email protected]>
Cc: Boris Ostrovsky <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>
Cc: Guixiong Wei <[email protected]>
Cc: Jann Horn <[email protected]>

---
  arch/x86/tools/relocs.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
arch/x86/tools/relocs.c +0 -0
index a3bae2b24626..0811fff23b9c 100644

arch/x86/tools/relocs.c static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, · line 742

Quoted 10 lines
arch/x86/tools/relocs.c +7 -0
@@ -733,6 +733,16 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
  		if (sec->shdr.sh_type != SHT_REL_TYPE) {
  			continue;
  		}
+
+		/*
+		 * Do not perform relocations in .notes section; any
+		 * values there are meant for pre-boot consumption (e.g.
+		 * startup_xen).
+		 */
+		if (strcmp(sec_name(sec->shdr.sh_info), ".notes") == 0) {

Instead of a strcmp(), wouldnt't ...

arch/x86/tools/relocs.c static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, · line 748

Quoted 6 lines
arch/x86/tools/relocs.c +3 -0
+			continue;
+		}
+
  		sec_symtab  = sec->link;
  		sec_applies = &secs[sec->shdr.sh_info];
  		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {

... a test of "sec_applies->shdr.sh_type == SHT_NOTE" work as well?

In the end I'm fine with both variants, so:

Juergen

#2 lore raw
Quoted 65 lines · Jürgen Groß
Quoted 42 lines · Kees Cook

When building with CONFIG_XEN_PV=y, .text symbols are emitted into the .notes section so that Xen can find the "startup_xen" entry point. This information is used prior to booting the kernel, so relocations are not useful. In fact, performing relocations against the .notes section means that the KASLR base is exposed since /sys/kernel/notes is world-readable.

To avoid leaking the KASLR base without breaking unprivileged tools that are expecting to read /sys/kernel/notes, skip performing relocations in the .notes section. The values readable in .notes are then identical to those found in System.map.

Fixes 5ead97c84fa7 ("xen: Core Xen implementation")
Fixes da1a679cde9b ("Add /sys/kernel/notes")

---
Cc: Borislav Petkov <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>
Cc: "Peter Zijlstra (Intel)" <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Tony Luck <[email protected]>
Cc: Kristen Carlson Accardi <[email protected]>
Cc: "Jürgen Groß" <[email protected]>
Cc: Boris Ostrovsky <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>
Cc: Guixiong Wei <[email protected]>
Cc: Jann Horn <[email protected]>

---
  arch/x86/tools/relocs.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
arch/x86/tools/relocs.c +0 -0
index a3bae2b24626..0811fff23b9c 100644

arch/x86/tools/relocs.c static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, · line 742

Quoted 10 lines
arch/x86/tools/relocs.c +7 -0
@@ -733,6 +733,16 @@ static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
  		if (sec->shdr.sh_type != SHT_REL_TYPE) {
  			continue;
  		}
+
+		/*
+		 * Do not perform relocations in .notes section; any
+		 * values there are meant for pre-boot consumption (e.g.
+		 * startup_xen).
+		 */
+		if (strcmp(sec_name(sec->shdr.sh_info), ".notes") == 0) {

Instead of a strcmp(), wouldnt't ...

arch/x86/tools/relocs.c static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, · line 748

Quoted 6 lines
arch/x86/tools/relocs.c +3 -0
+			continue;
+		}
+
  		sec_symtab  = sec->link;
  		sec_applies = &secs[sec->shdr.sh_info];
  		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {

... a test of "sec_applies->shdr.sh_type == SHT_NOTE" work as well?

Quoted 6 lines

In the end I'm fine with both variants, so:

Juergen

A type check would probably be better...

author
#3 lore raw
In reply to Jürgen Groß
Quoted 1 line · Jürgen Groß

... a test of "sec_applies->shdr.sh_type == SHT_NOTE" work as well?

Oh yeah! Thanks. I'll send a v2.

Signature · 1 line
Kees Cook