Branch data Line data Source code
1 : : /* Return string pointer from string section.
2 : : Copyright (C) 1998-2002, 2004, 2008, 2009, 2015 Red Hat, Inc.
3 : : Copyright (C) 2025 Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : : Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
6 : :
7 : : This file is free software; you can redistribute it and/or modify
8 : : it under the terms of either
9 : :
10 : : * the GNU Lesser General Public License as published by the Free
11 : : Software Foundation; either version 3 of the License, or (at
12 : : your option) any later version
13 : :
14 : : or
15 : :
16 : : * the GNU General Public License as published by the Free
17 : : Software Foundation; either version 2 of the License, or (at
18 : : your option) any later version
19 : :
20 : : or both in parallel, as here.
21 : :
22 : : elfutils is distributed in the hope that it will be useful, but
23 : : WITHOUT ANY WARRANTY; without even the implied warranty of
24 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 : : General Public License for more details.
26 : :
27 : : You should have received copies of the GNU General Public License and
28 : : the GNU Lesser General Public License along with this program. If
29 : : not, see <http://www.gnu.org/licenses/>. */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : # include <config.h>
33 : : #endif
34 : :
35 : : #include <libelf.h>
36 : : #include <stdbool.h>
37 : : #include <stddef.h>
38 : :
39 : : #include "libelfP.h"
40 : :
41 : :
42 : : static void *
43 : 12 : get_zdata_wrlock (Elf_Scn *strscn)
44 : : {
45 : 12 : size_t zsize, zalign;
46 : 12 : void *zdata = __libelf_decompress_elf_wrlock (strscn, &zsize, &zalign);
47 [ + - ]: 12 : if (zdata == NULL)
48 : : return NULL;
49 : :
50 : 12 : strscn->zdata_base = zdata;
51 : 12 : strscn->zdata_size = zsize;
52 : 12 : strscn->zdata_align = zalign;
53 : :
54 : 12 : return zdata;
55 : : }
56 : :
57 : : char *
58 : 12883480 : elf_strptr (Elf *elf, size_t idx, size_t offset)
59 : : {
60 [ + - ]: 12883480 : if (elf == NULL)
61 : : return NULL;
62 : :
63 [ - + ]: 12883480 : if (elf->kind != ELF_K_ELF)
64 : : {
65 : 0 : __libelf_seterrno (ELF_E_INVALID_HANDLE);
66 : 0 : return NULL;
67 : : }
68 : :
69 : 12883480 : rwlock_rdlock (elf->lock);
70 : :
71 : 12883480 : char *result = NULL;
72 : 12883480 : Elf_Scn *strscn;
73 : :
74 : : /* Find the section in the list. */
75 : 12883480 : Elf_ScnList *runp = (elf->class == ELFCLASS32
76 : : || (offsetof (struct Elf, state.elf32.scns)
77 : : == offsetof (struct Elf, state.elf64.scns))
78 : : ? &elf->state.elf32.scns : &elf->state.elf64.scns);
79 : 13244866 : while (1)
80 : : {
81 [ + + ]: 13244866 : if (idx < runp->max)
82 : : {
83 [ + - ]: 12883480 : if (idx < runp->cnt)
84 : 12883480 : strscn = &runp->data[idx];
85 : : else
86 : : {
87 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
88 : 0 : goto out;
89 : : }
90 : 12883480 : break;
91 : : }
92 : :
93 : 361386 : idx -= runp->max;
94 : :
95 : 361386 : runp = runp->next;
96 [ + - ]: 361386 : if (runp == NULL)
97 : : {
98 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
99 : 0 : goto out;
100 : : }
101 : : }
102 : :
103 : 12883480 : int wrlocked = 0;
104 : 12883480 : size_t sh_size = 0;
105 [ + + ]: 12883480 : if (elf->class == ELFCLASS32)
106 : : {
107 [ + + ]: 4487902 : Elf32_Shdr *shdr = strscn->shdr.e32 ?: __elf32_getshdr_rdlock (strscn);
108 [ + - + + ]: 4487902 : if (unlikely (shdr == NULL || shdr->sh_type != SHT_STRTAB))
109 : : {
110 : : /* This is no string section. */
111 : 92 : __libelf_seterrno (ELF_E_INVALID_SECTION);
112 : 92 : goto out;
113 : : }
114 : :
115 [ + + ]: 4487810 : if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
116 : 4487730 : sh_size = shdr->sh_size;
117 : : else
118 : : {
119 [ + + ]: 80 : if (strscn->zdata_base == NULL)
120 : : {
121 : 6 : rwlock_unlock (elf->lock);
122 : 6 : rwlock_wrlock (elf->lock);
123 : 6 : wrlocked = 1;
124 : :
125 : : /* Skip decompression if it occurred while grabbing
126 : : the wrlock. */
127 : 6 : if (strscn->zdata_base == NULL
128 [ - + ]: 6 : && get_zdata_wrlock (strscn) == NULL)
129 : 0 : goto out;
130 : : }
131 : :
132 : 80 : sh_size = strscn->zdata_size;
133 : : }
134 : :
135 [ + + ]: 4487810 : if (unlikely (offset >= sh_size))
136 : : {
137 : : /* The given offset is too big, it is beyond this section. */
138 : 6 : __libelf_seterrno (ELF_E_OFFSET_RANGE);
139 : 6 : goto out;
140 : : }
141 : : }
142 : : else
143 : : {
144 [ + + ]: 8395578 : Elf64_Shdr *shdr = strscn->shdr.e64 ?: __elf64_getshdr_rdlock (strscn);
145 [ + - - + ]: 8395578 : if (unlikely (shdr == NULL || shdr->sh_type != SHT_STRTAB))
146 : : {
147 : : /* This is no string section. */
148 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION);
149 : 0 : goto out;
150 : : }
151 : :
152 [ + + ]: 8395578 : if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
153 : 8395504 : sh_size = shdr->sh_size;
154 : : else
155 : : {
156 [ + + ]: 74 : if (strscn->zdata_base == NULL)
157 : : {
158 : 6 : rwlock_unlock (elf->lock);
159 : 6 : rwlock_wrlock (elf->lock);
160 : 6 : wrlocked = 1;
161 : :
162 : : /* Skip decompression if it occurred while grabbing
163 : : the wrlock. */
164 : 6 : if (strscn->zdata_base == NULL
165 [ - + ]: 6 : && get_zdata_wrlock (strscn) == NULL)
166 : 0 : goto out;
167 : : }
168 : :
169 : 74 : sh_size = strscn->zdata_size;
170 : : }
171 : :
172 [ + + ]: 8395578 : if (unlikely (offset >= sh_size))
173 : : {
174 : : /* The given offset is too big, it is beyond this section. */
175 : 6 : __libelf_seterrno (ELF_E_OFFSET_RANGE);
176 : 6 : goto out;
177 : : }
178 : : }
179 : :
180 [ + + + + ]: 12883376 : if (strscn->rawdata_base == NULL && ! strscn->data_read)
181 : : {
182 : 5968 : if (wrlocked == 0)
183 : : {
184 : 5968 : rwlock_unlock (elf->lock);
185 : 5968 : rwlock_wrlock (elf->lock);
186 : 5968 : wrlocked = 1;
187 : : }
188 : 5968 : if (strscn->rawdata_base == NULL && ! strscn->data_read
189 : : /* Read the section data. */
190 [ - + ]: 5968 : && __libelf_set_rawdata_wrlock (strscn) != 0)
191 : 0 : goto out;
192 : : }
193 : :
194 [ + + ]: 12883376 : if (unlikely (strscn->zdata_base != NULL))
195 : : {
196 : : /* Make sure the string is NUL terminated. Start from the end,
197 : : which very likely is a NUL char. */
198 [ + - ]: 57524 : if (likely (validate_str (strscn->zdata_base, offset, sh_size)))
199 : 57524 : result = &strscn->zdata_base[offset];
200 : : else
201 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
202 : : }
203 [ + + ]: 12825852 : else if (likely (strscn->data_list_rear == NULL))
204 : : {
205 : : // XXX The above is currently correct since elf_newdata will
206 : : // make sure to convert the rawdata into the datalist if
207 : : // necessary. But it would be more efficient to keep the rawdata
208 : : // unconverted and only then iterate over the rest of the (newly
209 : : // added data) list. Note that when the ELF file is mmapped
210 : : // rawdata_base can be set while rawdata.d hasn't been
211 : : // initialized yet (when data_read is zero). So we cannot just
212 : : // look at the rawdata.d.d_size.
213 : :
214 : : /* First check there actually is any data. This could be a new
215 : : section which hasn't had any data set yet. Then make sure
216 : : the string is at a valid offset and NUL terminated. */
217 [ - + ]: 11558910 : if (unlikely (strscn->rawdata_base == NULL))
218 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION);
219 [ + - ]: 11558910 : else if (likely (validate_str (strscn->rawdata_base, offset, sh_size)))
220 : 11558910 : result = &strscn->rawdata_base[offset];
221 : : else
222 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
223 : : }
224 : : else
225 : : {
226 : : /* This is a file which is currently created. Use the list of
227 : : data blocks. */
228 : 1266942 : struct Elf_Data_List *dl = &strscn->data_list;
229 [ + - ]: 1267374 : while (dl != NULL)
230 : : {
231 [ + - ]: 1267374 : if (offset >= (size_t) dl->data.d.d_off
232 [ + + ]: 1267374 : && offset < dl->data.d.d_off + dl->data.d.d_size)
233 : : {
234 : : /* Make sure the string is NUL terminated. Start from
235 : : the end, which very likely is a NUL char. */
236 [ + - ]: 1266942 : if (likely (validate_str ((char *) dl->data.d.d_buf,
237 : : offset - dl->data.d.d_off,
238 : : dl->data.d.d_size)))
239 : 1266942 : result = ((char *) dl->data.d.d_buf
240 : : + (offset - dl->data.d.d_off));
241 : : else
242 : 0 : __libelf_seterrno (ELF_E_INVALID_INDEX);
243 : : break;
244 : : }
245 : :
246 : 432 : dl = dl->next;
247 : : }
248 : : }
249 : :
250 : 0 : out:
251 : : rwlock_unlock (elf->lock);
252 : :
253 : : return result;
254 : : }
255 : : INTDEF(elf_strptr)
|