kernel-hardening.lists.openwall.com archive mirror
 help / color / mirror / Atom feed
From: Nicolas Pitre <nicolas.pitre@linaro.org>
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Arnd Bergmann <arnd@arndb.de>,
	Kernel Hardening <kernel-hardening@lists.openwall.com>,
	Linux ARM <linux-arm-kernel@lists.infradead.org>,
	Russell King <linux@armlinux.org.uk>,
	Kees Cook <keescook@chromium.org>,
	Thomas Garnier <thgarnie@google.com>,
	Marc Zyngier <marc.zyngier@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Tony Lindgren <tony@atomide.com>,
	Matt Fleming <matt@codeblueprint.co.uk>,
	Dave Martin <dave.martin@arm.com>
Subject: [kernel-hardening] Re: [PATCH 00/30] implement KASLR for ARM
Date: Mon, 14 Aug 2017 14:01:32 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LFD.2.20.1708141333540.17016@knanqh.ubzr> (raw)
In-Reply-To: <CAKv+Gu_he5AFct0MzNcbZT-ynp_NRSkRpmNTpbfk1BuUS9+aGw@mail.gmail.com>

On Mon, 14 Aug 2017, Ard Biesheuvel wrote:

> On 14 August 2017 at 17:28, Nicolas Pitre <nicolas.pitre@linaro.org> wrote:
> > On Mon, 14 Aug 2017, Arnd Bergmann wrote:
> >
> >> On Mon, Aug 14, 2017 at 5:49 PM, Ard Biesheuvel
> >> <ard.biesheuvel@linaro.org> wrote:
> >> > On 14 August 2017 at 16:30, Arnd Bergmann <arnd@arndb.de> wrote:
> >>
> >>
> >> >> Can you explain how the random seed is passed from the bootloader
> >> >> to the kernel when we don't use EFI? Is this implemented at all? I see
> >> >> that you add a seed to "/chosen/kaslr-seed" in the EFI stub when using
> >> >> the EFI boot services, but I don't see where that value gets read again
> >> >> when we relocate the kernel.
> >>
> >> > To allow other bootloaders to do the same, the kaslr metadata is
> >> > exposed via a zImage header, containing the values of PAGE_OFFSET, the
> >> > base of the vmalloc area and the randomization granularity. A
> >> > bootloader can read these values, and taking the size of DRAM and the
> >> > placement of initrd and DTB into account, it can choose a value for
> >> > kaslr offset and write it back into the zImage header.
> >> >
> >> > This is a bit involved, but it is really difficult to make these
> >> > things backward compatible, i.e., passing something in a register is
> >> > not possible if that register was not mandated to be zero initially.
> >> >
> >> > Similarly, the decompressor passed the kaslr offset to the startup
> >> > code in the core kernel. It does so by passing it in r3 and jumping 4
> >> > bytes past the entry point. This way, we are backward compatible with
> >> > configurations where the decompressor is not used, because in that
> >> > case, you always jump to the first instruction, which zeroes r3.
> >>
> >> There are two ideas we discussed in the past (but never implemented
> >> them obviously):
> >>
> >> - instead of reading the "kaslr-seed" in the decompressor, it could
> >>   simply hash all of the DT blob to get the seed. This way the bootloader
> >>   can put the random see anywhere it likes, and as an added bonus,
> >>   we also get a little bit more random behavior on machines that have
> >>   no entropy source at all but that do have things like a serial number or
> >>   mac address in DT. Obviously those would be constant across boots
> >>   but different between machines. The OS can also store a random
> >>   seed during shutdown in a location that the bootloader uses to
> >>   initialize /chosen/kaslr-seed or another property that we use to seed
> >>   the kernel PRNG at boot time.
> >>
> >> - If we have a random number at boot but no way to pass it through
> >>   the DT, I think we actually /can/ pass it through registers: the
> >>   register state is undefined, so in the worst case using the XOR of
> >>   all registers gives us the same number on each boot, but the
> >>   if the loader is modified to store a random 32-bit number in any
> >>   of the registers that don't pass other information, we can use that
> >>   to calculate the kaslr-base.
> >
> > I really like the later. This way there is no hard protocol to define
> > and follow. The bootloader may exploit any source of randomness it can
> > find, including the RTC in addition to the serial number for example. So
> > doing both on the kernel side might actually be the best approach,
> > giving the boot environment all the flexibility it wants and being
> > compatible with all of them.
> >
> 
> Finding a source of entropy is not trivial, but it is not the difficult part.
> 
> So when we pass some random seed to the decompressor, what will it do
> with it? In order to find a suitable KASLR offset, it needs to know
> the size of DRAM, the placement of the DT and potentially an initrd,
> and the size of the vmalloc region in order to decide where it can put
> the kernel.
> 
> In my implementation, the decompressor simply receives the offset from
> the bootloader, and exposes the value of PAGE_OFFSET, the base of  the
> vmalloc region and the kaslr granularity supported by the kernel. The
> bootloader should already know the size of DRAM and where it loaded
> the DT and initrd, so it can roll the dice in an informed manner.
> 
> Note that my UEFI stub implementation essentially does the same, which
> was trivial to implement because UEFI already keeps track of all
> allocations in DRAM. Note that this version simply disables KASLR if
> it encounters a vmalloc= command line argument, given that it affects
> the size of the lowmem region. We could enhance that to actually parse
> the value, but I kept it simple for now.

What I dislike about such an arrangement (and I've brought up this 
argument forward in a different context before) is that you create a lot 
of additional dependencies between the kernel and the boot environment. 
The kernel is no longer self-sufficient and all this boot preparation 
has to be duplicated in all boot environments from UEFI to U-Boot to 
qemu. The fact that the bootloader now has to care about very Linux 
internal concepts such as vmalloc_start makes it rather inelegant to me.

I'm even wondering if, design wise, the best solution wouldn't be for 
the actual kernel to move itself during the boot process and reboot 
itself without any external help. Not necessarily go as far as the full 
kexec danse , but boot far enough to parse the DT, initialize the 
bootmem allocator, then find a new location for itself, move it there, 
do the relocs and restart the boot process.  For this to work well, you 
would have to make a copy of the .data section so to reboot again with a 
pristine version except for one flag indicating that the move has been 
done.

Doing it this way gives you a full kernel environment to work from and 
be completely self-sufficient with zero reliance on any boot 
environment. This would even make it compatible with Image i.e. the 
compressor-less kernel. And if the DT and/or initrd is in the way then 
you could even go as far as moving them away if you wanted.

This would add some boot latency of course, but certainly in the 
sub-second range. That is negligible for those systems where KASLR is 
most relevant.


Nicolas

  reply	other threads:[~2017-08-14 18:01 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-14 12:53 [kernel-hardening] [PATCH 00/30] implement KASLR for ARM Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 01/30] asm-generic: add .data.rel.ro sections to __ro_after_init Ard Biesheuvel
2017-08-14 14:26   ` [kernel-hardening] " Arnd Bergmann
2017-08-14 12:53 ` [kernel-hardening] [PATCH 02/30] ARM: assembler: introduce adr_l, ldr_l and str_l macros Ard Biesheuvel
2017-08-14 15:29   ` [kernel-hardening] " Dave Martin
2017-08-14 15:38     ` Ard Biesheuvel
2017-08-14 15:50       ` Dave Martin
2017-08-14 16:18         ` Nicolas Pitre
2017-08-14 16:22           ` Ard Biesheuvel
2017-08-14 16:33             ` Nicolas Pitre
2017-08-14 16:42             ` Russell King - ARM Linux
2017-08-14 16:56               ` Ard Biesheuvel
2017-08-14 15:32   ` Dave Martin
2017-08-14 15:40     ` Ard Biesheuvel
2017-08-14 15:53       ` Dave Martin
2017-08-14 12:53 ` [kernel-hardening] [PATCH 03/30] ARM: head-common.S: use PC-relative insn sequence for __proc_info Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 04/30] ARM: head-common.S: use PC-relative insn sequence for idmap creation Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 05/30] ARM: head.S: use PC-relative insn sequence for secondary_data Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 06/30] ARM: kernel: use relative references for UP/SMP alternatives Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 07/30] ARM: head: use PC-relative insn sequence for __smp_alt Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 08/30] ARM: sleep.S: use PC-relative insn sequence for sleep_save_sp/mpidr_hash Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 09/30] ARM: head.S: use PC-relative insn sequences for __fixup_pv_table Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 10/30] ARM: head.S: use PC relative insn sequence to calculate PHYS_OFFSET Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 11/30] ARM: kvm: replace open coded VA->PA calculations with adr_l call Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 12/30] arm-soc: exynos: replace open coded VA->PA conversions Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 13/30] arm-soc: mvebu: replace open coded VA->PA conversion Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 14/30] arm-soc: various: replace open coded VA->PA calculation of pen_release Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 15/30] ARM: kernel: switch to relative exception tables Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 16/30] ARM: kernel: use relative phys-to-virt patch tables Ard Biesheuvel
2017-08-14 12:53 ` [kernel-hardening] [PATCH 17/30] arm-soc: tegra: make sleep asm code runtime relocatable Ard Biesheuvel
2017-08-14 14:42   ` [kernel-hardening] " Dave Martin
2017-08-14 14:49     ` Ard Biesheuvel
2017-08-14 15:29       ` Dave Martin
2017-08-14 12:53 ` [kernel-hardening] [PATCH 18/30] ARM: kernel: make vmlinux buildable as a PIE executable Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 19/30] ARM: kernel: use PC-relative symbol references in MMU switch code Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 20/30] ARM: kernel: use PC relative symbol references in suspend/resume code Ard Biesheuvel
2017-08-14 16:02   ` [kernel-hardening] " Nicolas Pitre
2017-08-14 18:14     ` Ard Biesheuvel
2017-08-14 18:37       ` Nicolas Pitre
2017-08-14 12:54 ` [kernel-hardening] [PATCH 21/30] ARM: mm: export default vmalloc base address Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 22/30] ARM: kernel: refer to swapper_pg_dir via its symbol Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 23/30] ARM: kernel: implement randomization of the kernel load address Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 24/30] ARM: decompressor: explicitly map decompressor binary cacheable Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 25/30] ARM: compressed: factor out zImage header and make it extensible Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 26/30] ARM: decompressor: add KASLR support Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 27/30] efi/libstub: add 'max' parameter to efi_random_alloc() Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 28/30] efi/libstub: check for vmalloc= command line argument Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 29/30] efi/libstub: arm: reserve bootloader supplied initrd in memory map Ard Biesheuvel
2017-08-18 11:48   ` [kernel-hardening] " Ard Biesheuvel
2017-08-21 10:37   ` Mark Rutland
2017-08-21 10:39     ` Ard Biesheuvel
2017-08-14 12:54 ` [kernel-hardening] [PATCH 30/30] efi/libstub: arm: implement KASLR Ard Biesheuvel
2017-08-14 15:30 ` [kernel-hardening] Re: [PATCH 00/30] implement KASLR for ARM Arnd Bergmann
2017-08-14 15:49   ` Ard Biesheuvel
2017-08-14 16:03     ` Arnd Bergmann
2017-08-14 16:28       ` Nicolas Pitre
2017-08-14 17:28         ` Ard Biesheuvel
2017-08-14 18:01           ` Nicolas Pitre [this message]
2017-08-14 18:08             ` Ard Biesheuvel
2017-08-14 16:16     ` Nicolas Pitre

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=alpine.LFD.2.20.1708141333540.17016@knanqh.ubzr \
    --to=nicolas.pitre@linaro.org \
    --cc=ard.biesheuvel@linaro.org \
    --cc=arnd@arndb.de \
    --cc=dave.martin@arm.com \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=marc.zyngier@arm.com \
    --cc=mark.rutland@arm.com \
    --cc=matt@codeblueprint.co.uk \
    --cc=thgarnie@google.com \
    --cc=tony@atomide.com \
    /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;
as well as URLs for NNTP newsgroup(s).