OvmfPkg/X86QemuLoadImageLib: Handle allocation failure for CommandLine

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.

Signed-off-by: Martin Radev <martin.b.radev@gmail.com>
Message-Id: <YFPJsaGzVWQxoEU4@martin-ThinkPad-T440p>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Tom Lendacky <thomas.lendacky@amd.com>
[lersek@redhat.com: drop unnecessary empty line from code; remove personal
 (hence likely unstable) repo reference from commit message]
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
Martin Radev 2021-03-18 22:44:17 +01:00 committed by mergify[bot]
parent eb07bfb09e
commit ca31888271
1 changed files with 10 additions and 0 deletions

View File

@ -161,6 +161,11 @@ QemuLoadLegacyImage (
LoadedImage->CommandLine = LoadLinuxAllocateCommandLinePages ( LoadedImage->CommandLine = LoadLinuxAllocateCommandLinePages (
EFI_SIZE_TO_PAGES ( EFI_SIZE_TO_PAGES (
LoadedImage->CommandLineSize)); 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); QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData);
QemuFwCfgReadBytes (LoadedImage->CommandLineSize, LoadedImage->CommandLine); QemuFwCfgReadBytes (LoadedImage->CommandLineSize, LoadedImage->CommandLine);
} }
@ -178,6 +183,11 @@ QemuLoadLegacyImage (
LoadedImage->InitrdData = LoadLinuxAllocateInitrdPages ( LoadedImage->InitrdData = LoadLinuxAllocateInitrdPages (
LoadedImage->SetupBuf, LoadedImage->SetupBuf,
EFI_SIZE_TO_PAGES (LoadedImage->InitrdSize)); 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", DEBUG ((DEBUG_INFO, "Initrd size: 0x%x\n",
(UINT32)LoadedImage->InitrdSize)); (UINT32)LoadedImage->InitrdSize));
DEBUG ((DEBUG_INFO, "Reading initrd image ...")); DEBUG ((DEBUG_INFO, "Reading initrd image ..."));