1 | /* Support for GNU properties. x86 version. |
2 | Copyright (C) 2018-2021 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 ("%s: CPU ISA level is lower than required\n" , |
58 | *l->l_name != '\0' ? l->l_name : program); |
59 | else |
60 | _dl_signal_error (0, l->l_name, "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 | static inline void __attribute__ ((unused)) |
85 | _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, |
86 | const ElfW(Addr) size, const ElfW(Addr) align) |
87 | { |
88 | /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ |
89 | if (l->l_property != lc_property_unknown) |
90 | return; |
91 | |
92 | /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in |
93 | 32-bit objects and to 8 bytes in 64-bit objects. Skip notes |
94 | with incorrect alignment. */ |
95 | if (align != (__ELF_NATIVE_CLASS / 8)) |
96 | return; |
97 | |
98 | const ElfW(Addr) start = (ElfW(Addr)) note; |
99 | |
100 | unsigned int feature_1_and = 0; |
101 | unsigned int isa_1_needed = 0; |
102 | unsigned int last_type = 0; |
103 | |
104 | while ((ElfW(Addr)) (note + 1) - start < size) |
105 | { |
106 | /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ |
107 | if (note->n_namesz == 4 |
108 | && note->n_type == NT_GNU_PROPERTY_TYPE_0 |
109 | && memcmp (note + 1, "GNU" , 4) == 0) |
110 | { |
111 | /* Stop if we see more than one GNU property note which may |
112 | be generated by the older linker. */ |
113 | if (l->l_property != lc_property_unknown) |
114 | return; |
115 | |
116 | /* Check CET status and ISA levels now. */ |
117 | l->l_property = lc_property_none; |
118 | |
119 | /* Check for invalid property. */ |
120 | if (note->n_descsz < 8 |
121 | || (note->n_descsz % sizeof (ElfW(Addr))) != 0) |
122 | return; |
123 | |
124 | /* Start and end of property array. */ |
125 | unsigned char *ptr = (unsigned char *) (note + 1) + 4; |
126 | unsigned char *ptr_end = ptr + note->n_descsz; |
127 | |
128 | do |
129 | { |
130 | unsigned int type = *(unsigned int *) ptr; |
131 | unsigned int datasz = *(unsigned int *) (ptr + 4); |
132 | |
133 | /* Property type must be in ascending order. */ |
134 | if (type < last_type) |
135 | return; |
136 | |
137 | ptr += 8; |
138 | if ((ptr + datasz) > ptr_end) |
139 | return; |
140 | |
141 | last_type = type; |
142 | |
143 | if (type == GNU_PROPERTY_X86_FEATURE_1_AND |
144 | || type == GNU_PROPERTY_X86_ISA_1_NEEDED) |
145 | { |
146 | /* The sizes of types which we are searching for are |
147 | 4 bytes. There is no point to continue if this |
148 | note is ill-formed. */ |
149 | if (datasz != 4) |
150 | return; |
151 | |
152 | /* NB: Stop the scan only after seeing all types which |
153 | we are searching for. */ |
154 | _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > |
155 | GNU_PROPERTY_X86_FEATURE_1_AND), |
156 | "GNU_PROPERTY_X86_ISA_1_NEEDED > " |
157 | "GNU_PROPERTY_X86_FEATURE_1_AND" ); |
158 | if (type == GNU_PROPERTY_X86_FEATURE_1_AND) |
159 | feature_1_and = *(unsigned int *) ptr; |
160 | else |
161 | { |
162 | isa_1_needed = *(unsigned int *) ptr; |
163 | |
164 | /* Keep searching for the next GNU property note |
165 | generated by the older linker. */ |
166 | break; |
167 | } |
168 | } |
169 | else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) |
170 | { |
171 | /* Stop the scan since property type is in ascending |
172 | order. */ |
173 | break; |
174 | } |
175 | |
176 | /* Check the next property item. */ |
177 | ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); |
178 | } |
179 | while ((ptr_end - ptr) >= 8); |
180 | } |
181 | |
182 | /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are |
183 | aligned to 4 bytes in 64-bit ELF objects. */ |
184 | note = ((const void *) note |
185 | + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz, |
186 | align)); |
187 | } |
188 | |
189 | /* We get here only if there is one or no GNU property note. */ |
190 | if (isa_1_needed != 0 || feature_1_and != 0) |
191 | { |
192 | l->l_property = lc_property_valid; |
193 | l->l_x86_isa_1_needed = isa_1_needed; |
194 | l->l_x86_feature_1_and = feature_1_and; |
195 | } |
196 | else |
197 | l->l_property = lc_property_none; |
198 | } |
199 | |
200 | static inline void __attribute__ ((unused)) |
201 | _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) |
202 | { |
203 | const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); |
204 | _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); |
205 | } |
206 | |
207 | static inline int __attribute__ ((always_inline)) |
208 | _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, |
209 | uint32_t datasz, void *data) |
210 | { |
211 | return 0; |
212 | } |
213 | |
214 | #endif /* _DL_PROP_H */ |
215 | |