Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
2795 lines (2675 loc) · 105 KB
/
Copy pathmain.c
File metadata and controls
2795 lines (2675 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2026 Andrew Gaul <andrew@gaul.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <capstone/capstone.h>
#include "armlint.h"
// === ELF64 minimal definitions ===
//
// Darwin has no <elf.h>; declaring the bits we need keeps the project
// self-contained and identical-on-disk across hosts.
#define EI_NIDENT 16
#define EI_CLASS 4
#define EI_DATA 5
#define ELFMAG "\x7f""ELF"
#define SELFMAG 4
#define ELFCLASS64 2
#define ELFDATA2LSB 1
#define ET_REL 1
#define EM_AARCH64 183
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_RELA 4
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_DYNSYM 11
#define SHT_INIT_ARRAY 14
#define SHT_FINI_ARRAY 15
#define SHT_PREINIT_ARRAY 16
#define SHT_RELR 19
#define SHF_ALLOC 0x2
#define SHF_EXECINSTR 0x4
#define SHN_UNDEF 0
#define PT_INTERP 3
#define NT_GNU_PROPERTY_TYPE_0 5
// The AArch64 GNU property word and its bits: BTI (every indirect
// branch target carries a landing pad, so the loader maps the code
// PROT_BTI), PAC (every function signs its return address) and GCS
// (the code runs under a guarded control stack).
#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000u
#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 0x1u
#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 0x2u
#define GNU_PROPERTY_AARCH64_FEATURE_1_GCS 0x4u
#define R_AARCH64_ABS64 257
#define R_AARCH64_GLOB_DAT 1025
#define R_AARCH64_JUMP_SLOT 1026
#define R_AARCH64_RELATIVE 1027
#define R_AARCH64_IRELATIVE 1032
#define ELF64_R_SYM(i) ((uint32_t)((i) >> 32))
#define ELF64_R_TYPE(i) ((uint32_t)((i) & 0xffffffffu))
#define STB_LOCAL 0
#define STT_NOTYPE 0
#define STT_FUNC 2
#define STT_GNU_IFUNC 10
typedef struct {
unsigned char e_ident[EI_NIDENT];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff;
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
} Elf64_Ehdr;
typedef struct {
uint32_t sh_name;
uint32_t sh_type;
uint64_t sh_flags;
uint64_t sh_addr;
uint64_t sh_offset;
uint64_t sh_size;
uint32_t sh_link;
uint32_t sh_info;
uint64_t sh_addralign;
uint64_t sh_entsize;
} Elf64_Shdr;
typedef struct {
uint32_t st_name;
uint8_t st_info;
uint8_t st_other;
uint16_t st_shndx;
uint64_t st_value;
uint64_t st_size;
} Elf64_Sym;
typedef struct {
uint32_t p_type;
uint32_t p_flags;
uint64_t p_offset;
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
} Elf64_Phdr;
typedef struct {
uint64_t r_offset;
uint64_t r_info;
int64_t r_addend;
} Elf64_Rela;
typedef struct {
uint32_t n_namesz;
uint32_t n_descsz;
uint32_t n_type;
} Elf64_Nhdr;
// === Mach-O minimal definitions ===
//
// Apple's <mach-o/*.h> would do, but they pull in <mach/machine.h> and
// host-only types. Reproducing the on-disk layout here keeps Linux
// builds buildable too.
#define MH_MAGIC_64 0xfeedfacfu // little-endian Mach-O 64
#define FAT_MAGIC 0xcafebabeu // fat header, stored big-endian
#define FAT_MAGIC_64 0xcafebabfu // fat header with 64-bit offsets
#define CPU_TYPE_ARM64 0x0100000cu // includes arm64 and arm64e
// The cpusubtype's low 24 bits name the variant; the high byte holds
// capability/feature bits (CPU_SUBTYPE_PTRAUTH_ABI etc.). arm64e is 2;
// arm64e.x1 (12) is the same pointer-authentication ABI for cores
// with FEAT_PAuth_LR, whose code signs and authenticates the return
// address with pacibsppc/autibsppc/retabsppc.
#define CPU_SUBTYPE_MASK 0x00ffffffu
#define CPU_SUBTYPE_ARM64_ALL 0x00000000u
#define CPU_SUBTYPE_ARM64_V8 0x00000001u
#define CPU_SUBTYPE_ARM64E 0x00000002u
#define CPU_SUBTYPE_ARM64E_X1 0x0000000cu
#define LC_SYMTAB 0x2u
#define LC_SEGMENT_64 0x19u
#define LC_FUNCTION_STARTS 0x26u
#define S_ATTR_PURE_INSTRUCTIONS 0x80000000u
// The low byte of section flags is the section *type* (the attribute
// bits live above it); S_SYMBOL_STUBS marks the linker's dyld import
// stubs (__stubs).
#define SECTION_TYPE 0x000000ffu
#define S_SYMBOL_STUBS 0x8u
// nlist_64.n_type decomposition: any stab bit marks a debug entry,
// N_SECT means defined in the section n_sect names (1-based ordinal
// across every segment's sections), N_EXT marks nm-visible externals.
#define N_STAB_MASK 0xe0u
#define N_TYPE_MASK 0x0eu
#define N_SECT 0x0eu
#define N_EXT 0x01u
typedef struct {
uint32_t magic;
int32_t cputype;
int32_t cpusubtype;
uint32_t filetype;
uint32_t ncmds;
uint32_t sizeofcmds;
uint32_t flags;
uint32_t reserved;
} mach_header_64;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
} load_command_hdr;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
char segname[16];
uint64_t vmaddr;
uint64_t vmsize;
uint64_t fileoff;
uint64_t filesize;
int32_t maxprot;
int32_t initprot;
uint32_t nsects;
uint32_t flags;
} segment_command_64;
typedef struct {
char sectname[16];
char segname[16];
uint64_t addr;
uint64_t size;
uint32_t offset;
uint32_t align;
uint32_t reloff;
uint32_t nreloc;
uint32_t flags;
uint32_t reserved1;
uint32_t reserved2;
uint32_t reserved3;
} section_64;
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t symoff;
uint32_t nsyms;
uint32_t stroff;
uint32_t strsize;
} symtab_command;
// LC_FUNCTION_STARTS (among others) points at an opaque linkedit blob.
typedef struct {
uint32_t cmd;
uint32_t cmdsize;
uint32_t dataoff;
uint32_t datasize;
} linkedit_data_command;
typedef struct {
uint32_t n_strx;
uint8_t n_type;
uint8_t n_sect;
uint16_t n_desc;
uint64_t n_value;
} nlist_64;
typedef struct {
uint32_t magic;
uint32_t nfat_arch;
} fat_header;
typedef struct {
uint32_t cputype;
uint32_t cpusubtype;
uint32_t offset;
uint32_t size;
uint32_t align;
} fat_arch_32;
typedef struct {
uint32_t cputype;
uint32_t cpusubtype;
uint64_t offset;
uint64_t size;
uint32_t align;
uint32_t reserved;
} fat_arch_64;
static uint32_t be32(uint32_t v)
{
return ((v & 0xffu) << 24) | ((v & 0xff00u) << 8)
| ((v & 0xff0000u) >> 8) | ((v >> 24) & 0xffu);
}
static uint64_t be64(uint64_t v)
{
return ((uint64_t)be32((uint32_t)(v & 0xffffffffu)) << 32)
| (uint64_t)be32((uint32_t)(v >> 32));
}
static bool read_at(FILE *f, long off, void *buf, size_t n)
{
if (fseek(f, off, SEEK_SET) != 0) {
return false;
}
return fread(buf, 1, n, f) == n;
}
// === BTI landing-pad verification, -b ===
//
// With FEAT_BTI enforced -- the loader maps the code PROT_BTI when the
// GNU property note carries the BTI bit -- an indirect BR or BLR must
// land on a BTI whose targets admit it, or the CPU raises a Branch
// Target exception. Which addresses an indirect branch can reach is
// not a property of the instruction stream: nothing in the bytes
// distinguishes a landing site from fall-through code. But the ELF
// metadata of a linked binary evidences a checkable subset, collected
// below exactly as x86lint's -e collects it for ENDBR64. The subset
// under-approximates (a function pointer materialized by ADRP+ADD
// leaves no relocation behind), so a target the metadata does
// evidence really is reachable indirectly and a missing pad there
// really faults; the converse -- flagging a superfluous BTI -- would
// be guessing, and is not attempted.
//
// Every evidenced target is entered by a call or through a PLT: the
// loader's BLR into the entry point, init/fini and resolvers, a GOT
// slot's BLR, and a PLT entry's BR x16/x17. BTI c admits all of
// those (BR is permitted when it comes through x16 or x17, which is
// why a PLT branches that way and why the compilers' indirect tail
// calls use those registers under -mbranch-protection), as do BTI jc
// and the PACIASP/PACIBSP a pac-ret prologue opens with. A BTI j is
// a pad of the wrong flavour -- it admits BR alone -- and is reported
// as such: a sharper verdict than x86 has, where there is one ENDBR64.
// Evidence sources, strongest first: when several name the same
// address (an exported function also referenced by a GOT relocation),
// dedup keeps the smallest kind. Every kind but the last proves the
// address is *branched* to. An address relocation proves only that
// the address is *taken*: glibc bakes pointers to bracket labels that
// are only compared against interrupted PCs, with no pad by design.
// The verify loop therefore counts BTI_ADDR evidence only when the
// target is a known function entry, and its last-place rank keeps it
// from shadowing a stronger claim to the same address.
enum bti_kind {
BTI_ENTRY, // e_entry: ld.so transfers to it indirectly
BTI_INIT, // .init/.fini: DT_INIT/DT_FINI, called through a pointer
BTI_ARRAY, // .preinit_array/.init_array/.fini_array slot
BTI_PLT, // R_AARCH64_JUMP_SLOT resolving to a local definition
BTI_GOT, // R_AARCH64_GLOB_DAT resolving to a local definition
BTI_IFUNC, // R_AARCH64_IRELATIVE: the loader calls the resolver
BTI_EXPORT, // defined dynsym function: reachable via any caller's PLT
BTI_ADDR, // absolute-address relocation into an executable section
};
static const char *const bti_kind_name[] = {
"entry point",
".init/.fini entry",
"init/fini array entry",
"PLT relocation",
"GOT relocation",
"ifunc resolver",
"exported function",
"address relocation",
};
struct bti_target {
uint64_t va;
uint32_t sym; // index into .dynsym, or 0 (STN_UNDEF) for none
uint8_t kind; // enum bti_kind
};
struct bti_targets {
struct bti_target *v;
size_t n;
size_t cap;
};
static bool bti_push(struct bti_targets *t, uint64_t va, uint32_t sym,
uint8_t kind)
{
if (t->n == t->cap) {
size_t cap = t->cap == 0 ? 64 : t->cap * 2;
struct bti_target *v = realloc(t->v, cap * sizeof(*v));
if (v == NULL) {
return false;
}
t->v = v;
t->cap = cap;
}
t->v[t->n].va = va;
t->v[t->n].sym = sym;
t->v[t->n].kind = kind;
++t->n;
return true;
}
static int bti_target_cmp(const void *a, const void *b)
{
const struct bti_target *ta = a, *tb = b;
if (ta->va != tb->va) {
return ta->va < tb->va ? -1 : 1;
}
if (ta->kind != tb->kind) {
return ta->kind < tb->kind ? -1 : 1;
}
return 0;
}
static bool u64_push(uint64_t **v, size_t *n, size_t *cap, uint64_t x)
{
if (*n == *cap) {
size_t ncap = *cap == 0 ? 64 : *cap * 2;
uint64_t *nv = realloc(*v, ncap * sizeof(*nv));
if (nv == NULL) {
return false;
}
*v = nv;
*cap = ncap;
}
(*v)[(*n)++] = x;
return true;
}
static int u64_cmp(const void *a, const void *b)
{
uint64_t ua = *(const uint64_t *)a, ub = *(const uint64_t *)b;
return ua < ub ? -1 : ua > ub ? 1 : 0;
}
static bool u64_contains(const uint64_t *v, size_t n, uint64_t x)
{
size_t lo = 0, hi = n;
while (lo < hi) {
size_t mid = lo + (hi - lo) / 2;
if (v[mid] < x) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo < n && v[lo] == x;
}
// Read a whole section's contents, bounds-checked against the file.
// NULL for a section with no bytes in the file or on I/O or
// allocation failure; the callers treat all of these as absent
// evidence and move on (the summary line still states how many
// targets were actually checked).
static uint8_t *load_section(FILE *f, const Elf64_Shdr *shdr,
uint64_t file_size)
{
if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0
|| shdr->sh_offset > file_size
|| shdr->sh_size > file_size - shdr->sh_offset) {
return NULL;
}
uint8_t *buf = malloc(shdr->sh_size);
if (buf != NULL && !read_at(f, (long)shdr->sh_offset, buf, shdr->sh_size)) {
free(buf);
buf = NULL;
}
return buf;
}
// The AArch64 feature word of the GNU property note (an
// NT_GNU_PROPERTY_TYPE_0 note named "GNU"), or 0 when no note carries
// one. Property notes are 8-aligned in ELF64; other note sections
// (build-id) use 4, hence the per-section alignment. Each property is
// {u32 pr_type, u32 pr_datasz, data padded to 8}.
static uint32_t elf_aarch64_feature_1_and(FILE *f, const Elf64_Shdr *sh,
uint64_t shnum, uint64_t file_size)
{
uint32_t feat = 0;
bool found = false;
for (uint64_t i = 0; i < shnum && !found; ++i) {
if (sh[i].sh_type != SHT_NOTE) {
continue;
}
uint8_t *buf = load_section(f, &sh[i], file_size);
if (buf == NULL) {
continue;
}
uint64_t size = sh[i].sh_size;
uint64_t align = sh[i].sh_addralign == 8 ? 8 : 4;
uint64_t off = 0;
while (off + sizeof(Elf64_Nhdr) <= size) {
Elf64_Nhdr nh;
memcpy(&nh, buf + off, sizeof(nh));
uint64_t name_off = off + sizeof(nh);
uint64_t desc_off = (name_off + nh.n_namesz + align - 1)
& ~(align - 1);
if (desc_off > size || nh.n_descsz > size - desc_off
|| nh.n_namesz > size - name_off) {
break; // malformed: stop parsing this section
}
if (nh.n_type == NT_GNU_PROPERTY_TYPE_0 && nh.n_namesz == 4
&& memcmp(buf + name_off, "GNU", 4) == 0) {
uint64_t p = desc_off;
uint64_t dend = desc_off + nh.n_descsz;
while (p + 8 <= dend) {
uint32_t pr_type, pr_datasz;
memcpy(&pr_type, buf + p, 4);
memcpy(&pr_datasz, buf + p + 4, 4);
if (pr_datasz > dend - (p + 8)) {
break;
}
if (!found
&& pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND
&& pr_datasz >= 4) {
memcpy(&feat, buf + p + 8, 4);
found = true;
}
p += 8 + (((uint64_t)pr_datasz + 7) & ~(uint64_t)7);
}
}
off = (desc_off + nh.n_descsz + align - 1) & ~(align - 1);
}
free(buf);
}
return feat;
}
// Resolve one RELR-relocated slot: the packed format encodes only
// WHERE a relative relocation applies; its target is the slot's raw
// link-time value, read here from whichever section holds the slot.
// Slots arrive sorted, so the single-section cache almost always
// hits. Returns false only on allocation failure; a slot outside any
// loadable section, an unreadable section, and a 0/-1 sentinel value
// are skipped.
static bool bti_relr_slot(FILE *f, const Elf64_Shdr *sh, uint64_t shnum,
uint64_t file_size, uint64_t where,
uint64_t *cache_i, uint8_t **cache,
struct bti_targets *targets)
{
if (*cache_i >= shnum || where < sh[*cache_i].sh_addr
|| where - sh[*cache_i].sh_addr > sh[*cache_i].sh_size - 8) {
free(*cache);
*cache = NULL;
*cache_i = shnum;
for (uint64_t i = 0; i < shnum; ++i) {
if ((sh[i].sh_flags & SHF_ALLOC) && sh[i].sh_type != SHT_NOBITS
&& sh[i].sh_size >= 8 && where >= sh[i].sh_addr
&& where - sh[i].sh_addr <= sh[i].sh_size - 8) {
*cache = load_section(f, &sh[i], file_size);
if (*cache == NULL) {
return true;
}
*cache_i = i;
break;
}
}
if (*cache_i >= shnum) {
return true;
}
}
uint64_t value;
memcpy(&value, *cache + (where - sh[*cache_i].sh_addr), 8);
if (value == 0 || value == UINT64_MAX) {
return true;
}
return bti_push(targets, value, 0, BTI_ADDR);
}
// Name a target: by its recorded dynsym index, or by reverse value
// lookup for targets evidenced without a symbol. NULL when nothing
// matches.
static const char *bti_sym_name(const Elf64_Sym *dynsym, size_t ndynsym,
const char *dynstr, uint64_t dynstr_size,
uint32_t symi, uint64_t va)
{
if (dynstr == NULL) {
return NULL;
}
if (symi == 0) {
for (size_t s = 1; s < ndynsym; ++s) {
unsigned st = dynsym[s].st_info & 0xfu;
if ((st == STT_FUNC || st == STT_GNU_IFUNC)
&& dynsym[s].st_shndx != SHN_UNDEF
&& dynsym[s].st_value == va) {
symi = (uint32_t)s;
break;
}
}
if (symi == 0) {
return NULL;
}
}
if (symi >= ndynsym || dynsym[symi].st_name == 0
|| dynsym[symi].st_name >= dynstr_size) {
return NULL;
}
return dynstr + dynsym[symi].st_name;
}
// Verify that every indirect branch target the ELF metadata evidences
// begins with a landing pad that admits a call (armlint_bti_landing),
// and reconcile the result with the binary's property note. Four
// verdicts: BTI declared and every target padded -- clean; BTI
// declared with misses -- each is a Branch Target exception once the
// pages are mapped PROT_BTI, so every miss is printed regardless of
// verbosity; BTI not declared but pads present -- the compiler emitted
// them and the link lost the note (the property is ANDed across
// inputs, so one object built without -mbranch-protection silently
// disarms the whole binary), one finding, with the per-target detail
// behind -v; no declaration and no pads -- not a BTI binary, no
// findings. Returns the finding count, or -1 on tool failure.
static int check_elf_bti(FILE *f, const char *path, uint64_t file_size,
bool verbose)
{
Elf64_Ehdr ehdr;
if (!read_at(f, 0, &ehdr, sizeof(ehdr))) {
fprintf(stderr, "%s: failed to read ELF header\n", path);
return -1;
}
// A relocatable object's indirect targets are a compile-time
// question (its address-taken evidence dissolves into the final
// link), and half its metadata does not exist yet.
if (ehdr.e_type == ET_REL) {
fprintf(stderr,
"%s: -b requires a linked executable or shared object\n", path);
return -1;
}
uint64_t shnum = ehdr.e_shnum;
if (shnum == 0) {
printf("BTI: no section headers; nothing to check\n");
return 0;
}
int rc = -1;
struct bti_targets targets = {NULL, 0, 0};
Elf64_Sym *dynsym = NULL;
size_t ndynsym = 0;
char *dynstr = NULL;
uint64_t dynstr_size = 0;
char *shstr = NULL;
uint64_t shstrsize = 0;
uint8_t **exec_bufs = NULL;
uint64_t *fentry = NULL;
size_t nfentry = 0;
size_t fentry_cap = 0;
Elf64_Shdr *sh = malloc(shnum * sizeof(*sh));
if (sh == NULL
|| ehdr.e_shoff > file_size
|| shnum > (file_size - ehdr.e_shoff) / sizeof(*sh)
|| !read_at(f, (long)ehdr.e_shoff, sh, shnum * sizeof(*sh))) {
fprintf(stderr, "%s: failed to read section headers\n", path);
goto out;
}
if (ehdr.e_shstrndx != 0 && ehdr.e_shstrndx < shnum) {
shstr = (char *)load_section(f, &sh[ehdr.e_shstrndx], file_size);
if (shstr != NULL) {
shstrsize = sh[ehdr.e_shstrndx].sh_size;
shstr[shstrsize - 1] = '\0';
}
}
uint32_t feat = elf_aarch64_feature_1_and(f, sh, shnum, file_size);
bool bti = (feat & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) != 0;
bool pac = (feat & GNU_PROPERTY_AARCH64_FEATURE_1_PAC) != 0;
bool gcs = (feat & GNU_PROPERTY_AARCH64_FEATURE_1_GCS) != 0;
// The dynamic symbol table names relocation targets and lists the
// exported functions, each an indirect target in its own right.
for (uint64_t i = 0; i < shnum; ++i) {
if (sh[i].sh_type != SHT_DYNSYM
|| sh[i].sh_entsize != sizeof(Elf64_Sym)) {
continue;
}
dynsym = (Elf64_Sym *)load_section(f, &sh[i], file_size);
if (dynsym == NULL) {
break;
}
ndynsym = sh[i].sh_size / sizeof(Elf64_Sym);
if (sh[i].sh_link < shnum) {
dynstr = (char *)load_section(f, &sh[sh[i].sh_link], file_size);
if (dynstr != NULL) {
dynstr_size = sh[sh[i].sh_link].sh_size;
dynstr[dynstr_size - 1] = '\0'; // bound every lookup
}
}
break;
}
// Function entries known to the symbol tables (.symtab when the
// binary kept one, .dynsym always), for grading the
// address-relocation evidence in the verify loop: a baked code
// pointer is proven a branch target only when it addresses a
// function entry. A compare-only label is NOTYPE and drops out
// here; a stripped binary loses this evidence class and nothing
// else -- erring, as everywhere, toward the false negative.
bool oom = false;
for (uint64_t i = 0; i < shnum && !oom; ++i) {
if (sh[i].sh_type != SHT_SYMTAB
|| sh[i].sh_entsize != sizeof(Elf64_Sym)) {
continue;
}
Elf64_Sym *syms = (Elf64_Sym *)load_section(f, &sh[i], file_size);
if (syms == NULL) {
continue;
}
size_t nsyms = sh[i].sh_size / sizeof(Elf64_Sym);
for (size_t s = 1; s < nsyms && !oom; ++s) {
unsigned st = syms[s].st_info & 0xfu;
if ((st == STT_FUNC || st == STT_GNU_IFUNC)
&& syms[s].st_shndx != SHN_UNDEF
&& syms[s].st_value != 0) {
oom |= !u64_push(&fentry, &nfentry, &fentry_cap,
syms[s].st_value);
}
}
free(syms);
}
for (size_t s = 1; s < ndynsym && !oom; ++s) {
unsigned st = dynsym[s].st_info & 0xfu;
if ((st == STT_FUNC || st == STT_GNU_IFUNC)
&& dynsym[s].st_shndx != SHN_UNDEF && dynsym[s].st_value != 0) {
oom |= !u64_push(&fentry, &nfentry, &fentry_cap,
dynsym[s].st_value);
}
}
if (nfentry != 0) {
qsort(fentry, nfentry, sizeof(*fentry), u64_cmp);
}
// The entry point is an indirect branch target only for a program
// with an interpreter: ld.so's _dl_start_user transfers to e_entry
// with a BR through x16, which a BTI c admits. A kernel-entered
// binary -- static, static-PIE, ld.so itself -- starts with no
// guarded page to land on and owes no pad there.
bool has_interp = false;
if (ehdr.e_phoff != 0 && ehdr.e_phentsize == sizeof(Elf64_Phdr)
&& ehdr.e_phoff <= file_size
&& ehdr.e_phnum <= (file_size - ehdr.e_phoff) / sizeof(Elf64_Phdr)) {
for (uint16_t i = 0; i < ehdr.e_phnum; ++i) {
Elf64_Phdr ph;
if (!read_at(f, (long)(ehdr.e_phoff + i * sizeof(ph)), &ph,
sizeof(ph))) {
break;
}
if (ph.p_type == PT_INTERP) {
has_interp = true;
break;
}
}
}
if (has_interp && ehdr.e_entry != 0) {
oom |= !bti_push(&targets, ehdr.e_entry, 0, BTI_ENTRY);
}
for (uint64_t i = 0; i < shnum && !oom; ++i) {
const Elf64_Shdr *s = &sh[i];
switch (s->sh_type) {
case SHT_PROGBITS:
// .init and .fini hold DT_INIT/DT_FINI, which the loader
// calls through a function pointer. Identified by name;
// without a section string table they are simply not
// evidenced.
if (shstr != NULL && s->sh_name < shstrsize
&& (s->sh_flags & SHF_EXECINSTR) && s->sh_addr != 0) {
const char *name = shstr + s->sh_name;
if (strcmp(name, ".init") == 0 || strcmp(name, ".fini") == 0) {
oom |= !bti_push(&targets, s->sh_addr, 0, BTI_INIT);
}
}
break;
case SHT_PREINIT_ARRAY:
case SHT_INIT_ARRAY:
case SHT_FINI_ARRAY: {
// Each slot is a function pointer the startup code calls
// indirectly. The linker stores the link-time address in
// the slot even when a RELATIVE (or packed RELR)
// relocation also covers it, so the raw values serve PIE
// and non-PIE alike.
uint8_t *buf = load_section(f, s, file_size);
if (buf == NULL) {
break;
}
for (uint64_t off = 0; off + 8 <= s->sh_size && !oom; off += 8) {
uint64_t va;
memcpy(&va, buf + off, 8);
if (va != 0 && va != UINT64_MAX) {
oom |= !bti_push(&targets, va, 0, BTI_ARRAY);
}
}
free(buf);
break;
}
case SHT_RELA: {
if (s->sh_entsize != sizeof(Elf64_Rela)) {
break;
}
uint8_t *buf = load_section(f, s, file_size);
if (buf == NULL) {
break;
}
size_t n = s->sh_size / sizeof(Elf64_Rela);
for (size_t r = 0; r < n && !oom; ++r) {
Elf64_Rela rela;
memcpy(&rela, buf + r * sizeof(rela), sizeof(rela));
uint32_t type = ELF64_R_TYPE(rela.r_info);
uint32_t symi = ELF64_R_SYM(rela.r_info);
switch (type) {
case R_AARCH64_JUMP_SLOT:
case R_AARCH64_GLOB_DAT:
case R_AARCH64_ABS64:
// Meaningful when the symbol is defined here: the
// runtime slot value is then this object's own
// function entry. Imports resolve elsewhere. A
// symbolless absolute relocation is a baked
// address, handled like RELATIVE.
if (symi != 0 && symi < ndynsym) {
const Elf64_Sym *y = &dynsym[symi];
unsigned st = y->st_info & 0xfu;
if ((st != STT_FUNC && st != STT_GNU_IFUNC)
|| y->st_shndx == SHN_UNDEF || y->st_value == 0) {
break;
}
uint64_t va = y->st_value;
uint8_t kind = type == R_AARCH64_JUMP_SLOT ? BTI_PLT
: type == R_AARCH64_GLOB_DAT ? BTI_GOT : BTI_ADDR;
if (type == R_AARCH64_ABS64) {
va += (uint64_t)rela.r_addend;
}
oom |= !bti_push(&targets, va, symi, kind);
} else if (type == R_AARCH64_ABS64 && rela.r_addend != 0) {
oom |= !bti_push(&targets, (uint64_t)rela.r_addend, 0,
BTI_ADDR);
}
break;
case R_AARCH64_IRELATIVE:
// The addend is the ifunc resolver, which the
// loader calls indirectly at startup.
if (rela.r_addend != 0) {
oom |= !bti_push(&targets, (uint64_t)rela.r_addend, 0,
BTI_IFUNC);
}
break;
case R_AARCH64_RELATIVE:
// A baked pointer; one landing in an executable
// section is an address-taken function or label.
if (rela.r_addend != 0) {
oom |= !bti_push(&targets, (uint64_t)rela.r_addend, 0,
BTI_ADDR);
}
break;
}
}
free(buf);
break;
}
case SHT_RELR: {
if (s->sh_entsize != 8) {
break;
}
uint8_t *buf = load_section(f, s, file_size);
if (buf == NULL) {
break;
}
uint64_t cache_i = shnum;
uint8_t *cache = NULL;
uint64_t where = 0;
size_t n = s->sh_size / 8;
for (size_t r = 0; r < n && !oom; ++r) {
uint64_t e;
memcpy(&e, buf + r * 8, 8);
if ((e & 1) == 0) {
oom |= !bti_relr_slot(f, sh, shnum, file_size, e,
&cache_i, &cache, &targets);
where = e + 8;
} else {
for (unsigned b = 1; b < 64 && !oom; ++b) {
if (e & (1ULL << b)) {
oom |= !bti_relr_slot(f, sh, shnum, file_size,
where + (b - 1) * 8, &cache_i, &cache,
&targets);
}
}
where += 63 * 8;
}
}
free(cache);
free(buf);
break;
}
}
}
// Every defined dynamic function symbol is indirectly reachable:
// a cross-object call always arrives through the caller's PLT or
// GOT, an indirect branch landing here.
for (size_t s = 1; s < ndynsym && !oom; ++s) {
unsigned st = dynsym[s].st_info & 0xfu;
if ((st == STT_FUNC || st == STT_GNU_IFUNC)
&& dynsym[s].st_shndx != SHN_UNDEF && dynsym[s].st_value != 0) {
oom |= !bti_push(&targets, dynsym[s].st_value, (uint32_t)s,
BTI_EXPORT);
}
}
if (oom) {
fprintf(stderr, "%s: failed to allocate target list\n", path);
goto out;
}
if (targets.n != 0) {
qsort(targets.v, targets.n, sizeof(*targets.v), bti_target_cmp);
}
// The landing pads are read raw, never through the symbol-table
// mask. Only targets inside an executable section are checked:
// the address-relocation sweep pushes every baked pointer it
// sees, and the ones aimed at data drop out here.
exec_bufs = calloc(shnum, sizeof(*exec_bufs));
if (exec_bufs == NULL) {
fprintf(stderr, "%s: failed to allocate section list\n", path);
goto out;
}
for (uint64_t i = 0; i < shnum; ++i) {
if (sh[i].sh_type == SHT_PROGBITS && (sh[i].sh_flags & SHF_EXECINSTR)
&& (sh[i].sh_flags & SHF_ALLOC) && sh[i].sh_addr != 0) {
exec_bufs[i] = load_section(f, &sh[i], file_size);
}
}
size_t checked = 0;
size_t misses = 0;
size_t wrong = 0;
uint64_t prev_va = 0;
bool have_prev = false;
for (size_t t = 0; t < targets.n; ++t) {
uint64_t va = targets.v[t].va;
if (have_prev && va == prev_va) {
continue; // sorted dedup; the most specific kind is first
}
prev_va = va;
have_prev = true;
if (targets.v[t].kind == BTI_ADDR && !u64_contains(fentry, nfentry, va)) {
continue; // address-taken only: see enum bti_kind
}
uint64_t sec = shnum;
for (uint64_t i = 0; i < shnum; ++i) {
if (exec_bufs[i] != NULL && va >= sh[i].sh_addr
&& va - sh[i].sh_addr < sh[i].sh_size) {
sec = i;
break;
}
}
if (sec == shnum) {
continue;
}
++checked;
unsigned pad = armlint_bti_landing(exec_bufs[sec], sh[sec].sh_size,
va - sh[sec].sh_addr);
if (pad & ARMLINT_BTI_CALL) {
continue;
}
++misses;
wrong += pad != 0;
if (bti || verbose) {
const char *nm = bti_sym_name(dynsym, ndynsym, dynstr,
dynstr_size, targets.v[t].sym, va);
printf("indirect branch target %s: 0x%" PRIx64 "%s%s%s (%s)\n",
pad != 0 ? "has BTI j, which admits no call"
: "missing BTI c",
va, nm != NULL ? " <" : "", nm != NULL ? nm : "",
nm != NULL ? ">" : "", bti_kind_name[targets.v[t].kind]);
}
}
if (checked == 0) {
printf("BTI: no indirect branch targets evidenced; nothing to check\n");
rc = 0;
} else if (bti) {
if (misses == 0) {
printf("BTI: BTI property note present; all %zu indirect branch "
"targets land on a BTI c\n", checked);
} else {
printf("BTI: BTI property note present; %zu of %zu indirect "
"branch targets lack a BTI c%s (Branch Target exception "
"when BTI is enforced)\n", misses, checked,
wrong != 0 ? ", some carrying a BTI j" : "");
}
rc = (int)misses;
} else if (misses < checked) {
printf("BTI: no BTI in the property note, but %zu of %zu indirect "
"branch targets begin with a BTI c (BTI annotation lost at "
"link?)\n", checked - misses, checked);
if (misses > 0 && !verbose) {
printf(" (pass -v to list the %zu targets without one)\n",
misses);
}
rc = (int)misses + 1;
} else {
printf("BTI: no BTI in the property note and no BTI landing pads; "
"not a BTI binary\n");
rc = 0;
}
// The same word carries the pac-ret bit, and
// -mbranch-protection=standard emits both, so an asymmetric
// declaration earns a line. Informational only, never the exit
// status: pac-ret leaves a prologue in the instruction stream
// (which the -a pac audit judges, function by function), but the
// note's claim is about every input object having signed, which a
// linked binary's instruction stream cannot be held to -- a leaf
// function legitimately never signs. In the annotation-lost
// verdict the pads evidence branch protection only -- a lost note
// takes BTI and PAC down together -- so the line states the bit's
// absence without claiming a loss. GCS has no instruction-stream
// signature at all and is reported as declared.
if (bti && pac) {
printf("PAC: pac-ret declared alongside BTI\n");
} else if (bti) {
printf("PAC: no pac-ret in the property note despite BTI "
"(-mbranch-protection=bti?)\n");
} else if (pac) {
printf("PAC: pac-ret declared without BTI (pac-ret-only asm in the "
"link, or -mbranch-protection=pac-ret?)\n");
} else if (checked != 0 && misses < checked) {
printf("PAC: no pac-ret in the property note\n");
}