summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Jones <pjones@redhat.com>2023-06-28 16:47:48 -0400
committerPeter Jones <pjones@redhat.com>2023-06-29 14:35:50 -0400
commita0673e36db13e660f809de3e60900cb9381f8589 (patch)
tree5733779ef8f4c909bdb4170bfea5239e3bbdb027
parent1578b55a70bd2204cb0ad6db18ae4c003494df81 (diff)
downloadefi-boot-shim-a0673e36db13e660f809de3e60900cb9381f8589.tar.gz
efi-boot-shim-a0673e36db13e660f809de3e60900cb9381f8589.zip
Fix a 1-byte memory leak in .sbat parsing.
On the occasion that .sbat is entirely made of characters that aren't meaningfully CSV /data/, but which the parser accepts (i.e. newline), we currently allocate a byte of memory which then gets leaked. This patch tests for that condition and skips the allocation when there aren't any actual /entries/ to parse. Signed-off-by: Peter Jones <pjones@redhat.com>
-rw-r--r--sbat.c10
-rw-r--r--test-sbat.c18
2 files changed, 27 insertions, 1 deletions
diff --git a/sbat.c b/sbat.c
index a08c5b2a..4e51f2e8 100644
--- a/sbat.c
+++ b/sbat.c
@@ -15,7 +15,7 @@ parse_sbat_section(char *section_base, size_t section_size,
size_t *n_entries,
struct sbat_section_entry ***entriesp)
{
- struct sbat_section_entry *entry = NULL, **entries;
+ struct sbat_section_entry *entry = NULL, **entries = NULL;
EFI_STATUS efi_status = EFI_SUCCESS;
list_t csv, *pos = NULL;
char * end = section_base + section_size - 1;
@@ -67,6 +67,13 @@ parse_sbat_section(char *section_base, size_t section_size,
n++;
}
+ /*
+ * Not necessarily actually an *error* since we eat newlines and
+ * the like; it could actually just be /empty/.
+ */
+ if (n == 0)
+ goto out;
+
strtab = AllocateZeroPool(allocsz);
if (!strtab) {
efi_status = EFI_OUT_OF_RESOURCES;
@@ -101,6 +108,7 @@ parse_sbat_section(char *section_base, size_t section_size,
entry++;
n++;
}
+out:
*entriesp = entries;
*n_entries = n;
err:
diff --git a/test-sbat.c b/test-sbat.c
index 1bba6e22..0ee3d694 100644
--- a/test-sbat.c
+++ b/test-sbat.c
@@ -196,6 +196,22 @@ free_mock_sbat_entries(list_t *entries)
* parse_sbat_section() tests
*/
int
+test_parse_sbat_tiny(void)
+{
+ char section_base[] = "\0a\00";
+ size_t section_size = 2;
+ struct sbat_section_entry **entries;
+ size_t n = 0;
+ EFI_STATUS status;
+
+ status = parse_sbat_section(section_base, section_size, &n, &entries);
+ assert_equal_return(status, EFI_SUCCESS, -1, "got %#hhx expected %#hhx\n");
+ assert_equal_return(n, 0, -1, "got %#hhx expected %#hhx\n");
+
+ return 0;
+}
+
+int
test_parse_sbat_section_null_sbat_base(void)
{
char *section_base = NULL;
@@ -1141,7 +1157,9 @@ int
main(void)
{
int status = 0;
+
// parse_sbat section tests
+ test(test_parse_sbat_tiny);
test(test_parse_sbat_section_null_sbat_base);
test(test_parse_sbat_section_zero_sbat_size);
test(test_parse_sbat_section_null_entries);