dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / amfi-hardened-dlopen-leaf.dtest / main.c
1 // BUILD_ONLY: MacOSX
2
3 // BOOT_ARGS: dyld_flags=2
4
5 // BUILD: $CC my.c -dynamiclib -o $BUILD_DIR/libmy.dylib -install_name $RUN_DIR/libmy.dylib
6 // BUILD: $CC main.c -o $BUILD_DIR/amfi-hardened-dlopen-leaf.exe -DHARDENED=1
7 // BUILD: $CC main.c -o $BUILD_DIR/amfi-not-hardened-dlopen-leaf.exe
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 void tryPath(const char* prog, const char* path)
22 {
23 void* handle = dlopen(path, RTLD_LAZY);
24 #if HARDENED
25 if ( handle != NULL ) {
26 printf("[FAIL] %s dlopen(%s) unexpectedly succeeded\n", prog, path);
27 exit(0);
28 }
29 #else
30 if ( handle == NULL ) {
31 printf("[FAIL] %s dlopen(%s) - %s\n", prog, path, dlerror());
32 exit(0);
33 }
34 #endif
35
36 }
37
38 int main(int arg, const char* argv[])
39 {
40 printf("[BEGIN] %s\n", argv[0]);
41
42 // verify leaf name leads to dylib in /usr/lib/
43 void* handle = dlopen("libc.dylib", RTLD_LAZY);
44 if ( handle == NULL ) {
45 printf("[FAIL] %s dlopen - %s\n", argv[0], dlerror());
46 return 0;
47 }
48
49 // verify file system relative paths: hardened should fail
50 tryPath(argv[0], "libmy.dylib");
51 tryPath(argv[0], "./libmy.dylib");
52 tryPath(argv[0], "../amfi-hardened-dlopen-leaf/libmy.dylib");
53
54 printf("[PASS] %s\n", argv[0]);
55
56 return 0;
57 }
58
59
60