Skip to content

Commit 223abe9

Browse files
ardbiesheuveljgross1
authored andcommitted
x86/xen: Avoid relocatable quantities in Xen ELF notes
Xen puts virtual and physical addresses into ELF notes that are treated by the linker as relocatable by default. Doing so is not only pointless, given that the ELF notes are only intended for consumption by Xen before the kernel boots. It is also a KASLR leak, given that the kernel's ELF notes are exposed via the world readable /sys/kernel/notes. So emit these constants in a way that prevents the linker from marking them as relocatable. This involves place-relative relocations (which subtract their own virtual address from the symbol value) and linker provided absolute symbols that add the address of the place to the desired value. Tested-by: Jason Andryuk <jason.andryuk@amd.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Jason Andryuk <jason.andryuk@amd.com> Message-ID: <20241009160438.3884381-11-ardb+git@google.com> Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent d583542 commit 223abe9

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

arch/x86/kernel/vmlinux.lds.S

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,22 @@ INIT_PER_CPU(irq_stack_backing_store);
528528
#endif
529529

530530
#endif /* CONFIG_X86_64 */
531+
532+
/*
533+
* The symbols below are referenced using relative relocations in the
534+
* respective ELF notes. This produces build time constants that the
535+
* linker will never mark as relocatable. (Using just ABSOLUTE() is not
536+
* sufficient for that).
537+
*/
538+
#ifdef CONFIG_XEN
539+
#ifdef CONFIG_XEN_PV
540+
xen_elfnote_entry_value =
541+
ABSOLUTE(xen_elfnote_entry) + ABSOLUTE(startup_xen);
542+
#endif
543+
xen_elfnote_hypercall_page_value =
544+
ABSOLUTE(xen_elfnote_hypercall_page) + ABSOLUTE(hypercall_page);
545+
#endif
546+
#ifdef CONFIG_PVH
547+
xen_elfnote_phys32_entry_value =
548+
ABSOLUTE(xen_elfnote_phys32_entry) + ABSOLUTE(pvh_start_xen - LOAD_OFFSET);
549+
#endif

arch/x86/platform/pvh/head.S

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8)
5353
#define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8)
5454

55-
SYM_CODE_START_LOCAL(pvh_start_xen)
55+
SYM_CODE_START(pvh_start_xen)
5656
UNWIND_HINT_END_OF_STACK
5757
cld
5858

@@ -300,5 +300,5 @@ SYM_DATA_END(pvh_level2_kernel_pgt)
300300
.long KERNEL_IMAGE_SIZE - 1)
301301
#endif
302302

303-
ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY,
304-
_ASM_PTR (pvh_start_xen - __START_KERNEL_map))
303+
ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .global xen_elfnote_phys32_entry;
304+
xen_elfnote_phys32_entry: _ASM_PTR xen_elfnote_phys32_entry_value - .)

arch/x86/tools/relocs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static const char * const sym_regex_kernel[S_NSYMTYPES] = {
5656
[S_ABS] =
5757
"^(xen_irq_disable_direct_reloc$|"
5858
"xen_save_fl_direct_reloc$|"
59+
"xen_elfnote_.+_offset$|"
5960
"VDSO|"
6061
"__kcfi_typeid_|"
6162
"__crc_)",

arch/x86/xen/xen-head.S

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ SYM_CODE_END(xen_cpu_bringup_again)
9494
ELFNOTE(Xen, XEN_ELFNOTE_VIRT_BASE, _ASM_PTR __START_KERNEL_map)
9595
/* Map the p2m table to a 512GB-aligned user address. */
9696
ELFNOTE(Xen, XEN_ELFNOTE_INIT_P2M, .quad (PUD_SIZE * PTRS_PER_PUD))
97-
ELFNOTE(Xen, XEN_ELFNOTE_ENTRY, _ASM_PTR startup_xen)
97+
ELFNOTE(Xen, XEN_ELFNOTE_ENTRY, .globl xen_elfnote_entry;
98+
xen_elfnote_entry: _ASM_PTR xen_elfnote_entry_value - .)
9899
ELFNOTE(Xen, XEN_ELFNOTE_FEATURES, .ascii "!writable_page_tables")
99100
ELFNOTE(Xen, XEN_ELFNOTE_PAE_MODE, .asciz "yes")
100101
ELFNOTE(Xen, XEN_ELFNOTE_L1_MFN_VALID,
@@ -115,7 +116,8 @@ SYM_CODE_END(xen_cpu_bringup_again)
115116
#else
116117
# define FEATURES_DOM0 0
117118
#endif
118-
ELFNOTE(Xen, XEN_ELFNOTE_HYPERCALL_PAGE, _ASM_PTR hypercall_page)
119+
ELFNOTE(Xen, XEN_ELFNOTE_HYPERCALL_PAGE, .globl xen_elfnote_hypercall_page;
120+
xen_elfnote_hypercall_page: _ASM_PTR xen_elfnote_hypercall_page_value - .)
119121
ELFNOTE(Xen, XEN_ELFNOTE_SUPPORTED_FEATURES,
120122
.long FEATURES_PV | FEATURES_PVH | FEATURES_DOM0)
121123
ELFNOTE(Xen, XEN_ELFNOTE_LOADER, .asciz "generic")

0 commit comments

Comments
 (0)