diff options
| author | Mark Wielaard <[email protected]> | 2018-10-15 23:35:47 +0200 |
|---|---|---|
| committer | Mark Wielaard <[email protected]> | 2018-10-29 00:57:57 +0100 |
| commit | 5199e15870e05e5b0b9f98c20fc9b5427aa6dd6a (patch) | |
| tree | 300abbc40ba85162eabf061393f9f5c0cff9b9b2 /libelf/gelf_getnote.c | |
| parent | b75ff1bbd060404565fa28d72441a9b02f331bae (diff) | |
Recognize and parse GNU Property notes.
GNU Property notes are different from normal notes because they use
variable alignment/padding of their fields. They are 8 byte aligned,
but use 4 byte fields. The name is aligned at 4 bytes and padded so
that, the desc is aligned at 8 bytes. The whole note is padded to
8 bytes again. For normal notes all fields are both 4 bytes wide and
4 bytes aligned.
To recognize these new kind of ELF Notes a new Elf_Type is introduced,
ELF_T_NHDR8. This type is used in the xlate functions to determine
how to align and pad the various fields. Since the fields themselves
can now have different alignments we will have to keep track of the
current alignement and use either NOTE_ALIGN4 or NOTE_ALIGN8 to
determine the padding.
To set the correct Elf_Type on the Elf_Data we use either the section
sh_addralign or the segment p_align values. Assuming 8 means the
section or segment contains the new style notes, otherwise normal
notes.
When we cannot determine the "alignment" directly, like when parsing
special kernel sys files, we check the name "GNU" and type
"GNU_PROPERTY_TYPE_0" fields.
ebl_object_note now parses the new NT_GNU_PROPERTY_TYPE_0 and can
extract the GNU_PROPERTY_STACK_SIZE, GNU_PROPERTY_NO_COPY_ON_PROTECTED
and GNU_PROPERTY_X86_FEATURE_1_AND types GNU_PROPERTY_X86_FEATURE_1_IBT
and GNU_PROPERTY_X86_FEATURE_1_SHSTK.
Tests are added for extracting the note from sections or segments
as set by gcc -fcf-protection.
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'libelf/gelf_getnote.c')
| -rw-r--r-- | libelf/gelf_getnote.c | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/libelf/gelf_getnote.c b/libelf/gelf_getnote.c index c75eddab..6d33b355 100644 --- a/libelf/gelf_getnote.c +++ b/libelf/gelf_getnote.c @@ -1,5 +1,5 @@ /* Get note information at the supplied offset. - Copyright (C) 2007, 2014, 2015 Red Hat, Inc. + Copyright (C) 2007, 2014, 2015, 2018 Red Hat, Inc. This file is part of elfutils. This file is free software; you can redistribute it and/or modify @@ -43,7 +43,7 @@ gelf_getnote (Elf_Data *data, size_t offset, GElf_Nhdr *result, if (data == NULL) return 0; - if (unlikely (data->d_type != ELF_T_NHDR)) + if (unlikely (data->d_type != ELF_T_NHDR && data->d_type != ELF_T_NHDR8)) { __libelf_seterrno (ELF_E_INVALID_HANDLE); return 0; @@ -69,27 +69,42 @@ gelf_getnote (Elf_Data *data, size_t offset, GElf_Nhdr *result, const GElf_Nhdr *n = data->d_buf + offset; offset += sizeof *n; - /* Include padding. Check below for overflow. */ - GElf_Word namesz = NOTE_ALIGN (n->n_namesz); - GElf_Word descsz = NOTE_ALIGN (n->n_descsz); - - if (unlikely (offset > data->d_size - || data->d_size - offset < namesz - || (namesz == 0 && n->n_namesz != 0))) + if (offset > data->d_size) offset = 0; else { + /* This is slightly tricky, offset is guaranteed to be 4 + byte aligned, which is what we need for the name_offset. + And normally desc_offset is also 4 byte aligned, but not + for GNU Property notes, then it should be 8. So align + the offset, after adding the namesz, and include padding + in descsz to get to the end. */ *name_offset = offset; - offset += namesz; - if (unlikely (offset > data->d_size - || data->d_size - offset < descsz - || (descsz == 0 && n->n_descsz != 0))) + offset += n->n_namesz; + if (offset > data->d_size) offset = 0; else { - *desc_offset = offset; - offset += descsz; - *result = *n; + /* Include padding. Check below for overflow. */ + GElf_Word descsz = (data->d_type == ELF_T_NHDR8 + ? NOTE_ALIGN8 (n->n_descsz) + : NOTE_ALIGN4 (n->n_descsz)); + + if (data->d_type == ELF_T_NHDR8) + offset = NOTE_ALIGN8 (offset); + else + offset = NOTE_ALIGN4 (offset); + + if (unlikely (offset > data->d_size + || data->d_size - offset < descsz + || (descsz == 0 && n->n_descsz != 0))) + offset = 0; + else + { + *desc_offset = offset; + offset += descsz; + *result = *n; + } } } } |
