blob: 57e4b3890ab7943b955e11829bee48a3cff4cdda [file] [log] [blame]
Patrick Williamsb58112e2024-03-07 11:16:36 -06001From 19d9e9d838e74e4a0a22f08ae03167380f8aa490 Mon Sep 17 00:00:00 2001
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08002From: Robert Yang <liezhi.yang@windriver.com>
3Date: Thu, 16 Aug 2018 09:58:26 +0800
4Subject: [PATCH] libelf/elf_end.c: check data_list.data.d.d_buf before free it
5
6The one which actually saves the data is data_list.data.d.d_buf, so check it
7before free rawdata_base.
8
9This can fix a segmentation fault when prelink libqb_1.0.3:
10prelink: /usr/lib/libqb.so.0.18.2: Symbol section index outside of section numbers
11
12The segmentation fault happens when prelink call elf_end().
13
14Upstream-Status: Submitted [https://sourceware.org/ml/elfutils-devel/2018-q3/msg00085.html]
15
16Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
17---
18 libelf/elf_end.c | 6 ++++--
19 1 file changed, 4 insertions(+), 2 deletions(-)
20
21diff --git a/libelf/elf_end.c b/libelf/elf_end.c
Patrick Williamsb58112e2024-03-07 11:16:36 -060022index 80f4d13..b103959 100644
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080023--- a/libelf/elf_end.c
24+++ b/libelf/elf_end.c
Patrick Williamsb58112e2024-03-07 11:16:36 -060025@@ -169,14 +169,16 @@ elf_end (Elf *elf)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080026 architecture doesn't require overly stringent
27 alignment the raw data buffer is the same as the
28 one used for presenting to the caller. */
29- if (scn->data_base != scn->rawdata_base)
30+ if ((scn->data_base != scn->rawdata_base)
31+ && (scn->data_list.data.d.d_buf != NULL))
32 free (scn->data_base);
33
34 /* The section data is allocated if we couldn't mmap
35 the file. Or if we had to decompress. */
36- if (elf->map_address == NULL
37+ if ((elf->map_address == NULL
38 || scn->rawdata_base == scn->zdata_base
39 || (scn->flags & ELF_F_MALLOCED) != 0)
40+ && (scn->data_list.data.d.d_buf != NULL))
41 free (scn->rawdata_base);
42
43 /* Free the list of data buffers for the section.