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