-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathmach_loader.c
3910 lines (3512 loc) · 106 KB
/
mach_loader.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
/*
* Copyright (c) 2000-2020 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* Copyright (C) 1988, 1989, NeXT, Inc.
*
* File: kern/mach_loader.c
* Author: Avadis Tevanian, Jr.
*
* Mach object file loader (kernel version, for now).
*
* 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
* Started.
*/
#include <sys/param.h>
#include <sys/vnode_internal.h>
#include <sys/uio.h>
#include <sys/namei.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/mount_internal.h>
#include <sys/fcntl.h>
#include <sys/file_internal.h>
#include <sys/ubc_internal.h>
#include <sys/imgact.h>
#include <sys/codesign.h>
#include <sys/proc_uuid_policy.h>
#include <sys/reason.h>
#include <sys/kdebug.h>
#include <sys/spawn_internal.h>
#include <mach/mach_types.h>
#include <mach/vm_map.h> /* vm_allocate() */
#include <mach/mach_vm.h> /* mach_vm_allocate() */
#include <mach/vm_statistics.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <machine/vmparam.h>
#include <machine/exec.h>
#include <machine/pal_routines.h>
#include <kern/ast.h>
#include <kern/kern_types.h>
#include <kern/cpu_number.h>
#include <kern/mach_loader.h>
#include <kern/mach_fat.h>
#include <kern/kalloc.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/page_decrypt.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <vm/vm_pager.h>
#include <vm/vnode_pager.h>
#include <vm/vm_protos.h>
#include <vm/vm_shared_region.h>
#include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
#include <IOKit/IOBSD.h> /* for IOVnodeHasEntitlement */
#include <os/log.h>
#include <os/overflow.h>
#include "kern_exec_internal.h"
/* XXX should have prototypes in a shared header file */
extern int get_map_nentries(vm_map_t);
extern kern_return_t memory_object_signed(memory_object_control_t control,
boolean_t is_signed);
/* An empty load_result_t */
static const load_result_t load_result_null = {
.mach_header = MACH_VM_MIN_ADDRESS,
.entry_point = MACH_VM_MIN_ADDRESS,
.user_stack = MACH_VM_MIN_ADDRESS,
.user_stack_size = 0,
.user_stack_alloc = MACH_VM_MIN_ADDRESS,
.user_stack_alloc_size = 0,
.all_image_info_addr = MACH_VM_MIN_ADDRESS,
.all_image_info_size = 0,
.thread_count = 0,
.unixproc = 0,
.dynlinker = 0,
.needs_dynlinker = 0,
.validentry = 0,
.using_lcmain = 0,
.is_64bit_addr = 0,
.is_64bit_data = 0,
.custom_stack = 0,
.csflags = 0,
.has_pagezero = 0,
.uuid = { 0 },
.min_vm_addr = MACH_VM_MAX_ADDRESS,
.max_vm_addr = MACH_VM_MIN_ADDRESS,
.ro_vm_start = MACH_VM_MIN_ADDRESS,
.ro_vm_end = MACH_VM_MIN_ADDRESS,
.cs_end_offset = 0,
.threadstate = NULL,
.threadstate_sz = 0,
.is_rosetta = 0,
.dynlinker_ro_vm_start = 0,
.dynlinker_ro_vm_end = 0,
.dynlinker_mach_header = MACH_VM_MIN_ADDRESS,
.dynlinker_fd = -1,
};
/*
* Prototypes of static functions.
*/
static load_return_t
parse_machfile(
struct vnode *vp,
vm_map_t map,
thread_t thread,
struct mach_header *header,
off_t file_offset,
off_t macho_size,
int depth,
int64_t slide,
int64_t dyld_slide,
load_result_t *result,
load_result_t *binresult,
struct image_params *imgp
);
static load_return_t
load_segment(
struct load_command *lcp,
uint32_t filetype,
void *control,
off_t pager_offset,
off_t macho_size,
struct vnode *vp,
vm_map_t map,
int64_t slide,
load_result_t *result,
struct image_params *imgp
);
static load_return_t
load_uuid(
struct uuid_command *uulp,
char *command_end,
load_result_t *result
);
static load_return_t
load_version(
struct version_min_command *vmc,
boolean_t *found_version_cmd,
struct image_params *imgp,
load_result_t *result
);
static load_return_t
load_code_signature(
struct linkedit_data_command *lcp,
struct vnode *vp,
off_t macho_offset,
off_t macho_size,
cpu_type_t cputype,
cpu_subtype_t cpusubtype,
load_result_t *result,
struct image_params *imgp);
#if CONFIG_CODE_DECRYPTION
static load_return_t
set_code_unprotect(
struct encryption_info_command *lcp,
caddr_t addr,
vm_map_t map,
int64_t slide,
struct vnode *vp,
off_t macho_offset,
cpu_type_t cputype,
cpu_subtype_t cpusubtype);
#endif
static
load_return_t
load_main(
struct entry_point_command *epc,
thread_t thread,
int64_t slide,
load_result_t *result
);
static
load_return_t
setup_driver_main(
thread_t thread,
int64_t slide,
load_result_t *result
);
static load_return_t
load_unixthread(
struct thread_command *tcp,
thread_t thread,
int64_t slide,
boolean_t is_x86_64_compat_binary,
load_result_t *result
);
static load_return_t
load_threadstate(
thread_t thread,
uint32_t *ts,
uint32_t total_size,
load_result_t *
);
static load_return_t
load_threadstack(
thread_t thread,
uint32_t *ts,
uint32_t total_size,
mach_vm_offset_t *user_stack,
int *customstack,
boolean_t is_x86_64_compat_binary,
load_result_t *result
);
static load_return_t
load_threadentry(
thread_t thread,
uint32_t *ts,
uint32_t total_size,
mach_vm_offset_t *entry_point
);
static load_return_t
load_dylinker(
struct dylinker_command *lcp,
integer_t archbits,
vm_map_t map,
thread_t thread,
int depth,
int64_t slide,
load_result_t *result,
struct image_params *imgp
);
#if CONFIG_ROSETTA
static load_return_t
load_rosetta(
vm_map_t map,
thread_t thread,
load_result_t *result,
struct image_params *imgp
);
#endif
#if __x86_64__
extern int bootarg_no32exec;
static boolean_t
check_if_simulator_binary(
struct image_params *imgp,
off_t file_offset,
off_t macho_size);
#endif
struct macho_data;
static load_return_t
get_macho_vnode(
const char *path,
integer_t archbits,
struct mach_header *mach_header,
off_t *file_offset,
off_t *macho_size,
struct macho_data *macho_data,
struct vnode **vpp,
struct image_params *imgp
);
static inline void
widen_segment_command(const struct segment_command *scp32,
struct segment_command_64 *scp)
{
scp->cmd = scp32->cmd;
scp->cmdsize = scp32->cmdsize;
bcopy(scp32->segname, scp->segname, sizeof(scp->segname));
scp->vmaddr = scp32->vmaddr;
scp->vmsize = scp32->vmsize;
scp->fileoff = scp32->fileoff;
scp->filesize = scp32->filesize;
scp->maxprot = scp32->maxprot;
scp->initprot = scp32->initprot;
scp->nsects = scp32->nsects;
scp->flags = scp32->flags;
}
static void
note_all_image_info_section(const struct segment_command_64 *scp,
boolean_t is64, size_t section_size, const void *sections,
int64_t slide, load_result_t *result)
{
const union {
struct section s32;
struct section_64 s64;
} *sectionp;
unsigned int i;
if (strncmp(scp->segname, "__DATA_DIRTY", sizeof(scp->segname)) != 0 &&
strncmp(scp->segname, "__DATA", sizeof(scp->segname)) != 0) {
return;
}
for (i = 0; i < scp->nsects; ++i) {
sectionp = (const void *)
((const char *)sections + section_size * i);
if (0 == strncmp(sectionp->s64.sectname, "__all_image_info",
sizeof(sectionp->s64.sectname))) {
result->all_image_info_addr =
is64 ? sectionp->s64.addr : sectionp->s32.addr;
result->all_image_info_addr += slide;
result->all_image_info_size =
is64 ? sectionp->s64.size : sectionp->s32.size;
return;
}
}
}
#if __arm64__
/*
* Allow bypassing some security rules (hard pagezero, no write+execute)
* in exchange for better binary compatibility for legacy apps built
* before 16KB-alignment was enforced.
*/
const int fourk_binary_compatibility_unsafe = TRUE;
const int fourk_binary_compatibility_allow_wx = FALSE;
#endif /* __arm64__ */
#if __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX
/**
* Determines whether this is an arm64e process which may host in-process
* plugins.
*/
static inline bool
arm64e_plugin_host(struct image_params *imgp, load_result_t *result)
{
if (imgp->ip_flags & IMGPF_NOJOP) {
return false;
}
if (!result->platform_binary) {
return false;
}
struct cs_blob *csblob = csvnode_get_blob(imgp->ip_vp, imgp->ip_arch_offset);
const char *identity = csblob_get_identity(csblob);
if (!identity) {
return false;
}
/* Check if override host plugin entitlement is present and posix spawn attribute to disable A keys is passed */
if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, OVERRIDE_PLUGIN_HOST_ENTITLEMENT)) {
bool ret = imgp->ip_flags & IMGPF_PLUGIN_HOST_DISABLE_A_KEYS;
if (ret) {
proc_t p = vfs_context_proc(imgp->ip_vfs_context);
set_proc_name(imgp, p);
os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to posix_spawnattr_disable_ptr_auth_a_keys_np", __func__, p->p_name);
}
return ret;
}
/* Disabling library validation is a good signal that this process plans to host plugins */
const char *const disable_lv_entitlements[] = {
"com.apple.security.cs.disable-library-validation",
"com.apple.private.cs.automator-plugins",
CLEAR_LV_ENTITLEMENT,
};
for (size_t i = 0; i < ARRAY_COUNT(disable_lv_entitlements); i++) {
const char *entitlement = disable_lv_entitlements[i];
if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, entitlement)) {
proc_t p = vfs_context_proc(imgp->ip_vfs_context);
set_proc_name(imgp, p);
os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to entitlement: %s", __func__, p->p_name, entitlement);
return true;
}
}
/* From /System/Library/Security/HardeningExceptions.plist */
const char *const hardening_exceptions[] = {
"com.apple.perl5", /* Scripting engines may load third party code and jit*/
"com.apple.perl", /* Scripting engines may load third party code and jit*/
"org.python.python", /* Scripting engines may load third party code and jit*/
"com.apple.expect", /* Scripting engines may load third party code and jit*/
"com.tcltk.wish", /* Scripting engines may load third party code and jit*/
"com.tcltk.tclsh", /* Scripting engines may load third party code and jit*/
"com.apple.ruby", /* Scripting engines may load third party code and jit*/
"com.apple.bash", /* Required for the 'enable' command */
"com.apple.zsh", /* Required for the 'zmodload' command */
"com.apple.ksh", /* Required for 'builtin' command */
};
for (size_t i = 0; i < ARRAY_COUNT(hardening_exceptions); i++) {
if (strncmp(hardening_exceptions[i], identity, strlen(hardening_exceptions[i])) == 0) {
proc_t p = vfs_context_proc(imgp->ip_vfs_context);
set_proc_name(imgp, p);
os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to identity: %s", __func__, p->p_name, identity);
return true;
}
}
return false;
}
#endif /* __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX */
load_return_t
load_machfile(
struct image_params *imgp,
struct mach_header *header,
thread_t thread,
vm_map_t *mapp,
load_result_t *result
)
{
struct vnode *vp = imgp->ip_vp;
off_t file_offset = imgp->ip_arch_offset;
off_t macho_size = imgp->ip_arch_size;
off_t total_size = 0;
off_t file_size = imgp->ip_vattr->va_data_size;
pmap_t pmap = 0; /* protected by create_map */
vm_map_t map;
load_result_t myresult;
load_return_t lret;
boolean_t enforce_hard_pagezero = TRUE;
int in_exec = (imgp->ip_flags & IMGPF_EXEC);
task_t task = current_task();
int64_t aslr_page_offset = 0;
int64_t dyld_aslr_page_offset = 0;
int64_t aslr_section_size = 0;
int64_t aslr_section_offset = 0;
kern_return_t kret;
unsigned int pmap_flags = 0;
if (os_add_overflow(file_offset, macho_size, &total_size) ||
total_size > file_size) {
return LOAD_BADMACHO;
}
result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR);
result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA);
#if defined(HAS_APPLE_PAC)
pmap_flags |= (imgp->ip_flags & IMGPF_NOJOP) ? PMAP_CREATE_DISABLE_JOP : 0;
#endif /* defined(HAS_APPLE_PAC) */
#if CONFIG_ROSETTA
pmap_flags |= (imgp->ip_flags & IMGPF_ROSETTA) ? PMAP_CREATE_ROSETTA : 0;
#endif
pmap_flags |= result->is_64bit_addr ? PMAP_CREATE_64BIT : 0;
task_t ledger_task;
if (imgp->ip_new_thread) {
ledger_task = get_threadtask(imgp->ip_new_thread);
} else {
ledger_task = task;
}
#if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
if (imgp->ip_px_sa != NULL) {
struct _posix_spawnattr* psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
if (psa->psa_flags & _POSIX_SPAWN_FORCE_4K_PAGES) {
pmap_flags |= PMAP_CREATE_FORCE_4K_PAGES;
}
}
#endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGE */
pmap = pmap_create_options(get_task_ledger(ledger_task),
(vm_map_size_t) 0,
pmap_flags);
if (pmap == NULL) {
return LOAD_RESOURCE;
}
map = vm_map_create_options(pmap, 0,
vm_compute_max_offset(result->is_64bit_addr),
VM_MAP_CREATE_PAGEABLE);
#if defined(__arm64__)
if (result->is_64bit_addr) {
/* enforce 16KB alignment of VM map entries */
vm_map_set_page_shift(map, SIXTEENK_PAGE_SHIFT);
} else {
vm_map_set_page_shift(map, page_shift_user32);
}
#endif /* __arm64__ */
#if PMAP_CREATE_FORCE_4K_PAGES
if (pmap_flags & PMAP_CREATE_FORCE_4K_PAGES) {
DEBUG4K_LIFE("***** launching '%s' as 4k *****\n", vp->v_name);
vm_map_set_page_shift(map, FOURK_PAGE_SHIFT);
}
#endif /* PMAP_CREATE_FORCE_4K_PAGES */
#ifndef CONFIG_ENFORCE_SIGNED_CODE
/* This turns off faulting for executable pages, which allows
* to circumvent Code Signing Enforcement. The per process
* flag (CS_ENFORCEMENT) is not set yet, but we can use the
* global flag.
*/
if (!cs_process_global_enforcement() && (header->flags & MH_ALLOW_STACK_EXECUTION)) {
vm_map_disable_NX(map);
// TODO: Message Trace or log that this is happening
}
#endif
/* Forcibly disallow execution from data pages on even if the arch
* normally permits it. */
if ((header->flags & MH_NO_HEAP_EXECUTION) && !(imgp->ip_flags & IMGPF_ALLOW_DATA_EXEC)) {
vm_map_disallow_data_exec(map);
}
/*
* Compute a random offset for ASLR, and an independent random offset for dyld.
*/
if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) {
vm_map_get_max_aslr_slide_section(map, &aslr_section_offset, &aslr_section_size);
aslr_section_offset = (random() % aslr_section_offset) * aslr_section_size;
aslr_page_offset = random();
aslr_page_offset = (aslr_page_offset % (vm_map_get_max_aslr_slide_pages(map) - 1)) + 1;
aslr_page_offset <<= vm_map_page_shift(map);
dyld_aslr_page_offset = random();
dyld_aslr_page_offset = (dyld_aslr_page_offset %
(vm_map_get_max_loader_aslr_slide_pages(map) - 1)) + 1;
dyld_aslr_page_offset <<= vm_map_page_shift(map);
aslr_page_offset += aslr_section_offset;
}
if (vm_map_page_shift(map) < (int)PAGE_SHIFT) {
DEBUG4K_LOAD("slide=0x%llx dyld_slide=0x%llx\n", aslr_page_offset, dyld_aslr_page_offset);
}
if (!result) {
result = &myresult;
}
*result = load_result_null;
/*
* re-set the bitness on the load result since we cleared the load result above.
*/
result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR);
result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA);
lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
0, aslr_page_offset, dyld_aslr_page_offset, result,
NULL, imgp);
if (lret != LOAD_SUCCESS) {
vm_map_deallocate(map); /* will lose pmap reference too */
return lret;
}
#if __x86_64__
/*
* On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
*/
if (!result->is_64bit_addr) {
enforce_hard_pagezero = FALSE;
}
/*
* For processes with IMGPF_HIGH_BITS_ASLR, add a few random high bits
* to the start address for "anywhere" memory allocations.
*/
#define VM_MAP_HIGH_START_BITS_COUNT 8
#define VM_MAP_HIGH_START_BITS_SHIFT 27
if (result->is_64bit_addr &&
(imgp->ip_flags & IMGPF_HIGH_BITS_ASLR)) {
int random_bits;
vm_map_offset_t high_start;
random_bits = random();
random_bits &= (1 << VM_MAP_HIGH_START_BITS_COUNT) - 1;
high_start = (((vm_map_offset_t)random_bits)
<< VM_MAP_HIGH_START_BITS_SHIFT);
vm_map_set_high_start(map, high_start);
}
#endif /* __x86_64__ */
/*
* Check to see if the page zero is enforced by the map->min_offset.
*/
if (enforce_hard_pagezero &&
(vm_map_has_hard_pagezero(map, 0x1000) == FALSE)) {
#if __arm64__
if (
!result->is_64bit_addr && /* not 64-bit address space */
!(header->flags & MH_PIE) && /* not PIE */
(vm_map_page_shift(map) != FOURK_PAGE_SHIFT ||
PAGE_SHIFT != FOURK_PAGE_SHIFT) && /* page size != 4KB */
result->has_pagezero && /* has a "soft" page zero */
fourk_binary_compatibility_unsafe) {
/*
* For backwards compatibility of "4K" apps on
* a 16K system, do not enforce a hard page zero...
*/
} else
#endif /* __arm64__ */
{
vm_map_deallocate(map); /* will lose pmap reference too */
return LOAD_BADMACHO;
}
}
#if __arm64__
if (enforce_hard_pagezero && result->is_64bit_addr && (header->cputype == CPU_TYPE_ARM64)) {
/* 64 bit ARM binary must have "hard page zero" of 4GB to cover the lower 32 bit address space */
if (vm_map_has_hard_pagezero(map, 0x100000000) == FALSE) {
vm_map_deallocate(map); /* will lose pmap reference too */
return LOAD_BADMACHO;
}
}
#endif
vm_commit_pagezero_status(map);
/*
* If this is an exec, then we are going to destroy the old
* task, and it's correct to halt it; if it's spawn, the
* task is not yet running, and it makes no sense.
*/
if (in_exec) {
proc_t p = current_proc();
/*
* Mark the task as halting and start the other
* threads towards terminating themselves. Then
* make sure any threads waiting for a process
* transition get informed that we are committed to
* this transition, and then finally complete the
* task halting (wait for threads and then cleanup
* task resources).
*
* NOTE: task_start_halt() makes sure that no new
* threads are created in the task during the transition.
* We need to mark the workqueue as exiting before we
* wait for threads to terminate (at the end of which
* we no longer have a prohibition on thread creation).
*
* Finally, clean up any lingering workqueue data structures
* that may have been left behind by the workqueue threads
* as they exited (and then clean up the work queue itself).
*/
kret = task_start_halt(task);
if (kret != KERN_SUCCESS) {
vm_map_deallocate(map); /* will lose pmap reference too */
return LOAD_FAILURE;
}
proc_transcommit(p, 0);
workq_mark_exiting(p);
task_complete_halt(task);
workq_exit(p);
/*
* Roll up accounting info to new task. The roll up is done after
* task_complete_halt to make sure the thread accounting info is
* rolled up to current_task.
*/
task_rollup_accounting_info(get_threadtask(thread), task);
}
*mapp = map;
#if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX)
/*
* arm64e plugin hosts currently run with JOP keys disabled, since they
* may need to run arm64 plugins.
*/
if (arm64e_plugin_host(imgp, result)) {
imgp->ip_flags |= IMGPF_NOJOP;
pmap_disable_user_jop(pmap);
}
#if CONFIG_ROSETTA
/* Disable JOP keys if the Rosetta runtime being used isn't arm64e */
if (result->is_rosetta && (imgp->ip_flags & IMGPF_NOJOP)) {
pmap_disable_user_jop(pmap);
}
#endif /* CONFIG_ROSETTA */
#endif /* __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) */
return LOAD_SUCCESS;
}
int macho_printf = 0;
#define MACHO_PRINTF(args) \
do { \
if (macho_printf) { \
printf args; \
} \
} while (0)
static boolean_t
pie_required(
cpu_type_t exectype,
cpu_subtype_t execsubtype)
{
switch (exectype) {
case CPU_TYPE_X86_64:
return FALSE;
case CPU_TYPE_ARM64:
return TRUE;
case CPU_TYPE_ARM:
switch (execsubtype) {
case CPU_SUBTYPE_ARM_V7K:
return TRUE;
}
break;
}
return FALSE;
}
/*
* The file size of a mach-o file is limited to 32 bits; this is because
* this is the limit on the kalloc() of enough bytes for a mach_header and
* the contents of its sizeofcmds, which is currently constrained to 32
* bits in the file format itself. We read into the kernel buffer the
* commands section, and then parse it in order to parse the mach-o file
* format load_command segment(s). We are only interested in a subset of
* the total set of possible commands. If "map"==VM_MAP_NULL or
* "thread"==THREAD_NULL, do not make permament VM modifications,
* just preflight the parse.
*/
static
load_return_t
parse_machfile(
struct vnode *vp,
vm_map_t map,
thread_t thread,
struct mach_header *header,
off_t file_offset,
off_t macho_size,
int depth,
int64_t aslr_offset,
int64_t dyld_aslr_offset,
load_result_t *result,
load_result_t *binresult,
struct image_params *imgp
)
{
uint32_t ncmds;
struct load_command *lcp;
struct dylinker_command *dlp = 0;
void * control;
load_return_t ret = LOAD_SUCCESS;
void * addr;
vm_size_t alloc_size, cmds_size;
size_t offset;
size_t oldoffset; /* for overflow check */
int pass;
proc_t p = vfs_context_proc(imgp->ip_vfs_context);
int error;
int resid = 0;
int spawn = (imgp->ip_flags & IMGPF_SPAWN);
size_t mach_header_sz = sizeof(struct mach_header);
boolean_t abi64;
boolean_t got_code_signatures = FALSE;
boolean_t found_header_segment = FALSE;
boolean_t found_xhdr = FALSE;
boolean_t found_version_cmd = FALSE;
int64_t slide = 0;
boolean_t dyld_no_load_addr = FALSE;
boolean_t is_dyld = FALSE;
vm_map_offset_t effective_page_mask = PAGE_MASK;
#if __arm64__
uint64_t pagezero_end = 0;
uint64_t executable_end = 0;
uint64_t writable_start = 0;
vm_map_size_t effective_page_size;
effective_page_mask = vm_map_page_mask(map);
effective_page_size = vm_map_page_size(map);
#endif /* __arm64__ */
if (header->magic == MH_MAGIC_64 ||
header->magic == MH_CIGAM_64) {
mach_header_sz = sizeof(struct mach_header_64);
}
/*
* Break infinite recursion
*/
if (depth > 2) {
return LOAD_FAILURE;
}
depth++;
/*
* Set CS_NO_UNTRUSTED_HELPERS by default; load_dylinker and load_rosetta
* will unset it if necessary.
*/
if (depth == 1) {
result->csflags |= CS_NO_UNTRUSTED_HELPERS;
}
/*
* Check to see if right machine type.
*/
if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK))
) {
return LOAD_BADARCH;
}
if (!grade_binary(header->cputype,
header->cpusubtype & ~CPU_SUBTYPE_MASK,
header->cpusubtype & CPU_SUBTYPE_MASK, TRUE)) {
return LOAD_BADARCH;
}
abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
switch (header->filetype) {
case MH_EXECUTE:
if (depth != 1 && depth != 3) {
return LOAD_FAILURE;
}
if (header->flags & MH_DYLDLINK) {
/* Check properties of dynamic executables */
if (!(header->flags & MH_PIE) && pie_required(header->cputype, header->cpusubtype & ~CPU_SUBTYPE_MASK)) {
return LOAD_FAILURE;
}
result->needs_dynlinker = TRUE;
} else if (header->cputype == CPU_TYPE_X86_64) {
/* x86_64 static binaries allowed */
#if CONFIG_ROSETTA
} else if (imgp->ip_flags & IMGPF_ROSETTA) {
/* Rosetta runtime allowed */
#endif /* CONFIG_X86_64_COMPAT */
} else {
/* Check properties of static executables (disallowed except for development) */
#if !(DEVELOPMENT || DEBUG)
return LOAD_FAILURE;
#endif
}
break;
case MH_DYLINKER:
if (depth != 2) {
return LOAD_FAILURE;
}
is_dyld = TRUE;
break;
default:
return LOAD_FAILURE;
}
/*
* For PIE and dyld, slide everything by the ASLR offset.
*/
if ((header->flags & MH_PIE) || is_dyld) {
slide = aslr_offset;
}
/*
* Get the pager for the file.
*/
control = ubc_getobject(vp, UBC_FLAGS_NONE);
/* ensure header + sizeofcmds falls within the file */
if (os_add_overflow(mach_header_sz, header->sizeofcmds, &cmds_size) ||
(off_t)cmds_size > macho_size ||
round_page_overflow(cmds_size, &alloc_size) ||
alloc_size > INT_MAX) {
return LOAD_BADMACHO;
}
/*
* Map the load commands into kernel memory.
*/
addr = kalloc_data(alloc_size, Z_WAITOK);
if (addr == NULL) {
return LOAD_NOSPACE;
}
error = vn_rdwr(UIO_READ, vp, addr, (int)alloc_size, file_offset,
UIO_SYSSPACE, 0, vfs_context_ucred(imgp->ip_vfs_context), &resid, p);
if (error) {
kfree_data(addr, alloc_size);
return LOAD_IOERROR;
}
if (resid) {
{
/* We must be able to read in as much as the mach_header indicated */
kfree_data(addr, alloc_size);
return LOAD_BADMACHO;
}
}
/*
* Scan through the commands, processing each one as necessary.
* We parse in three passes through the headers:
* 0: determine if TEXT and DATA boundary can be page-aligned, load platform version
* 1: thread state, uuid, code signature
* 2: segments
* 3: dyld, encryption, check entry point
*/
boolean_t slide_realign = FALSE;
#if __arm64__
if (!abi64) {
slide_realign = TRUE;
}
#endif
for (pass = 0; pass <= 3; pass++) {
if (pass == 1) {
#if __arm64__
boolean_t is_pie;
int64_t adjust;
is_pie = ((header->flags & MH_PIE) != 0);
if (pagezero_end != 0 &&
pagezero_end < effective_page_size) {
/* need at least 1 page for PAGEZERO */
adjust = effective_page_size;
MACHO_PRINTF(("pagezero boundary at "
"0x%llx; adjust slide from "
"0x%llx to 0x%llx%s\n",
(uint64_t) pagezero_end,
slide,
slide + adjust,
(is_pie
? ""
: " BUT NO PIE ****** :-(")));
if (is_pie) {
slide += adjust;
pagezero_end += adjust;
executable_end += adjust;
writable_start += adjust;
}
}
if (pagezero_end != 0) {
result->has_pagezero = TRUE;
}
if (executable_end == writable_start &&
(executable_end & effective_page_mask) != 0 &&
(executable_end & FOURK_PAGE_MASK) == 0) {
/*
* The TEXT/DATA boundary is 4K-aligned but
* not page-aligned. Adjust the slide to make
* it page-aligned and avoid having a page
* with both write and execute permissions.
*/
adjust =
(effective_page_size -
(executable_end & effective_page_mask));
MACHO_PRINTF(("page-unaligned X-W boundary at "
"0x%llx; adjust slide from "
"0x%llx to 0x%llx%s\n",
(uint64_t) executable_end,
slide,
slide + adjust,
(is_pie
? ""
: " BUT NO PIE ****** :-(")));
if (is_pie) {
slide += adjust;
}
}
#endif /* __arm64__ */