Skip to content

Commit d4deb82

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
lib/buildid: take into account e_phoff when fetching program headers
Current code assumption is that program (segment) headers are following ELF header immediately. This is a common case, but is not guaranteed. So take into account e_phoff field of the ELF header when accessing program headers. Reviewed-by: Eduard Zingerman <[email protected]> Reported-by: Alexey Dobriyan <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent de3ec36 commit d4deb82

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

lib/buildid.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,26 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si
213213
{
214214
const Elf32_Ehdr *ehdr;
215215
const Elf32_Phdr *phdr;
216-
__u32 phnum, i;
216+
__u32 phnum, phoff, i;
217217

218218
ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr));
219219
if (!ehdr)
220220
return r->err;
221221

222-
/*
223-
* FIXME
224-
* Neither ELF spec nor ELF loader require that program headers
225-
* start immediately after ELF header.
226-
*/
227-
if (ehdr->e_phoff != sizeof(Elf32_Ehdr))
228-
return -EINVAL;
229-
230222
/* subsequent freader_fetch() calls invalidate pointers, so remember locally */
231223
phnum = READ_ONCE(ehdr->e_phnum);
224+
phoff = READ_ONCE(ehdr->e_phoff);
225+
232226
/* only supports phdr that fits in one page */
233227
if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
234228
return -EINVAL;
235229

230+
/* check that phoff is not large enough to cause an overflow */
231+
if (phoff + phnum * sizeof(Elf32_Phdr) < phoff)
232+
return -EINVAL;
233+
236234
for (i = 0; i < phnum; ++i) {
237-
phdr = freader_fetch(r, i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr));
235+
phdr = freader_fetch(r, phoff + i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr));
238236
if (!phdr)
239237
return r->err;
240238

@@ -252,27 +250,26 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si
252250
const Elf64_Ehdr *ehdr;
253251
const Elf64_Phdr *phdr;
254252
__u32 phnum, i;
253+
__u64 phoff;
255254

256255
ehdr = freader_fetch(r, 0, sizeof(Elf64_Ehdr));
257256
if (!ehdr)
258257
return r->err;
259258

260-
/*
261-
* FIXME
262-
* Neither ELF spec nor ELF loader require that program headers
263-
* start immediately after ELF header.
264-
*/
265-
if (ehdr->e_phoff != sizeof(Elf64_Ehdr))
266-
return -EINVAL;
267-
268259
/* subsequent freader_fetch() calls invalidate pointers, so remember locally */
269260
phnum = READ_ONCE(ehdr->e_phnum);
261+
phoff = READ_ONCE(ehdr->e_phoff);
262+
270263
/* only supports phdr that fits in one page */
271264
if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
272265
return -EINVAL;
273266

267+
/* check that phoff is not large enough to cause an overflow */
268+
if (phoff + phnum * sizeof(Elf64_Phdr) < phoff)
269+
return -EINVAL;
270+
274271
for (i = 0; i < phnum; ++i) {
275-
phdr = freader_fetch(r, i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr));
272+
phdr = freader_fetch(r, phoff + i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr));
276273
if (!phdr)
277274
return r->err;
278275

0 commit comments

Comments
 (0)