Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p2BBU-0001e6-TI for mharc-grub-devel@gnu.org; Mon, 05 Dec 2022 08:07:01 -0500 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 1p2BB5-0001ax-Ub for grub-devel@gnu.org; Mon, 05 Dec 2022 08:06:37 -0500 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p2BB3-0003Fu-JG for grub-devel@gnu.org; Mon, 05 Dec 2022 08:06:35 -0500 Received: by mail-pj1-x1036.google.com with SMTP id q17-20020a17090aa01100b002194cba32e9so14775415pjp.1 for ; Mon, 05 Dec 2022 05:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3N9rU7Kth7kiUEVE9DnyM1sLrMVkZM9QcKCNilnD/PM=; b=AQJtYzr/msgW/1RNpeHmukM9Ttt6anla2VJD/UUJpUN1AaQWsPFgkhySvsmx600TEJ HkKTXO4cIKIOeE4dwphAP42xH2SVxpjJJWuJswY4dIMPK2CBTkdFjAiUUkNv7qOjyPyn hHcm+T+O/cQdfFvVfWVbRUAPcjBgmEReC96y1sddbK8k8wrTTD7ThFV+s4vmOz8HBKrD 9zHlyq4/D7bJO3mdyw/kcx7IEQFpfIx+MGulkz7yuH1MRNT3PcI9K16WaOcjEO1J+/9s M5ZCPDWZAC1RR4kmrRbumK/22kpUvzUk/VS1I/B9A25htGezXcvZfQGBU2PaUp/JGuRB jWKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3N9rU7Kth7kiUEVE9DnyM1sLrMVkZM9QcKCNilnD/PM=; b=1go6rTN2lYZlZUYFrDeF/2JGhSHMzXP6s10eYCDfJvTHrnzbaXLFQ4bx4foTGNK750 WGpCNNRZz8bDvDoUHi5QFirwtdKC0COcWq5oCs/y0clSXzDdXdaqTz3SzBqmjZDbzoNs DezGB8YB/kT16u4+hnGynbJbDrGjhEmUXuPKCuGwHeURt/OfsRAQFqoF2DJRD6OBwYFy yWOvwG/HLv3pUZnwGSyq39ZkeD0Z6kTt5F0D0ZuoTBjlizUZ7eqKusVytwxoLvwupLTf YzL0W06nfEIYw/v2v4aEzOBOxnMDaRIZrJRA/u1/vUmcf2Z5L1VV5zsaQusrnquK24Za yC3w== X-Gm-Message-State: ANoB5pn4S6TSRb/qs2QkDxium0cc2cd9nyYqMXNd1qIboc31TJMFCp0r OujzHTbtuJKLWkLnjdC3FXTVaqR6CgT3eQ== X-Google-Smtp-Source: AA0mqf68MdfdXE5OUSB7z2JSrAo/eRoSGuspiw2tw8yoyXUBRCfdZ9owrPmOs1lrFqzv/grQqjJcAg== X-Received: by 2002:a17:903:2498:b0:189:7e2f:d64e with SMTP id p24-20020a170903249800b001897e2fd64emr42485759plw.162.1670245592067; Mon, 05 Dec 2022 05:06:32 -0800 (PST) Received: from localhost.localdomain (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id ij27-20020a170902ab5b00b00188f7ad561asm10522441plb.249.2022.12.05.05.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 05:06:31 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: steve@einval.com, rharwood@redhat.com, mchang@suse.com, Zhang Boyang Subject: [RFC PATCH 2/4] kern/unwrap: File filter to unwrap files wrapped by grub-wrap Date: Mon, 5 Dec 2022 21:06:03 +0800 Message-Id: <20221205130605.250715-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221205130605.250715-1-zhangboyang.id@gmail.com> References: <20221205130605.250715-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: Mon, 05 Dec 2022 13:06:37 -0000 X-List-Received-Date: Mon, 05 Dec 2022 13:06:37 -0000 This file filter can unwrap a file wrapped by grub-wrap. This process is done automatically and transparently, thus no additional code is required at user's side. First, it checks for file type flags and ignore irrelevant files. Then, it find target PE section and unwraps the file. Subsequent read operations will operate on unwrapped data. If error occurred during unwrapping, e.g. file is not wrapped, it passthroughs original file. Currently, this filter only operate on font files (GRUB_FILE_TYPE_FONT), and they must be wrapped with section name set to ".GRUBpf2". Signed-off-by: Zhang Boyang --- grub-core/Makefile.core.def | 1 + grub-core/kern/main.c | 3 + grub-core/kern/unwrap.c | 188 ++++++++++++++++++++++++++++++++++++ include/grub/efi/pe32.h | 75 ++++++++++++++ include/grub/file.h | 1 + include/grub/unwrap.h | 25 +++++ 6 files changed, 293 insertions(+) create mode 100644 grub-core/kern/unwrap.c create mode 100644 include/grub/unwrap.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 95942fc8c..fb6a25ed1 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -148,6 +148,7 @@ kernel = { common = kern/rescue_reader.c; common = kern/term.c; common = kern/verifiers.c; + common = kern/unwrap.c; noemu = kern/compiler-rt.c; noemu = kern/mm.c; diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c index 731c07c29..fa08c0086 100644 --- a/grub-core/kern/main.c +++ b/grub-core/kern/main.c @@ -30,6 +30,7 @@ #include #include #include +#include #ifdef GRUB_MACHINE_PCBIOS #include @@ -281,6 +282,8 @@ grub_main (void) /* Init verifiers API. */ grub_verifiers_init (); + grub_unwrap_init (); + grub_load_config (); grub_boot_time ("Before loading embedded modules."); diff --git a/grub-core/kern/unwrap.c b/grub-core/kern/unwrap.c new file mode 100644 index 000000000..dc32ccdad --- /dev/null +++ b/grub-core/kern/unwrap.c @@ -0,0 +1,188 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + * + * PE unwrapper. + */ + +#include +#include +#include +#include + +struct grub_unwrapped +{ + grub_file_t file; + void *buf; + void *real_data; + grub_size_t real_size; +}; +typedef struct grub_unwrapped *grub_unwrapped_t; + +static void +unwrapped_free (grub_unwrapped_t unwrapped) +{ + if (unwrapped) + { + grub_free (unwrapped->buf); + grub_free (unwrapped); + } +} + +static grub_ssize_t +unwrapped_read (struct grub_file *file, char *buf, grub_size_t len) +{ + grub_unwrapped_t unwrapped = file->data; + + grub_memcpy (buf, (char *) unwrapped->real_data + file->offset, len); + return len; +} + +static grub_err_t +unwrapped_close (struct grub_file *file) +{ + grub_unwrapped_t unwrapped = file->data; + + grub_file_close (unwrapped->file); + unwrapped_free (unwrapped); + file->data = 0; + + /* Device and name are freed by parent. */ + file->device = 0; + file->name = 0; + + return grub_errno; +} + +struct grub_fs unwrapped_fs = +{ + .name = "unwrapped_read", + .fs_read = unwrapped_read, + .fs_close = unwrapped_close +}; + +static void +try_unwrap (grub_unwrapped_t unwrapped, const char (*name)[8]) +{ + void *buffer = unwrapped->real_data; + grub_size_t size = unwrapped->real_size; + int i; + grub_uint32_t pe_image_header, section_table; + grub_uint16_t num_sections; + struct grub_pe32_section_table *sections; + + pe_image_header = grub_pe32_get_pe_image_header (buffer, size); + if (pe_image_header == 0) + goto fail; + grub_dprintf ("unwrap", "pe image header at 0x%" PRIxGRUB_UINT32_T ".\n", pe_image_header); + + section_table = grub_pe32_get_section_table (buffer, size, pe_image_header, &num_sections); + if (section_table == 0) + goto fail; + grub_dprintf ("unwrap", "section table at 0x%" PRIxGRUB_UINT32_T ".\n", section_table); + + sections = (void *) ((char *) buffer + section_table); + for (i = 0; i < (int) num_sections; i++) + { + struct grub_pe32_section_table *section = §ions[i]; + if (grub_memcmp (section->name, *name, 8) == 0) + { + grub_uint32_t offset, length, end; + grub_memcpy (&offset, §ion->raw_data_offset, sizeof (offset)); + grub_memcpy (&length, §ion->virtual_size, sizeof (length)); + if (grub_add (offset, length, &end) || end > size) + goto fail; + + grub_dprintf ("unwrap", "unwrap ok, offset 0x%" PRIxGRUB_UINT32_T ", length 0x%" PRIxGRUB_UINT32_T ".\n", offset, length); + unwrapped->real_data = (char *) buffer + offset; + unwrapped->real_size = length; + return; + } + } + grub_dprintf ("unwrap", "section %.8s not found.\n", *name); + +fail: + grub_dprintf ("unwrap", "unwrap failed, passthrough original file.\n"); +} + +static grub_file_t +grub_unwrap_open (grub_file_t io, enum grub_file_type type) +{ + grub_unwrapped_t unwrapped = NULL; + grub_file_t ret = NULL; + char name[8] = { 0 }; + + switch (type & GRUB_FILE_TYPE_MASK) + { + /* Only process file types we known. */ + case GRUB_FILE_TYPE_FONT: + grub_strncpy(name, ".GRUBpf2", 8); + break; + + /* Don't touch other files. */ + default: + return io; + } + + ret = grub_malloc (sizeof (*ret)); + if (ret == NULL) + goto fail; + *ret = *io; + + ret->fs = &unwrapped_fs; + ret->not_easily_seekable = 0; + if (ret->size >> (sizeof (grub_size_t) * GRUB_CHAR_BIT - 1)) + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + N_("big file unwrapping isn't implemented yet")); + goto fail; + } + + unwrapped = grub_malloc (sizeof (*unwrapped)); + if (unwrapped == NULL) + goto fail; + + unwrapped->buf = grub_malloc (ret->size); + if (unwrapped->buf == NULL) + goto fail; + if (grub_file_read (io, unwrapped->buf, ret->size) != (grub_ssize_t) ret->size) + { + if (!grub_errno) + grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), + io->name); + goto fail; + } + unwrapped->real_data = unwrapped->buf; + unwrapped->real_size = ret->size; + + try_unwrap (unwrapped, &name); + + unwrapped->file = io; + ret->size = unwrapped->real_size; + ret->data = unwrapped; + return ret; + +fail: + unwrapped_free (unwrapped); + grub_free (ret); + return NULL; +} + +void +grub_unwrap_init (void) +{ + grub_file_filter_register (GRUB_FILE_FILTER_UNWRAP, grub_unwrap_open); +} diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h index 7609b5198..a52de0ed2 100644 --- a/include/grub/efi/pe32.h +++ b/include/grub/efi/pe32.h @@ -20,6 +20,8 @@ #define GRUB_EFI_PE32_HEADER 1 #include +#include +#include #include /* The MSDOS compatibility stub. This was copied from the output of @@ -280,9 +282,11 @@ struct grub_pe_image_header #if GRUB_TARGET_SIZEOF_VOID_P == 8 /* The Optional header. */ struct grub_pe64_optional_header optional_header; +#define GRUB_PE32_OHDR_MAGIC GRUB_PE32_PE64_MAGIC #else /* The Optional header. */ struct grub_pe32_optional_header optional_header; +#define GRUB_PE32_OHDR_MAGIC GRUB_PE32_PE32_MAGIC #endif }; @@ -343,4 +347,75 @@ struct grub_pe32_reloc #define GRUB_PE32_REL_I386_DIR32 0x6 #define GRUB_PE32_REL_I386_REL32 0x14 +/* + * Get offset of PE image header of given in-memory file, as grub_uint32_t. + * Returns 0 if magic numbers are not found or header location is illegal. + */ +static inline grub_uint32_t +grub_pe32_get_pe_image_header (const void *buffer, grub_size_t size) +{ + grub_uint16_t magic; + grub_uint32_t offset, end; + const struct grub_msdos_image_header *msdos_hdr; + const struct grub_pe_image_header *pe_hdr; + + if (size < sizeof (*msdos_hdr)) + return 0; + msdos_hdr = buffer; + + magic = grub_cpu_to_le16 (GRUB_PE32_MAGIC); + if (grub_memcmp (&msdos_hdr->msdos_magic, &magic, sizeof (magic)) != 0) + return 0; + + grub_memcpy (&offset, &msdos_hdr->pe_image_header_offset, sizeof (offset)); + offset = grub_le_to_cpu32 (offset); + if (grub_add (offset, sizeof (*pe_hdr), &end) || end > size) + return 0; + pe_hdr = (const void *) ((const char *) buffer + offset); + + if (grub_memcmp (&pe_hdr->signature, "PE\0\0", GRUB_PE32_SIGNATURE_SIZE) != 0) + return 0; + + magic = grub_cpu_to_le16 (GRUB_PE32_OHDR_MAGIC); + if (grub_memcmp (&pe_hdr->optional_header.magic, &magic, sizeof (magic)) != 0) + return 0; + + return offset; +} + +/* + * Get offset of PE section table of given in-memory file, as grub_uint32_t. + * `pe_image_header` should be the value returned by grub_pe32_get_pe_image_header(). + * Number of sections is stored at `*num_sections`, as grub_uint16_t. + * Returns 0 if section table's location is illegal. + */ +static inline grub_uint32_t +grub_pe32_get_section_table (const void *buffer, grub_size_t size, + grub_uint32_t pe_image_header, + grub_uint16_t *num_sections) +{ + grub_uint32_t offset, length, end; + const struct grub_pe_image_header *pe_hdr; + grub_uint16_t ohdr_size, section_count; + + /* grub_pe32_get_pe_image_header() has already checked boundaries. */ + pe_hdr = (const void *) ((const char *) buffer + pe_image_header); + offset = pe_image_header + (grub_uint32_t) ((const char *) &pe_hdr->optional_header - (const char *) pe_hdr); + + grub_memcpy (&ohdr_size, &pe_hdr->coff_header.optional_header_size, sizeof (ohdr_size)); + ohdr_size = grub_le_to_cpu16 (ohdr_size); + if (grub_add (offset, ohdr_size, &offset)) + return 0; + + grub_memcpy (§ion_count, &pe_hdr->coff_header.num_sections, sizeof (section_count)); + section_count = grub_le_to_cpu16 (section_count); + if (grub_mul (section_count, sizeof (struct grub_pe32_section_table), &length)) + return 0; + if (grub_add (offset, length, &end) || end > size) + return 0; + + *num_sections = section_count; + return offset; +} + #endif /* ! GRUB_EFI_PE32_HEADER */ diff --git a/include/grub/file.h b/include/grub/file.h index a5bf3a792..5fae2a8fc 100644 --- a/include/grub/file.h +++ b/include/grub/file.h @@ -183,6 +183,7 @@ extern grub_disk_read_hook_t EXPORT_VAR(grub_file_progress_hook); typedef enum grub_file_filter_id { GRUB_FILE_FILTER_VERIFY, + GRUB_FILE_FILTER_UNWRAP, GRUB_FILE_FILTER_GZIO, GRUB_FILE_FILTER_XZIO, GRUB_FILE_FILTER_LZOPIO, diff --git a/include/grub/unwrap.h b/include/grub/unwrap.h new file mode 100644 index 000000000..c5155fc6d --- /dev/null +++ b/include/grub/unwrap.h @@ -0,0 +1,25 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_UNWRAP_HEADER +#define GRUB_UNWRAP_HEADER 1 + +extern void +grub_unwrap_init (void); + +#endif /* ! GRUB_UNWRAP_HEADER */ -- 2.30.2