From: "Michał Cłapiński" <mclapinski@google.com>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Dan Williams <dan.j.williams@intel.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] x86/boot/compressed: Fix avoiding memmap in physical KASLR
Date: Sat, 21 Jun 2025 11:51:10 +0200 [thread overview]
Message-ID: <CAAi7L5dmAYJoB0BfKvoT0iKOeWuc5hLqFPBPQr99TkdEB1dtHg@mail.gmail.com> (raw)
In-Reply-To: <CAMj1kXErKwmiM5AiGOCE5D39U+3o_MXz5vqJmE5pkjQV308d9w@mail.gmail.com>
Hi Ard,
On Sat, Jun 21, 2025 at 10:19 AM Ard Biesheuvel <ardb@kernel.org> wrote:
>
> Hi Michal,
>
> On Tue, 10 Jun 2025 at 23:42, Michal Clapinski <mclapinski@google.com> wrote:
> >
> > The intent of the code was to cancel KASLR if there are more than 4
> > memmap args. Unfortunately, it was only doing that if the memmap args
> > were comma delimited, not if they were entirely separate.
> > This change fixes it.
> >
> > Signed-off-by: Michal Clapinski <mclapinski@google.com>
> > ---
> > I would like KASLR to support more than 4 memmap args. Do you think
> > I can just increase the MAX_MEMMAP_REGIONS or should I implement
> > support for the general case?
> >
> > arch/x86/boot/compressed/kaslr.c | 3 ---
> > 1 file changed, 3 deletions(-)
> >
> > diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
> > index f03d59ea6e40..4aa9c9781ca7 100644
> > --- a/arch/x86/boot/compressed/kaslr.c
> > +++ b/arch/x86/boot/compressed/kaslr.c
> > @@ -162,9 +162,6 @@ static void mem_avoid_memmap(char *str)
> > {
> > static int i;
> >
> > - if (i >= MAX_MEMMAP_REGIONS)
> > - return;
> > -
>
> It isn't obvious at all why simply dropping this condition is fine.
> Could you elaborate?
Of course. Let's look at the whole function without my change:
static void mem_avoid_memmap(char *str)
{
static int i;
if (i >= MAX_MEMMAP_REGIONS)
return;
while (str && (i < MAX_MEMMAP_REGIONS)) {
int rc;
u64 start, size;
char *k = strchr(str, ',');
if (k)
*k++ = 0;
rc = parse_memmap(str, &start, &size);
if (rc < 0)
break;
str = k;
if (start == 0) {
/* Store the specified memory limit if size > 0 */
if (size > 0 && size < mem_limit)
mem_limit = size;
continue;
}
mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].start = start;
mem_avoid[MEM_AVOID_MEMMAP_BEGIN + i].size = size;
i++;
}
/* More than 4 memmaps, fail kaslr */
if ((i >= MAX_MEMMAP_REGIONS) && str)
memmap_too_large = true;
}
This function is called for every memmap= param. Let's say we supply
only separate memmap= params (instead of comma delimited). Then on the
4th param, `i` will be equal to MAX_MEMMAP_REGIONS but the last `if`
won't execute since `str` will be null. Then on the 5th param the
first `if` (the one I want to remove) will execute and
`memmap_too_large` will never be set.
With my change, while parsing the 5th param, the `while` loop will be
skipped since `i` is not smaller than MAX_MEMMAP_REGIONS and the last
`if` will execute setting `memmap_too_large`. Basically, my change is
safe because the `if` I want to remove is already baked into the
`while` loop condition.
>
> > while (str && (i < MAX_MEMMAP_REGIONS)) {
> > int rc;
> > u64 start, size;
> > --
> > 2.50.0.rc0.642.g800a2b2222-goog
> >
next prev parent reply other threads:[~2025-06-21 9:51 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-10 21:41 [PATCH 1/1] x86/boot/compressed: Fix avoiding memmap in physical KASLR Michal Clapinski
2025-06-10 22:44 ` Pasha Tatashin
2025-06-21 8:18 ` Ard Biesheuvel
2025-06-21 9:51 ` Michał Cłapiński [this message]
2025-07-02 15:57 ` Chris Li
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=CAAi7L5dmAYJoB0BfKvoT0iKOeWuc5hLqFPBPQr99TkdEB1dtHg@mail.gmail.com \
--to=mclapinski@google.com \
--cc=ardb@kernel.org \
--cc=bp@alien8.de \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pasha.tatashin@soleen.com \
--cc=tglx@linutronix.de \
--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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).