forked from OP-TEE/optee_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathta_elf.c
2016 lines (1700 loc) · 49.3 KB
/
ta_elf.c
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
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2019, Linaro Limited
* Copyright (c) 2020-2023, Arm Limited
*/
#include <assert.h>
#include <config.h>
#include <confine_array_index.h>
#include <elf32.h>
#include <elf64.h>
#include <elf_common.h>
#include <ldelf.h>
#include <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <string_ext.h>
#include <string.h>
#include <tee_api_types.h>
#include <tee_internal_api_extensions.h>
#include <unw/unwind.h>
#include <user_ta_header.h>
#include <util.h>
#include "sys.h"
#include "ta_elf.h"
/*
* Layout of a 32-bit struct dl_phdr_info for a 64-bit ldelf to access a 32-bit
* TA
*/
struct dl_phdr_info32 {
uint32_t dlpi_addr;
uint32_t dlpi_name;
uint32_t dlpi_phdr;
uint16_t dlpi_phnum;
uint64_t dlpi_adds;
uint64_t dlpi_subs;
uint32_t dlpi_tls_modid;
uint32_t dlpi_tls_data;
};
static vaddr_t ta_stack;
static vaddr_t ta_stack_size;
struct ta_elf_queue main_elf_queue = TAILQ_HEAD_INITIALIZER(main_elf_queue);
/*
* Main application is always ID 1, shared libraries with TLS take IDs 2 and
* above
*/
static void assign_tls_mod_id(struct ta_elf *elf)
{
static size_t last_tls_mod_id = 1;
if (elf->is_main)
assert(last_tls_mod_id == 1); /* Main always comes first */
elf->tls_mod_id = last_tls_mod_id++;
}
static struct ta_elf *queue_elf_helper(const TEE_UUID *uuid)
{
struct ta_elf *elf = calloc(1, sizeof(*elf));
if (!elf)
return NULL;
TAILQ_INIT(&elf->segs);
elf->uuid = *uuid;
TAILQ_INSERT_TAIL(&main_elf_queue, elf, link);
return elf;
}
static struct ta_elf *queue_elf(const TEE_UUID *uuid)
{
struct ta_elf *elf = ta_elf_find_elf(uuid);
if (elf)
return NULL;
elf = queue_elf_helper(uuid);
if (!elf)
err(TEE_ERROR_OUT_OF_MEMORY, "queue_elf_helper");
return elf;
}
struct ta_elf *ta_elf_find_elf(const TEE_UUID *uuid)
{
struct ta_elf *elf = NULL;
TAILQ_FOREACH(elf, &main_elf_queue, link)
if (!memcmp(uuid, &elf->uuid, sizeof(*uuid)))
return elf;
return NULL;
}
#if defined(ARM32) || defined(ARM64)
static TEE_Result e32_parse_ehdr(struct ta_elf *elf, Elf32_Ehdr *ehdr)
{
if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
(ehdr->e_ident[EI_OSABI] != ELFOSABI_NONE &&
ehdr->e_ident[EI_OSABI] != ELFOSABI_ARM) ||
ehdr->e_type != ET_DYN || ehdr->e_machine != EM_ARM ||
#ifndef CFG_WITH_VFP
(ehdr->e_flags & EF_ARM_ABI_FLOAT_HARD) ||
#endif
ehdr->e_phentsize != sizeof(Elf32_Phdr) ||
ehdr->e_shentsize != sizeof(Elf32_Shdr))
return TEE_ERROR_BAD_FORMAT;
if (ehdr->e_ident[EI_OSABI] == ELFOSABI_NONE &&
(ehdr->e_flags & EF_ARM_ABIMASK) != EF_ARM_ABI_V5)
return TEE_ERROR_BAD_FORMAT;
if (ehdr->e_ident[EI_OSABI] == ELFOSABI_ARM &&
(ehdr->e_flags & EF_ARM_ABIMASK) != EF_ARM_ABI_UNKNOWN)
return TEE_ERROR_BAD_FORMAT;
elf->is_32bit = true;
elf->e_entry = ehdr->e_entry;
elf->e_phoff = ehdr->e_phoff;
elf->e_shoff = ehdr->e_shoff;
elf->e_phnum = ehdr->e_phnum;
elf->e_shnum = ehdr->e_shnum;
elf->e_phentsize = ehdr->e_phentsize;
elf->e_shentsize = ehdr->e_shentsize;
return TEE_SUCCESS;
}
#ifdef ARM64
static TEE_Result e64_parse_ehdr(struct ta_elf *elf, Elf64_Ehdr *ehdr)
{
if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
ehdr->e_ident[EI_OSABI] != ELFOSABI_NONE ||
ehdr->e_type != ET_DYN || ehdr->e_machine != EM_AARCH64 ||
ehdr->e_flags || ehdr->e_phentsize != sizeof(Elf64_Phdr) ||
ehdr->e_shentsize != sizeof(Elf64_Shdr))
return TEE_ERROR_BAD_FORMAT;
elf->is_32bit = false;
elf->e_entry = ehdr->e_entry;
elf->e_phoff = ehdr->e_phoff;
elf->e_shoff = ehdr->e_shoff;
elf->e_phnum = ehdr->e_phnum;
elf->e_shnum = ehdr->e_shnum;
elf->e_phentsize = ehdr->e_phentsize;
elf->e_shentsize = ehdr->e_shentsize;
return TEE_SUCCESS;
}
#else /*ARM64*/
static TEE_Result e64_parse_ehdr(struct ta_elf *elf __unused,
Elf64_Ehdr *ehdr __unused)
{
return TEE_ERROR_NOT_SUPPORTED;
}
#endif /*ARM64*/
#endif /* ARM32 || ARM64 */
#if defined(RV64)
static TEE_Result e32_parse_ehdr(struct ta_elf *elf __unused,
Elf32_Ehdr *ehdr __unused)
{
return TEE_ERROR_BAD_FORMAT;
}
static TEE_Result e64_parse_ehdr(struct ta_elf *elf, Elf64_Ehdr *ehdr)
{
if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
ehdr->e_ident[EI_OSABI] != ELFOSABI_NONE ||
ehdr->e_type != ET_DYN || ehdr->e_machine != EM_RISCV ||
ehdr->e_phentsize != sizeof(Elf64_Phdr) ||
ehdr->e_shentsize != sizeof(Elf64_Shdr))
return TEE_ERROR_BAD_FORMAT;
elf->is_32bit = false;
elf->e_entry = ehdr->e_entry;
elf->e_phoff = ehdr->e_phoff;
elf->e_shoff = ehdr->e_shoff;
elf->e_phnum = ehdr->e_phnum;
elf->e_shnum = ehdr->e_shnum;
elf->e_phentsize = ehdr->e_phentsize;
elf->e_shentsize = ehdr->e_shentsize;
return TEE_SUCCESS;
}
#endif /* RV64 */
static void check_phdr_in_range(struct ta_elf *elf, unsigned int type,
vaddr_t addr, size_t memsz)
{
vaddr_t max_addr = 0;
if (ADD_OVERFLOW(addr, memsz, &max_addr))
err(TEE_ERROR_BAD_FORMAT, "Program header %#x overflow", type);
/*
* elf->load_addr and elf->max_addr are both using the
* final virtual addresses, while this program header is
* relative to 0.
*/
if (max_addr > elf->max_addr - elf->load_addr)
err(TEE_ERROR_BAD_FORMAT, "Program header %#x out of bounds",
type);
}
static void read_dyn(struct ta_elf *elf, vaddr_t addr,
size_t idx, unsigned int *tag, size_t *val)
{
if (elf->is_32bit) {
Elf32_Dyn *dyn = (Elf32_Dyn *)(addr + elf->load_addr);
*tag = dyn[idx].d_tag;
*val = dyn[idx].d_un.d_val;
} else {
Elf64_Dyn *dyn = (Elf64_Dyn *)(addr + elf->load_addr);
*tag = dyn[idx].d_tag;
*val = dyn[idx].d_un.d_val;
}
}
static void check_range(struct ta_elf *elf, const char *name, const void *ptr,
size_t sz)
{
size_t max_addr = 0;
if ((vaddr_t)ptr < elf->load_addr)
err(TEE_ERROR_BAD_FORMAT, "%s %p out of range", name, ptr);
if (ADD_OVERFLOW((vaddr_t)ptr, sz, &max_addr))
err(TEE_ERROR_BAD_FORMAT, "%s range overflow", name);
if (max_addr > elf->max_addr)
err(TEE_ERROR_BAD_FORMAT,
"%s %p..%#zx out of range", name, ptr, max_addr);
}
static void check_hashtab(struct ta_elf *elf, void *ptr, size_t num_buckets,
size_t num_chains)
{
/*
* Starting from 2 as the first two words are mandatory and hold
* num_buckets and num_chains. So this function is called twice,
* first to see that there's indeed room for num_buckets and
* num_chains and then to see that all of it fits.
* See http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash
*/
size_t num_words = 2;
size_t sz = 0;
if (!IS_ALIGNED_WITH_TYPE(ptr, uint32_t))
err(TEE_ERROR_BAD_FORMAT, "Bad alignment of DT_HASH %p", ptr);
if (ADD_OVERFLOW(num_words, num_buckets, &num_words) ||
ADD_OVERFLOW(num_words, num_chains, &num_words) ||
MUL_OVERFLOW(num_words, sizeof(uint32_t), &sz))
err(TEE_ERROR_BAD_FORMAT, "DT_HASH overflow");
check_range(elf, "DT_HASH", ptr, sz);
}
static void check_gnu_hashtab(struct ta_elf *elf, void *ptr)
{
struct gnu_hashtab *h = ptr;
size_t num_words = 4; /* nbuckets, symoffset, bloom_size, bloom_shift */
size_t bloom_words = 0;
size_t sz = 0;
if (!IS_ALIGNED_WITH_TYPE(ptr, uint32_t))
err(TEE_ERROR_BAD_FORMAT, "Bad alignment of DT_GNU_HASH %p",
ptr);
if (elf->gnu_hashtab_size < sizeof(*h))
err(TEE_ERROR_BAD_FORMAT, "DT_GNU_HASH too small");
/* Check validity of h->nbuckets and h->bloom_size */
if (elf->is_32bit)
bloom_words = h->bloom_size;
else
bloom_words = h->bloom_size * 2;
if (ADD_OVERFLOW(num_words, h->nbuckets, &num_words) ||
ADD_OVERFLOW(num_words, bloom_words, &num_words) ||
MUL_OVERFLOW(num_words, sizeof(uint32_t), &sz) ||
sz > elf->gnu_hashtab_size)
err(TEE_ERROR_BAD_FORMAT, "DT_GNU_HASH overflow");
}
static void save_hashtab(struct ta_elf *elf)
{
uint32_t *hashtab = NULL;
size_t n = 0;
if (elf->is_32bit) {
Elf32_Shdr *shdr = elf->shdr;
for (n = 0; n < elf->e_shnum; n++) {
void *addr = (void *)(vaddr_t)(shdr[n].sh_addr +
elf->load_addr);
if (shdr[n].sh_type == SHT_HASH) {
elf->hashtab = addr;
} else if (shdr[n].sh_type == SHT_GNU_HASH) {
elf->gnu_hashtab = addr;
elf->gnu_hashtab_size = shdr[n].sh_size;
}
}
} else {
Elf64_Shdr *shdr = elf->shdr;
for (n = 0; n < elf->e_shnum; n++) {
void *addr = (void *)(vaddr_t)(shdr[n].sh_addr +
elf->load_addr);
if (shdr[n].sh_type == SHT_HASH) {
elf->hashtab = addr;
} else if (shdr[n].sh_type == SHT_GNU_HASH) {
elf->gnu_hashtab = addr;
elf->gnu_hashtab_size = shdr[n].sh_size;
}
}
}
if (elf->hashtab) {
check_hashtab(elf, elf->hashtab, 0, 0);
hashtab = elf->hashtab;
check_hashtab(elf, elf->hashtab, hashtab[0], hashtab[1]);
}
if (elf->gnu_hashtab)
check_gnu_hashtab(elf, elf->gnu_hashtab);
}
static void save_soname_from_segment(struct ta_elf *elf, unsigned int type,
vaddr_t addr, size_t memsz)
{
size_t dyn_entsize = 0;
size_t num_dyns = 0;
size_t n = 0;
unsigned int tag = 0;
size_t val = 0;
char *str_tab = NULL;
if (type != PT_DYNAMIC)
return;
if (elf->is_32bit)
dyn_entsize = sizeof(Elf32_Dyn);
else
dyn_entsize = sizeof(Elf64_Dyn);
assert(!(memsz % dyn_entsize));
num_dyns = memsz / dyn_entsize;
for (n = 0; n < num_dyns; n++) {
read_dyn(elf, addr, n, &tag, &val);
if (tag == DT_STRTAB) {
str_tab = (char *)(val + elf->load_addr);
break;
}
}
for (n = 0; n < num_dyns; n++) {
read_dyn(elf, addr, n, &tag, &val);
if (tag == DT_SONAME) {
elf->soname = str_tab + val;
break;
}
}
}
static void save_soname(struct ta_elf *elf)
{
size_t n = 0;
if (elf->is_32bit) {
Elf32_Phdr *phdr = elf->phdr;
for (n = 0; n < elf->e_phnum; n++)
save_soname_from_segment(elf, phdr[n].p_type,
phdr[n].p_vaddr,
phdr[n].p_memsz);
} else {
Elf64_Phdr *phdr = elf->phdr;
for (n = 0; n < elf->e_phnum; n++)
save_soname_from_segment(elf, phdr[n].p_type,
phdr[n].p_vaddr,
phdr[n].p_memsz);
}
}
static void e32_save_symtab(struct ta_elf *elf, size_t tab_idx)
{
Elf32_Shdr *shdr = elf->shdr;
size_t str_idx = shdr[tab_idx].sh_link;
elf->dynsymtab = (void *)(shdr[tab_idx].sh_addr + elf->load_addr);
if (!IS_ALIGNED_WITH_TYPE(elf->dynsymtab, Elf32_Sym))
err(TEE_ERROR_BAD_FORMAT, "Bad alignment of dynsymtab %p",
elf->dynsymtab);
check_range(elf, "Dynsymtab", elf->dynsymtab, shdr[tab_idx].sh_size);
if (shdr[tab_idx].sh_size % sizeof(Elf32_Sym))
err(TEE_ERROR_BAD_FORMAT,
"Size of dynsymtab not an even multiple of Elf32_Sym");
elf->num_dynsyms = shdr[tab_idx].sh_size / sizeof(Elf32_Sym);
if (str_idx >= elf->e_shnum)
err(TEE_ERROR_BAD_FORMAT, "Dynstr section index out of range");
elf->dynstr = (void *)(shdr[str_idx].sh_addr + elf->load_addr);
check_range(elf, "Dynstr", elf->dynstr, shdr[str_idx].sh_size);
elf->dynstr_size = shdr[str_idx].sh_size;
}
static void e64_save_symtab(struct ta_elf *elf, size_t tab_idx)
{
Elf64_Shdr *shdr = elf->shdr;
size_t str_idx = shdr[tab_idx].sh_link;
elf->dynsymtab = (void *)(vaddr_t)(shdr[tab_idx].sh_addr +
elf->load_addr);
if (!IS_ALIGNED_WITH_TYPE(elf->dynsymtab, Elf64_Sym))
err(TEE_ERROR_BAD_FORMAT, "Bad alignment of .dynsym/DYNSYM %p",
elf->dynsymtab);
check_range(elf, ".dynsym/DYNSYM", elf->dynsymtab,
shdr[tab_idx].sh_size);
if (shdr[tab_idx].sh_size % sizeof(Elf64_Sym))
err(TEE_ERROR_BAD_FORMAT,
"Size of .dynsym/DYNSYM not an even multiple of Elf64_Sym");
elf->num_dynsyms = shdr[tab_idx].sh_size / sizeof(Elf64_Sym);
if (str_idx >= elf->e_shnum)
err(TEE_ERROR_BAD_FORMAT,
".dynstr/STRTAB section index out of range");
elf->dynstr = (void *)(vaddr_t)(shdr[str_idx].sh_addr + elf->load_addr);
check_range(elf, ".dynstr/STRTAB", elf->dynstr, shdr[str_idx].sh_size);
elf->dynstr_size = shdr[str_idx].sh_size;
}
static void save_symtab(struct ta_elf *elf)
{
size_t n = 0;
if (elf->is_32bit) {
Elf32_Shdr *shdr = elf->shdr;
for (n = 0; n < elf->e_shnum; n++) {
if (shdr[n].sh_type == SHT_DYNSYM) {
e32_save_symtab(elf, n);
break;
}
}
} else {
Elf64_Shdr *shdr = elf->shdr;
for (n = 0; n < elf->e_shnum; n++) {
if (shdr[n].sh_type == SHT_DYNSYM) {
e64_save_symtab(elf, n);
break;
}
}
}
save_hashtab(elf);
save_soname(elf);
}
static void init_elf(struct ta_elf *elf)
{
TEE_Result res = TEE_SUCCESS;
vaddr_t va = 0;
uint32_t flags = LDELF_MAP_FLAG_SHAREABLE;
size_t sz = 0;
res = sys_open_ta_bin(&elf->uuid, &elf->handle);
if (res)
err(res, "sys_open_ta_bin(%pUl)", (void *)&elf->uuid);
/*
* Map it read-only executable when we're loading a library where
* the ELF header is included in a load segment.
*/
if (!elf->is_main)
flags |= LDELF_MAP_FLAG_EXECUTABLE;
res = sys_map_ta_bin(&va, SMALL_PAGE_SIZE, flags, elf->handle, 0, 0, 0);
if (res)
err(res, "sys_map_ta_bin");
elf->ehdr_addr = va;
if (!elf->is_main) {
elf->load_addr = va;
elf->max_addr = va + SMALL_PAGE_SIZE;
elf->max_offs = SMALL_PAGE_SIZE;
}
if (!IS_ELF(*(Elf32_Ehdr *)va))
err(TEE_ERROR_BAD_FORMAT, "TA is not an ELF");
res = e32_parse_ehdr(elf, (void *)va);
if (res == TEE_ERROR_BAD_FORMAT)
res = e64_parse_ehdr(elf, (void *)va);
if (res)
err(res, "Cannot parse ELF");
if (MUL_OVERFLOW(elf->e_phnum, elf->e_phentsize, &sz) ||
ADD_OVERFLOW(sz, elf->e_phoff, &sz))
err(TEE_ERROR_BAD_FORMAT, "Program headers size overflow");
if (sz > SMALL_PAGE_SIZE)
err(TEE_ERROR_NOT_SUPPORTED, "Cannot read program headers");
elf->phdr = (void *)(va + elf->e_phoff);
}
static size_t roundup(size_t v)
{
return ROUNDUP(v, SMALL_PAGE_SIZE);
}
static size_t rounddown(size_t v)
{
return ROUNDDOWN(v, SMALL_PAGE_SIZE);
}
static void add_segment(struct ta_elf *elf, size_t offset, size_t vaddr,
size_t filesz, size_t memsz, size_t flags, size_t align)
{
struct segment *seg = calloc(1, sizeof(*seg));
if (!seg)
err(TEE_ERROR_OUT_OF_MEMORY, "calloc");
if (memsz < filesz)
err(TEE_ERROR_BAD_FORMAT, "Memsz smaller than filesz");
seg->offset = offset;
seg->vaddr = vaddr;
seg->filesz = filesz;
seg->memsz = memsz;
seg->flags = flags;
seg->align = align;
TAILQ_INSERT_TAIL(&elf->segs, seg, link);
}
static void parse_load_segments(struct ta_elf *elf)
{
size_t n = 0;
if (elf->is_32bit) {
Elf32_Phdr *phdr = elf->phdr;
for (n = 0; n < elf->e_phnum; n++)
if (phdr[n].p_type == PT_LOAD) {
add_segment(elf, phdr[n].p_offset,
phdr[n].p_vaddr, phdr[n].p_filesz,
phdr[n].p_memsz, phdr[n].p_flags,
phdr[n].p_align);
} else if (phdr[n].p_type == PT_ARM_EXIDX) {
elf->exidx_start = phdr[n].p_vaddr;
elf->exidx_size = phdr[n].p_filesz;
} else if (phdr[n].p_type == PT_TLS) {
assign_tls_mod_id(elf);
}
} else {
Elf64_Phdr *phdr = elf->phdr;
for (n = 0; n < elf->e_phnum; n++)
if (phdr[n].p_type == PT_LOAD) {
add_segment(elf, phdr[n].p_offset,
phdr[n].p_vaddr, phdr[n].p_filesz,
phdr[n].p_memsz, phdr[n].p_flags,
phdr[n].p_align);
} else if (phdr[n].p_type == PT_TLS) {
elf->tls_start = phdr[n].p_vaddr;
elf->tls_filesz = phdr[n].p_filesz;
elf->tls_memsz = phdr[n].p_memsz;
} else if (IS_ENABLED(CFG_TA_BTI) &&
phdr[n].p_type == PT_GNU_PROPERTY) {
elf->prop_start = phdr[n].p_vaddr;
elf->prop_align = phdr[n].p_align;
elf->prop_memsz = phdr[n].p_memsz;
}
}
}
static void copy_remapped_to(struct ta_elf *elf, const struct segment *seg)
{
uint8_t *dst = (void *)(seg->vaddr + elf->load_addr);
size_t n = 0;
size_t offs = seg->offset;
size_t num_bytes = seg->filesz;
if (offs < elf->max_offs) {
n = MIN(elf->max_offs - offs, num_bytes);
memcpy(dst, (void *)(elf->max_addr + offs - elf->max_offs), n);
dst += n;
offs += n;
num_bytes -= n;
}
if (num_bytes) {
TEE_Result res = sys_copy_from_ta_bin(dst, num_bytes,
elf->handle, offs);
if (res)
err(res, "sys_copy_from_ta_bin");
elf->max_offs += offs;
}
}
static void adjust_segments(struct ta_elf *elf)
{
struct segment *seg = NULL;
struct segment *prev_seg = NULL;
size_t prev_end_addr = 0;
size_t align = 0;
size_t mask = 0;
/* Sanity check */
TAILQ_FOREACH(seg, &elf->segs, link) {
size_t dummy __maybe_unused = 0;
assert(seg->align >= SMALL_PAGE_SIZE);
assert(!ADD_OVERFLOW(seg->vaddr, seg->memsz, &dummy));
assert(seg->filesz <= seg->memsz);
assert((seg->offset & SMALL_PAGE_MASK) ==
(seg->vaddr & SMALL_PAGE_MASK));
prev_seg = TAILQ_PREV(seg, segment_head, link);
if (prev_seg) {
assert(seg->vaddr >= prev_seg->vaddr + prev_seg->memsz);
assert(seg->offset >=
prev_seg->offset + prev_seg->filesz);
}
if (!align)
align = seg->align;
assert(align == seg->align);
}
mask = align - 1;
seg = TAILQ_FIRST(&elf->segs);
if (seg)
seg = TAILQ_NEXT(seg, link);
while (seg) {
prev_seg = TAILQ_PREV(seg, segment_head, link);
prev_end_addr = prev_seg->vaddr + prev_seg->memsz;
/*
* This segment may overlap with the last "page" in the
* previous segment in two different ways:
* 1. Virtual address (and offset) overlaps =>
* Permissions needs to be merged. The offset must have
* the SMALL_PAGE_MASK bits set as vaddr and offset must
* add up with prevsion segment.
*
* 2. Only offset overlaps =>
* The same page in the ELF is mapped at two different
* virtual addresses. As a limitation this segment must
* be mapped as writeable.
*/
/* Case 1. */
if (rounddown(seg->vaddr) < prev_end_addr) {
assert((seg->vaddr & mask) == (seg->offset & mask));
assert(prev_seg->memsz == prev_seg->filesz);
/*
* Merge the segments and their permissions.
* Note that the may be a small hole between the
* two sections.
*/
prev_seg->filesz = seg->vaddr + seg->filesz -
prev_seg->vaddr;
prev_seg->memsz = seg->vaddr + seg->memsz -
prev_seg->vaddr;
prev_seg->flags |= seg->flags;
TAILQ_REMOVE(&elf->segs, seg, link);
free(seg);
seg = TAILQ_NEXT(prev_seg, link);
continue;
}
/* Case 2. */
if ((seg->offset & mask) &&
rounddown(seg->offset) <
(prev_seg->offset + prev_seg->filesz)) {
assert(seg->flags & PF_W);
seg->remapped_writeable = true;
}
/*
* No overlap, but we may need to align address, offset and
* size.
*/
seg->filesz += seg->vaddr - rounddown(seg->vaddr);
seg->memsz += seg->vaddr - rounddown(seg->vaddr);
seg->vaddr = rounddown(seg->vaddr);
seg->offset = rounddown(seg->offset);
seg = TAILQ_NEXT(seg, link);
}
}
static void populate_segments_legacy(struct ta_elf *elf)
{
TEE_Result res = TEE_SUCCESS;
struct segment *seg = NULL;
vaddr_t va = 0;
assert(elf->is_legacy);
TAILQ_FOREACH(seg, &elf->segs, link) {
struct segment *last_seg = TAILQ_LAST(&elf->segs, segment_head);
size_t pad_end = roundup(last_seg->vaddr + last_seg->memsz -
seg->vaddr - seg->memsz);
size_t num_bytes = roundup(seg->memsz);
if (!elf->load_addr)
va = 0;
else
va = seg->vaddr + elf->load_addr;
if (!(seg->flags & PF_R))
err(TEE_ERROR_NOT_SUPPORTED,
"Segment must be readable");
res = sys_map_zi(num_bytes, 0, &va, 0, pad_end);
if (res)
err(res, "sys_map_zi");
res = sys_copy_from_ta_bin((void *)va, seg->filesz,
elf->handle, seg->offset);
if (res)
err(res, "sys_copy_from_ta_bin");
if (!elf->load_addr)
elf->load_addr = va;
elf->max_addr = va + num_bytes;
elf->max_offs = seg->offset + seg->filesz;
}
}
static size_t get_pad_begin(void)
{
#ifdef CFG_TA_ASLR
size_t min = CFG_TA_ASLR_MIN_OFFSET_PAGES;
size_t max = CFG_TA_ASLR_MAX_OFFSET_PAGES;
TEE_Result res = TEE_SUCCESS;
uint32_t rnd32 = 0;
size_t rnd = 0;
COMPILE_TIME_ASSERT(CFG_TA_ASLR_MIN_OFFSET_PAGES <
CFG_TA_ASLR_MAX_OFFSET_PAGES);
if (max > min) {
res = sys_gen_random_num(&rnd32, sizeof(rnd32));
if (res) {
DMSG("Random read failed: %#"PRIx32, res);
return min * SMALL_PAGE_SIZE;
}
rnd = rnd32 % (max - min);
}
return (min + rnd) * SMALL_PAGE_SIZE;
#else /*!CFG_TA_ASLR*/
return 0;
#endif /*!CFG_TA_ASLR*/
}
static void populate_segments(struct ta_elf *elf)
{
TEE_Result res = TEE_SUCCESS;
struct segment *seg = NULL;
vaddr_t va = 0;
size_t pad_begin = 0;
assert(!elf->is_legacy);
TAILQ_FOREACH(seg, &elf->segs, link) {
struct segment *last_seg = TAILQ_LAST(&elf->segs, segment_head);
size_t pad_end = roundup(last_seg->vaddr + last_seg->memsz -
seg->vaddr - seg->memsz);
if (seg->remapped_writeable) {
size_t num_bytes = roundup(seg->vaddr + seg->memsz) -
rounddown(seg->vaddr);
assert(elf->load_addr);
va = rounddown(elf->load_addr + seg->vaddr);
assert(va >= elf->max_addr);
res = sys_map_zi(num_bytes, 0, &va, 0, pad_end);
if (res)
err(res, "sys_map_zi");
copy_remapped_to(elf, seg);
elf->max_addr = va + num_bytes;
} else {
uint32_t flags = 0;
size_t filesz = seg->filesz;
size_t memsz = seg->memsz;
size_t offset = seg->offset;
size_t vaddr = seg->vaddr;
if (offset < elf->max_offs) {
/*
* We're in a load segment which overlaps
* with (or is covered by) the first page
* of a shared library.
*/
if (vaddr + filesz < SMALL_PAGE_SIZE) {
size_t num_bytes = 0;
/*
* If this segment is completely
* covered, take next.
*/
if (vaddr + memsz <= SMALL_PAGE_SIZE)
continue;
/*
* All data of the segment is
* loaded, but we need to zero
* extend it.
*/
va = elf->max_addr;
num_bytes = roundup(vaddr + memsz) -
roundup(vaddr) -
SMALL_PAGE_SIZE;
assert(num_bytes);
res = sys_map_zi(num_bytes, 0, &va, 0,
0);
if (res)
err(res, "sys_map_zi");
elf->max_addr = roundup(va + num_bytes);
continue;
}
/* Partial overlap, remove the first page. */
vaddr += SMALL_PAGE_SIZE;
filesz -= SMALL_PAGE_SIZE;
memsz -= SMALL_PAGE_SIZE;
offset += SMALL_PAGE_SIZE;
}
if (!elf->load_addr) {
va = 0;
pad_begin = get_pad_begin();
/*
* If mapping with pad_begin fails we'll
* retry without pad_begin, effectively
* disabling ASLR for the current ELF file.
*/
} else {
va = vaddr + elf->load_addr;
pad_begin = 0;
}
if (seg->flags & PF_W)
flags |= LDELF_MAP_FLAG_WRITEABLE;
else
flags |= LDELF_MAP_FLAG_SHAREABLE;
if (seg->flags & PF_X)
flags |= LDELF_MAP_FLAG_EXECUTABLE;
if (!(seg->flags & PF_R))
err(TEE_ERROR_NOT_SUPPORTED,
"Segment must be readable");
if (flags & LDELF_MAP_FLAG_WRITEABLE) {
res = sys_map_zi(memsz, 0, &va, pad_begin,
pad_end);
if (pad_begin && res == TEE_ERROR_OUT_OF_MEMORY)
res = sys_map_zi(memsz, 0, &va, 0,
pad_end);
if (res)
err(res, "sys_map_zi");
res = sys_copy_from_ta_bin((void *)va, filesz,
elf->handle, offset);
if (res)
err(res, "sys_copy_from_ta_bin");
} else {
if (filesz != memsz)
err(TEE_ERROR_BAD_FORMAT,
"Filesz and memsz mismatch");
res = sys_map_ta_bin(&va, filesz, flags,
elf->handle, offset,
pad_begin, pad_end);
if (pad_begin && res == TEE_ERROR_OUT_OF_MEMORY)
res = sys_map_ta_bin(&va, filesz, flags,
elf->handle,
offset, 0,
pad_end);
if (res)
err(res, "sys_map_ta_bin");
}
if (!elf->load_addr)
elf->load_addr = va;
elf->max_addr = roundup(va + memsz);
elf->max_offs += filesz;
}
}
}
static void ta_elf_add_bti(struct ta_elf *elf)
{
TEE_Result res = TEE_SUCCESS;
struct segment *seg = NULL;
uint32_t flags = LDELF_MAP_FLAG_EXECUTABLE | LDELF_MAP_FLAG_BTI;
TAILQ_FOREACH(seg, &elf->segs, link) {
vaddr_t va = elf->load_addr + seg->vaddr;
if (seg->flags & PF_X) {
res = sys_set_prot(va, seg->memsz, flags);
if (res)
err(res, "sys_set_prot");
}
}
}
static void parse_property_segment(struct ta_elf *elf)
{
char *desc = NULL;
size_t align = elf->prop_align;
size_t desc_offset = 0;
size_t prop_offset = 0;
vaddr_t va = 0;
Elf_Note *note = NULL;
char *name = NULL;
if (!IS_ENABLED(CFG_TA_BTI) || !elf->prop_start)
return;
check_phdr_in_range(elf, PT_GNU_PROPERTY, elf->prop_start,
elf->prop_memsz);
va = elf->load_addr + elf->prop_start;
note = (void *)va;
name = (char *)(note + 1);
if (elf->prop_memsz < sizeof(*note) + sizeof(ELF_NOTE_GNU))
return;
if (note->n_type != NT_GNU_PROPERTY_TYPE_0 ||
note->n_namesz != sizeof(ELF_NOTE_GNU) ||
memcmp(name, ELF_NOTE_GNU, sizeof(ELF_NOTE_GNU)) ||
!IS_POWER_OF_TWO(align))
return;
desc_offset = ROUNDUP(sizeof(*note) + sizeof(ELF_NOTE_GNU), align);
if (desc_offset > elf->prop_memsz ||
ROUNDUP(desc_offset + note->n_descsz, align) > elf->prop_memsz)
return;
desc = (char *)(va + desc_offset);
do {
Elf_Prop *prop = (void *)(desc + prop_offset);
size_t data_offset = prop_offset + sizeof(*prop);
if (note->n_descsz < data_offset)
return;
data_offset = confine_array_index(data_offset, note->n_descsz);
if (prop->pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
uint32_t *pr_data = (void *)(desc + data_offset);
if (note->n_descsz < (data_offset + sizeof(*pr_data)) &&
prop->pr_datasz != sizeof(*pr_data))
return;
if (*pr_data & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) {
DMSG("BTI Feature present in note property");
elf->bti_enabled = true;
}
}
prop_offset += ROUNDUP(sizeof(*prop) + prop->pr_datasz, align);
} while (prop_offset < note->n_descsz);
}
static void map_segments(struct ta_elf *elf)
{