]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/amfi-hardened-dlopen-leaf.dtest/main.c
dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / amfi-hardened-dlopen-leaf.dtest / main.c
1 // BOOT_ARGS: dyld_flags=2
2
3 // BUILD(macos): $CC my.c -dynamiclib -o $BUILD_DIR/libmy.dylib -install_name $RUN_DIR/libmy.dylib
4 // BUILD(macos): $CC main.c -o $BUILD_DIR/amfi-hardened-dlopen-leaf.exe -DHARDENED=1
5 // BUILD(macos): $CC main.c -o $BUILD_DIR/amfi-not-hardened-dlopen-leaf.exe
6
7 // BUILD(ios,tvos,watchos,bridgeos):
8
9 // RUN: DYLD_AMFI_FAKE=0x14 ./amfi-hardened-dlopen-leaf.exe
10 // RUN: DYLD_AMFI_FAKE=0x3F ./amfi-not-hardened-dlopen-leaf.exe
11
12
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <dlfcn.h>
17 #include <mach/host_info.h>
18 #include <mach/mach.h>
19 #include <mach/mach_host.h>
20
21 #include "test_support.h"
22
23 void tryPath(const char* prog, const char* path)
24 {
25 void* handle = dlopen(path, RTLD_LAZY);
26 #if HARDENED
27 if ( handle != NULL ) {
28 FAIL("dlopen(%s) unexpectedly succeeded", path);
29 exit(0);
30 }
31 #else
32 if ( handle == NULL ) {
33 FAIL("dlopen(%s) - %s", path, dlerror());
34 exit(0);
35 }
36 #endif
37
38 }
39
40 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
41 // verify leaf name leads to dylib in /usr/lib/
42 void* handle = dlopen("libc.dylib", RTLD_LAZY);
43 if ( handle == NULL ) {
44 FAIL("dlopen - %s", dlerror());
45 }
46
47 // verify file system relative paths: hardened should fail
48 tryPath(argv[0], "libmy.dylib");
49 tryPath(argv[0], "./libmy.dylib");
50 tryPath(argv[0], "../amfi-hardened-dlopen-leaf/libmy.dylib");
51 PASS("Succcess");
52 }
53
54
55