| 1 | /* Support for GNU properties. x86 version. |
| 2 | Copyright (C) 2018-2024 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 | #include <libintl.h> |
| 23 | |
| 24 | extern void _dl_cet_check (struct link_map *, const char *) |
| 25 | attribute_hidden; |
| 26 | extern void _dl_cet_open_check (struct link_map *) |
| 27 | attribute_hidden; |
| 28 | |
| 29 | static void |
| 30 | dl_isa_level_check (struct link_map *m, const char *program) |
| 31 | { |
| 32 | const struct cpu_features *cpu_features = __get_cpu_features (); |
| 33 | unsigned int i; |
| 34 | struct link_map *l; |
| 35 | |
| 36 | i = m->l_searchlist.r_nlist; |
| 37 | while (i-- > 0) |
| 38 | { |
| 39 | /* Check each shared object to see if ISA level is compatible. */ |
| 40 | l = m->l_initfini[i]; |
| 41 | |
| 42 | /* Skip ISA level check if functions have been executed. */ |
| 43 | if (l->l_init_called) |
| 44 | continue; |
| 45 | |
| 46 | #ifdef SHARED |
| 47 | /* Skip ISA level check for ld.so since ld.so won't run if its ISA |
| 48 | level is higher than CPU. */ |
| 49 | if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) |
| 50 | continue; |
| 51 | #endif |
| 52 | |
| 53 | if ((l->l_x86_isa_1_needed & cpu_features->isa_1) |
| 54 | != l->l_x86_isa_1_needed) |
| 55 | { |
| 56 | if (program) |
| 57 | _dl_fatal_printf (fmt: "%s: CPU ISA level is lower than required\n" , |
| 58 | *l->l_name != '\0' ? l->l_name : program); |
| 59 | else |
| 60 | _dl_signal_error (errcode: 0, object: l->l_name, occasion: "dlopen" , |
| 61 | N_("CPU ISA level is lower than required" )); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static inline void __attribute__ ((always_inline)) |
| 67 | _rtld_main_check (struct link_map *m, const char *program) |
| 68 | { |
| 69 | dl_isa_level_check (m, program); |
| 70 | #if CET_ENABLED |
| 71 | _dl_cet_check (m, program); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | static inline void __attribute__ ((always_inline)) |
| 76 | _dl_open_check (struct link_map *m) |
| 77 | { |
| 78 | dl_isa_level_check (m, NULL); |
| 79 | #if CET_ENABLED |
| 80 | _dl_cet_open_check (m); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | /* Check the GNU property and return its value. It returns: |
| 85 | -1: Skip this note. |
| 86 | 0: Stop checking. |
| 87 | 1: Continue to check. |
| 88 | */ |
| 89 | static inline int |
| 90 | _dl_check_gnu_property (unsigned int type, unsigned int datasz, |
| 91 | void *ptr, unsigned int *feature_1_and, |
| 92 | unsigned int *needed_1, |
| 93 | unsigned int *isa_1_needed) |
| 94 | { |
| 95 | if (type == GNU_PROPERTY_X86_FEATURE_1_AND |
| 96 | || type == GNU_PROPERTY_X86_ISA_1_NEEDED |
| 97 | || type == GNU_PROPERTY_1_NEEDED) |
| 98 | { |
| 99 | /* The sizes of types which we are searching for are |
| 100 | 4 bytes. There is no point to continue if this |
| 101 | note is ill-formed. */ |
| 102 | if (datasz != 4) |
| 103 | return -1; |
| 104 | |
| 105 | /* NB: Stop the scan only after seeing all types which |
| 106 | we are searching for. */ |
| 107 | _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED |
| 108 | > GNU_PROPERTY_X86_FEATURE_1_AND) |
| 109 | && (GNU_PROPERTY_X86_FEATURE_1_AND |
| 110 | > GNU_PROPERTY_1_NEEDED)), |
| 111 | "GNU_PROPERTY_X86_ISA_1_NEEDED > " |
| 112 | "GNU_PROPERTY_X86_FEATURE_1_AND && " |
| 113 | "GNU_PROPERTY_X86_FEATURE_1_AND > " |
| 114 | "GNU_PROPERTY_1_NEEDED" ); |
| 115 | if (type == GNU_PROPERTY_X86_FEATURE_1_AND) |
| 116 | *feature_1_and = *(unsigned int *) ptr; |
| 117 | else if (type == GNU_PROPERTY_1_NEEDED) |
| 118 | *needed_1 = *(unsigned int *) ptr; |
| 119 | else |
| 120 | { |
| 121 | *isa_1_needed = *(unsigned int *) ptr; |
| 122 | |
| 123 | /* Keep searching for the next GNU property note |
| 124 | generated by the older linker. */ |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) |
| 129 | { |
| 130 | /* Stop the scan since property type is in ascending |
| 131 | order. */ |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | static inline void __attribute__ ((unused)) |
| 139 | _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, |
| 140 | const ElfW(Addr) size, const ElfW(Addr) align) |
| 141 | { |
| 142 | /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ |
| 143 | if (l->l_property != lc_property_unknown) |
| 144 | return; |
| 145 | |
| 146 | /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in |
| 147 | 32-bit objects and to 8 bytes in 64-bit objects. Skip notes |
| 148 | with incorrect alignment. */ |
| 149 | if (align != (__ELF_NATIVE_CLASS / 8)) |
| 150 | return; |
| 151 | |
| 152 | const ElfW(Addr) start = (ElfW(Addr)) note; |
| 153 | |
| 154 | unsigned int needed_1 = 0; |
| 155 | unsigned int feature_1_and = 0; |
| 156 | unsigned int isa_1_needed = 0; |
| 157 | unsigned int last_type = 0; |
| 158 | |
| 159 | while ((ElfW(Addr)) (note + 1) - start < size) |
| 160 | { |
| 161 | /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ |
| 162 | if (note->n_namesz == 4 |
| 163 | && note->n_type == NT_GNU_PROPERTY_TYPE_0 |
| 164 | && memcmp (note + 1, "GNU" , 4) == 0) |
| 165 | { |
| 166 | /* Stop if we see more than one GNU property note which may |
| 167 | be generated by the older linker. */ |
| 168 | if (l->l_property != lc_property_unknown) |
| 169 | return; |
| 170 | |
| 171 | /* Check CET status and ISA levels now. */ |
| 172 | l->l_property = lc_property_none; |
| 173 | |
| 174 | /* Check for invalid property. */ |
| 175 | if (note->n_descsz < 8 |
| 176 | || (note->n_descsz % sizeof (ElfW(Addr))) != 0) |
| 177 | return; |
| 178 | |
| 179 | /* Start and end of property array. */ |
| 180 | unsigned char *ptr = (unsigned char *) (note + 1) + 4; |
| 181 | unsigned char *ptr_end = ptr + note->n_descsz; |
| 182 | |
| 183 | do |
| 184 | { |
| 185 | unsigned int type = *(unsigned int *) ptr; |
| 186 | unsigned int datasz = *(unsigned int *) (ptr + 4); |
| 187 | |
| 188 | /* Property type must be in ascending order. */ |
| 189 | if (type < last_type) |
| 190 | return; |
| 191 | |
| 192 | ptr += 8; |
| 193 | if ((ptr + datasz) > ptr_end) |
| 194 | return; |
| 195 | |
| 196 | last_type = type; |
| 197 | |
| 198 | int result = _dl_check_gnu_property (type, datasz, ptr, |
| 199 | feature_1_and: &feature_1_and, |
| 200 | needed_1: &needed_1, |
| 201 | isa_1_needed: &isa_1_needed); |
| 202 | if (result == -1) |
| 203 | return; /* Skip this note. */ |
| 204 | else if (result == 0) |
| 205 | break; /* Stop checking. */ |
| 206 | |
| 207 | /* Check the next property item. */ |
| 208 | ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); |
| 209 | } |
| 210 | while ((ptr_end - ptr) >= 8); |
| 211 | } |
| 212 | |
| 213 | /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are |
| 214 | aligned to 4 bytes in 64-bit ELF objects. */ |
| 215 | note = ((const void *) note |
| 216 | + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz, |
| 217 | align)); |
| 218 | } |
| 219 | |
| 220 | /* We get here only if there is one or no GNU property note. */ |
| 221 | if (needed_1 != 0 || isa_1_needed != 0 || feature_1_and != 0) |
| 222 | { |
| 223 | l->l_property = lc_property_valid; |
| 224 | l->l_1_needed = needed_1; |
| 225 | l->l_x86_isa_1_needed = isa_1_needed; |
| 226 | l->l_x86_feature_1_and = feature_1_and; |
| 227 | } |
| 228 | else |
| 229 | l->l_property = lc_property_none; |
| 230 | } |
| 231 | |
| 232 | static inline void __attribute__ ((unused)) |
| 233 | _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) |
| 234 | { |
| 235 | const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); |
| 236 | _dl_process_property_note (l, note, size: ph->p_memsz, align: ph->p_align); |
| 237 | } |
| 238 | |
| 239 | static inline int __attribute__ ((always_inline)) |
| 240 | _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, |
| 241 | uint32_t datasz, void *data) |
| 242 | { |
| 243 | /* This is called on each GNU property. */ |
| 244 | unsigned int needed_1 = 0; |
| 245 | unsigned int feature_1_and = 0; |
| 246 | unsigned int isa_1_needed = 0; |
| 247 | int result = _dl_check_gnu_property (type, datasz, ptr: data, |
| 248 | feature_1_and: &feature_1_and, needed_1: &needed_1, |
| 249 | isa_1_needed: &isa_1_needed); |
| 250 | if (needed_1 != 0) |
| 251 | l->l_1_needed = needed_1; |
| 252 | if (isa_1_needed != 0) |
| 253 | l->l_x86_isa_1_needed = isa_1_needed; |
| 254 | if (feature_1_and != 0) |
| 255 | l->l_x86_feature_1_and = feature_1_and; |
| 256 | if ((needed_1 | isa_1_needed | feature_1_and) != 0) |
| 257 | l->l_property = lc_property_valid; |
| 258 | else if (l->l_property == lc_property_unknown) |
| 259 | l->l_property = lc_property_none; |
| 260 | return result <= 0 ? 0 : result; |
| 261 | } |
| 262 | |
| 263 | #endif /* _DL_PROP_H */ |
| 264 | |