1 /* Support for GNU properties. x86 version.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <not-cancel.h>
24 extern void _dl_cet_check (struct link_map
*, const char *)
26 extern void _dl_cet_open_check (struct link_map
*)
29 static inline void __attribute__ ((always_inline
))
30 _rtld_main_check (struct link_map
*m
, const char *program
)
33 _dl_cet_check (m
, program
);
37 static inline void __attribute__ ((always_inline
))
38 _dl_open_check (struct link_map
*m
)
41 _dl_cet_open_check (m
);
45 static inline void __attribute__ ((unused
))
46 _dl_process_cet_property_note (struct link_map
*l
,
47 const ElfW(Nhdr
) *note
,
48 const ElfW(Addr
) size
,
49 const ElfW(Addr
) align
)
52 /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */
53 if (l
->l_cet
!= lc_unknown
)
56 /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in
57 32-bit objects and to 8 bytes in 64-bit objects. Skip notes
58 with incorrect alignment. */
59 if (align
!= (__ELF_NATIVE_CLASS
/ 8))
62 const ElfW(Addr
) start
= (ElfW(Addr
)) note
;
64 unsigned int feature_1
= 0;
65 unsigned int last_type
= 0;
67 while ((ElfW(Addr
)) (note
+ 1) - start
< size
)
69 /* Find the NT_GNU_PROPERTY_TYPE_0 note. */
70 if (note
->n_namesz
== 4
71 && note
->n_type
== NT_GNU_PROPERTY_TYPE_0
72 && memcmp (note
+ 1, "GNU", 4) == 0)
74 /* Stop if we see more than one GNU property note which may
75 be generated by the older linker. */
76 if (l
->l_cet
!= lc_unknown
)
79 /* Check CET status now. */
82 /* Check for invalid property. */
83 if (note
->n_descsz
< 8
84 || (note
->n_descsz
% sizeof (ElfW(Addr
))) != 0)
87 /* Start and end of property array. */
88 unsigned char *ptr
= (unsigned char *) (note
+ 1) + 4;
89 unsigned char *ptr_end
= ptr
+ note
->n_descsz
;
93 unsigned int type
= *(unsigned int *) ptr
;
94 unsigned int datasz
= *(unsigned int *) (ptr
+ 4);
96 /* Property type must be in ascending order. */
101 if ((ptr
+ datasz
) > ptr_end
)
106 if (type
== GNU_PROPERTY_X86_FEATURE_1_AND
)
108 /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4
109 bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND,
110 we stop the search regardless if its size is correct
111 or not. There is no point to continue if this note
116 feature_1
= *(unsigned int *) ptr
;
118 /* Keep searching for the next GNU property note
119 generated by the older linker. */
122 else if (type
> GNU_PROPERTY_X86_FEATURE_1_AND
)
124 /* Stop since property type is in ascending order. */
128 /* Check the next property item. */
129 ptr
+= ALIGN_UP (datasz
, sizeof (ElfW(Addr
)));
131 while ((ptr_end
- ptr
) >= 8);
134 /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are
135 aligned to 4 bytes in 64-bit ELF objects. */
136 note
= ((const void *) note
137 + ELF_NOTE_NEXT_OFFSET (note
->n_namesz
, note
->n_descsz
,
141 /* We get here only if there is one or no GNU property note. */
142 if ((feature_1
& GNU_PROPERTY_X86_FEATURE_1_IBT
))
144 if ((feature_1
& GNU_PROPERTY_X86_FEATURE_1_SHSTK
))
145 l
->l_cet
|= lc_shstk
;
150 static inline int __attribute__ ((unused
))
151 _dl_process_pt_note (struct link_map
*l
, const ElfW(Phdr
) *ph
,
152 int fd
, struct filebuf
*fbp
)
155 const ElfW(Nhdr
) *note
;
156 ElfW(Nhdr
) *note_malloced
= NULL
;
157 ElfW(Addr
) size
= ph
->p_filesz
;
159 if (ph
->p_offset
+ size
<= (size_t) fbp
->len
)
160 note
= (const void *) (fbp
->buf
+ ph
->p_offset
);
163 if (size
< __MAX_ALLOCA_CUTOFF
)
164 note
= alloca (size
);
167 note_malloced
= malloc (size
);
168 note
= note_malloced
;
170 __lseek (fd
, ph
->p_offset
, SEEK_SET
);
171 if (__read_nocancel (fd
, (void *) note
, size
) != size
)
174 free (note_malloced
);
179 _dl_process_cet_property_note (l
, note
, size
, ph
->p_align
);
181 free (note_malloced
);
187 static inline int __attribute__ ((unused
))
188 _rtld_process_pt_note (struct link_map
*l
, const ElfW(Phdr
) *ph
)
190 const ElfW(Nhdr
) *note
= (const void *) (ph
->p_vaddr
+ l
->l_addr
);
191 _dl_process_cet_property_note (l
, note
, ph
->p_memsz
, ph
->p_align
);
195 #endif /* _DL_PROP_H */