Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1puCtB-0000jp-U1 for mharc-grub-devel@gnu.org; Wed, 03 May 2023 09:51:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1puCt9-0000hp-Kn for grub-devel@gnu.org; Wed, 03 May 2023 09:51:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1puCt7-0007ie-Jp for grub-devel@gnu.org; Wed, 03 May 2023 09:51:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683121880; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JTrIaZSwmjpwIgIZ4LPktiKhj4odZBTBW7O6Q7K6RhQ=; b=fAJ9CpStVSGn7HEk19/I9ISaWHQ7BP39rG0m1YB29lPf6Scn3JlHHjlPOsLz7FVtp/LTeK nLByvo7zSASneP3d770M71IlOU00eiSiAzBTMbjgQTqV8SqkuHV9mU8lRavMCM8Fjg7NpF vCcdem0KFtybR/fDm07xV0xcappTI/w= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-117-bIEAgl9NPA-sSV4_zsGfkQ-1; Wed, 03 May 2023 09:51:15 -0400 X-MC-Unique: bIEAgl9NPA-sSV4_zsGfkQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7C7513C0F372; Wed, 3 May 2023 13:51:15 +0000 (UTC) Received: from osteffen-laptop.redhat.com (unknown [10.39.193.168]) by smtp.corp.redhat.com (Postfix) with ESMTP id 843732166B29; Wed, 3 May 2023 13:51:13 +0000 (UTC) From: Oliver Steffen To: grub-devel@gnu.org Cc: Daniel Kiper , Gerd Hoffmann , Oliver Steffen , Daniel Kiper Subject: [PATCH v7 06/10] kern/misc, kern/efi: Extract UTF-8 to UTF-16 code Date: Wed, 3 May 2023 15:50:15 +0200 Message-Id: <20230503135019.101181-7-osteffen@redhat.com> In-Reply-To: <20230503135019.101181-1-osteffen@redhat.com> References: <20230503135019.101181-1-osteffen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Received-SPF: pass client-ip=170.10.129.124; envelope-from=osteffen@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.161, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 May 2023 13:51:23 -0000 Create a new function for UTF-8 to UTF-16 conversion called grub_utf8_to_utf16_alloc() in the grub-code/kern/misc.c and replace charset conversion code used in some places in the EFI code. It is modeled after the grub_utf8_to_ucs4_alloc() like functions in charset.h. It can't live in charset.h, because it needs to be reachable from the kern/efi code. Add a check for integer overflow and remove redundant NUL-termination. Signed-off-by: Oliver Steffen Reviewed-by: Daniel Kiper --- grub-core/kern/efi/efi.c | 21 ++++++--------------- grub-core/kern/misc.c | 32 ++++++++++++++++++++++++++++++++ include/grub/misc.h | 3 +++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index 8df861a59..8fc3edfae 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -207,15 +207,11 @@ grub_efi_set_variable_with_attributes (const char *var, const grub_guid_t *guid, grub_efi_status_t status; grub_efi_runtime_services_t *r; grub_efi_char16_t *var16; - grub_size_t len, len16; - len = grub_strlen (var); - len16 = len * GRUB_MAX_UTF16_PER_UTF8; - var16 = grub_calloc (len16 + 1, sizeof (var16[0])); - if (!var16) + grub_utf8_to_utf16_alloc (var, &var16, NULL); + + if (var16 == NULL) return grub_errno; - len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL); - var16[len16] = 0; r = grub_efi_system_table->runtime_services; @@ -250,18 +246,13 @@ grub_efi_get_variable_with_attributes (const char *var, grub_efi_runtime_services_t *r; grub_efi_char16_t *var16; void *data; - grub_size_t len, len16; *data_out = NULL; *datasize_out = 0; - len = grub_strlen (var); - len16 = len * GRUB_MAX_UTF16_PER_UTF8; - var16 = grub_calloc (len16 + 1, sizeof (var16[0])); - if (!var16) - return GRUB_EFI_OUT_OF_RESOURCES; - len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL); - var16[len16] = 0; + grub_utf8_to_utf16_alloc (var, &var16, NULL); + if (var16 == NULL) + return grub_errno; r = grub_efi_system_table->runtime_services; diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c index eadb46988..dba815569 100644 --- a/grub-core/kern/misc.c +++ b/grub-core/kern/misc.c @@ -25,6 +25,7 @@ #include #include #include +#include union printf_arg { @@ -1318,6 +1319,37 @@ grub_fatal (const char *fmt, ...) grub_abort (); } +grub_ssize_t +grub_utf8_to_utf16_alloc (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position) +{ + grub_size_t len; + grub_size_t len16; + + len = grub_strlen (str8); + + /* Check for integer overflow */ + if (len > GRUB_SSIZE_MAX / GRUB_MAX_UTF16_PER_UTF8 - 1) + { + grub_error (GRUB_ERR_BAD_ARGUMENT, N_("string too long")); + *utf16_msg = NULL; + return -1; + } + + len16 = len * GRUB_MAX_UTF16_PER_UTF8; + + *utf16_msg = grub_calloc (len16 + 1, sizeof (*utf16_msg[0])); + if (*utf16_msg == NULL) + return -1; + + len16 = grub_utf8_to_utf16 (*utf16_msg, len16, (grub_uint8_t *) str8, len, NULL); + + if (last_position != NULL) + *last_position = *utf16_msg + len16; + + return len16; +} + + #if BOOT_TIME_STATS #include diff --git a/include/grub/misc.h b/include/grub/misc.h index 6c9d56de9..1b35a167f 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -529,4 +529,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file, #define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) - __builtin_clzll (n) - 1) +grub_ssize_t +EXPORT_FUNC(grub_utf8_to_utf16_alloc) (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position); + #endif /* ! GRUB_MISC_HEADER */ -- 2.40.1