From: Thomas Garnier <thgarnie@google.com> To: "Herbert Xu" <herbert@gondor.apana.org.au>, "David S . Miller" <davem@davemloft.net>, "Thomas Gleixner" <tglx@linutronix.de>, "Ingo Molnar" <mingo@redhat.com>, "H . Peter Anvin" <hpa@zytor.com>, "Peter Zijlstra" <peterz@infradead.org>, "Josh Poimboeuf" <jpoimboe@redhat.com>, "Thomas Garnier" <thgarnie@google.com>, "Arnd Bergmann" <arnd@arndb.de>, "Matthias Kaehlcke" <mka@chromium.org>, "Boris Ostrovsky" <boris.ostrovsky@oracle.com>, "Juergen Gross" <jgross@suse.com>, "Paolo Bonzini" <pbonzini@redhat.com>, "Radim Krčmář" <rkrcmar@redhat.com>, "Joerg Roedel" <joro@8bytes.org>, "Andy Lutomirski" <luto@kernel.org>, "Borislav Petkov" <bp@alien8.de>, "Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>, "Brian Gerst" <brgerst@gmail.com>, "Borislav Petkov" <bp@suse.de>, "Christian Borntraeger" <borntraeger@de.ibm.com>, "Rafael J . Wysocki" <rjw@rjwysocki.net> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [RFC 12/22] x86/boot/64: Adapt assembly for PIE support Date: Tue, 18 Jul 2017 15:33:23 -0700 [thread overview] Message-ID: <20170718223333.110371-13-thgarnie@google.com> (raw) In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible. Early at boot, the kernel is mapped at a temporary address while preparing the page table. To know the changes needed for the page table with KASLR, the boot code calculate the difference between the expected address of the kernel and the one chosen by KASLR. It does not work with PIE because all symbols in code are relatives. Instead of getting the future relocated virtual address, you will get the current temporary mapping. The solution is using global variables that will be relocated as expected. Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit. Signed-off-by: Thomas Garnier <thgarnie@google.com> --- arch/x86/kernel/head_64.S | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 6225550883df..7e4f7a83a15a 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -78,8 +78,23 @@ startup_64: call __startup_64 popq %rsi - movq $(early_top_pgt - __START_KERNEL_map), %rax + movq _early_top_pgt_offset(%rip), %rax jmp 1f + + /* + * Position Independent Code takes only relative references in code + * meaning a global variable address is relative to RIP and not its + * future virtual address. Global variables can be used instead as they + * are still relocated on the expected kernel mapping address. + */ + .align 8 +_early_top_pgt_offset: + .quad early_top_pgt - __START_KERNEL_map +_init_top_offset: + .quad init_top_pgt - __START_KERNEL_map +_va_jump: + .quad 2f + ENTRY(secondary_startup_64) /* * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, @@ -98,7 +113,8 @@ ENTRY(secondary_startup_64) /* Sanitize CPU configuration */ call verify_cpu - movq $(init_top_pgt - __START_KERNEL_map), %rax + movq _init_top_offset(%rip), %rax + 1: /* Enable PAE mode, PGE and LA57 */ @@ -113,9 +129,8 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax - jmp *%rax -1: + jmp *_va_jump(%rip) +2: /* Check if nx is implemented */ movl $0x80000001, %eax @@ -211,11 +226,12 @@ ENTRY(secondary_startup_64) * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect, * address given in m16:64. */ - pushq $.Lafter_lret # put return address on stack for unwinder + leaq .Lafter_lret(%rip), %rax + pushq %rax # put return address on stack for unwinder xorq %rbp, %rbp # clear frame pointer - movq initial_code(%rip), %rax + leaq initial_code(%rip), %rax pushq $__KERNEL_CS # set correct cs - pushq %rax # target address in negative space + pushq (%rax) # target address in negative space lretq .Lafter_lret: ENDPROC(secondary_startup_64) -- 2.13.2.932.g7449e964c-goog _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org https://lists.xen.org/xen-devel
WARNING: multiple messages have this Message-ID (diff)
From: Thomas Garnier <thgarnie@google.com> To: "Herbert Xu" <herbert@gondor.apana.org.au>, "David S . Miller" <davem@davemloft.net>, "Thomas Gleixner" <tglx@linutronix.de>, "Ingo Molnar" <mingo@redhat.com>, "H . Peter Anvin" <hpa@zytor.com>, "Peter Zijlstra" <peterz@infradead.org>, "Josh Poimboeuf" <jpoimboe@redhat.com>, "Thomas Garnier" <thgarnie@google.com>, "Arnd Bergmann" <arnd@arndb.de>, "Matthias Kaehlcke" <mka@chromium.org>, "Boris Ostrovsky" <boris.ostrovsky@oracle.com>, "Juergen Gross" <jgross@suse.com>, "Paolo Bonzini" <pbonzini@redhat.com>, "Radim Krčmář" <rkrcmar@redhat.com>, "Joerg Roedel" <joro@8bytes.org>, "Andy Lutomirski" <luto@kernel.org>, "Borislav Petkov" <bp@alien8.de>, "Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>, "Brian Gerst" <brgerst@gmail.com>, "Borislav Petkov" <bp@suse.de>, "Christian Borntraeger" <borntraeger@de.ibm.com>, "Rafael J . Wysocki" <rjw@rjwysocki.net>, "Len Brown" <len.brown@intel.com>, "Pavel Machek" <pavel@ucw.cz>, "Tejun Heo" <tj@kernel.org>, "Christoph Lameter" <cl@linux.com>, "Kees Cook" <keescook@chromium.org>, "Paul Gortmaker" <paul.gortmaker@windriver.com>, "Chris Metcalf" <cmetcalf@mellanox.com>, "Paul E . McKenney" <paulmck@linux.vnet.ibm.com>, "Andrew Morton" <akpm@linux-foundation.org>, "Christopher Li" <sparse@chrisli.org>, "Dou Liyang" <douly.fnst@cn.fujitsu.com>, "Masahiro Yamada" <yamada.masahiro@socionext.com>, "Daniel Borkmann" <daniel@iogearbox.net>, "Markus Trippelsdorf" <markus@trippelsdorf.de>, "Peter Foley" <pefoley2@pefoley.com>, "Steven Rostedt" <rostedt@goodmis.org>, "Tim Chen" <tim.c.chen@linux.intel.com>, "Ard Biesheuvel" <ard.biesheuvel@linaro.org>, "Catalin Marinas" <catalin.marinas@arm.com>, "Matthew Wilcox" <mawilcox@microsoft.com>, "Michal Hocko" <mhocko@suse.com>, "Rob Landley" <rob@landley.net>, "Jiri Kosina" <jkosina@suse.cz>, "H . J . Lu" <hjl.tools@gmail.com>, "Paul Bolle" <pebolle@tiscali.nl>, "Baoquan He" <bhe@redhat.com>, "Daniel Micay" <danielmicay@gmail.com> Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [kernel-hardening] [RFC 12/22] x86/boot/64: Adapt assembly for PIE support Date: Tue, 18 Jul 2017 15:33:23 -0700 [thread overview] Message-ID: <20170718223333.110371-13-thgarnie@google.com> (raw) In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible. Early at boot, the kernel is mapped at a temporary address while preparing the page table. To know the changes needed for the page table with KASLR, the boot code calculate the difference between the expected address of the kernel and the one chosen by KASLR. It does not work with PIE because all symbols in code are relatives. Instead of getting the future relocated virtual address, you will get the current temporary mapping. The solution is using global variables that will be relocated as expected. Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit. Signed-off-by: Thomas Garnier <thgarnie@google.com> --- arch/x86/kernel/head_64.S | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 6225550883df..7e4f7a83a15a 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -78,8 +78,23 @@ startup_64: call __startup_64 popq %rsi - movq $(early_top_pgt - __START_KERNEL_map), %rax + movq _early_top_pgt_offset(%rip), %rax jmp 1f + + /* + * Position Independent Code takes only relative references in code + * meaning a global variable address is relative to RIP and not its + * future virtual address. Global variables can be used instead as they + * are still relocated on the expected kernel mapping address. + */ + .align 8 +_early_top_pgt_offset: + .quad early_top_pgt - __START_KERNEL_map +_init_top_offset: + .quad init_top_pgt - __START_KERNEL_map +_va_jump: + .quad 2f + ENTRY(secondary_startup_64) /* * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, @@ -98,7 +113,8 @@ ENTRY(secondary_startup_64) /* Sanitize CPU configuration */ call verify_cpu - movq $(init_top_pgt - __START_KERNEL_map), %rax + movq _init_top_offset(%rip), %rax + 1: /* Enable PAE mode, PGE and LA57 */ @@ -113,9 +129,8 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax - jmp *%rax -1: + jmp *_va_jump(%rip) +2: /* Check if nx is implemented */ movl $0x80000001, %eax @@ -211,11 +226,12 @@ ENTRY(secondary_startup_64) * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect, * address given in m16:64. */ - pushq $.Lafter_lret # put return address on stack for unwinder + leaq .Lafter_lret(%rip), %rax + pushq %rax # put return address on stack for unwinder xorq %rbp, %rbp # clear frame pointer - movq initial_code(%rip), %rax + leaq initial_code(%rip), %rax pushq $__KERNEL_CS # set correct cs - pushq %rax # target address in negative space + pushq (%rax) # target address in negative space lretq .Lafter_lret: ENDPROC(secondary_startup_64) -- 2.13.2.932.g7449e964c-goog
next prev parent reply other threads:[~2017-07-18 22:33 UTC|newest] Thread overview: 151+ messages / expand[flat|nested] mbox.gz Atom feed top 2017-07-18 22:33 x86: PIE support and option to extend KASLR randomization Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 01/22] x86/crypto: Adapt assembly for PIE support Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` Thomas Garnier 2017-07-18 22:33 ` [RFC 02/22] x86: Use symbol name on bug table " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 03/22] x86: Use symbol name in jump " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 04/22] x86: Add macro to get symbol address " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 05/22] xen: Adapt assembly " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 06/22] kvm: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 2:49 ` Brian Gerst 2017-07-19 2:49 ` [kernel-hardening] " Brian Gerst 2017-07-19 15:40 ` Thomas Garnier 2017-07-19 15:40 ` Thomas Garnier 2017-07-19 15:40 ` [kernel-hardening] " Thomas Garnier 2017-07-19 22:27 ` H. Peter Anvin 2017-07-19 22:27 ` H. Peter Anvin 2017-07-19 22:27 ` [kernel-hardening] " H. Peter Anvin 2017-07-19 22:44 ` Thomas Garnier 2017-07-19 22:44 ` [kernel-hardening] " Thomas Garnier 2017-07-19 22:44 ` Thomas Garnier 2017-07-19 22:58 ` Ard Biesheuvel 2017-07-19 22:58 ` [kernel-hardening] " Ard Biesheuvel 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 23:47 ` H. Peter Anvin 2017-07-19 22:58 ` Ard Biesheuvel 2017-07-19 2:49 ` Brian Gerst 2017-07-18 22:33 ` [RFC 07/22] x86: relocate_kernel - " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 22:58 ` H. Peter Anvin 2017-07-19 22:58 ` [kernel-hardening] " H. Peter Anvin 2017-07-19 23:23 ` Thomas Garnier 2017-07-19 23:23 ` [kernel-hardening] " Thomas Garnier 2017-07-19 23:23 ` Thomas Garnier 2017-07-19 22:58 ` H. Peter Anvin 2017-07-18 22:33 ` [RFC 08/22] x86/entry/64: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 09/22] x86: pm-trace - " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 10/22] x86/CPU: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 11/22] x86/acpi: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` Thomas Garnier [this message] 2017-07-18 22:33 ` [kernel-hardening] [RFC 12/22] x86/boot/64: " Thomas Garnier 2017-07-18 22:33 ` [RFC 13/22] x86/power/64: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 18:41 ` Pavel Machek 2017-07-19 18:41 ` [kernel-hardening] " Pavel Machek 2017-07-19 18:41 ` Pavel Machek 2017-07-18 22:33 ` [RFC 14/22] x86/paravirt: " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 15/22] x86/boot/64: Use _text in a global " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 16/22] x86/percpu: Adapt percpu " Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 3:08 ` Brian Gerst 2017-07-19 3:08 ` [kernel-hardening] " Brian Gerst 2017-07-19 18:26 ` Thomas Garnier 2017-07-19 18:26 ` Thomas Garnier 2017-07-19 18:26 ` [kernel-hardening] " Thomas Garnier 2017-07-19 23:33 ` H. Peter Anvin 2017-07-19 23:33 ` [kernel-hardening] " H. Peter Anvin 2017-07-20 2:21 ` H. Peter Anvin 2017-07-20 2:21 ` H. Peter Anvin 2017-07-20 2:21 ` [kernel-hardening] " H. Peter Anvin 2017-07-20 3:03 ` H. Peter Anvin 2017-07-20 3:03 ` [kernel-hardening] " H. Peter Anvin 2017-07-20 3:03 ` H. Peter Anvin 2017-07-20 14:26 ` Thomas Garnier 2017-07-20 14:26 ` Thomas Garnier 2017-07-20 14:26 ` [kernel-hardening] " Thomas Garnier 2017-08-02 16:42 ` Thomas Garnier 2017-08-02 16:42 ` Thomas Garnier 2017-08-02 16:42 ` [kernel-hardening] " Thomas Garnier 2017-08-02 16:56 ` Kees Cook 2017-08-02 16:56 ` [kernel-hardening] " Kees Cook 2017-08-02 18:05 ` Thomas Garnier 2017-08-02 18:05 ` Thomas Garnier 2017-08-02 18:05 ` [kernel-hardening] " Thomas Garnier 2017-08-02 16:56 ` Kees Cook 2017-07-19 23:33 ` H. Peter Anvin 2017-07-19 3:08 ` Brian Gerst 2017-07-18 22:33 ` [RFC 17/22] compiler: Option to default to hidden symbols Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 18/22] x86/relocs: Handle DYN relocations for PIE support Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 19/22] x86/pie: Add option to build the kernel as PIE for x86_64 Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-18 22:33 ` [RFC 20/22] x86/relocs: Add option to generate 64-bit relocations Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 22:33 ` H. Peter Anvin 2017-07-19 22:33 ` H. Peter Anvin 2017-07-19 22:33 ` [kernel-hardening] " H. Peter Anvin 2017-07-19 22:47 ` Thomas Garnier 2017-07-19 22:47 ` Thomas Garnier 2017-07-19 22:47 ` [kernel-hardening] " Thomas Garnier 2017-07-19 23:08 ` H. Peter Anvin 2017-07-19 23:08 ` [kernel-hardening] " H. Peter Anvin 2017-07-19 23:25 ` Thomas Garnier 2017-07-19 23:25 ` [kernel-hardening] " Thomas Garnier 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:45 ` H. Peter Anvin 2017-07-19 23:25 ` Thomas Garnier 2017-07-19 23:08 ` H. Peter Anvin 2017-07-18 22:33 ` [RFC 21/22] x86/module: Add support for mcmodel large and PLTs Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 1:35 ` H. Peter Anvin 2017-07-19 1:35 ` [kernel-hardening] " H. Peter Anvin 2017-07-19 3:59 ` Brian Gerst 2017-07-19 3:59 ` Brian Gerst 2017-07-19 3:59 ` [kernel-hardening] " Brian Gerst 2017-07-19 15:58 ` Thomas Garnier 2017-07-19 15:58 ` [kernel-hardening] " Thomas Garnier 2017-07-19 17:34 ` Brian Gerst 2017-07-19 17:34 ` [kernel-hardening] " Brian Gerst 2017-07-24 16:32 ` Thomas Garnier 2017-07-24 16:32 ` Thomas Garnier 2017-07-24 16:32 ` [kernel-hardening] " Thomas Garnier 2017-07-19 17:34 ` Brian Gerst 2017-07-19 15:58 ` Thomas Garnier 2017-07-19 1:35 ` H. Peter Anvin 2017-07-18 22:33 ` [RFC 22/22] x86/kaslr: Add option to extend KASLR range from 1GB to 3GB Thomas Garnier 2017-07-18 22:33 ` [kernel-hardening] " Thomas Garnier 2017-07-19 12:10 ` Baoquan He 2017-07-19 12:10 ` [kernel-hardening] " Baoquan He 2017-07-19 13:49 ` Baoquan He 2017-07-19 13:49 ` [kernel-hardening] " Baoquan He 2017-07-19 13:49 ` Baoquan He 2017-07-19 12:10 ` Baoquan He 2017-07-19 14:08 ` x86: PIE support and option to extend KASLR randomization Christopher Lameter 2017-07-19 14:08 ` Christopher Lameter 2017-07-19 14:08 ` [kernel-hardening] " Christopher Lameter 2017-07-19 19:21 ` Kees Cook 2017-07-19 19:21 ` Kees Cook 2017-07-19 19:21 ` [kernel-hardening] " Kees Cook
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=20170718223333.110371-13-thgarnie@google.com \ --to=thgarnie@google.com \ --cc=arnd@arndb.de \ --cc=boris.ostrovsky@oracle.com \ --cc=borntraeger@de.ibm.com \ --cc=bp@alien8.de \ --cc=bp@suse.de \ --cc=brgerst@gmail.com \ --cc=davem@davemloft.net \ --cc=herbert@gondor.apana.org.au \ --cc=hpa@zytor.com \ --cc=jgross@suse.com \ --cc=joro@8bytes.org \ --cc=jpoimboe@redhat.com \ --cc=kernel-hardening@lists.openwall.com \ --cc=kirill.shutemov@linux.intel.com \ --cc=kvm@vger.kernel.org \ --cc=linux-arch@vger.kernel.org \ --cc=linux-crypto@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-pm@vger.kernel.org \ --cc=linux-sparse@vger.kernel.org \ --cc=luto@kernel.org \ --cc=mingo@redhat.com \ --cc=mka@chromium.org \ --cc=pbonzini@redhat.com \ --cc=peterz@infradead.org \ --cc=rjw@rjwysocki.net \ --cc=rkrcmar@redhat.com \ --cc=tglx@linutronix.de \ --cc=x86@kernel.org \ --cc=xen-devel@lists.xenproject.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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.