[PATCH v4 00/24] vdso: Reject absolute relocations during build

Thomas Weißschuh posted 24 patches 1 month, 3 weeks ago
arch/arm/include/asm/elf.h          |  24 --
arch/arm/vdso/Makefile              |   4 +-
arch/arm64/include/asm/elf.h        |  55 ----
arch/arm64/kernel/vdso/Makefile     |   4 +-
arch/loongarch/include/asm/elf.h    | 100 ------
arch/loongarch/vdso/Makefile        |   4 +-
arch/mips/include/asm/elf.h         |  53 ---
arch/mips/vdso/Makefile             |   8 +-
arch/powerpc/include/uapi/asm/elf.h | 201 ------------
arch/powerpc/kernel/vdso/Makefile   |   4 +-
arch/riscv/include/uapi/asm/elf.h   |  66 ----
arch/riscv/kernel/vdso/Makefile     |  11 +-
arch/s390/include/asm/elf.h         |  83 -----
arch/s390/kernel/vdso32/Makefile    |   4 +-
arch/s390/kernel/vdso64/Makefile    |   4 +-
arch/x86/entry/vdso/Makefile        |   6 +-
arch/x86/include/asm/elf.h          |  34 --
arch/x86/um/asm/elf.h               |  33 --
include/uapi/linux/elf-r.h          | 631 ++++++++++++++++++++++++++++++++++++
include/uapi/linux/elf.h            |   1 +
lib/vdso/Makefile                   |   2 +
lib/vdso/Makefile.include           |  17 +
lib/vdso/check/.gitignore           |   3 +
lib/vdso/check/Makefile             |  28 ++
lib/vdso/check/elf.rs               | 488 ++++++++++++++++++++++++++++
lib/vdso/check/vdsocheck.rs         | 279 ++++++++++++++++
tools/include/uapi/linux/elf-em.h   |  71 ++++
tools/include/uapi/linux/elf-r.h    | 631 ++++++++++++++++++++++++++++++++++++
tools/include/uapi/linux/elf.h      | 112 ++++++-
29 files changed, 2277 insertions(+), 684 deletions(-)
[PATCH v4 00/24] vdso: Reject absolute relocations during build
Posted by Thomas Weißschuh 1 month, 3 weeks ago
The compiler can emit absolute relocations in vDSO code,
which are invalid in vDSO code.
Detect them at compile-time.

libc elf.h is missing some of the relocation constants,
so make user of the kernels own UAPI headers instead.

Kbuild and Rust folks: This contains custom definitions of hostprog
bindgen and rust library commands.
These are currently only defined inside the subsystem directory.
Let me know if they should go into scripts/Makefile.host.

This will conflict with my SPARC64 generic vDSO patches [0].
If both end up being applied at the same time, please leave out commit 
'vdso/vdsocheck: Drop the transitional kconfig option' from this series.

[0] https://lore.kernel.org/lkml/20250724-vdso-sparc64-generic-2-v1-0-e376a3bd24d1@linutronix.de/

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Changes in v4:
- Replace the inline shell logic with a dedicated build-time tool
- Link to v3: https://lore.kernel.org/r/20250611-vdso-absolute-reloc-v3-0-47897d73784b@linutronix.de

Changes in v3:
- Drop already applied bugfix for arm64
- Disable LTO for the riscv vDSO, as it is incompatible
- Link to v2: https://lore.kernel.org/r/20250430-vdso-absolute-reloc-v2-0-5efcc3bc4b26@linutronix.de

Changes in v2:
- Link to openend (invalid) GCC bug containing more explanations
- Refine commit messages
- Don't fail on commit absolute relocations in debug info
- Link to v1: https://lore.kernel.org/r/20250429-vdso-absolute-reloc-v1-0-987a0afd10b5@linutronix.de

---
Thomas Weißschuh (24):
      elf, uapi: Add a header for relocation constants
      x86/elf, um/x86/elf: Move relocation constants to UAPI
      ARM: elf: Move relocation constants to UAPI
      arm64: elf: Move relocation constants to UAPI
      powerpc/elf: Move relocation constants to UAPI
      riscv: elf: Move relocation constants to UAPI
      LoongArch: Move relocation constants to UAPI
      s390/elf: Move relocation constants to UAPI
      MIPS: ELF: Move relocation constants to UAPI
      tools headers UAPI: Sync ELF headers with the kernel sources
      vdso: Add the vdsocheck tool
      x86/vdso: Enable the vdsocheck tool
      ARM: vdso: Enable the vdsocheck tool
      arm64: vdso: Enable the vdsocheck tool
      powerpc/elf: Add 32-bit REL16 relocation definitions
      powerpc/vdso: Enable the vdsocheck tool
      riscv: vdso: Deduplicate CFLAGS_REMOVE_* variables
      riscv: vdso: Disable LTO for the vDSO
      riscv: vdso: Enable the vdsocheck tool
      LoongArch: vDSO: Enable the vdsocheck tool
      s390/vdso: Enable the vdsocheck tool
      MIPS: ELF: Add more PC-relative relocation definitions
      MIPS: vdso: Enable the vdsocheck tool
      vdso/vdsocheck: Drop the transitional kconfig option

 arch/arm/include/asm/elf.h          |  24 --
 arch/arm/vdso/Makefile              |   4 +-
 arch/arm64/include/asm/elf.h        |  55 ----
 arch/arm64/kernel/vdso/Makefile     |   4 +-
 arch/loongarch/include/asm/elf.h    | 100 ------
 arch/loongarch/vdso/Makefile        |   4 +-
 arch/mips/include/asm/elf.h         |  53 ---
 arch/mips/vdso/Makefile             |   8 +-
 arch/powerpc/include/uapi/asm/elf.h | 201 ------------
 arch/powerpc/kernel/vdso/Makefile   |   4 +-
 arch/riscv/include/uapi/asm/elf.h   |  66 ----
 arch/riscv/kernel/vdso/Makefile     |  11 +-
 arch/s390/include/asm/elf.h         |  83 -----
 arch/s390/kernel/vdso32/Makefile    |   4 +-
 arch/s390/kernel/vdso64/Makefile    |   4 +-
 arch/x86/entry/vdso/Makefile        |   6 +-
 arch/x86/include/asm/elf.h          |  34 --
 arch/x86/um/asm/elf.h               |  33 --
 include/uapi/linux/elf-r.h          | 631 ++++++++++++++++++++++++++++++++++++
 include/uapi/linux/elf.h            |   1 +
 lib/vdso/Makefile                   |   2 +
 lib/vdso/Makefile.include           |  17 +
 lib/vdso/check/.gitignore           |   3 +
 lib/vdso/check/Makefile             |  28 ++
 lib/vdso/check/elf.rs               | 488 ++++++++++++++++++++++++++++
 lib/vdso/check/vdsocheck.rs         | 279 ++++++++++++++++
 tools/include/uapi/linux/elf-em.h   |  71 ++++
 tools/include/uapi/linux/elf-r.h    | 631 ++++++++++++++++++++++++++++++++++++
 tools/include/uapi/linux/elf.h      | 112 ++++++-
 29 files changed, 2277 insertions(+), 684 deletions(-)
---
base-commit: 5180c6526acc9f1cb58f8b11fba67583c22e0854
change-id: 20250428-vdso-absolute-reloc-a226293c1761

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>

Re: [PATCH v4 00/24] vdso: Reject absolute relocations during build
Posted by Christophe Leroy 1 month, 3 weeks ago

Le 12/08/2025 à 07:44, Thomas Weißschuh a écrit :
> The compiler can emit absolute relocations in vDSO code,
> which are invalid in vDSO code.
> Detect them at compile-time.

I'm a bit puzzled with this series.

If I understand correctly, the check will be done only when you have 
RUST available ?

I wouldn't expect having RUST to build a C kernel.

By the way, aren't relocations already detected by command 
cmd_vdso_check in lib/vdso/Makefile.include , using readelf ? Why is a 
new tool needed and why does it have to be written in RUST langage ?

Thanks
Christophe

> 
> libc elf.h is missing some of the relocation constants,
> so make user of the kernels own UAPI headers instead.
> 
> Kbuild and Rust folks: This contains custom definitions of hostprog
> bindgen and rust library commands.
> These are currently only defined inside the subsystem directory.
> Let me know if they should go into scripts/Makefile.host.
> 
> This will conflict with my SPARC64 generic vDSO patches [0].
> If both end up being applied at the same time, please leave out commit
> 'vdso/vdsocheck: Drop the transitional kconfig option' from this series.
> 
> [0] https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20250724-vdso-sparc64-generic-2-v1-0-e376a3bd24d1%40linutronix.de%2F&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C3f03f6747325451ee98808ddd9634f27%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638905742749561064%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=HacF%2FvlUoxA9P6fTiN1ytw49gwayX1wNE7IxfEkFutE%3D&reserved=0
> 
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
> ---
> Changes in v4:
> - Replace the inline shell logic with a dedicated build-time tool
> - Link to v3: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fr%2F20250611-vdso-absolute-reloc-v3-0-47897d73784b%40linutronix.de&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C3f03f6747325451ee98808ddd9634f27%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638905742749584369%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=7NxAhutk6IXl%2B3fe1kkZEzhZz6CWye%2FVAcFO%2BgtS4uo%3D&reserved=0
> 
> Changes in v3:
> - Drop already applied bugfix for arm64
> - Disable LTO for the riscv vDSO, as it is incompatible
> - Link to v2: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fr%2F20250430-vdso-absolute-reloc-v2-0-5efcc3bc4b26%40linutronix.de&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C3f03f6747325451ee98808ddd9634f27%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638905742749600546%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=uv0qMS5qq0DovLHtxfRvT42atbJEkztylpOS8zt6bJ4%3D&reserved=0
> 
> Changes in v2:
> - Link to openend (invalid) GCC bug containing more explanations
> - Refine commit messages
> - Don't fail on commit absolute relocations in debug info
> - Link to v1: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fr%2F20250429-vdso-absolute-reloc-v1-0-987a0afd10b5%40linutronix.de&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C3f03f6747325451ee98808ddd9634f27%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638905742749616057%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=K5iwoz0Xqc8kheotWEc4M2KrZ7UVMDObOyFfCPj4N8Q%3D&reserved=0
> 
> ---
> Thomas Weißschuh (24):
>        elf, uapi: Add a header for relocation constants
>        x86/elf, um/x86/elf: Move relocation constants to UAPI
>        ARM: elf: Move relocation constants to UAPI
>        arm64: elf: Move relocation constants to UAPI
>        powerpc/elf: Move relocation constants to UAPI
>        riscv: elf: Move relocation constants to UAPI
>        LoongArch: Move relocation constants to UAPI
>        s390/elf: Move relocation constants to UAPI
>        MIPS: ELF: Move relocation constants to UAPI
>        tools headers UAPI: Sync ELF headers with the kernel sources
>        vdso: Add the vdsocheck tool
>        x86/vdso: Enable the vdsocheck tool
>        ARM: vdso: Enable the vdsocheck tool
>        arm64: vdso: Enable the vdsocheck tool
>        powerpc/elf: Add 32-bit REL16 relocation definitions
>        powerpc/vdso: Enable the vdsocheck tool
>        riscv: vdso: Deduplicate CFLAGS_REMOVE_* variables
>        riscv: vdso: Disable LTO for the vDSO
>        riscv: vdso: Enable the vdsocheck tool
>        LoongArch: vDSO: Enable the vdsocheck tool
>        s390/vdso: Enable the vdsocheck tool
>        MIPS: ELF: Add more PC-relative relocation definitions
>        MIPS: vdso: Enable the vdsocheck tool
>        vdso/vdsocheck: Drop the transitional kconfig option
> 
>   arch/arm/include/asm/elf.h          |  24 --
>   arch/arm/vdso/Makefile              |   4 +-
>   arch/arm64/include/asm/elf.h        |  55 ----
>   arch/arm64/kernel/vdso/Makefile     |   4 +-
>   arch/loongarch/include/asm/elf.h    | 100 ------
>   arch/loongarch/vdso/Makefile        |   4 +-
>   arch/mips/include/asm/elf.h         |  53 ---
>   arch/mips/vdso/Makefile             |   8 +-
>   arch/powerpc/include/uapi/asm/elf.h | 201 ------------
>   arch/powerpc/kernel/vdso/Makefile   |   4 +-
>   arch/riscv/include/uapi/asm/elf.h   |  66 ----
>   arch/riscv/kernel/vdso/Makefile     |  11 +-
>   arch/s390/include/asm/elf.h         |  83 -----
>   arch/s390/kernel/vdso32/Makefile    |   4 +-
>   arch/s390/kernel/vdso64/Makefile    |   4 +-
>   arch/x86/entry/vdso/Makefile        |   6 +-
>   arch/x86/include/asm/elf.h          |  34 --
>   arch/x86/um/asm/elf.h               |  33 --
>   include/uapi/linux/elf-r.h          | 631 ++++++++++++++++++++++++++++++++++++
>   include/uapi/linux/elf.h            |   1 +
>   lib/vdso/Makefile                   |   2 +
>   lib/vdso/Makefile.include           |  17 +
>   lib/vdso/check/.gitignore           |   3 +
>   lib/vdso/check/Makefile             |  28 ++
>   lib/vdso/check/elf.rs               | 488 ++++++++++++++++++++++++++++
>   lib/vdso/check/vdsocheck.rs         | 279 ++++++++++++++++
>   tools/include/uapi/linux/elf-em.h   |  71 ++++
>   tools/include/uapi/linux/elf-r.h    | 631 ++++++++++++++++++++++++++++++++++++
>   tools/include/uapi/linux/elf.h      | 112 ++++++-
>   29 files changed, 2277 insertions(+), 684 deletions(-)
> ---
> base-commit: 5180c6526acc9f1cb58f8b11fba67583c22e0854
> change-id: 20250428-vdso-absolute-reloc-a226293c1761
> 
> Best regards,

Re: [PATCH v4 00/24] vdso: Reject absolute relocations during build
Posted by Thomas Weißschuh 1 month, 2 weeks ago
Hi Christophe,

On Thu, Aug 14, 2025 at 03:43:09PM +0200, Christophe Leroy wrote:
> Le 12/08/2025 à 07:44, Thomas Weißschuh a écrit :
> > The compiler can emit absolute relocations in vDSO code,
> > which are invalid in vDSO code.
> > Detect them at compile-time.
> 
> I'm a bit puzzled with this series.
> 
> If I understand correctly, the check will be done only when you have RUST
> available ?

Yes, this new check will only be performed if a rust toolchain is available.
CONFIG_RUST however is *not* required.

> I wouldn't expect having RUST to build a C kernel.

The build will work fine without Rust present and will fall back to the
simplistic readelf test. A single report of breakage will allow us to fix the code,
not everybody needs to run the full thing.

> By the way, aren't relocations already detected by command cmd_vdso_check in
> lib/vdso/Makefile.include , using readelf ? Why is a new tool needed

The current cmd_vdso_check only validates the final vDSO image.
However that is not sufficient, as some problematic relocations will not show
up in the final image anymore but only the intermediary object files.
And there the logic is more complex than can be reasonably expressed in inline
shell scripts, see the previous revisions of this series for the attempts.
The valid relocations depend on each architecture and the specific ELF section
they appear in.
For the real example that triggered all of this, see commit
0c314cda9325 ("arm64: vdso: Work around invalid absolute relocations from GCC")

> and why does it have to be written in RUST langage ?

There is no hard requirement for Rust. I chose it for convenience of
implementation, especially around descriptive error handling and generic
functions. tglx was fine with it.


Thomas
Re: [PATCH v4 00/24] vdso: Reject absolute relocations during build
Posted by Miguel Ojeda 1 month, 3 weeks ago
On Tue, Aug 12, 2025 at 7:44 AM Thomas Weißschuh
<thomas.weissschuh@linutronix.de> wrote:
>
> Kbuild and Rust folks: This contains custom definitions of hostprog
> bindgen and rust library commands.
> These are currently only defined inside the subsystem directory.
> Let me know if they should go into scripts/Makefile.host.

Glad to see more Rust host progs :)

Keeping them local may be a bit easier initially to land, I guess
(e.g. no docs), and then we can generalize when needed later.

By the way, for consistency with elsewhere, probably we want
`HOSTRUSTLIB` -> `HOSTRUSTC L`. Though I am thinking to remove the `L`
anyway since eventually a lot of code will be "lib".

Cheers,
Miguel
Re: [PATCH v4 00/24] vdso: Reject absolute relocations during build
Posted by Thomas Weißschuh 1 month, 3 weeks ago
On Tue, Aug 12, 2025 at 01:07:34PM +0200, Miguel Ojeda wrote:
> On Tue, Aug 12, 2025 at 7:44 AM Thomas Weißschuh
> <thomas.weissschuh@linutronix.de> wrote:
> >
> > Kbuild and Rust folks: This contains custom definitions of hostprog
> > bindgen and rust library commands.
> > These are currently only defined inside the subsystem directory.
> > Let me know if they should go into scripts/Makefile.host.
> 
> Glad to see more Rust host progs :)
> 
> Keeping them local may be a bit easier initially to land, I guess
> (e.g. no docs), and then we can generalize when needed later.

I'm happy to do the docs etc. I only wanted to avoid doing all that work,
only for it to stay a subsystem-local solution.

Also it would be nice to have a Kconfig symbol, RUSTC_CAN_LINK or similar,
which indicates that the rust compiler can build host programs.

> By the way, for consistency with elsewhere, probably we want
> `HOSTRUSTLIB` -> `HOSTRUSTC L`. Though I am thinking to remove the `L`
> anyway since eventually a lot of code will be "lib".

Ack.