| 1 | /* Support for GNU properties. x86 version. |
| 2 | Copyright (C) 2018-2020 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _DL_PROP_H |
| 20 | #define _DL_PROP_H |
| 21 | |
| 22 | extern void _dl_cet_check (struct link_map *, const char *) |
| 23 | attribute_hidden; |
| 24 | extern void _dl_cet_open_check (struct link_map *) |
| 25 | attribute_hidden; |
| 26 | |
| 27 | static inline void __attribute__ ((always_inline)) |
| 28 | _rtld_main_check (struct link_map *m, const char *program) |
| 29 | { |
| 30 | #if CET_ENABLED |
| 31 | _dl_cet_check (m, program); |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | static inline void __attribute__ ((always_inline)) |
| 36 | _dl_open_check (struct link_map *m) |
| 37 | { |
| 38 | #if CET_ENABLED |
| 39 | _dl_cet_open_check (m); |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | static inline void __attribute__ ((unused)) |
| 44 | _dl_process_cet_property_note (struct link_map *l, |
| 45 | const ElfW(Nhdr) *note, |
| 46 | const ElfW(Addr) size, |
| 47 | const ElfW(Addr) align) |
| 48 | { |
| 49 | #if CET_ENABLED |
| 50 | /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ |
| 51 | if (l->l_cet != lc_unknown) |
| 52 | return; |
| 53 | |
| 54 | /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in |
| 55 | 32-bit objects and to 8 bytes in 64-bit objects. Skip notes |
| 56 | with incorrect alignment. */ |
| 57 | if (align != (__ELF_NATIVE_CLASS / 8)) |
| 58 | return; |
| 59 | |
| 60 | const ElfW(Addr) start = (ElfW(Addr)) note; |
| 61 | |
| 62 | unsigned int feature_1 = 0; |
| 63 | unsigned int last_type = 0; |
| 64 | |
| 65 | while ((ElfW(Addr)) (note + 1) - start < size) |
| 66 | { |
| 67 | /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ |
| 68 | if (note->n_namesz == 4 |
| 69 | && note->n_type == NT_GNU_PROPERTY_TYPE_0 |
| 70 | && memcmp (note + 1, "GNU" , 4) == 0) |
| 71 | { |
| 72 | /* Stop if we see more than one GNU property note which may |
| 73 | be generated by the older linker. */ |
| 74 | if (l->l_cet != lc_unknown) |
| 75 | return; |
| 76 | |
| 77 | /* Check CET status now. */ |
| 78 | l->l_cet = lc_none; |
| 79 | |
| 80 | /* Check for invalid property. */ |
| 81 | if (note->n_descsz < 8 |
| 82 | || (note->n_descsz % sizeof (ElfW(Addr))) != 0) |
| 83 | return; |
| 84 | |
| 85 | /* Start and end of property array. */ |
| 86 | unsigned char *ptr = (unsigned char *) (note + 1) + 4; |
| 87 | unsigned char *ptr_end = ptr + note->n_descsz; |
| 88 | |
| 89 | do |
| 90 | { |
| 91 | unsigned int type = *(unsigned int *) ptr; |
| 92 | unsigned int datasz = *(unsigned int *) (ptr + 4); |
| 93 | |
| 94 | /* Property type must be in ascending order. */ |
| 95 | if (type < last_type) |
| 96 | return; |
| 97 | |
| 98 | ptr += 8; |
| 99 | if ((ptr + datasz) > ptr_end) |
| 100 | return; |
| 101 | |
| 102 | last_type = type; |
| 103 | |
| 104 | if (type == GNU_PROPERTY_X86_FEATURE_1_AND) |
| 105 | { |
| 106 | /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 |
| 107 | bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, |
| 108 | we stop the search regardless if its size is correct |
| 109 | or not. There is no point to continue if this note |
| 110 | is ill-formed. */ |
| 111 | if (datasz != 4) |
| 112 | return; |
| 113 | |
| 114 | feature_1 = *(unsigned int *) ptr; |
| 115 | |
| 116 | /* Keep searching for the next GNU property note |
| 117 | generated by the older linker. */ |
| 118 | break; |
| 119 | } |
| 120 | else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) |
| 121 | { |
| 122 | /* Stop since property type is in ascending order. */ |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Check the next property item. */ |
| 127 | ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); |
| 128 | } |
| 129 | while ((ptr_end - ptr) >= 8); |
| 130 | } |
| 131 | |
| 132 | /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are |
| 133 | aligned to 4 bytes in 64-bit ELF objects. */ |
| 134 | note = ((const void *) note |
| 135 | + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz, |
| 136 | align)); |
| 137 | } |
| 138 | |
| 139 | /* We get here only if there is one or no GNU property note. */ |
| 140 | if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) |
| 141 | l->l_cet |= lc_ibt; |
| 142 | if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) |
| 143 | l->l_cet |= lc_shstk; |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | static inline void __attribute__ ((unused)) |
| 148 | _dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph) |
| 149 | { |
| 150 | const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); |
| 151 | _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); |
| 152 | } |
| 153 | |
| 154 | static inline int __attribute__ ((always_inline)) |
| 155 | _dl_process_gnu_property (struct link_map *l, uint32_t type, uint32_t datasz, |
| 156 | void *data) |
| 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | #endif /* _DL_PROP_H */ |
| 162 | |