]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-boot: Use non-failing allocators everywhere else 21307/head
authorJan Janssen <medhefgo@web.de>
Wed, 17 Nov 2021 10:11:23 +0000 (11:11 +0100)
committerJan Janssen <medhefgo@web.de>
Mon, 29 Nov 2021 15:20:45 +0000 (16:20 +0100)
12 files changed:
src/boot/efi/cpio.c
src/boot/efi/drivers.c
src/boot/efi/initrd.c
src/boot/efi/linux.c
src/boot/efi/measure.c
src/boot/efi/pe.c
src/boot/efi/random-seed.c
src/boot/efi/splash.c
src/boot/efi/stub.c
src/boot/efi/util.c
src/boot/efi/util.h
src/boot/efi/xbootldr.c

index 109003e9de3971918d367542d33785dfe331027f..d7dd50fc8fa2615fd4063edfab9a7c7677456862 100644 (file)
@@ -113,9 +113,7 @@ static EFI_STATUS pack_cpio_one(
 
         if (*cpio_buffer_size > UINTN_MAX - l) /* overflow check */
                 return EFI_OUT_OF_RESOURCES;
-        a = ReallocatePool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
-        if (!a)
-                return EFI_OUT_OF_RESOURCES;
+        a = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
 
         *cpio_buffer = a;
         a = (CHAR8*) *cpio_buffer + *cpio_buffer_size;
@@ -198,11 +196,8 @@ static EFI_STATUS pack_cpio_dir(
 
         if (*cpio_buffer_size > UINTN_MAX - l) /* overflow check */
                 return EFI_OUT_OF_RESOURCES;
-        a = ReallocatePool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
-        if (!a)
-                return EFI_OUT_OF_RESOURCES;
 
-        *cpio_buffer = a;
+        *cpio_buffer = a = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
         a = (CHAR8*) *cpio_buffer + *cpio_buffer_size;
 
         CopyMem(a, "070701", 6); /* magic ID */
@@ -262,7 +257,7 @@ static EFI_STATUS pack_cpio_prefix(
                 if (e > p) {
                         _cleanup_freepool_ CHAR8 *t = NULL;
 
-                        t = strndup8(path, e - path);
+                        t = xstrndup8(path, e - path);
                         if (!t)
                                 return EFI_OUT_OF_RESOURCES;
 
@@ -298,19 +293,13 @@ static EFI_STATUS pack_cpio_trailer(
                 "00000000"
                 "TRAILER!!!\0\0\0"; /* There's a fourth NUL byte appended here, because this is a string */
 
-        void *a;
-
         /* Generates the cpio trailer record that indicates the end of our initrd cpio archive */
 
         assert(cpio_buffer);
         assert(cpio_buffer_size);
         assert_cc(sizeof(trailer) % 4 == 0);
 
-        a = ReallocatePool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + sizeof(trailer));
-        if (!a)
-                return EFI_OUT_OF_RESOURCES;
-
-        *cpio_buffer = a;
+        *cpio_buffer = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + sizeof(trailer));
         CopyMem((UINT8*) *cpio_buffer + *cpio_buffer_size, trailer, sizeof(trailer));
         *cpio_buffer_size += sizeof(trailer);
 
@@ -346,10 +335,7 @@ EFI_STATUS pack_cpio(
         if (!root)
                 return log_error_status_stall(EFI_LOAD_ERROR, L"Unable to open root directory.");
 
-        extra_dir_path = PoolPrint(L"%D" EXTRA_DIR_SUFFIX, loaded_image->FilePath);
-        if (!extra_dir_path)
-                return log_oom();
-
+        extra_dir_path = xpool_print(L"%D" EXTRA_DIR_SUFFIX, loaded_image->FilePath);
         err = open_directory(root, extra_dir_path, &extra_dir);
         if (err == EFI_NOT_FOUND) {
                 /* No extra subdir, that's totally OK */
@@ -380,9 +366,7 @@ EFI_STATUS pack_cpio(
                 if (StrLen(dirent->FileName) > 255) /* Max filename size on Linux */
                         continue;
 
-                d = StrDuplicate(dirent->FileName);
-                if (!d)
-                        return log_oom();
+                d = xstrdup(dirent->FileName);
 
                 if (n_items+2 > n_allocated) {
                         UINTN m;
@@ -392,10 +376,7 @@ EFI_STATUS pack_cpio(
                                 return log_oom();
 
                         m = n_items + 16;
-                        items = ReallocatePool(items, n_allocated * sizeof(UINT16*), m * sizeof(UINT16*));
-                        if (!items)
-                                return log_oom();
-
+                        items = xreallocate_pool(items, n_allocated * sizeof(UINT16*), m * sizeof(UINT16*));
                         n_allocated = m;
                 }
 
index 4bb4dedaa0402f3f5ce9673969fa93efca1673b0..ce5cf3dcd740e19811864907455d9d9c6714ec61 100644 (file)
@@ -25,10 +25,7 @@ static EFI_STATUS load_one_driver(
         assert(loaded_image);
         assert(fname);
 
-        spath = PoolPrint(L"\\EFI\\systemd\\drivers\\%s", fname);
-        if (!spath)
-                return log_oom();
-
+        spath = xpool_print(L"\\EFI\\systemd\\drivers\\%s", fname);
         path = FileDevicePath(loaded_image->DeviceHandle, spath);
         if (!path)
                 return log_oom();
index 4475a3d5eee87611e443198138ec8e6a35f626c7..3136d3b8fa35fe138e6ff121cab260b8ffe98fba 100644 (file)
@@ -6,6 +6,7 @@
 #include "initrd.h"
 #include "macro-fundamental.h"
 #include "missing_efi.h"
+#include "util.h"
 
 /* extend LoadFileProtocol */
 struct initrd_loader {
@@ -88,10 +89,7 @@ EFI_STATUS initrd_register(
         if (err != EFI_NOT_FOUND) /* InitrdMedia is already registered */
                 return EFI_ALREADY_STARTED;
 
-        loader = AllocatePool(sizeof(struct initrd_loader));
-        if (!loader)
-                return EFI_OUT_OF_RESOURCES;
-
+        loader = xnew(struct initrd_loader, 1);
         *loader = (struct initrd_loader) {
                 .load_file.LoadFile = initrd_load_file,
                 .address = initrd_address,
index 8e3afb795b4c52d7d14985bf878614a2e478744c..ce0f4985c04211cb2cb97c591dcaf42ab353597c 100644 (file)
@@ -36,11 +36,7 @@ static EFI_STATUS loaded_image_register(
         assert(ret_image);
 
         /* create and install new LoadedImage Protocol */
-        loaded_image = AllocatePool(sizeof(EFI_LOADED_IMAGE));
-        if (!loaded_image)
-                return EFI_OUT_OF_RESOURCES;
-
-        /* provide the image base address and size */
+        loaded_image = xnew(EFI_LOADED_IMAGE, 1);
         *loaded_image = (EFI_LOADED_IMAGE) {
                 .ImageBase = (void *) linux_buffer,
                 .ImageSize = linux_length
index 220a8fbb357b296ca742fa5ed4d323e48fe99677..4b6e8776ca0fc05096fca1b4afab84e030e2bb28 100644 (file)
@@ -26,10 +26,7 @@ static EFI_STATUS tpm1_measure_to_pcr_and_event_log(
         assert(description);
 
         desc_len = StrSize(description);
-        tcg_event = AllocateZeroPool(OFFSETOF(TCG_PCR_EVENT, Event) + desc_len);
-        if (!tcg_event)
-                return EFI_OUT_OF_RESOURCES;
-
+        tcg_event = xallocate_zero_pool(OFFSETOF(TCG_PCR_EVENT, Event) + desc_len);
         *tcg_event = (TCG_PCR_EVENT) {
                 .EventSize = desc_len,
                 .PCRIndex = pcrindex,
@@ -60,10 +57,7 @@ static EFI_STATUS tpm2_measure_to_pcr_and_event_log(
         assert(description);
 
         desc_len = StrSize(description);
-        tcg_event = AllocateZeroPool(OFFSETOF(EFI_TCG2_EVENT, Event) + desc_len);
-        if (!tcg_event)
-                return EFI_OUT_OF_RESOURCES;
-
+        tcg_event = xallocate_zero_pool(OFFSETOF(EFI_TCG2_EVENT, Event) + desc_len);
         *tcg_event = (EFI_TCG2_EVENT) {
                 .Size = OFFSETOF(EFI_TCG2_EVENT, Event) + desc_len,
                 .Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER),
index e87b720dfa05affda0bfad010e2d6207e2f20b2f..91e7529fb1fbdeaf694ca461bcce35142281ff7f 100644 (file)
@@ -259,7 +259,7 @@ EFI_STATUS pe_file_locate_sections(
                 return EFI_LOAD_ERROR;
 
         section_table_len = pe.FileHeader.NumberOfSections * sizeof(struct PeSectionHeader);
-        section_table = AllocatePool(section_table_len);
+        section_table = xallocate_pool(section_table_len);
         if (!section_table)
                 return EFI_OUT_OF_RESOURCES;
 
index e829cf98b1d88020006fb38da3672a4e340f71f0..ecfd8a6b5198b1a5747b70e5a0179532f990ece0 100644 (file)
@@ -32,9 +32,7 @@ static EFI_STATUS acquire_rng(UINTN size, void **ret) {
         if (!rng)
                 return EFI_UNSUPPORTED;
 
-        data = AllocatePool(size);
-        if (!data)
-                return log_oom();
+        data = xallocate_pool(size);
 
         err = rng->GetRNG(rng, NULL, size, data);
         if (EFI_ERROR(err))
@@ -99,9 +97,7 @@ static EFI_STATUS hash_many(
         /* Hashes the specified parameters in counter mode, generating n hash values, with the counter in the
          * range counter_start…counter_start+n-1. */
 
-        output = AllocatePool(n * HASH_VALUE_SIZE);
-        if (!output)
-                return log_oom();
+        output = xallocate_pool(n * HASH_VALUE_SIZE);
 
         for (UINTN i = 0; i < n; i++)
                 hash_once(old_seed, rng, size,
@@ -274,9 +270,7 @@ EFI_STATUS process_random_seed(EFI_FILE *root_dir, RandomSeedMode mode) {
         if (size > RANDOM_MAX_SIZE_MAX)
                 return log_error_status_stall(EFI_INVALID_PARAMETER, L"Random seed file is too large.");
 
-        seed = AllocatePool(size);
-        if (!seed)
-                return log_oom();
+        seed = xallocate_pool(size);
 
         rsize = size;
         err = handle->Read(handle, &rsize, seed);
index fa923e77f20d6559d87884e707f633af9fdf09f9..71bd008d9612c0da37bd2590b7084fbc6f199fce 100644 (file)
@@ -260,7 +260,6 @@ EFI_STATUS graphics_splash(const UINT8 *content, UINTN len, const EFI_GRAPHICS_O
         struct bmp_dib *dib;
         struct bmp_map *map;
         const UINT8 *pixmap;
-        UINT64 blt_size;
         _cleanup_freepool_ void *blt = NULL;
         UINTN x_pos = 0;
         UINTN y_pos = 0;
@@ -302,10 +301,7 @@ EFI_STATUS graphics_splash(const UINT8 *content, UINTN len, const EFI_GRAPHICS_O
                 return err;
 
         /* EFI buffer */
-        blt_size = sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * dib->x * dib->y;
-        blt = AllocatePool(blt_size);
-        if (!blt)
-                return EFI_OUT_OF_RESOURCES;
+        blt = xnew(EFI_GRAPHICS_OUTPUT_BLT_PIXEL, dib->x * dib->y);
 
         err = GraphicsOutput->Blt(
                         GraphicsOutput, blt,
index 4de23792b5722dda82cc28413e40a30710f256e2..c99b3b7d6d6d08f94d2f556ae13271e11ba79233 100644 (file)
@@ -119,23 +119,15 @@ static void export_variables(EFI_LOADED_IMAGE *loaded_image) {
         /* if LoaderFirmwareInfo is not set, let's set it */
         if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareInfo", NULL, NULL) != EFI_SUCCESS) {
                 _cleanup_freepool_ CHAR16 *s = NULL;
-
-                s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
-                if (s)
-                        efivar_set(LOADER_GUID, L"LoaderFirmwareInfo", s, 0);
-                else
-                        log_oom();
+                s = xpool_print(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
+                efivar_set(LOADER_GUID, L"LoaderFirmwareInfo", s, 0);
         }
 
         /* ditto for LoaderFirmwareType */
         if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareType", NULL, NULL) != EFI_SUCCESS) {
                 _cleanup_freepool_ CHAR16 *s = NULL;
-
-                s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
-                if (s)
-                        efivar_set(LOADER_GUID, L"LoaderFirmwareType", s, 0);
-                else
-                        log_oom();
+                s = xpool_print(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
+                efivar_set(LOADER_GUID, L"LoaderFirmwareType", s, 0);
         }
 
         /* add StubInfo */
@@ -206,9 +198,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
         if ((!secure_boot_enabled() || cmdline_len == 0) && loaded_image->LoadOptionsSize > 0 &&
             *(CHAR16 *) loaded_image->LoadOptions > 0x1F) {
                 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
-                cmdline = cmdline_owned = AllocatePool(cmdline_len);
-                if (!cmdline)
-                        return log_oom();
+                cmdline = cmdline_owned = xallocate_pool(cmdline_len);
 
                 for (UINTN i = 0; i < cmdline_len; i++)
                         cmdline[i] = ((CHAR16 *) loaded_image->LoadOptions)[i];
index aa3c249562b166959ee67f9f2b01869b146badef..04e390b0fa79382db7a6fec57d2d3f3909b4cd78 100644 (file)
@@ -180,9 +180,7 @@ EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value
         }
 
         /* Make sure a terminating NUL is available at the end */
-        val = AllocatePool(size + sizeof(CHAR16));
-        if (!val)
-                return EFI_OUT_OF_RESOURCES;
+        val = xallocate_pool(size + sizeof(CHAR16));
 
         CopyMem(val, buf, size);
         val[size / sizeof(CHAR16)] = 0; /* NUL terminate */
@@ -256,9 +254,7 @@ EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **bu
         assert(name);
 
         l = sizeof(CHAR16 *) * EFI_MAXIMUM_VARIABLE_SIZE;
-        buf = AllocatePool(l);
-        if (!buf)
-                return EFI_OUT_OF_RESOURCES;
+        buf = xallocate_pool(l);
 
         err = RT->GetVariable((CHAR16 *) name, (EFI_GUID *) vendor, NULL, &l, buf);
         if (!EFI_ERROR(err)) {
@@ -467,10 +463,7 @@ EFI_STATUS file_read(EFI_FILE_HANDLE dir, const CHAR16 *name, UINTN off, UINTN s
                         return err;
         }
 
-        buf = AllocatePool(size + 1);
-        if (!buf)
-                return EFI_OUT_OF_RESOURCES;
-
+        buf = xallocate_pool(size + 1);
         err = handle->Read(handle, &size, buf);
         if (EFI_ERROR(err))
                 return err;
@@ -582,17 +575,11 @@ EFI_STATUS get_file_info_harder(
 
         /* A lot like LibFileInfo() but with useful error propagation */
 
-        fi = AllocatePool(size);
-        if (!fi)
-                return EFI_OUT_OF_RESOURCES;
-
+        fi = xallocate_pool(size);
         err = handle->GetInfo(handle, &GenericFileInfo, &size, fi);
         if (err == EFI_BUFFER_TOO_SMALL) {
                 FreePool(fi);
-                fi = AllocatePool(size);  /* GetInfo tells us the required size, let's use that now */
-                if (!fi)
-                        return EFI_OUT_OF_RESOURCES;
-
+                fi = xallocate_pool(size);  /* GetInfo tells us the required size, let's use that now */
                 err = handle->GetInfo(handle, &GenericFileInfo, &size, fi);
         }
 
@@ -624,11 +611,7 @@ EFI_STATUS readdir_harder(
 
         if (!*buffer) {
                 sz = OFFSETOF(EFI_FILE_INFO, FileName) /* + 256 */;
-
-                *buffer = AllocatePool(sz);
-                if (!*buffer)
-                        return EFI_OUT_OF_RESOURCES;
-
+                *buffer = xallocate_pool(sz);
                 *buffer_size = sz;
         } else
                 sz = *buffer_size;
@@ -636,15 +619,8 @@ EFI_STATUS readdir_harder(
         err = handle->Read(handle, &sz, *buffer);
         if (err == EFI_BUFFER_TOO_SMALL) {
                 FreePool(*buffer);
-
-                *buffer = AllocatePool(sz);
-                if (!*buffer) {
-                        *buffer_size = 0;
-                        return EFI_OUT_OF_RESOURCES;
-                }
-
+                *buffer = xallocate_pool(sz);
                 *buffer_size = sz;
-
                 err = handle->Read(handle, &sz, *buffer);
         }
         if (EFI_ERROR(err))
@@ -673,7 +649,7 @@ UINTN strnlena(const CHAR8 *p, UINTN maxlen) {
         return c;
 }
 
-CHAR8 *strndup8(const CHAR8 *p, UINTN sz) {
+CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz) {
         CHAR8 *n;
 
         /* Following efilib's naming scheme this function would be called strndupa(), but we already have a
@@ -684,9 +660,7 @@ CHAR8 *strndup8(const CHAR8 *p, UINTN sz) {
 
         sz = strnlena(p, sz);
 
-        n = AllocatePool(sz + 1);
-        if (!n)
-                return NULL;
+        n = xallocate_pool(sz + 1);
 
         if (sz > 0)
                 CopyMem(n, p, sz);
index d5f32dbb3890b6f69058b975c1c706e38147a49d..425d25d73f9b0d8d889c80322efb51b0d078d7df 100644 (file)
@@ -129,7 +129,7 @@ EFI_STATUS get_file_info_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **ret, UIN
 EFI_STATUS readdir_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **buffer, UINTN *buffer_size);
 
 UINTN strnlena(const CHAR8 *p, UINTN maxlen);
-CHAR8 *strndup8(const CHAR8 *p, UINTN sz);
+CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz);
 
 BOOLEAN is_ascii(const CHAR16 *f);
 
index 39c3c99d6e0a013828e2945f01058d9c1cbd5be0..81aa7f61eef3f6e18171db00edf7304312b30d43 100644 (file)
@@ -20,9 +20,7 @@ static EFI_DEVICE_PATH *path_parent(EFI_DEVICE_PATH *path, EFI_DEVICE_PATH *node
         assert(node);
 
         len = (UINT8*) NextDevicePathNode(node) - (UINT8*) path;
-        parent = (EFI_DEVICE_PATH*) AllocatePool(len + sizeof(EFI_DEVICE_PATH));
-        if (!parent)
-                return NULL;
+        parent = (EFI_DEVICE_PATH*) xallocate_pool(len + sizeof(EFI_DEVICE_PATH));
 
         CopyMem(parent, path, len);
         CopyMem((UINT8*) parent + len, EndDevicePath, sizeof(EFI_DEVICE_PATH));
@@ -112,9 +110,7 @@ static EFI_STATUS try_gpt(
 
         /* Now load the GPT entry table */
         size = ALIGN_TO((UINTN) gpt.gpt_header.SizeOfPartitionEntry * (UINTN) gpt.gpt_header.NumberOfPartitionEntries, 512);
-        entries = AllocatePool(size);
-        if (!entries)
-                return EFI_OUT_OF_RESOURCES;
+        entries = xallocate_pool(size);
 
         err = block_io->ReadBlocks(
                         block_io,