From: "Ard Biesheuvel" <ardb@kernel.org>
To: Martin Radev <martin.b.radev@gmail.com>
Cc: devel@edk2.groups.io, Laszlo Ersek <lersek@redhat.com>,
Ard Biesheuvel <ardb+tianocore@kernel.org>,
Jordan Justen <jordan.l.justen@intel.com>,
Tom Lendacky <thomas.lendacky@amd.com>
Subject: Re: [PATCH v1] OvmfPkg/X86QemuLoadImageLib: Handle allocation failure for CommandLine
Date: Fri, 19 Mar 2021 15:55:29 +0100 [thread overview]
Message-ID: <CAMj1kXG+UO0sW88ym3-6FQ77DwYuECJ2N5yDHP3enUWxx-ctOg@mail.gmail.com> (raw)
In-Reply-To: <YFS3t4SDyfYL+Wxn@martin-ThinkPad-T440p>
On Fri, 19 Mar 2021 at 15:39, Martin Radev <martin.b.radev@gmail.com> wrote:
>
> On Fri, Mar 19, 2021 at 03:27:00PM +0100, Ard Biesheuvel wrote:
> > On Thu, 18 Mar 2021 at 22:44, Martin Radev <martin.b.radev@gmail.com> wrote:
> > >
> > > The CommandLine and InitrdData may be set to NULL if the provided
> > > size is too large. Because the zero page is mapped, this would not
> > > cause an immediate crash but can lead to memory corruption instead.
> > > This patch just adds validation and returns error if either allocation
> > > has failed.
> > >
> > > Ref: https://github.com/martinradev/edk2/commit/6c0ce748b97393240c006e24b73652f30e597a05
> > >
> > > Signed-off-by: Martin Radev <martin.b.radev@gmail.com>
> >
> > This seems reasonable to me.
> >
> > Acked-by: Ard Biesheuvel <ardb@kernel.org>
> >
> > > ---
> > > OvmfPkg/Library/X86QemuLoadImageLib/X86QemuLoadImageLib.c | 11 +++++++++++
> > > 1 file changed, 11 insertions(+)
> > >
> > > diff --git a/OvmfPkg/Library/X86QemuLoadImageLib/X86QemuLoadImageLib.c b/OvmfPkg/Library/X86QemuLoadImageLib/X86QemuLoadImageLib.c
> > > index 931553c0c1..b983c4d7d0 100644
> > > --- a/OvmfPkg/Library/X86QemuLoadImageLib/X86QemuLoadImageLib.c
> > > +++ b/OvmfPkg/Library/X86QemuLoadImageLib/X86QemuLoadImageLib.c
> > > @@ -161,6 +161,12 @@ QemuLoadLegacyImage (
> > > LoadedImage->CommandLine = LoadLinuxAllocateCommandLinePages (
> > > EFI_SIZE_TO_PAGES (
> > > LoadedImage->CommandLineSize));
> > > +
> > > + if (LoadedImage->CommandLine == NULL) {
> > > + DEBUG ((DEBUG_ERROR, "Unable to allocate memory for kernel command line!\n"));
> > > + Status = EFI_OUT_OF_RESOURCES;
> > > + goto FreeImage;
> > > + }
> > > QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData);
> > > QemuFwCfgReadBytes (LoadedImage->CommandLineSize, LoadedImage->CommandLine);
> > > }
> > > @@ -178,6 +184,11 @@ QemuLoadLegacyImage (
> > > LoadedImage->InitrdData = LoadLinuxAllocateInitrdPages (
> > > LoadedImage->SetupBuf,
> > > EFI_SIZE_TO_PAGES (LoadedImage->InitrdSize));
> > > + if (LoadedImage->InitrdData == NULL) {
> > > + DEBUG ((DEBUG_ERROR, "Unable to allocate memory for initrd!\n"));
> > > + Status = EFI_OUT_OF_RESOURCES;
> > > + goto FreeImage;
> > > + }
> > > DEBUG ((DEBUG_INFO, "Initrd size: 0x%x\n",
> > > (UINT32)LoadedImage->InitrdSize));
> > > DEBUG ((DEBUG_INFO, "Reading initrd image ..."));
> > > --
> > > 2.17.1
> > >
>
> Thanks. I have a curiousity question:
> Is there a good reason the zero page is kept mapped?
> IMO, it makes sense to have the first page unmapped to avoid cases when some piece of code
> returns NULL as a failure to an allocation, but then later code uses it by mistake.
> Unmapping it could be a security hardening.
I agree.
Amusingly, we had to add a special case to the X86 option rom emulator
[0] because many proprietary EFI drivers written for x86 contain NULL
pointer dereferences that simply go unnoticed when executed natively,
but under emulation on a AArch64 host, they trigger page faults as ARM
systems usually don't have anything accessible at physical address
0x0.
[0] https://github.com/ardbiesheuvel/X86EmulatorPkg
next prev parent reply other threads:[~2021-03-19 14:55 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-18 21:44 [PATCH v1] OvmfPkg/X86QemuLoadImageLib: Handle allocation failure for CommandLine Martin Radev
2021-03-19 14:27 ` Ard Biesheuvel
2021-03-19 14:39 ` Martin Radev
2021-03-19 14:55 ` Ard Biesheuvel [this message]
2021-03-19 19:17 ` [edk2-devel] " Laszlo Ersek
2021-03-19 15:40 ` Lendacky, Thomas
2021-03-19 18:49 ` [edk2-devel] " Laszlo Ersek
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-list 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=CAMj1kXG+UO0sW88ym3-6FQ77DwYuECJ2N5yDHP3enUWxx-ctOg@mail.gmail.com \
--to=devel@edk2.groups.io \
/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