linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] x86: Warn on orphan section placement
@ 2020-06-22 20:53 Kees Cook
  2020-06-22 20:53 ` [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 20:53 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Thomas Gleixner, Ingo Molnar, x86, Arnd Bergmann,
	Nick Desaulniers, Nathan Chancellor, clang-built-linux,
	linux-arch, linux-kernel

v2:
- split by architecture, rebase to v5.8-rc2
v1: https://lore.kernel.org/lkml/20200228002244.15240-1-keescook@chromium.org/

A recent bug[1] was solved for builds linked with ld.lld, and tracking
it down took way longer than it needed to (a year). Ultimately, it
boiled down to differences between ld.bfd and ld.lld's handling of
orphan sections. Similarly, the recent FGKASLR series brough up orphan
section handling too[2]. In both cases, it would have been nice if the
linker was running with --orphan-handling=warn so that surprise sections
wouldn't silently get mapped into the kernel image at locations up to the
whim of the linker's orphan handling logic. Instead, all desired sections
should be explicitly identified in the linker script (to be either kept or
discarded) with any orphans throwing a warning. The powerpc architecture
actually already does this, so this series extends coverage to x86.

Thanks!

-Kees

[1] https://github.com/ClangBuiltLinux/linux/issues/282
[2] https://lore.kernel.org/lkml/202002242122.AA4D1B8@keescook/

Kees Cook (3):
  vmlinux.lds.h: Add .gnu.version* to DISCARDS
  x86/build: Warn on orphan section placement
  x86/boot: Warn on orphan section placement

 arch/x86/Makefile                      |  4 ++++
 arch/x86/boot/compressed/Makefile      |  3 ++-
 arch/x86/boot/compressed/vmlinux.lds.S | 11 +++++++++++
 arch/x86/kernel/vmlinux.lds.S          |  6 ++++++
 include/asm-generic/vmlinux.lds.h      |  1 +
 5 files changed, 24 insertions(+), 1 deletion(-)

-- 
2.25.1


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

* [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 20:53 [PATCH v2 0/3] x86: Warn on orphan section placement Kees Cook
@ 2020-06-22 20:53 ` Kees Cook
  2020-06-22 22:00   ` Fangrui Song
  2020-06-22 20:53 ` [PATCH v2 2/3] x86/build: Warn on orphan section placement Kees Cook
  2020-06-22 20:53 ` [PATCH v2 3/3] x86/boot: " Kees Cook
  2 siblings, 1 reply; 16+ messages in thread
From: Kees Cook @ 2020-06-22 20:53 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Thomas Gleixner, Ingo Molnar, x86, Arnd Bergmann,
	Nick Desaulniers, Nathan Chancellor, clang-built-linux,
	linux-arch, linux-kernel

For vmlinux linking, no architecture uses the .gnu.version* section,
so remove it via the common DISCARDS macro in preparation for adding
--orphan-handling=warn more widely.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/asm-generic/vmlinux.lds.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index db600ef218d7..6fbe9ed10cdb 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -934,6 +934,7 @@
 	*(.discard)							\
 	*(.discard.*)							\
 	*(.modinfo)							\
+	*(.gnu.version*)						\
 	}
 
 /**
-- 
2.25.1


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

* [PATCH v2 2/3] x86/build: Warn on orphan section placement
  2020-06-22 20:53 [PATCH v2 0/3] x86: Warn on orphan section placement Kees Cook
  2020-06-22 20:53 ` [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS Kees Cook
@ 2020-06-22 20:53 ` Kees Cook
  2020-06-22 20:53 ` [PATCH v2 3/3] x86/boot: " Kees Cook
  2 siblings, 0 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 20:53 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Thomas Gleixner, Ingo Molnar, x86, Arnd Bergmann,
	Nick Desaulniers, Nathan Chancellor, clang-built-linux,
	linux-arch, linux-kernel

We don't want to depend on the linker's orphan section placement
heuristics as these can vary between linkers, and may change between
versions. All sections need to be explicitly named in the linker
script.

Discards the unused rela, plt, and got sections that are not needed
in the final vmlinux, and enable orphan section warnings.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/Makefile             | 4 ++++
 arch/x86/kernel/vmlinux.lds.S | 6 ++++++
 2 files changed, 10 insertions(+)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 00e378de8bc0..f8a5b2333729 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -51,6 +51,10 @@ ifdef CONFIG_X86_NEED_RELOCS
         LDFLAGS_vmlinux := --emit-relocs --discard-none
 endif
 
+# We never want expected sections to be placed heuristically by the
+# linker. All sections should be explicitly named in the linker script.
+LDFLAGS_vmlinux += --orphan-handling=warn
+
 #
 # Prevent GCC from generating any FP code by mistake.
 #
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 3bfc8dd8a43d..bb085ceeaaad 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -412,6 +412,12 @@ SECTIONS
 	DWARF_DEBUG
 
 	DISCARDS
+	/DISCARD/ : {
+		*(.rela.*) *(.rela_*)
+		*(.rel.*) *(.rel_*)
+		*(.got) *(.got.*)
+		*(.igot.*) *(.iplt)
+	}
 }
 
 
-- 
2.25.1


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

* [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 20:53 [PATCH v2 0/3] x86: Warn on orphan section placement Kees Cook
  2020-06-22 20:53 ` [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS Kees Cook
  2020-06-22 20:53 ` [PATCH v2 2/3] x86/build: Warn on orphan section placement Kees Cook
@ 2020-06-22 20:53 ` Kees Cook
  2020-06-22 22:06   ` Fangrui Song
  2 siblings, 1 reply; 16+ messages in thread
From: Kees Cook @ 2020-06-22 20:53 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Kees Cook, Thomas Gleixner, Ingo Molnar, x86, Arnd Bergmann,
	Nick Desaulniers, Nathan Chancellor, clang-built-linux,
	linux-arch, linux-kernel

We don't want to depend on the linker's orphan section placement
heuristics as these can vary between linkers, and may change between
versions. All sections need to be explicitly named in the linker
script.

Add the common debugging sections. Discard the unused note, rel, plt,
dyn, and hash sections that are not needed in the compressed vmlinux.
Disable .eh_frame generation in the linker and enable orphan section
warnings.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/boot/compressed/Makefile      |  3 ++-
 arch/x86/boot/compressed/vmlinux.lds.S | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 7619742f91c9..646720a05f89 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -48,6 +48,7 @@ GCOV_PROFILE := n
 UBSAN_SANITIZE :=n
 
 KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
+KBUILD_LDFLAGS += $(call ld-option,--no-ld-generated-unwind-info)
 # Compressed kernel should be built as PIE since it may be loaded at any
 # address by the bootloader.
 ifeq ($(CONFIG_X86_32),y)
@@ -59,7 +60,7 @@ else
 KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
 	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
 endif
-LDFLAGS_vmlinux := -T
+LDFLAGS_vmlinux := --orphan-handling=warn -T
 
 hostprogs	:= mkpiggy
 HOST_EXTRACFLAGS += -I$(srctree)/tools/include
diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S
index 8f1025d1f681..6fe3ecdfd685 100644
--- a/arch/x86/boot/compressed/vmlinux.lds.S
+++ b/arch/x86/boot/compressed/vmlinux.lds.S
@@ -75,5 +75,16 @@ SECTIONS
 	. = ALIGN(PAGE_SIZE);	/* keep ZO size page aligned */
 	_end = .;
 
+	STABS_DEBUG
+	DWARF_DEBUG
+
 	DISCARDS
+	/DISCARD/ : {
+		*(.note.*)
+		*(.rela.*) *(.rela_*)
+		*(.rel.*) *(.rel_*)
+		*(.plt) *(.plt.*)
+		*(.dyn*)
+		*(.hash) *(.gnu.hash)
+	}
 }
-- 
2.25.1


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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 20:53 ` [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS Kees Cook
@ 2020-06-22 22:00   ` Fangrui Song
  2020-06-22 22:09     ` Kees Cook
  2020-06-22 22:27     ` Kees Cook
  0 siblings, 2 replies; 16+ messages in thread
From: Fangrui Song @ 2020-06-22 22:00 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On 2020-06-22, Kees Cook wrote:
>For vmlinux linking, no architecture uses the .gnu.version* section,
>so remove it via the common DISCARDS macro in preparation for adding
>--orphan-handling=warn more widely.
>
>Signed-off-by: Kees Cook <keescook@chromium.org>
>---
> include/asm-generic/vmlinux.lds.h | 1 +
> 1 file changed, 1 insertion(+)
>
>diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
>index db600ef218d7..6fbe9ed10cdb 100644
>--- a/include/asm-generic/vmlinux.lds.h
>+++ b/include/asm-generic/vmlinux.lds.h
>@@ -934,6 +934,7 @@
> 	*(.discard)							\
> 	*(.discard.*)							\
> 	*(.modinfo)							\
>+	*(.gnu.version*)						\
> 	}
>
> /**
>-- 
>2.25.1

I wonder what lead to .gnu.version{,_d,_r} sections in the kernel.

tools/lib/bpf/libbpf_internal.h uses `.symver` directive and
-Wl,--version-script, which may lead to .gnu.version{,_d}, but this only
applies to the userspace libbpf.so

libperf.so has a similar -Wl,--version-script.

Linking vmlinux does not appear to use any symbol versioning.

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

* Re: [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 20:53 ` [PATCH v2 3/3] x86/boot: " Kees Cook
@ 2020-06-22 22:06   ` Fangrui Song
  2020-06-22 22:35     ` Kees Cook
  2020-06-22 22:43     ` Kees Cook
  0 siblings, 2 replies; 16+ messages in thread
From: Fangrui Song @ 2020-06-22 22:06 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On 2020-06-22, Kees Cook wrote:
>We don't want to depend on the linker's orphan section placement
>heuristics as these can vary between linkers, and may change between
>versions. All sections need to be explicitly named in the linker
>script.
>
>Add the common debugging sections. Discard the unused note, rel, plt,
>dyn, and hash sections that are not needed in the compressed vmlinux.
>Disable .eh_frame generation in the linker and enable orphan section
>warnings.
>
>Signed-off-by: Kees Cook <keescook@chromium.org>
>---
> arch/x86/boot/compressed/Makefile      |  3 ++-
> arch/x86/boot/compressed/vmlinux.lds.S | 11 +++++++++++
> 2 files changed, 13 insertions(+), 1 deletion(-)
>
>diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
>index 7619742f91c9..646720a05f89 100644
>--- a/arch/x86/boot/compressed/Makefile
>+++ b/arch/x86/boot/compressed/Makefile
>@@ -48,6 +48,7 @@ GCOV_PROFILE := n
> UBSAN_SANITIZE :=n
>
> KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
>+KBUILD_LDFLAGS += $(call ld-option,--no-ld-generated-unwind-info)
> # Compressed kernel should be built as PIE since it may be loaded at any
> # address by the bootloader.
> ifeq ($(CONFIG_X86_32),y)
>@@ -59,7 +60,7 @@ else
> KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
> 	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
> endif
>-LDFLAGS_vmlinux := -T
>+LDFLAGS_vmlinux := --orphan-handling=warn -T
>
> hostprogs	:= mkpiggy
> HOST_EXTRACFLAGS += -I$(srctree)/tools/include
>diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S
>index 8f1025d1f681..6fe3ecdfd685 100644
>--- a/arch/x86/boot/compressed/vmlinux.lds.S
>+++ b/arch/x86/boot/compressed/vmlinux.lds.S
>@@ -75,5 +75,16 @@ SECTIONS
> 	. = ALIGN(PAGE_SIZE);	/* keep ZO size page aligned */
> 	_end = .;
>
>+	STABS_DEBUG
>+	DWARF_DEBUG
>+
> 	DISCARDS
>+	/DISCARD/ : {
>+		*(.note.*)
>+		*(.rela.*) *(.rela_*)
>+		*(.rel.*) *(.rel_*)
>+		*(.plt) *(.plt.*)
>+		*(.dyn*)
>+		*(.hash) *(.gnu.hash)
>+	}
> }
>-- 
>2.25.1

LLD may report warnings for 3 synthetic sections if they are orphans:

ld.lld: warning: <internal>:(.symtab) is being placed in '.symtab'
ld.lld: warning: <internal>:(.shstrtab) is being placed in '.shstrtab'
ld.lld: warning: <internal>:(.strtab) is being placed in '.strtab'

Are they described?

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 22:00   ` Fangrui Song
@ 2020-06-22 22:09     ` Kees Cook
  2020-06-22 22:27     ` Kees Cook
  1 sibling, 0 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 22:09 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:00:43PM -0700, Fangrui Song wrote:
> On 2020-06-22, Kees Cook wrote:
> > For vmlinux linking, no architecture uses the .gnu.version* section,
> > so remove it via the common DISCARDS macro in preparation for adding
> > --orphan-handling=warn more widely.
> > 
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> > include/asm-generic/vmlinux.lds.h | 1 +
> > 1 file changed, 1 insertion(+)
> > 
> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > index db600ef218d7..6fbe9ed10cdb 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -934,6 +934,7 @@
> > 	*(.discard)							\
> > 	*(.discard.*)							\
> > 	*(.modinfo)							\
> > +	*(.gnu.version*)						\
> > 	}
> > 
> > /**
> > -- 
> > 2.25.1
> 
> I wonder what lead to .gnu.version{,_d,_r} sections in the kernel.

Here's where I see it:

ld: warning: orphan section `.gnu.version_d' from `arch/x86/boot/compressed/kernel_info.o' being placed in section `.gnu.version_d'

-- 
Kees Cook

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 22:00   ` Fangrui Song
  2020-06-22 22:09     ` Kees Cook
@ 2020-06-22 22:27     ` Kees Cook
  2020-06-22 22:52       ` Fangrui Song
  1 sibling, 1 reply; 16+ messages in thread
From: Kees Cook @ 2020-06-22 22:27 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:00:43PM -0700, Fangrui Song wrote:
> On 2020-06-22, Kees Cook wrote:
> > For vmlinux linking, no architecture uses the .gnu.version* section,
> > so remove it via the common DISCARDS macro in preparation for adding
> > --orphan-handling=warn more widely.
> > 
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> > include/asm-generic/vmlinux.lds.h | 1 +
> > 1 file changed, 1 insertion(+)
> > 
> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > index db600ef218d7..6fbe9ed10cdb 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -934,6 +934,7 @@
> > 	*(.discard)							\
> > 	*(.discard.*)							\
> > 	*(.modinfo)							\
> > +	*(.gnu.version*)						\
> > 	}
> > 
> > /**
> > -- 
> > 2.25.1
> 
> I wonder what lead to .gnu.version{,_d,_r} sections in the kernel.

This looks like a bug in bfd.ld? There are no versioned symbols in any
of the input files (and no output section either!)

The link command is:
$ ld -m elf_x86_64 --no-ld-generated-unwind-info -z noreloc-overflow -pie \
--no-dynamic-linker   --orphan-handling=warn -T \
arch/x86/boot/compressed/vmlinux.lds \
arch/x86/boot/compressed/kernel_info.o \
arch/x86/boot/compressed/head_64.o arch/x86/boot/compressed/misc.o \
arch/x86/boot/compressed/string.o arch/x86/boot/compressed/cmdline.o \
arch/x86/boot/compressed/error.o arch/x86/boot/compressed/piggy.o \
arch/x86/boot/compressed/cpuflags.o \
arch/x86/boot/compressed/early_serial_console.o \
arch/x86/boot/compressed/kaslr.o arch/x86/boot/compressed/kaslr_64.o \
arch/x86/boot/compressed/mem_encrypt.o \
arch/x86/boot/compressed/pgtable_64.o arch/x86/boot/compressed/acpi.o \
-o arch/x86/boot/compressed/vmlinux

None of the inputs have the section:

$ for i in arch/x86/boot/compressed/kernel_info.o \
arch/x86/boot/compressed/head_64.o arch/x86/boot/compressed/misc.o \
arch/x86/boot/compressed/string.o arch/x86/boot/compressed/cmdline.o \
arch/x86/boot/compressed/error.o arch/x86/boot/compressed/piggy.o \
arch/x86/boot/compressed/cpuflags.o \
arch/x86/boot/compressed/early_serial_console.o \
arch/x86/boot/compressed/kaslr.o arch/x86/boot/compressed/kaslr_64.o \
arch/x86/boot/compressed/mem_encrypt.o \
arch/x86/boot/compressed/pgtable_64.o arch/x86/boot/compressed/acpi.o \
; do echo -n $i": "; readelf -Vs $i | grep 'version'; done
arch/x86/boot/compressed/kernel_info.o: No version information found in this file.
arch/x86/boot/compressed/head_64.o: No version information found in this file.
arch/x86/boot/compressed/misc.o: No version information found in this file.
arch/x86/boot/compressed/string.o: No version information found in this file.
arch/x86/boot/compressed/cmdline.o: No version information found in this file.
arch/x86/boot/compressed/error.o: No version information found in this file.
arch/x86/boot/compressed/piggy.o: No version information found in this file.
arch/x86/boot/compressed/cpuflags.o: No version information found in this file.
arch/x86/boot/compressed/early_serial_console.o: No version information found in this file.
arch/x86/boot/compressed/kaslr.o: No version information found in this file.
arch/x86/boot/compressed/kaslr_64.o: No version information found in this file.
arch/x86/boot/compressed/mem_encrypt.o: No version information found in this file.
arch/x86/boot/compressed/pgtable_64.o: No version information found in this file.
arch/x86/boot/compressed/acpi.o: No version information found in this file.

And it's not in the output:

$ readelf -Vs arch/x86/boot/compressed/vmlinux | grep version
No version information found in this file.

So... for the kernel we need to silence it right now.

-- 
Kees Cook

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

* Re: [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 22:06   ` Fangrui Song
@ 2020-06-22 22:35     ` Kees Cook
  2020-06-22 22:43     ` Kees Cook
  1 sibling, 0 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 22:35 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:06:28PM -0700, Fangrui Song wrote:
> On 2020-06-22, Kees Cook wrote:
> > We don't want to depend on the linker's orphan section placement
> > heuristics as these can vary between linkers, and may change between
> > versions. All sections need to be explicitly named in the linker
> > script.
> > 
> > Add the common debugging sections. Discard the unused note, rel, plt,
> > dyn, and hash sections that are not needed in the compressed vmlinux.
> > Disable .eh_frame generation in the linker and enable orphan section
> > warnings.
> > 
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> > arch/x86/boot/compressed/Makefile      |  3 ++-
> > arch/x86/boot/compressed/vmlinux.lds.S | 11 +++++++++++
> > 2 files changed, 13 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
> > index 7619742f91c9..646720a05f89 100644
> > --- a/arch/x86/boot/compressed/Makefile
> > +++ b/arch/x86/boot/compressed/Makefile
> > @@ -48,6 +48,7 @@ GCOV_PROFILE := n
> > UBSAN_SANITIZE :=n
> > 
> > KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
> > +KBUILD_LDFLAGS += $(call ld-option,--no-ld-generated-unwind-info)
> > # Compressed kernel should be built as PIE since it may be loaded at any
> > # address by the bootloader.
> > ifeq ($(CONFIG_X86_32),y)
> > @@ -59,7 +60,7 @@ else
> > KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
> > 	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
> > endif
> > -LDFLAGS_vmlinux := -T
> > +LDFLAGS_vmlinux := --orphan-handling=warn -T
> > 
> > hostprogs	:= mkpiggy
> > HOST_EXTRACFLAGS += -I$(srctree)/tools/include
> > diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S
> > index 8f1025d1f681..6fe3ecdfd685 100644
> > --- a/arch/x86/boot/compressed/vmlinux.lds.S
> > +++ b/arch/x86/boot/compressed/vmlinux.lds.S
> > @@ -75,5 +75,16 @@ SECTIONS
> > 	. = ALIGN(PAGE_SIZE);	/* keep ZO size page aligned */
> > 	_end = .;
> > 
> > +	STABS_DEBUG
> > +	DWARF_DEBUG
> > +
> > 	DISCARDS
> > +	/DISCARD/ : {
> > +		*(.note.*)
> > +		*(.rela.*) *(.rela_*)
> > +		*(.rel.*) *(.rel_*)
> > +		*(.plt) *(.plt.*)
> > +		*(.dyn*)
> > +		*(.hash) *(.gnu.hash)
> > +	}
> > }
> > -- 
> > 2.25.1
> 
> LLD may report warnings for 3 synthetic sections if they are orphans:
> 
> ld.lld: warning: <internal>:(.symtab) is being placed in '.symtab'
> ld.lld: warning: <internal>:(.shstrtab) is being placed in '.shstrtab'
> ld.lld: warning: <internal>:(.strtab) is being placed in '.strtab'
> 
> Are they described?

Ah, hm. I see gcc is just silent about these. It looks like both regular
and debug kernels end up with those sections for both GCC and Clang. How
would you expect them to be described?

-- 
Kees Cook

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

* Re: [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 22:06   ` Fangrui Song
  2020-06-22 22:35     ` Kees Cook
@ 2020-06-22 22:43     ` Kees Cook
  2020-06-22 22:49       ` Fangrui Song
  1 sibling, 1 reply; 16+ messages in thread
From: Kees Cook @ 2020-06-22 22:43 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:06:28PM -0700, Fangrui Song wrote:
> LLD may report warnings for 3 synthetic sections if they are orphans:
> 
> ld.lld: warning: <internal>:(.symtab) is being placed in '.symtab'
> ld.lld: warning: <internal>:(.shstrtab) is being placed in '.shstrtab'
> ld.lld: warning: <internal>:(.strtab) is being placed in '.strtab'
> 
> Are they described?

Perhaps:

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index db600ef218d7..57e9c142e401 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -792,6 +792,9 @@
 		.stab.exclstr 0 : { *(.stab.exclstr) }			\
 		.stab.index 0 : { *(.stab.index) }			\
 		.stab.indexstr 0 : { *(.stab.indexstr) }		\
+		.symtab 0 : { *(.symtab) }				\
+		.strtab 0 : { *(.strtab) }				\
+		.shstrtab 0 : { *(.shstrtab) }				\
 		.comment 0 : { *(.comment) }
 
 #ifdef CONFIG_GENERIC_BUG

-- 
Kees Cook

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

* Re: [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 22:43     ` Kees Cook
@ 2020-06-22 22:49       ` Fangrui Song
  2020-06-22 23:00         ` Kees Cook
  0 siblings, 1 reply; 16+ messages in thread
From: Fangrui Song @ 2020-06-22 22:49 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On 2020-06-22, Kees Cook wrote:
>On Mon, Jun 22, 2020 at 03:06:28PM -0700, Fangrui Song wrote:
>> LLD may report warnings for 3 synthetic sections if they are orphans:
>>
>> ld.lld: warning: <internal>:(.symtab) is being placed in '.symtab'
>> ld.lld: warning: <internal>:(.shstrtab) is being placed in '.shstrtab'
>> ld.lld: warning: <internal>:(.strtab) is being placed in '.strtab'
>>
>> Are they described?
>
>Perhaps:
>
>diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
>index db600ef218d7..57e9c142e401 100644
>--- a/include/asm-generic/vmlinux.lds.h
>+++ b/include/asm-generic/vmlinux.lds.h
>@@ -792,6 +792,9 @@
> 		.stab.exclstr 0 : { *(.stab.exclstr) }			\
> 		.stab.index 0 : { *(.stab.index) }			\
> 		.stab.indexstr 0 : { *(.stab.indexstr) }		\
>+		.symtab 0 : { *(.symtab) }				\
>+		.strtab 0 : { *(.strtab) }				\
>+		.shstrtab 0 : { *(.shstrtab) }				\
> 		.comment 0 : { *(.comment) }
>
> #ifdef CONFIG_GENERIC_BUG

This LGTM. Nit: .comment before .symtab is a more common order.

Reviewed-by: Fangrui Song <maskray@google.com>

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 22:27     ` Kees Cook
@ 2020-06-22 22:52       ` Fangrui Song
  2020-06-22 22:57         ` Kees Cook
  0 siblings, 1 reply; 16+ messages in thread
From: Fangrui Song @ 2020-06-22 22:52 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On 2020-06-22, Kees Cook wrote:
>On Mon, Jun 22, 2020 at 03:00:43PM -0700, Fangrui Song wrote:
>> On 2020-06-22, Kees Cook wrote:
>> > For vmlinux linking, no architecture uses the .gnu.version* section,
>> > so remove it via the common DISCARDS macro in preparation for adding
>> > --orphan-handling=warn more widely.
>> >
>> > Signed-off-by: Kees Cook <keescook@chromium.org>
>> > ---
>> > include/asm-generic/vmlinux.lds.h | 1 +
>> > 1 file changed, 1 insertion(+)
>> >
>> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
>> > index db600ef218d7..6fbe9ed10cdb 100644
>> > --- a/include/asm-generic/vmlinux.lds.h
>> > +++ b/include/asm-generic/vmlinux.lds.h
>> > @@ -934,6 +934,7 @@
>> > 	*(.discard)							\
>> > 	*(.discard.*)							\
>> > 	*(.modinfo)							\
>> > +	*(.gnu.version*)						\
>> > 	}
>> >
>> > /**
>> > --
>> > 2.25.1
>>
>> I wonder what lead to .gnu.version{,_d,_r} sections in the kernel.
>
>This looks like a bug in bfd.ld? There are no versioned symbols in any
>of the input files (and no output section either!)
>
>The link command is:
>$ ld -m elf_x86_64 --no-ld-generated-unwind-info -z noreloc-overflow -pie \
>--no-dynamic-linker   --orphan-handling=warn -T \
>arch/x86/boot/compressed/vmlinux.lds \
>arch/x86/boot/compressed/kernel_info.o \
>arch/x86/boot/compressed/head_64.o arch/x86/boot/compressed/misc.o \
>arch/x86/boot/compressed/string.o arch/x86/boot/compressed/cmdline.o \
>arch/x86/boot/compressed/error.o arch/x86/boot/compressed/piggy.o \
>arch/x86/boot/compressed/cpuflags.o \
>arch/x86/boot/compressed/early_serial_console.o \
>arch/x86/boot/compressed/kaslr.o arch/x86/boot/compressed/kaslr_64.o \
>arch/x86/boot/compressed/mem_encrypt.o \
>arch/x86/boot/compressed/pgtable_64.o arch/x86/boot/compressed/acpi.o \
>-o arch/x86/boot/compressed/vmlinux
>
>None of the inputs have the section:
>
>$ for i in arch/x86/boot/compressed/kernel_info.o \
>arch/x86/boot/compressed/head_64.o arch/x86/boot/compressed/misc.o \
>arch/x86/boot/compressed/string.o arch/x86/boot/compressed/cmdline.o \
>arch/x86/boot/compressed/error.o arch/x86/boot/compressed/piggy.o \
>arch/x86/boot/compressed/cpuflags.o \
>arch/x86/boot/compressed/early_serial_console.o \
>arch/x86/boot/compressed/kaslr.o arch/x86/boot/compressed/kaslr_64.o \
>arch/x86/boot/compressed/mem_encrypt.o \
>arch/x86/boot/compressed/pgtable_64.o arch/x86/boot/compressed/acpi.o \
>; do echo -n $i": "; readelf -Vs $i | grep 'version'; done
>arch/x86/boot/compressed/kernel_info.o: No version information found in this file.
>arch/x86/boot/compressed/head_64.o: No version information found in this file.
>arch/x86/boot/compressed/misc.o: No version information found in this file.
>arch/x86/boot/compressed/string.o: No version information found in this file.
>arch/x86/boot/compressed/cmdline.o: No version information found in this file.
>arch/x86/boot/compressed/error.o: No version information found in this file.
>arch/x86/boot/compressed/piggy.o: No version information found in this file.
>arch/x86/boot/compressed/cpuflags.o: No version information found in this file.
>arch/x86/boot/compressed/early_serial_console.o: No version information found in this file.
>arch/x86/boot/compressed/kaslr.o: No version information found in this file.
>arch/x86/boot/compressed/kaslr_64.o: No version information found in this file.
>arch/x86/boot/compressed/mem_encrypt.o: No version information found in this file.
>arch/x86/boot/compressed/pgtable_64.o: No version information found in this file.
>arch/x86/boot/compressed/acpi.o: No version information found in this file.
>
>And it's not in the output:
>
>$ readelf -Vs arch/x86/boot/compressed/vmlinux | grep version
>No version information found in this file.
>
>So... for the kernel we need to silence it right now.

Re-link with -M (or -Map file) to check where .gnu.version{,_d,_r} input
sections come from?

If it is a bug, we should probably figure out which version of binutils
has fixed the bug.

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 22:52       ` Fangrui Song
@ 2020-06-22 22:57         ` Kees Cook
  2020-06-22 23:04           ` Fāng-ruì Sòng
  0 siblings, 1 reply; 16+ messages in thread
From: Kees Cook @ 2020-06-22 22:57 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:52:37PM -0700, Fangrui Song wrote:
> > And it's not in the output:
> > 
> > $ readelf -Vs arch/x86/boot/compressed/vmlinux | grep version
> > No version information found in this file.
> > 
> > So... for the kernel we need to silence it right now.
> 
> Re-link with -M (or -Map file) to check where .gnu.version{,_d,_r} input
> sections come from?

It's not reporting it correctly:

.gnu.version_d  0x00000000008966b0        0x0
 .gnu.version_d
                0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o

.gnu.version    0x00000000008966b0        0x0
 .gnu.version   0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o

.gnu.version_r  0x00000000008966b0        0x0
 .gnu.version_r
                0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o

it just reports whatever file is listed on the link command line first.

> If it is a bug, we should probably figure out which version of binutils
> has fixed the bug.

I see this with binutils 2.34...

-- 
Kees Cook

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

* Re: [PATCH v2 3/3] x86/boot: Warn on orphan section placement
  2020-06-22 22:49       ` Fangrui Song
@ 2020-06-22 23:00         ` Kees Cook
  0 siblings, 0 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 23:00 UTC (permalink / raw)
  To: Fangrui Song
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, x86,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, linux-kernel

On Mon, Jun 22, 2020 at 03:49:28PM -0700, Fangrui Song wrote:
> On 2020-06-22, Kees Cook wrote:
> > On Mon, Jun 22, 2020 at 03:06:28PM -0700, Fangrui Song wrote:
> > > LLD may report warnings for 3 synthetic sections if they are orphans:
> > > 
> > > ld.lld: warning: <internal>:(.symtab) is being placed in '.symtab'
> > > ld.lld: warning: <internal>:(.shstrtab) is being placed in '.shstrtab'
> > > ld.lld: warning: <internal>:(.strtab) is being placed in '.strtab'
> > > 
> > > Are they described?
> > 
> > Perhaps:
> > 
> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > index db600ef218d7..57e9c142e401 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -792,6 +792,9 @@
> > 		.stab.exclstr 0 : { *(.stab.exclstr) }			\
> > 		.stab.index 0 : { *(.stab.index) }			\
> > 		.stab.indexstr 0 : { *(.stab.indexstr) }		\
> > +		.symtab 0 : { *(.symtab) }				\
> > +		.strtab 0 : { *(.strtab) }				\
> > +		.shstrtab 0 : { *(.shstrtab) }				\
> > 		.comment 0 : { *(.comment) }
> > 
> > #ifdef CONFIG_GENERIC_BUG
> 
> This LGTM. Nit: .comment before .symtab is a more common order.

Adjusted.

> Reviewed-by: Fangrui Song <maskray@google.com>

Thanks!

-- 
Kees Cook

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 22:57         ` Kees Cook
@ 2020-06-22 23:04           ` Fāng-ruì Sòng
  2020-06-22 23:30             ` Kees Cook
  0 siblings, 1 reply; 16+ messages in thread
From: Fāng-ruì Sòng @ 2020-06-22 23:04 UTC (permalink / raw)
  To: Kees Cook
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, X86 ML,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, LKML

On Mon, Jun 22, 2020 at 3:57 PM Kees Cook <keescook@chromium.org> wrote:
>
> On Mon, Jun 22, 2020 at 03:52:37PM -0700, Fangrui Song wrote:
> > > And it's not in the output:
> > >
> > > $ readelf -Vs arch/x86/boot/compressed/vmlinux | grep version
> > > No version information found in this file.
> > >
> > > So... for the kernel we need to silence it right now.
> >
> > Re-link with -M (or -Map file) to check where .gnu.version{,_d,_r} input
> > sections come from?
>
> It's not reporting it correctly:
>
> .gnu.version_d  0x00000000008966b0        0x0
>  .gnu.version_d
>                 0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
>
> .gnu.version    0x00000000008966b0        0x0
>  .gnu.version   0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
>
> .gnu.version_r  0x00000000008966b0        0x0
>  .gnu.version_r
>                 0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
>
> it just reports whatever file is listed on the link command line first.
>
> > If it is a bug, we should probably figure out which version of binutils
> > has fixed the bug.
>
> I see this with binutils 2.34...
>
> --
> Kees Cook

:( It deserves a binutils bug
(https://sourceware.org/bugzilla/enter_bug.cgi?product=binutils ) and
a comment..

With the description adjusted to say that this works around a bug

Reviewed-by: Fangrui Song <maskray@google.com>

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

* Re: [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS
  2020-06-22 23:04           ` Fāng-ruì Sòng
@ 2020-06-22 23:30             ` Kees Cook
  0 siblings, 0 replies; 16+ messages in thread
From: Kees Cook @ 2020-06-22 23:30 UTC (permalink / raw)
  To: Fāng-ruì Sòng
  Cc: Borislav Petkov, Thomas Gleixner, Ingo Molnar, X86 ML,
	Arnd Bergmann, Nick Desaulniers, Nathan Chancellor,
	clang-built-linux, linux-arch, LKML

On Mon, Jun 22, 2020 at 04:04:40PM -0700, Fāng-ruì Sòng wrote:
> On Mon, Jun 22, 2020 at 3:57 PM Kees Cook <keescook@chromium.org> wrote:
> >
> > On Mon, Jun 22, 2020 at 03:52:37PM -0700, Fangrui Song wrote:
> > > > And it's not in the output:
> > > >
> > > > $ readelf -Vs arch/x86/boot/compressed/vmlinux | grep version
> > > > No version information found in this file.
> > > >
> > > > So... for the kernel we need to silence it right now.
> > >
> > > Re-link with -M (or -Map file) to check where .gnu.version{,_d,_r} input
> > > sections come from?
> >
> > It's not reporting it correctly:
> >
> > .gnu.version_d  0x00000000008966b0        0x0
> >  .gnu.version_d
> >                 0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
> >
> > .gnu.version    0x00000000008966b0        0x0
> >  .gnu.version   0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
> >
> > .gnu.version_r  0x00000000008966b0        0x0
> >  .gnu.version_r
> >                 0x00000000008966b0        0x0 arch/x86/boot/compressed/kernel_info.o
> >
> > it just reports whatever file is listed on the link command line first.
> >
> > > If it is a bug, we should probably figure out which version of binutils
> > > has fixed the bug.
> >
> > I see this with binutils 2.34...
> >
> > --
> > Kees Cook
> 
> :( It deserves a binutils bug
> (https://sourceware.org/bugzilla/enter_bug.cgi?product=binutils ) and
> a comment..

https://sourceware.org/bugzilla/show_bug.cgi?id=26153

> With the description adjusted to say that this works around a bug
> 
> Reviewed-by: Fangrui Song <maskray@google.com>

Adjusted, and thanks!

-- 
Kees Cook

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

end of thread, other threads:[~2020-06-22 23:30 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-22 20:53 [PATCH v2 0/3] x86: Warn on orphan section placement Kees Cook
2020-06-22 20:53 ` [PATCH v2 1/3] vmlinux.lds.h: Add .gnu.version* to DISCARDS Kees Cook
2020-06-22 22:00   ` Fangrui Song
2020-06-22 22:09     ` Kees Cook
2020-06-22 22:27     ` Kees Cook
2020-06-22 22:52       ` Fangrui Song
2020-06-22 22:57         ` Kees Cook
2020-06-22 23:04           ` Fāng-ruì Sòng
2020-06-22 23:30             ` Kees Cook
2020-06-22 20:53 ` [PATCH v2 2/3] x86/build: Warn on orphan section placement Kees Cook
2020-06-22 20:53 ` [PATCH v2 3/3] x86/boot: " Kees Cook
2020-06-22 22:06   ` Fangrui Song
2020-06-22 22:35     ` Kees Cook
2020-06-22 22:43     ` Kees Cook
2020-06-22 22:49       ` Fangrui Song
2020-06-22 23:00         ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).