public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-kernel@vger.kernel.org
Cc: x86@kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	 Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	Andy Lutomirski <luto@kernel.org>,  Arnd Bergmann <arnd@arndb.de>,
	Kees Cook <keescook@chromium.org>,
	Brian Gerst <brgerst@gmail.com>,
	 Kevin Loughlin <kevinloughlin@google.com>
Subject: [PATCH v4 7/7] x86/boot: Reject absolute references in .head.text
Date: Thu,  5 Dec 2024 12:28:12 +0100	[thread overview]
Message-ID: <20241205112804.3416920-16-ardb+git@google.com> (raw)
In-Reply-To: <20241205112804.3416920-9-ardb+git@google.com>

From: Ard Biesheuvel <ardb@kernel.org>

The .head.text section used to contain asm code that bootstrapped the
page tables and switched to the kernel virtual address space before
executing C code. The asm code carefully avoided dereferencing absolute
symbol references, as those will fault before the page tables are
installed.

Today, the .head.text section contains lots of C code too, and getting
the compiler to reason about absolute addresses taken from, e.g.,
section markers such as _text[] or _end[] but never use such absolute
references to access global variables [*] is intractible.

So instead, forbid the use of absolute references in .head.text
entirely, and rely on explicit arithmetic involving VA-to-PA offsets
generated by the asm startup code to construct virtual addresses where
needed (e.g., to construct the page tables).

Note that the 'relocs' tool is only used on the core kernel image when
building a relocatable image, but this is the default, and so adding the
check there is sufficient to catch new occurrences of code that use
absolute references before the kernel mapping is up.

[*] it is feasible when using PIC codegen but there is strong pushback
    to using this for all of the core kernel, and using it only for
    .head.text is not straight-forward.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/tools/relocs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 27441e5863b2..e937be979ec8 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -841,10 +841,10 @@ static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
 static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
 		      const char *symname)
 {
+	int headtext = !strcmp(sec_name(sec->shdr.sh_info), ".head.text");
 	unsigned r_type = ELF64_R_TYPE(rel->r_info);
 	ElfW(Addr) offset = rel->r_offset;
 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
-
 	if (sym->st_shndx == SHN_UNDEF)
 		return 0;
 
@@ -900,6 +900,12 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
 			break;
 		}
 
+		if (headtext) {
+			die("Absolute reference to symbol '%s' not permitted in .head.text\n",
+			    symname);
+			break;
+		}
+
 		/*
 		 * Relocation offsets for 64 bit kernels are output
 		 * as 32 bits and sign extended back to 64 bits when
-- 
2.47.0.338.g60cca15819-goog


  parent reply	other threads:[~2024-12-05 11:28 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-05 11:28 [PATCH v4 0/7] x86: Rid .head.text of all abs references Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 1/7] x86/sev: Avoid WARN()s and panic()s in early boot code Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2025-01-06 15:23   ` [PATCH v4 1/7] " Tom Lendacky
2025-01-07 11:12     ` [tip: x86/boot] x86/sev: Don't hang but terminate on failure to remap SVSM CA tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 2/7] x86/boot/64: Determine VA/PA offset before entering C code Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 3/7] x86/boot/64: Avoid intentional absolute symbol references in .head.text Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 4/7] x86/boot: Disable UBSAN in early boot code Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 5/7] x86/kernel: Move ENTRY_TEXT to the start of the image Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` [PATCH v4 6/7] x86/boot: Move .head.text into its own output section Ard Biesheuvel
2024-12-05 12:28   ` [tip: x86/boot] " tip-bot2 for Ard Biesheuvel
2024-12-05 11:28 ` Ard Biesheuvel [this message]
2024-12-05 12:28   ` [tip: x86/boot] x86/boot: Reject absolute references in .head.text tip-bot2 for Ard Biesheuvel
2024-12-31 10:01 ` [PATCH v4 0/7] x86: Rid .head.text of all abs references Borislav Petkov
2024-12-31 10:12   ` Ard Biesheuvel
2024-12-31 10:35     ` Borislav Petkov
2024-12-31 19:29       ` Ard Biesheuvel
2025-01-01  2:43         ` Nathan Chancellor
2025-01-01  8:01           ` Ard Biesheuvel
2025-01-01 10:39             ` Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241205112804.3416920-16-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=keescook@chromium.org \
    --cc=kevinloughlin@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox