From patchwork Thu Mar 26 20:20:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 1262282 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48pGbf3L21z9sSL for ; Fri, 27 Mar 2020 07:21:34 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 48pGbf1d0lzDr24 for ; Fri, 27 Mar 2020 07:21:34 +1100 (AEDT) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=linux.vnet.ibm.com (client-ip=148.163.158.5; helo=mx0a-001b2d01.pphosted.com; envelope-from=stefanb@linux.vnet.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.vnet.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 48pGb0322JzDqb1 for ; Fri, 27 Mar 2020 07:21:00 +1100 (AEDT) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 02QK5RUI118616; Thu, 26 Mar 2020 16:20:57 -0400 Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com with ESMTP id 2ywewx5fh8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 26 Mar 2020 16:20:57 -0400 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.16.0.27/8.16.0.27) with SMTP id 02QKH7qv030298; Thu, 26 Mar 2020 20:20:56 GMT Received: from b01cxnp23032.gho.pok.ibm.com (b01cxnp23032.gho.pok.ibm.com [9.57.198.27]) by ppma04dal.us.ibm.com with ESMTP id 2ywawgfjd7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 26 Mar 2020 20:20:56 +0000 Received: from b01ledav004.gho.pok.ibm.com (b01ledav004.gho.pok.ibm.com [9.57.199.109]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 02QKKtCU53019048 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 26 Mar 2020 20:20:55 GMT Received: from b01ledav004.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8987A112065; Thu, 26 Mar 2020 20:20:55 +0000 (GMT) Received: from b01ledav004.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D6D611206B; Thu, 26 Mar 2020 20:20:55 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b01ledav004.gho.pok.ibm.com (Postfix) with ESMTP; Thu, 26 Mar 2020 20:20:55 +0000 (GMT) From: Stefan Berger To: slof@lists.ozlabs.org, aik@ozlabs.ru Date: Thu, 26 Mar 2020 16:20:52 -0400 Message-Id: <20200326202054.826301-2-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200326202054.826301-1-stefanb@linux.vnet.ibm.com> References: <20200326202054.826301-1-stefanb@linux.vnet.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.645 definitions=2020-03-26_11:2020-03-26, 2020-03-26 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 suspectscore=0 spamscore=0 clxscore=1015 malwarescore=0 impostorscore=0 bulkscore=0 phishscore=0 mlxscore=0 adultscore=0 mlxlogscore=999 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2003260144 Subject: [SLOF] [PATCH 1/3] elf: Implement elf_get_file_size to determine size of an ELF image X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Berger Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" From: Stefan Berger Implement elf_get_file_size to determine the size of an ELF image that has been loaded into a buffer much larger than the actual size of the original file. We determine the size by searching for the farthest offset declared by the ELF headers. Signed-off-by: Stefan Berger --- include/byteorder.h | 14 +++++++++ include/helpers.h | 2 ++ include/libelf.h | 4 +++ lib/libelf/elf.c | 28 +++++++++++++++++ lib/libelf/elf32.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ lib/libelf/elf64.c | 63 +++++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+) diff --git a/include/byteorder.h b/include/byteorder.h index d4a2c8c..d5ebd61 100644 --- a/include/byteorder.h +++ b/include/byteorder.h @@ -53,6 +53,20 @@ static inline void bswap_64p (uint64_t *x) *x = __builtin_bswap64(*x); } +static inline uint16_t cond_bswap_16 (uint16_t x, int swap) +{ + return swap ? bswap_16(x) : x; +} + +static inline uint32_t cond_bswap_32 (uint32_t x, int swap) +{ + return swap ? bswap_32(x) : x; +} + +static inline uint64_t cond_bswap_64 (uint64_t x, int swap) +{ + return swap ? bswap_64(x) : x; +} /* gcc defines __BIG_ENDIAN__ on big endian targets */ #ifdef __BIG_ENDIAN__ diff --git a/include/helpers.h b/include/helpers.h index 47b2674..43122c9 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -51,5 +51,7 @@ extern unsigned long SLOF_get_vtpm_unit(void); const typeof(((type *)0)->member)* struct_ptr = (ptr); \ (type *)((char *)struct_ptr - offset_of(type, member)); }) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +#define ROUNDUP(x,v) ((((x) + (v - 1)) / v) * v) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) #endif diff --git a/include/libelf.h b/include/libelf.h index 5fbf279..76bfc27 100644 --- a/include/libelf.h +++ b/include/libelf.h @@ -96,4 +96,8 @@ void elf_relocate64(void *file_addr, signed long offset); int elf_forth_claim(void *addr, long size); +int elf_get_file_size(const void *buffer, const long buffer_size); +int elf_get_file_size32(const void *buffer, const long buffer_size); +int elf_get_file_size64(const void *buffer, const long buffer_size); + #endif /* __LIBELF_H */ diff --git a/lib/libelf/elf.c b/lib/libelf/elf.c index 5204bc3..91dd3f7 100644 --- a/lib/libelf/elf.c +++ b/lib/libelf/elf.c @@ -196,3 +196,31 @@ elf_get_base_addr(void *file_addr) return -1; } + +/** + * Get the file size of the ELF image that has been loaded into a + * buffer larger than the size of the file + * @return The size of the ELF image or < 0 for error + */ +int elf_get_file_size(const void *buffer, const long buffer_size) +{ + const struct ehdr *ehdr = (const struct ehdr *)buffer; + + if (buffer_size < sizeof(struct ehdr)) + return -1; + + /* check if it is an ELF image at all */ + if (cpu_to_be32(ehdr->ei_ident) != 0x7f454c46) + return -1; + + switch (ehdr->ei_class) { + case 1: + /* Determine file size of a 32-bit file */ + return elf_get_file_size32(buffer, buffer_size); + case 2: + /* Determine file size of a 64-bit file */ + return elf_get_file_size64(buffer, buffer_size); + } + + return -1; +} diff --git a/lib/libelf/elf32.c b/lib/libelf/elf32.c index fea5cf4..5cef072 100644 --- a/lib/libelf/elf32.c +++ b/lib/libelf/elf32.c @@ -17,6 +17,7 @@ #include #include #include +#include struct ehdr32 { uint32_t ei_ident; @@ -50,6 +51,18 @@ struct phdr32 { uint32_t p_align; }; +struct shdr32 { + uint32_t sh_name; + uint32_t sh_type; + uint32_t sh_flags; + uint32_t sh_addr; + uint32_t sh_offset; + uint32_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint32_t sh_addralign; + uint32_t sh_entsize; +}; static struct phdr32* get_phdr32(void *file_addr) @@ -191,3 +204,65 @@ elf_byteswap_header32(void *file_addr) phdr = (struct phdr32 *)(((uint8_t *)phdr) + ehdr->e_phentsize); } } + +/* + * Determine the size of an ELF image that has been loaded into + * a buffer larger than its size. We search all program headers + * and sections for the one that shows the farthest extent of the + * file. + * @return Return -1 on error, size of file otherwise. + */ +int elf_get_file_size32(const void *buffer, const long buffer_size) +{ + const struct ehdr32 *ehdr = (const struct ehdr32 *) buffer; + const void *buffer_end = buffer + buffer_size; + const struct phdr32 *phdr; + const struct shdr32 *shdr; + long elf_size = -1; + uint16_t entsize; + int do_swap; + unsigned i; + + if (buffer_size < sizeof(struct ehdr) || ehdr->e_ehsize != 52) + return -1; + +#ifdef __BIG_ENDIAN__ + do_swap = ehdr->ei_data != ELFDATA2MSB; +#else + do_swap = ehdr->ei_data != ELFDATA2LSB; +#endif + + phdr = buffer + cond_bswap_32(ehdr->e_phoff, do_swap); + entsize = cond_bswap_16(ehdr->e_phentsize, do_swap); + for (i = 0; i < cond_bswap_16(ehdr->e_phnum, do_swap); i++) { + if ((void *)phdr + entsize > buffer_end) + return -1; + + elf_size = MAX(cond_bswap_32(phdr->p_offset, do_swap) + + cond_bswap_32(phdr->p_filesz, do_swap), + elf_size); + + /* step to next header */ + phdr = (struct phdr32 *)(((uint8_t *)phdr) + entsize); + } + + shdr = buffer + cond_bswap_32(ehdr->e_shoff, do_swap); + entsize = cond_bswap_16(ehdr->e_shentsize, do_swap); + for (i = 0; i < cond_bswap_16(ehdr->e_shnum, do_swap); i++) { + if ((void *)shdr + entsize > buffer_end) + return -1; + + elf_size = MAX(cond_bswap_32(shdr->sh_offset, do_swap) + + cond_bswap_32(shdr->sh_size, do_swap), + elf_size); + + /* step to next header */ + shdr = (struct shdr32 *)(((uint8_t *)shdr) + entsize); + } + + elf_size = ROUNDUP(elf_size, 4); + if (elf_size > buffer_size) + return -1; + + return elf_size; +} diff --git a/lib/libelf/elf64.c b/lib/libelf/elf64.c index 775cdee..c35050d 100644 --- a/lib/libelf/elf64.c +++ b/lib/libelf/elf64.c @@ -20,6 +20,7 @@ #include #include #include +#include struct ehdr64 { @@ -472,3 +473,65 @@ uint32_t elf_get_eflags_64(void *file_addr) return ehdr->e_flags; } + +/* + * Determine the size of an ELF image that has been loaded into + * a buffer larger than its size. We search all program headers + * and sections for the one that shows the farthest extent of the + * file. + * @return Return -1 on error, size of file otherwise. + */ +int elf_get_file_size64(const void *buffer, const long buffer_size) +{ + const struct ehdr64 *ehdr = (const struct ehdr64 *) buffer; + const void *buffer_end = buffer + buffer_size; + const struct phdr64 *phdr; + const struct shdr64 *shdr; + long elf_size = -1; + uint16_t entsize; + int do_swap; + unsigned i; + + if (buffer_size < sizeof(struct ehdr) || ehdr->e_ehsize != 64) + return -1; + +#ifdef __BIG_ENDIAN__ + do_swap = ehdr->ei_data != ELFDATA2MSB; +#else + do_swap = ehdr->ei_data != ELFDATA2LSB; +#endif + + phdr = buffer + cond_bswap_64(ehdr->e_phoff, do_swap); + entsize = cond_bswap_16(ehdr->e_phentsize, do_swap); + for (i = 0; i < cond_bswap_16(ehdr->e_phnum, do_swap); i++) { + if ((void *)phdr + entsize > buffer_end) + return -1; + + elf_size = MAX(cond_bswap_64(phdr->p_offset, do_swap) + + cond_bswap_64(phdr->p_filesz, do_swap), + elf_size); + + /* step to next header */ + phdr = (struct phdr64 *)(((uint8_t *)phdr) + entsize); + } + + shdr = buffer + cond_bswap_64(ehdr->e_shoff, do_swap); + entsize = cond_bswap_16(ehdr->e_shentsize, do_swap); + for (i = 0; i < cond_bswap_16(ehdr->e_shnum, do_swap); i++) { + if ((void *)shdr + entsize > buffer_end) + return -1; + + elf_size = MAX(cond_bswap_64(shdr->sh_offset, do_swap) + + cond_bswap_64(shdr->sh_size, do_swap), + elf_size); + + /* step to next header */ + shdr = (struct shdr64 *)(((uint8_t *)shdr) + entsize); + } + + elf_size = ROUNDUP(elf_size, 4); + if (elf_size > buffer_size) + return -1; + + return elf_size; +}