From 5b86194734d4021158312898e9e6e403cbbb7480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 4 Jan 2023 14:53:25 +0100 Subject: [PATCH 1/3] Remove reading of program headers. It is not necessary. --- src/elfpatch.c | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/elfpatch.c b/src/elfpatch.c index e1f7ef1..390284f 100644 --- a/src/elfpatch.c +++ b/src/elfpatch.c @@ -232,9 +232,6 @@ static int elf_patch_update_info(elfpatch_handle_t *ep) { Elf_Kind ek; const char *type_string = "unrecognized"; - size_t header_count = 0ull; - GElf_Phdr phdr; - size_t i; ret_val_if_ep_err(ep, -1001); @@ -276,20 +273,6 @@ static int elf_patch_update_info(elfpatch_handle_t *ep) return -1; } - /* Get program headers */ - if ( elf_getphdrnum(ep->elf, &header_count) != 0) { - print_err("Error reading count of program headers: %s\n", elf_errmsg(-1)); - return -1; - } - - for (i = 0ull; i < header_count; i++) { - if (gelf_getphdr(ep->elf, (int)i, &phdr) != &phdr) { - print_err("Error reading program header (%zu): %s\n", i, elf_errmsg(-1)); - return -1; - } - print_debug("Read program header %zu\n", i); - } - return 0; } -- 2.47.0 From e09a07b3d32d171849d8c3a16b8d61ff3d63a01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 4 Jan 2023 15:04:06 +0100 Subject: [PATCH 2/3] Fix segmentation fault if a NOBITS section is specified as a source section --- src/elfpatch.c | 10 +++++++++- src/main.c | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/elfpatch.c b/src/elfpatch.c index 390284f..7e9c3a0 100644 --- a/src/elfpatch.c +++ b/src/elfpatch.c @@ -423,8 +423,16 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio } print_debug("Section data length: %lu\n", data->d_size); - if (!data->d_size) + if (!data->d_size) { print_err("Section %s contains no data.\n", section); + return -2; + } + + /* NOBIT sections have a length but no data in the file. Abort in this case */ + if (!data->d_buf) { + print_err("Section %s does not contain loadable data.\n", section); + return -2; + } /* If big endian or granularity is byte, simply compute CRC. No reordering is necessary */ if (!little_endian || granularity == GRANULARITY_BYTE) { diff --git a/src/main.c b/src/main.c index 97c2047..bc14947 100644 --- a/src/main.c +++ b/src/main.c @@ -407,12 +407,14 @@ int main(int argc, char **argv) /* Check if all sections are present */ if (check_all_sections_present(ep, cmd_opts.section_list)) { ret = -2; - goto free_cmds; + goto ret_close_elf; } /* Compute CRCs over sections */ crcs = (uint32_t *)malloc(sl_list_length(cmd_opts.section_list) * sizeof(uint32_t)); - compute_crcs(ep, cmd_opts.section_list, &cmd_opts, crcs); + if (compute_crcs(ep, cmd_opts.section_list, &cmd_opts, crcs)) { + goto ret_close_elf; + } if (reporting_get_verbosity()) { print_crcs(cmd_opts.section_list, crcs); @@ -427,6 +429,7 @@ int main(int argc, char **argv) } } +ret_close_elf: elf_patch_close_and_free(ep); free_cmds: -- 2.47.0 From 2fca25d4c0e4d7e8f7d26621757f991d1163f342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 4 Jan 2023 15:07:01 +0100 Subject: [PATCH 3/3] Fix segmentation fault if a NOBITS section is specified for output --- src/elfpatch.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/elfpatch.c b/src/elfpatch.c index 7e9c3a0..978f8e6 100644 --- a/src/elfpatch.c +++ b/src/elfpatch.c @@ -534,6 +534,11 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *section, /* Get data object of section */ output_sec_data = elf_getdata(output_section->scn, NULL); sec_bytes = (uint8_t *)output_sec_data->d_buf; + if (!sec_bytes) { + print_err("Output section '%s' does not contain loadable data. It has to be allocated in the ELF file\n", + section); + goto ret_err; + } /* Check the start and end magics */ if (check_start_magic) { -- 2.47.0