Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1527752

Re: [PATCH] x86: fix kaslr and memmap collision

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.unit0.net!news.panservice.it!bofh.it!news.nic.it!robomod
From Dan Williams <dan.j.williams@intel.com>
Newsgroups linux.kernel
Subject Re: [PATCH] x86: fix kaslr and memmap collision
Date Tue, 22 Nov 2016 18:40:02 +0100
Message-ID <sGnd0-1Py-13@gated-at.bofh.it> (permalink)
References <sG78d-8kM-3@gated-at.bofh.it> <sGeW5-4Pc-15@gated-at.bofh.it>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=jsCD6kETWhHWVi8QWEYT0OGoq0KRyqOOnHSzpeOeXC8=; b=h6L0yv+mJVom1shB0G5qkb6ansPpw1b1x1n8Y/UKoZES7socowSlZ2AT+ddBhgCma5 XuDlQyLJ+XdZ7EX5Mg+D9iUHzyE6+wgG98Ot+1Dgw6SCwBhf/GPWaH5MtJWMf8iSVvNG QmbZb6YjMFnbMqRv6QOSW609D7acnvzvc5IC3jXzzNN2YET6QCe7UMzD2JSNf4HLbhQ3 YZKSLwu+ghBdKx7GJ9XEiT4odP8IxYTbMWh/rCHxQKnTxY0LdrhrLdx/oQTdebQzJ9TE j/aUhpy0nUglpNstntfvu4k9WIZXUtIaVXcZYRfZVCYhEz67EA43Io7ptMWb1gCtQpv/ 3ZMQ==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=jsCD6kETWhHWVi8QWEYT0OGoq0KRyqOOnHSzpeOeXC8=; b=Py8IkdXzOHeWkoUud9Xqm9CWp84yGiE5zkYBzR0bngH7mxLBh3EIxiiKLMOroxRZG6 WeWWtXNjv49e0jI9ySNNDHW+gePYfIOjbobm7sMljKOmyFjwzheOefLgQFeh9KJpFwUS UFSKYM+Q5vYs/ZIaWxqU42kKjNANEM1w0YvKgcpZxvRhAngLQeZ1ieBGaUjsUlpsQFXU n3SxJmdwKgtJ+y4UBSY0oGa9jgIrKuLED9uy8AxtYxvm8Rjx/bneDm/gt+YdhatKf12r knYgnyRLnNOl+svKO5QadaWUNCA5LBLDDfy8g9EYnBjot6IXIeyK3/lIxcdYeAVsKs5o Okaw==
X-Gm-Message-State AKaTC03HCKHnCNLiylt2s52lxs5W5xnAKDmaPRlhe5UzroU3WGUnrklrJBTi2+F3FKOwCtbkptFmXAn2yDUe3/JU
X-Received by 10.157.61.137 with SMTP id l9mr13120494otc.153.1479835588380; Tue, 22 Nov 2016 09:26:28 -0800 (PST)
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 143
Organization linux.* mail to news gateway
X-Original-Cc Dave Jiang <dave.jiang@intel.com>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>, X86 ML <x86@kernel.org>, david <david@fromorbit.com>, "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "linux-nvdimm@lists.01.org" <linux-nvdimm@lists.01.org>, Kees Cook <keescook@chromium.org>
X-Original-Date Tue, 22 Nov 2016 09:26:27 -0800
X-Original-Message-ID <CAPcyv4i+2NxdTc1xzVL+KQs8hLfUhbpGX3zstO_b=RY3hCy64w@mail.gmail.com>
X-Original-References <147977413859.13657.2181994710415174471.stgit@djiang5-desk3.ch.intel.com> <20161122084754.GA25596@gmail.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1527752

Show key headers only | View raw


[ replying for Dave since he's offline today and tomorrow ]

On Tue, Nov 22, 2016 at 12:47 AM, Ingo Molnar <mingo@kernel.org> wrote:
>
> * Dave Jiang <dave.jiang@intel.com> wrote:
>
>> CONFIG_RANDOMIZE_BASE relocates the kernel to a random base address.
>> However it does not take into account the memmap= parameter passed in from
>> the kernel commandline.
>
> memmap= parameters are often used as a list.
>
>> [...] This results in the kernel sometimes being put in the middle of the user
>> memmap. [...]
>
> What does this mean? If memmap= is used to re-define the memory map then the
> kernel getting in the middle of a RAM area is what we want, isn't it? What we
> don't want is for the kernel to get into reserved areas, right?

Right, this is about teaching kaslr to not land the kernel in newly
defined reserved regions that were not marked reserved in the initial
e820 map from platform firmware.

>> [...] Check has been added in the kaslr in order to avoid the region marked by
>> memmap.
>
> What does this mean?

Is this clearer? "Update the set of 'mem_avoid' entries to exclude
'memmap=' defined reserved regions from the set of valid address range
to land the kernel image."

>
>> Signed-off-by: Dave Jiang <dave.jiang@intel.com>
>> ---
>>  arch/x86/boot/boot.h             |    2 ++
>>  arch/x86/boot/compressed/kaslr.c |   45 ++++++++++++++++++++++++++++++++++++++
>>  arch/x86/boot/string.c           |   25 +++++++++++++++++++++
>>  3 files changed, 72 insertions(+)
>>
>> diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h
>> index e5612f3..0d5fe5b 100644
>> --- a/arch/x86/boot/boot.h
>> +++ b/arch/x86/boot/boot.h
>> @@ -332,6 +332,8 @@ int strncmp(const char *cs, const char *ct, size_t count);
>>  size_t strnlen(const char *s, size_t maxlen);
>>  unsigned int atou(const char *s);
>>  unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base);
>> +unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base);
>> +long simple_strtol(const char *cp, char **endp, unsigned int base);
>>  size_t strlen(const char *s);
>>
>>  /* tty.c */
>> diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
>> index a66854d..6fb8f1ec 100644
>> --- a/arch/x86/boot/compressed/kaslr.c
>> +++ b/arch/x86/boot/compressed/kaslr.c
>> @@ -11,6 +11,7 @@
>>   */
>>  #include "misc.h"
>>  #include "error.h"
>> +#include "../boot.h"
>>
>>  #include <generated/compile.h>
>>  #include <linux/module.h>
>> @@ -61,6 +62,7 @@ enum mem_avoid_index {
>>       MEM_AVOID_INITRD,
>>       MEM_AVOID_CMDLINE,
>>       MEM_AVOID_BOOTPARAMS,
>> +     MEM_AVOID_MEMMAP,
>>       MEM_AVOID_MAX,
>>  };
>>
>> @@ -77,6 +79,37 @@ static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
>>       return true;
>>  }
>>
>> +#include "../../../../lib/cmdline.c"
>> +
>> +static int
>> +parse_memmap(char *p, unsigned long long *start, unsigned long long *size)
>> +{
>> +     char *oldp;
>> +
>> +     if (!p)
>> +             return -EINVAL;
>> +
>> +     /* we don't care about this option here */
>> +     if (!strncmp(p, "exactmap", 8))
>> +             return -EINVAL;
>> +
>> +     oldp = p;
>> +     *size = memparse(p, &p);
>> +     if (p == oldp)
>> +             return -EINVAL;
>> +
>> +     switch (*p) {
>> +     case '@':
>> +     case '#':
>> +     case '$':
>> +     case '!':
>> +             *start = memparse(p+1, &p);
>> +             return 0;
>> +     }
>> +
>> +     return -EINVAL;
>> +}
>> +
>>  /*
>>   * In theory, KASLR can put the kernel anywhere in the range of [16M, 64T).
>>   * The mem_avoid array is used to store the ranges that need to be avoided
>> @@ -158,6 +191,8 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
>>       u64 initrd_start, initrd_size;
>>       u64 cmd_line, cmd_line_size;
>>       char *ptr;
>> +     char arg[38];
>
> Where does the magic '38' come from?
>
>> +     unsigned long long memmap_start, memmap_size;
>>
>>       /*
>>        * Avoid the region that is unsafe to overlap during
>> @@ -195,6 +230,16 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
>>       add_identity_map(mem_avoid[MEM_AVOID_BOOTPARAMS].start,
>>                        mem_avoid[MEM_AVOID_BOOTPARAMS].size);
>>
>> +     /* see if we have any memmap areas */
>> +     if (cmdline_find_option("memmap", arg, sizeof(arg)) > 0) {
>> +             int rc = parse_memmap(arg, &memmap_start, &memmap_size);
>> +
>> +             if (!rc) {
>> +                     mem_avoid[MEM_AVOID_MEMMAP].start = memmap_start;
>> +                     mem_avoid[MEM_AVOID_MEMMAP].size = memmap_size;
>> +             }
>> +     }
>> +
>
> This only handles a single (first) memmap argument, is that sufficient?

No, you're right, we need to handle multiple ranges.  Since the
mem_avoid array is statically allocated perhaps we can handle up to 4
memmap= entries, but past that point disable kaslr for that boot?

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] x86: fix kaslr and memmap collision Dave Jiang <dave.jiang@intel.com> - 2016-11-22 01:30 +0100
  Re: [PATCH] x86: fix kaslr and memmap collision Ingo Molnar <mingo@kernel.org> - 2016-11-22 09:50 +0100
    Re: [PATCH] x86: fix kaslr and memmap collision Dan Williams <dan.j.williams@intel.com> - 2016-11-22 18:40 +0100
      Re: [PATCH] x86: fix kaslr and memmap collision Kees Cook <keescook@chromium.org> - 2016-11-22 20:00 +0100
        Re: [PATCH] x86: fix kaslr and memmap collision Dan Williams <dan.j.williams@intel.com> - 2016-11-22 20:10 +0100
          Re: [PATCH] x86: fix kaslr and memmap collision Kees Cook <keescook@chromium.org> - 2016-11-22 23:40 +0100
          Re: [PATCH] x86: fix kaslr and memmap collision Dave Chinner <david@fromorbit.com> - 2016-11-24 01:10 +0100
            Re: [PATCH] x86: fix kaslr and memmap collision Dan Williams <dan.j.williams@intel.com> - 2016-11-24 20:40 +0100

csiph-web