Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jcW6g-0002FE-So for mharc-grub-devel@gnu.org; Sat, 23 May 2020 11:30:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37862) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jcW6f-0002F4-5t for grub-devel@gnu.org; Sat, 23 May 2020 11:30:37 -0400 Received: from 64epicks.com ([116.203.107.72]:43850) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jcW6d-0002GA-WF for grub-devel@gnu.org; Sat, 23 May 2020 11:30:36 -0400 Received: from [85.224.11.194] (c-c20be055.019-252-73746f67.bbcust.telenor.se [85.224.11.194]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by 64epicks.com (Postfix) with ESMTPSA id 9D8BD31F417E for ; Sat, 23 May 2020 17:28:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=64epicks.com; s=mail; t=1590247710; bh=iNQkZck0AT1C7EKEsVyJFk7rgb30DVaNMz0PY3KbEVc=; h=To:From:Subject:Date; b=j3Jlkc4dQtmt4/Xo1CphZQsIZiG/67ujUxPdbyW1MHyABprRe0KlJtZYC3/eVeRQ5 X4gB1bRpOQAVs8PQahU3nhW5Zlgf9v65Xy/ktL34eYedNUqucz5wuwzw63W5rEPP9t I/vAS+vsCNZwEt0GwEhJkycwzIA/L2MGlbANyLUTS9qkZa+OqOv1TOZwgOBSzfyNBX jWvkjt9WZN/VY1M45JxenDD2nO1TAHaeasvz/UXlL/nH7pCn3Bc2Ehc6PBLnwVVvts RAD8WKL2gFdPgmUEGJ3f5zMzb6NxcAfIgSS4PYvgr9PIDZrEpJkN5Qia3FQz5H1sna y0Aa/k/pQP0Kg== To: grub-devel@gnu.org From: Jacob Paul Subject: [PATCH] multiboot: Add support for program headers at "strange" locations Message-ID: <627d68cf-ed60-4ec2-a70b-065dee1605e4@64epicks.com> Date: Sat, 23 May 2020 17:30:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=116.203.107.72; envelope-from=jacob.paul@64epicks.com; helo=64epicks.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/23 11:30:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] 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, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 May 2020 15:30:37 -0000 Currently, grub_multiboot_load_elf() will throw an error if the program header is at a too high offset (since it would be outside the allocated buffer space of MULTIBOOT_SEARCH bytes). It is understandable why it does this, since 99.99% of all cases e_phoff has a value of 0x34 or 0x40. So, to be able to support a (uncommon) circumstance like this, the program header can be loaded into a separate buffer if needed, and then freed at the end. The only downside to this is that grub_multiboot_load_elf() must at every return statement check if phdr_base is a separate buffer and if so de-allocate it. Signed-off-by: Jacob Paul --- grub-core/loader/multiboot_elfxx.c | 86 +++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/grub-core/loader/multiboot_elfxx.c b/grub-core/loader/multiboot_elfxx.c index 70cd1db51..a538d871a 100644 --- a/grub-core/loader/multiboot_elfxx.c +++ b/grub-core/loader/multiboot_elfxx.c @@ -75,12 +75,37 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) return grub_error (GRUB_ERR_UNKNOWN_OS, N_("this ELF file is not of the right type")); - /* FIXME: Should we support program headers at strange locations? */ + /* Program header outside of the allocated buffer space. Weird, but just work around it by allocating a new buffer for it */ if (ehdr->e_phoff + (grub_uint32_t) ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH) - return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset"); + { + phdr_base = grub_malloc ((grub_size_t) ehdr->e_phnum * ehdr->e_phentsize); + if (!phdr_base) + return grub_errno; + + if (grub_file_seek (mld->file, (grub_off_t) ehdr->e_phoff) == (grub_off_t) -1) + { + grub_free(phdr_base); + return grub_error (GRUB_ERR_BAD_OS, "invalid program header offset"); + } - phdr_base = (char *) mld->buffer + ehdr->e_phoff; + if (grub_file_read (mld->file, phdr_base, (grub_size_t) ehdr->e_phnum * ehdr->e_phentsize) + != (grub_ssize_t) ehdr->e_phnum * ehdr->e_phentsize) + { + grub_free (phdr_base); + if (!grub_errno) + grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), mld->filename); + return grub_errno; + } + } + else + { + phdr_base = (char *) mld->buffer + ehdr->e_phoff; + } + #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize)) +/* FIXME: find a better way to de-allocate phdr_base if an error occurs. + Putting this in front of every exit is probably not the prettiest solution */ +#define phdr_free() if ((grub_size_t) phdr_base != (grub_size_t) mld->buffer + ehdr->e_phoff) grub_free(phdr_base); mld->link_base_addr = ~0; @@ -94,7 +119,10 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) #ifdef MULTIBOOT_LOAD_ELF64 if (highest_load >= 0x100000000) - return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border"); + { + phdr_free(); + return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border"); + } #endif if (mld->relocatable) @@ -107,7 +135,10 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) mld->avoid_efi_boot_services); if (load_size > mld->max_addr || mld->min_addr > mld->max_addr - load_size) - return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size"); + { + phdr_free(); + return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size"); + } err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch, mld->min_addr, mld->max_addr - load_size, @@ -117,6 +148,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (err) { grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n"); + phdr_free(); return err; } @@ -152,6 +184,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (err) { grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n"); + phdr_free(); return err; } @@ -162,7 +195,10 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) { if (grub_file_seek (mld->file, (grub_off_t) phdr(i)->p_offset) == (grub_off_t) -1) - return grub_errno; + { + phdr_free(); + return grub_errno; + } if (grub_file_read (mld->file, (grub_uint8_t *) source + load_offset, phdr(i)->p_filesz) != (grub_ssize_t) phdr(i)->p_filesz) @@ -170,6 +206,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (!grub_errno) grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), mld->filename); + phdr_free(); return grub_errno; } } @@ -191,19 +228,28 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) /* We still in 32-bit mode. */ if ((ehdr->e_entry - phdr(i)->p_vaddr) + phdr(i)->p_paddr < 0xffffffff80000000ULL) - return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64"); + { + phdr_free(); + return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64"); + } # else /* We still in 32-bit mode. */ if ((ehdr->e_entry - phdr(i)->p_vaddr) + phdr(i)->p_paddr > 0xffffffff) - return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64"); + { + phdr_free(); + return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64"); + } # endif #endif break; } if (i == ehdr->e_phnum) - return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment"); + { + phdr_free(); + return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment"); + } #if defined (__i386__) || defined (__x86_64__) @@ -219,11 +265,15 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) shdr = grub_malloc ((grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize); if (!shdr) - return grub_errno; + { + phdr_free(); + return grub_errno; + } if (grub_file_seek (mld->file, ehdr->e_shoff) == (grub_off_t) -1) { grub_free (shdr); + phdr_free(); return grub_errno; } @@ -233,6 +283,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (!grub_errno) grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), mld->filename); + phdr_free(); return grub_errno; } @@ -244,7 +295,10 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) grub_addr_t target; if (mld->mbi_ver >= 2 && (sh->sh_type == SHT_REL || sh->sh_type == SHT_RELA)) - return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "ELF files with relocs are not supported yet"); + { + phdr_free(); + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "ELF files with relocs are not supported yet"); + } /* This section is a loaded section, so we don't care. */ @@ -263,13 +317,17 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (err) { grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i); + phdr_free(); return err; } src = get_virtual_current_address (ch); target = get_physical_target_address (ch); if (grub_file_seek (mld->file, sh->sh_offset) == (grub_off_t) -1) - return grub_errno; + { + phdr_free(); + return grub_errno; + } if (grub_file_read (mld->file, src, sh->sh_size) != (grub_ssize_t) sh->sh_size) @@ -277,6 +335,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) if (!grub_errno) grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), mld->filename); + phdr_free(); return grub_errno; } sh->sh_addr = target; @@ -285,7 +344,10 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) ehdr->e_shstrndx, shdr); } + phdr_free(); + #undef phdr +#undef phdr_free return grub_errno; } -- 2.26.2