From 68d7c268f8b0e28ae719a8c783e3ded253563f9d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 22 Oct 2018 19:59:45 +0200 Subject: [PATCH] efivars: let's add some validation of boot menu entry name syntax --- src/shared/efivars.c | 21 +++++++++++++++++---- src/shared/efivars.h | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/shared/efivars.c b/src/shared/efivars.c index 360ef03400..fbed9027c1 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -771,6 +771,16 @@ int efi_loader_get_device_part_uuid(sd_id128_t *u) { return 0; } +bool efi_loader_entry_name_valid(const char *s) { + if (isempty(s)) + return false; + + if (strlen(s) > FILENAME_MAX) /* Make sure entry names fit in filenames */ + return false; + + return in_charset(s, ALPHANUMERICAL "-"); +} + int efi_loader_get_entries(char ***ret) { _cleanup_free_ char16_t *entries = NULL; _cleanup_strv_free_ char **l = NULL; @@ -789,7 +799,7 @@ int efi_loader_get_entries(char ***ret) { /* The variable contains a series of individually NUL terminated UTF-16 strings. */ for (i = 0, start = 0;; i++) { - char *decoded; + _cleanup_free_ char *decoded = NULL; bool end; /* Is this the end of the variable's data? */ @@ -805,9 +815,12 @@ int efi_loader_get_entries(char ***ret) { if (!decoded) return -ENOMEM; - r = strv_consume(&l, decoded); - if (r < 0) - return r; + if (efi_loader_entry_name_valid(decoded)) { + r = strv_consume(&l, TAKE_PTR(decoded)); + if (r < 0) + return r; + } else + log_debug("Ignoring invalid loader entry '%s'.", decoded); /* We reached the end of the variable */ if (end) diff --git a/src/shared/efivars.h b/src/shared/efivars.h index 51ecf6a279..92670c82c7 100644 --- a/src/shared/efivars.h +++ b/src/shared/efivars.h @@ -50,6 +50,8 @@ int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader); int efi_loader_get_entries(char ***ret); +bool efi_loader_entry_name_valid(const char *s); + int efi_loader_get_features(uint64_t *ret); #else