Skip to contentSkip to conversation

Salted build ids via linker sections

PATCH Superseded Laura Abbott 5 participants 13 messages May 23, 2018 – May 24, 2018 1 message not in the archive

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Salted build ids via linker sections

Hi,

This is v3 of the series to allow unique build-ids in the kernel. As a reminder of the context:

""
In Fedora, the debug information is packaged separately (foo-debuginfo) and can be installed separately. There's been a long standing issue where only one version of a debuginfo info package can be installed at a time. Mark Wielaard made an effort for Fedora 27 to allow parallel installation of debuginfo (see
https://fedoraproject.org/wiki/Changes/ParallelInstallableDebuginfo for more details)

Part of the requirement to allow this to work is that build ids are unique between builds. The existing upstream rpm implementation ensures this by re-calculating the build-id using the version and release as a seed. This doesn't work 100% for the kernel because of the vDSO which is its own binary and doesn't get updated. After poking holes in a few of my ideas, there was a discussion with some people from the binutils team about adding --build-id-salt to let ld do the calculation debugedit is doing. There was a counter proposal made about adding some extra information via a .comment which will affect the build id calculation but just get stripped out. ""

I mentioned in the previous version that there were problems with sporadic build failures. v3 switches to generating the linker script directly instead of a header + a linker script to avoid this problem. I also dropped the Kconfig completely (I can add it back as a guard if this seems not small enough to want all the time). I've also dropped the RFC tag since it's well formed enough at this point.

Laura Abbott (2):
kbuild: Introduce build-salt linker script
x86/vdso: Add build salt to the vDSO

 Makefile                     |  4 +++-
 arch/x86/entry/vdso/Makefile |  4 +++-
 scripts/.gitignore           |  1 +
 scripts/Makefile             |  9 ++++++++-
 scripts/gensalt              | 22 ++++++++++++++++++++++
 scripts/link-vmlinux.sh      |  3 ++-
 6 files changed, 39 insertions(+), 4 deletions(-)
 create mode 100755 scripts/gensalt
Signature · 1 line
2.17.0
#1 lore raw

PATCH v3 1/2 kbuild: Introduce build-salt linker script

The build id generated from --build-id can be generated in several different ways, with the default being the sha1 on the output of the linked file. For distributions, it can be useful to make sure this ID is unique, even if the actual file contents don't change. The easiest way to do this is to insert a comment section with some data.

Introduce a generated linker script to link against the kernel/modules. This puts the kernel version in a .comment section which will generate a unique build id if the kernel version changes.

---
v3: Generate the linker script directly instead of just a header.

---
 Makefile                |  4 +++-
 scripts/.gitignore      |  1 +
 scripts/Makefile        |  9 ++++++++-
 scripts/gensalt         | 22 ++++++++++++++++++++++
 scripts/link-vmlinux.sh |  3 ++-
 5 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100755 scripts/gensalt
Makefile +3 -1
index ec6f45928fd4..87fe8992afd8 100644
@@ -428,7 +428,8 @@ KBUILD_AFLAGS_KERNEL :=
 KBUILD_CFLAGS_KERNEL :=
 KBUILD_AFLAGS_MODULE  := -DMODULE
 KBUILD_CFLAGS_MODULE  := -DMODULE
-KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds
+KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds \
+			 -T $(obj)/scripts/build-salt.lds
 LDFLAGS :=
 GCC_PLUGINS_CFLAGS :=
 
@@ -997,6 +998,7 @@ export KBUILD_VMLINUX_INIT := $(head-y) $(init-y)
 export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y2) $(drivers-y) $(net-y) $(virt-y)
 export KBUILD_VMLINUX_LIBS := $(libs-y1)
 export KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
+export EXTRA_LDS           := scripts/build-salt.lds
 export LDFLAGS_vmlinux
 # used by scripts/package/Makefile
 export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)
scripts/.gitignore +1 -0
index 0442c06eefcb..1c840ef4f0c8 100644
@@ -13,3 +13,4 @@ asn1_compiler
 extract-cert
 sign-file
 insert-sys-cert
+build-salt.lds
scripts/Makefile +8 -1
index 25ab143cbe14..019b0749ff46 100644
@@ -25,7 +25,14 @@ HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
 HOSTLOADLIBES_sign-file = -lcrypto
 HOSTLOADLIBES_extract-cert = -lcrypto
 
-always		:= $(hostprogs-y) $(hostprogs-m)
+always		:= $(hostprogs-y) $(hostprogs-m) build-salt.lds
+
+define filechk_build-salt.lds
+	($(CONFIG_SHELL) $(srctree)/scripts/gensalt $(KERNELRELEASE))
+endef
+
+$(obj)/build-salt.lds: $(src)/gensalt FORCE
+	$(call filechk,build-salt.lds)
 
 # The following hostprogs-y programs are only build on demand
 hostprogs-y += unifdef
scripts/gensalt new file +22 -0
new file mode 100755
index 000000000000..846c0407cc43
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+if [[ $1 = "" ]]; then
+	echo "#define BUILD_ID_SALT"
+	exit 0
+fi
+
+BUILD_ID_SALT=$1
+
+echo "SECTIONS {"
+echo ".comment (INFO) :"
+echo " {"
+
+_TAG=`echo $BUILD_ID_SALT | sed -e 's/\(.\)/\1 /g'`
+for c in $_TAG; do
+	_HEX=`echo -n $c | od -A n -t x1 | tr -d ' ' `
+	echo "BYTE(0x$_HEX);"
+done
+echo "BYTE(0x00);"
+
+echo " } "
+echo " } "
scripts/link-vmlinux.sh +2 -1
index 9045823c7be7..588946dde658 100755
@@ -84,6 +84,7 @@ modpost_link()
 vmlinux_link()
 {
 	local lds="${objtree}/${KBUILD_LDS}"
+	local extra_lds="${objtree}/${EXTRA_LDS}"
 	local objects
 
 	if [ "${SRCARCH}" != "um" ]; then
@@ -96,7 +97,7 @@ vmlinux_link()
 			${1}"
 
 		${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}	\
-			-T ${lds} ${objects}
+			-T ${lds} -T ${extra_lds} ${objects}
 	else
 		objects="-Wl,--whole-archive			\
 			built-in.a				\
Signature · 1 line
2.17.0
#2 lore raw

PATCH v3 2/2 x86/vdso: Add build salt to the vDSO

In reply to Laura Abbott

The vDSO is linked separately from the kernel and modules. Ensure it picks up the comment section, if available.

---
v3: Invoke the generated linker script. The ".." nightmare is pretty ugly but I didn't see an easier way to pick up the generated file. That was actually part of my motivation for using an #include since paths for those are standardized.

---
 arch/x86/entry/vdso/Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/Makefile +3 -1
index d998a487c9b1..f54aa97dc9f0 100644
@@ -162,7 +162,9 @@ $(obj)/vdso32.so.dbg: FORCE \
 quiet_cmd_vdso = VDSO    $@
       cmd_vdso = $(CC) -nostdlib -o $@ \
 		       $(VDSO_LDFLAGS) $(VDSO_LDFLAGS_$(filter %.lds,$(^F))) \
-		       -Wl,-T,$(filter %.lds,$^) $(filter %.o,$^) && \
+		       -Wl,-T,$(filter %.lds,$^) \
+		       -Wl,-T$(obj)/../../../../scripts/build-salt.lds \
+		       $(filter %.o,$^) && \
 		 sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@'
 
 VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=both) \
Signature · 1 line
2.17.0
maintainer
#3 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

Quoted 2 lines · Laura Abbott

The vDSO is linked separately from the kernel and modules. Ensure it picks up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

#4 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

Quoted 9 lines · Andy Lutomirski
Quoted 2 lines · Laura Abbott

The vDSO is linked separately from the kernel and modules. Ensure it picks up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

That's a good idea I hadn't considered. I'll do that for the next version.

Thanks,
Laura

#5 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to Andy Lutomirski
Quoted 9 lines · Andy Lutomirski
Quoted 2 lines · Laura Abbott

The vDSO is linked separately from the kernel and modules. Ensure it picks up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte. The autogeneration was the easiest way to make that happen. Maybe there's some horrific c preprocessing or other generation that could happen but I doubt that's any worse than the generated linker script.

Thanks,
Laura

#6 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

Quoted 3 lines · Laura Abbott

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte.

I think you can use the "fill" thing to basically add any random data to a section.

So you can do something like

        . = ALIGN(16);
        .salt : AT(ADDR(.salt) - LOAD_OFFSET) {
                LONG(0xffaa5500);
                . = ALIGN(16);
        } =0x01234567890abcdef

in the lds file, and you'll get a section that looks like this:

[torvalds@i7 linux]$ objdump -h vmlinux -j .salt -s

vmlinux: file format elf64-x86-64

Sections:
Idx Name Size VMA LMA File off
Algn

     15 .salt         00000010  ffffffff8432b000  000000000432b000  0352b000

2**0
CONTENTS, ALLOC, LOAD, DATA
Contents of section .salt:
ffffffff8432b000 0055aaff 00123456 7890abcd ef001234 .U....4Vx......4

Now whether that is sufficient for your needs, I dunno.

Linus

#7 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

Quoted 30 lines · Linus Torvalds
Quoted 6 lines · Laura Abbott

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte.

I think you can use the "fill" thing to basically add any random data to a section.

So you can do something like

       . = ALIGN(16);
       .salt : AT(ADDR(.salt) - LOAD_OFFSET) {
               LONG(0xffaa5500);
               . = ALIGN(16);
       } =0x01234567890abcdef

in the lds file, and you'll get a section that looks like this:

[torvalds@i7 linux]$ objdump -h vmlinux -j .salt -s

vmlinux: file format elf64-x86-64

Sections:
Idx Name Size VMA LMA File off
Algn

Quoted 8 lines
    15 .salt         00000010  ffffffff8432b000  000000000432b000  0352b000
 2**0
                     CONTENTS, ALLOC, LOAD, DATA
   Contents of section .salt:
    ffffffff8432b000 0055aaff 00123456 7890abcd ef001234  .U....4Vx......4

Now whether that is sufficient for your needs, I dunno.

I don’t know whether I’m missing something obvious, but can’t this be in C?

asm (“.pushsection \”.comment\”; .ascii \”” WHATEVER “\”; .popsection”);

Or the .S equivalent.

#10 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to Laura Abbott

2018-05-24 7:53 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 14 lines · Andy Lutomirski
Quoted 12 lines · Laura Abbott
Quoted 5 lines

The vDSO is linked separately from the kernel and modules. Ensure it picks
up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

Quoted 7 lines

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte. The autogeneration was the easiest way to make that happen. Maybe there's some horrific c preprocessing or other generation that could happen but I doubt that's any worse than the generated linker script.

I am personally prefer CONFIG option (as you did in v2) to KERNELVERSION.

If you use "hex" type instead of "string" type in Kconfig, and LONG() instead of BYTE() in the script script, this can be much simpler, right?

config BUILD_ID_SALT
hex "Build ID Salt"
help
...

Then, in scripts/Makefile,

define filechk_build-salt.lds

        { \
                echo "SECTIONS {"; \
                echo ".comment (INFO) : { LONG($(CONFIG_BUILD_ID_SALT)); }"; \
                echo "}"; \
        }

endef

$(obj)/build-salt.lds: $(src)/Makefile FORCE

        $(call filechk,build-salt.lds)

This is now so simple that we can even remove the shell script.

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to a message not in the archive
Quoted 5 lines · Linus Torvalds

On Wed, May 23, 2018, 17:01 Andy Lutomirski <[email protected] <mailto:[email protected]>> wrote:

Quoted 8 lines

I don’t know whether I’m missing something obvious, but can’t this be in C?

Yes, but I thought Laura wanted to limit it to linker file tricks (this thread has gone on for so long that I've forgotten the details of why).

       Linus

So we have to update the kernel and every module and the easiest way to do that was the linker script. I was assuming I'd just use the same approach for the vDSO but you're right that there's no reason we can't apply a different technique. I notice there's already a vdso-note.S which adds LINUX_VERSION_CODE as a note. This doesn't include the extra version so it doesn't quite meet our needs. There's no reason why we can't throw something else in there for good measure.

Thanks,
Laura

#11 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to Masahiro Yamada

2018-05-24 9:11 GMT+09:00 Masahiro Yamada <[email protected]>:

Quoted 54 lines

2018-05-24 7:53 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 14 lines · Andy Lutomirski
Quoted 12 lines · Laura Abbott
Quoted 5 lines

The vDSO is linked separately from the kernel and modules. Ensure it picks
up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

Quoted 7 lines

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte. The autogeneration was the easiest way to make that happen. Maybe there's some horrific c preprocessing or other generation that could happen but I doubt that's any worse than the generated linker script.

I am personally prefer CONFIG option (as you did in v2) to KERNELVERSION.

If you use "hex" type instead of "string" type in Kconfig, and LONG() instead of BYTE() in the script script, this can be much simpler, right?

config BUILD_ID_SALT
hex "Build ID Salt"
help
...

Then, in scripts/Makefile,

define filechk_build-salt.lds

        { \
                echo "SECTIONS {"; \
                echo ".comment (INFO) : { LONG($(CONFIG_BUILD_ID_SALT)); }"; \
                echo "}"; \
        }

endef

Quoted 7 lines

$(obj)/build-salt.lds: $(src)/Makefile FORCE

        $(call filechk,build-salt.lds)

This is now so simple that we can even remove the shell script.

I had not noticed the comments from Linus and Andy before I posted mine.

Maybe, we should not add binary data into the .comment section.

Signature · 2 lines
Best Regards
Masahiro Yamada
#13 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to Laura Abbott

2018-05-23 9:19 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 10 lines

The vDSO is linked separately from the kernel and modules. Ensure it picks up the comment section, if available.

---
v3: Invoke the generated linker script. The ".." nightmare is pretty ugly but I didn't see an easier way to pick up the generated file. That was actually part of my motivation for using an #include since paths for those are standardized.

Maybe, you found a different approach.

FWIW,

If you use the linker script approach, you could simply use

scripts/build-salt.lds

instead of

$(obj)/../../../../scripts/build-salt.lds

Signature · 2 lines
Best Regards
Masahiro Yamada
#12 lore raw

Re: [PATCHv3 2/2] x86/vdso: Add build salt to the vDSO

In reply to Masahiro Yamada
Quoted 65 lines · Masahiro Yamada

2018-05-24 9:11 GMT+09:00 Masahiro Yamada <[email protected]>:

Quoted 54 lines

2018-05-24 7:53 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 14 lines · Andy Lutomirski
Quoted 12 lines · Laura Abbott
Quoted 5 lines

The vDSO is linked separately from the kernel and modules. Ensure it picks
up the comment section, if available.

Did you end up preferring this to just sticking the kernel version in a .comment in the vDSO for some reason?

Quoted 7 lines

Actually I remember now why this is necessary: there is not a simple way to encode a string into a linker file as it has to be spit out byte by byte. The autogeneration was the easiest way to make that happen. Maybe there's some horrific c preprocessing or other generation that could happen but I doubt that's any worse than the generated linker script.

I am personally prefer CONFIG option (as you did in v2) to KERNELVERSION.

If you use "hex" type instead of "string" type in Kconfig, and LONG() instead of BYTE() in the script script, this can be much simpler, right?

config BUILD_ID_SALT
hex "Build ID Salt"
help
...

Then, in scripts/Makefile,

define filechk_build-salt.lds

         { \
                 echo "SECTIONS {"; \
                 echo ".comment (INFO) : { LONG($(CONFIG_BUILD_ID_SALT)); }"; \
                 echo "}"; \
         }

endef

Quoted 7 lines

$(obj)/build-salt.lds: $(src)/Makefile FORCE

         $(call filechk,build-salt.lds)

This is now so simple that we can even remove the shell script.

Quoted 8 lines

I had not noticed the comments from Linus and Andy before I posted mine.

Maybe, we should not add binary data into the .comment section.

The comments from Linus and Andy apply to the vDSO but I don't think they work for the kernel/modules. We need something that can apply to every module and the kernel and the linker script seems like easiest way to do that. The vDSO is a self-contained binary so it makes sense to not use the linker script there and instead throw something in one of the existing files.

I'm kind of iffy about making the build-id salt a hex string since that requires bit more work to generate. I'll experiment in a new version.

Thanks,
Laura