]> git.saurik.com Git - apple/xnu.git/blob - tests/sr_entitlement.c
b0c496d15d1a6c5d1fd6830ecea96a8a19a66063
[apple/xnu.git] / tests / sr_entitlement.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <sys/sysctl.h>
4
5 #include <darwintest.h>
6 #include <darwintest_utils.h>
7
8 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(false));
9
10 static int after_regions = 0;
11
12 /*
13 * No system(3c) on watchOS, so provide our own.
14 */
15 static int
16 my_system(const char *command)
17 {
18 pid_t pid;
19 int status = 0;
20 const char *argv[] = {
21 "/bin/sh",
22 "-c",
23 command,
24 NULL
25 };
26
27 if (dt_launch_tool(&pid, (char **)(void *)argv, FALSE, NULL, NULL)) {
28 return -1;
29 }
30 sleep(2); /* let the child start running */
31
32 size_t size = sizeof(after_regions);
33 int ret = sysctlbyname("vm.shared_region_pager_count", &after_regions, &size, NULL, 0);
34 T_QUIET; T_EXPECT_POSIX_SUCCESS(ret, "get shared_region_pager_count after");
35
36 if (!dt_waitpid(pid, &status, NULL, 30)) {
37 if (status != 0) {
38 return status;
39 }
40 return -1;
41 }
42 return status;
43 }
44
45 /*
46 * If shared regions by entitlement was not originally active, turn it back off.
47 */
48 static int orig_setting = 0;
49 static void
50 cleanup(void)
51 {
52 int ret;
53 int off = 0;
54 size_t size_off = sizeof(off);
55
56 if (orig_setting == 0) {
57 ret = sysctlbyname("vm.vm_shared_region_by_entitlement", NULL, NULL, &off, size_off);
58 T_QUIET; T_EXPECT_POSIX_SUCCESS(ret, "turning sysctl back off");
59 }
60 }
61
62 /*
63 * This test:
64 * - looks at the number of shared region pagers,
65 * - launches a helper app that has entitlement for unique signing
66 * - gets the number of shared region pagers again.
67 * It expects to see additional shared region pager(s) to exist.
68 *
69 */
70 T_DECL(sr_entitlement, "shared region by entitlement test")
71 {
72 int ret;
73 size_t size;
74 int before_regions = 0;
75 int on = 1;
76 size_t size_on = sizeof(on);
77
78 T_SKIP("No pointer authentication support");
79
80 /*
81 * Check if the sysctl vm_shared_region_by_entitlement exists and if so make
82 * sure it is set.
83 */
84 size = sizeof(orig_setting);
85 ret = sysctlbyname("vm.vm_shared_region_by_entitlement", &orig_setting, &size, &on, size_on);
86 if (ret != 0) {
87 T_SKIP("No pointer authentication support");
88 }
89
90 T_ATEND(cleanup);
91
92 size = sizeof(before_regions);
93 ret = sysctlbyname("vm.shared_region_pager_count", &before_regions, &size, NULL, 0);
94 T_QUIET; T_EXPECT_POSIX_SUCCESS(ret, "get shared_region_pager_count before");
95 T_QUIET; T_EXPECT_GE_INT(before_regions, 1, "invalid before number of regions");
96
97 ret = my_system("./sr_entitlement_helper");
98 if (ret != 0) {
99 T_ASSERT_FAIL("Couldn't run helper first time ret = %d", ret);
100 }
101
102 T_EXPECT_GT_INT(after_regions, before_regions, "expected additional SR pagers after running helper");
103 }