ac3a84
From a83ec37232ca1ea817b3446b905f9e880223de21 Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Tue, 6 Dec 2022 13:06:57 +0900
ac3a84
Subject: [PATCH] boot: cleanups for efivar_get() and friends
ac3a84
ac3a84
- rename function arguments for storing results, and support the case
ac3a84
  that they are NULL,
ac3a84
- return earlier on error,
ac3a84
- always validate read size in efivar_get_uint32_le() and efivar_get_uint64_le().
ac3a84
ac3a84
(cherry picked from commit 9e406b1141da2d93b73428910f2504850631a3ee)
ac3a84
ac3a84
Related: #2141979
ac3a84
---
ac3a84
 src/boot/efi/util.c | 63 ++++++++++++++++++++++++---------------------
ac3a84
 src/boot/efi/util.h |  6 ++---
ac3a84
 2 files changed, 37 insertions(+), 32 deletions(-)
ac3a84
ac3a84
diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c
ac3a84
index 57436dbf0c..3eba2ade07 100644
ac3a84
--- a/src/boot/efi/util.c
ac3a84
+++ b/src/boot/efi/util.c
ac3a84
@@ -91,7 +91,7 @@ EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, const char16_t *name, ui
ac3a84
         return efivar_set_raw(vendor, name, buf, sizeof(buf), flags);
ac3a84
 }
ac3a84
 
ac3a84
-EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **value) {
ac3a84
+EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **ret) {
ac3a84
         _cleanup_free_ char16_t *buf = NULL;
ac3a84
         EFI_STATUS err;
ac3a84
         char16_t *val;
ac3a84
@@ -108,12 +108,12 @@ EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **v
ac3a84
         if ((size % sizeof(char16_t)) != 0)
ac3a84
                 return EFI_INVALID_PARAMETER;
ac3a84
 
ac3a84
-        if (!value)
ac3a84
+        if (!ret)
ac3a84
                 return EFI_SUCCESS;
ac3a84
 
ac3a84
         /* Return buffer directly if it happens to be NUL terminated already */
ac3a84
         if (size >= sizeof(char16_t) && buf[size / sizeof(char16_t) - 1] == 0) {
ac3a84
-                *value = TAKE_PTR(buf);
ac3a84
+                *ret = TAKE_PTR(buf);
ac3a84
                 return EFI_SUCCESS;
ac3a84
         }
ac3a84
 
ac3a84
@@ -123,18 +123,17 @@ EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **v
ac3a84
         memcpy(val, buf, size);
ac3a84
         val[size / sizeof(char16_t) - 1] = 0; /* NUL terminate */
ac3a84
 
ac3a84
-        *value = val;
ac3a84
+        *ret = val;
ac3a84
         return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
-EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN *i) {
ac3a84
+EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN *ret) {
ac3a84
         _cleanup_free_ char16_t *val = NULL;
ac3a84
         EFI_STATUS err;
ac3a84
         uint64_t u;
ac3a84
 
ac3a84
         assert(vendor);
ac3a84
         assert(name);
ac3a84
-        assert(i);
ac3a84
 
ac3a84
         err = efivar_get(vendor, name, &val;;
ac3a84
         if (err != EFI_SUCCESS)
ac3a84
@@ -143,7 +142,8 @@ EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name,
ac3a84
         if (!parse_number16(val, &u, NULL) || u > UINTN_MAX)
ac3a84
                 return EFI_INVALID_PARAMETER;
ac3a84
 
ac3a84
-        *i = u;
ac3a84
+        if (ret)
ac3a84
+                *ret = u;
ac3a84
         return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
@@ -156,15 +156,17 @@ EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const char16_t *name, ui
ac3a84
         assert(name);
ac3a84
 
ac3a84
         err = efivar_get_raw(vendor, name, &buf, &size);
ac3a84
-        if (err == EFI_SUCCESS && ret) {
ac3a84
-                if (size != sizeof(uint32_t))
ac3a84
-                        return EFI_BUFFER_TOO_SMALL;
ac3a84
+        if (err != EFI_SUCCESS)
ac3a84
+                return err;
ac3a84
 
ac3a84
+        if (size != sizeof(uint32_t))
ac3a84
+                return EFI_BUFFER_TOO_SMALL;
ac3a84
+
ac3a84
+        if (ret)
ac3a84
                 *ret = (uint32_t) buf[0] << 0U | (uint32_t) buf[1] << 8U | (uint32_t) buf[2] << 16U |
ac3a84
                         (uint32_t) buf[3] << 24U;
ac3a84
-        }
ac3a84
 
ac3a84
-        return err;
ac3a84
+        return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
 EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t *ret) {
ac3a84
@@ -176,19 +178,21 @@ EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const char16_t *name, ui
ac3a84
         assert(name);
ac3a84
 
ac3a84
         err = efivar_get_raw(vendor, name, &buf, &size);
ac3a84
-        if (err == EFI_SUCCESS && ret) {
ac3a84
-                if (size != sizeof(uint64_t))
ac3a84
-                        return EFI_BUFFER_TOO_SMALL;
ac3a84
+        if (err != EFI_SUCCESS)
ac3a84
+                return err;
ac3a84
+
ac3a84
+        if (size != sizeof(uint64_t))
ac3a84
+                return EFI_BUFFER_TOO_SMALL;
ac3a84
 
ac3a84
+        if (ret)
ac3a84
                 *ret = (uint64_t) buf[0] << 0U | (uint64_t) buf[1] << 8U | (uint64_t) buf[2] << 16U |
ac3a84
                         (uint64_t) buf[3] << 24U | (uint64_t) buf[4] << 32U | (uint64_t) buf[5] << 40U |
ac3a84
                         (uint64_t) buf[6] << 48U | (uint64_t) buf[7] << 56U;
ac3a84
-        }
ac3a84
 
ac3a84
-        return err;
ac3a84
+        return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
-EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **buffer, UINTN *size) {
ac3a84
+EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **ret, UINTN *ret_size) {
ac3a84
         _cleanup_free_ char *buf = NULL;
ac3a84
         UINTN l;
ac3a84
         EFI_STATUS err;
ac3a84
@@ -200,16 +204,15 @@ EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **b
ac3a84
         buf = xmalloc(l);
ac3a84
 
ac3a84
         err = RT->GetVariable((char16_t *) name, (EFI_GUID *) vendor, NULL, &l, buf);
ac3a84
-        if (err == EFI_SUCCESS) {
ac3a84
-
ac3a84
-                if (buffer)
ac3a84
-                        *buffer = TAKE_PTR(buf);
ac3a84
+        if (err != EFI_SUCCESS)
ac3a84
+                return err;
ac3a84
 
ac3a84
-                if (size)
ac3a84
-                        *size = l;
ac3a84
-        }
ac3a84
+        if (ret)
ac3a84
+                *ret = TAKE_PTR(buf);
ac3a84
+        if (ret_size)
ac3a84
+                *ret_size = l;
ac3a84
 
ac3a84
-        return err;
ac3a84
+        return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, bool *ret) {
ac3a84
@@ -219,13 +222,15 @@ EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, b
ac3a84
 
ac3a84
         assert(vendor);
ac3a84
         assert(name);
ac3a84
-        assert(ret);
ac3a84
 
ac3a84
         err = efivar_get_raw(vendor, name, &b, &size);
ac3a84
-        if (err == EFI_SUCCESS)
ac3a84
+        if (err != EFI_SUCCESS)
ac3a84
+                return err;
ac3a84
+
ac3a84
+        if (ret)
ac3a84
                 *ret = *b > 0;
ac3a84
 
ac3a84
-        return err;
ac3a84
+        return EFI_SUCCESS;
ac3a84
 }
ac3a84
 
ac3a84
 void efivar_set_time_usec(const EFI_GUID *vendor, const char16_t *name, uint64_t usec) {
ac3a84
diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h
ac3a84
index b33c50f9fc..994cf52ad6 100644
ac3a84
--- a/src/boot/efi/util.h
ac3a84
+++ b/src/boot/efi/util.h
ac3a84
@@ -105,9 +105,9 @@ EFI_STATUS efivar_set_uint32_le(const EFI_GUID *vendor, const char16_t *NAME, ui
ac3a84
 EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t value, uint32_t flags);
ac3a84
 void efivar_set_time_usec(const EFI_GUID *vendor, const char16_t *name, uint64_t usec);
ac3a84
 
ac3a84
-EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **value);
ac3a84
-EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **buffer, UINTN *size);
ac3a84
-EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN *i);
ac3a84
+EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **ret);
ac3a84
+EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **ret, UINTN *ret_size);
ac3a84
+EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN *ret);
ac3a84
 EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const char16_t *name, uint32_t *ret);
ac3a84
 EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t *ret);
ac3a84
 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, bool *ret);