]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/unit_tests/libproc_privilege_test_13203438_src/libproc_privilege_test_13203438.c
65f876b70eb914ae98b2dc8668baf5d528beb2cf
[apple/xnu.git] / tools / tests / unit_tests / libproc_privilege_test_13203438_src / libproc_privilege_test_13203438.c
1 /*
2 * Unit test to verify that PROC_PIDUNIQIDENTIFIERINFO is an unprivilege operation.
3 *
4 * Test calls PROC_PIDTBSDINFO, PROC_PIDTASKINFO, PROC_PIDT_SHORTBSDINFO, PROC_PIDUNIQIDENTIFIERINFO on the process
5 * as well as on launchd to verify that PROC_PIDT_SHORTBSDINFO and PROC_PIDUNIQIDENTIFIERINFO are unpirivilege
6 * operations while PROC_PIDTBSDINFO and PROC_PIDTASKINFO are privelege ones.
7 */
8
9 #include <System/sys/proc_info.h>
10 #include <libproc.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <sys/wait.h>
15
16
17 #define TEST_PASS 1
18 #define TEST_FAIL 0
19
20 int
21 bsdinfo_test(int pid, int result)
22 {
23 struct proc_bsdinfo bsdinfo;
24 int error;
25
26
27 error = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &bsdinfo, sizeof(bsdinfo));
28 if ((error > 0 && result == TEST_PASS) || (error <= 0 && result == TEST_FAIL)) {
29 printf("[PASS]: Privilege test on pid = %d for PROC_PIDTBSDINFO passed\n", pid);
30 return 0;
31 } else {
32 printf("[FAIL]: Privilege test on pid = %d for PROC_PIDTBSDINFO failed\n", pid);
33 return 1;
34 }
35
36 }
37
38 int
39 taskinfo_test(int pid, int result)
40 {
41 struct proc_taskinfo taskinfo;
42 int error;
43
44
45 error = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskinfo, sizeof(taskinfo));
46 if ((error > 0 && result == TEST_PASS) || (error <= 0 && result == TEST_FAIL)) {
47 printf("[PASS]: Privilege test on pid = %d for PROC_PIDTASKINFO passed\n", pid);
48 return 0;
49 } else {
50 printf("[FAIL] Privilege test on pid = %d for PROC_PIDTASKINFO failed\n", pid);
51 return 1;
52 }
53 }
54
55 int
56 bsdshortinfo_test(int pid, int result)
57 {
58 struct proc_bsdshortinfo bsdshortinfo;
59 int error;
60
61
62 error = proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &bsdshortinfo, sizeof(bsdshortinfo));
63 if ((error > 0 && result == TEST_PASS) || (error <= 0 && result == TEST_FAIL)) {
64 printf("[PASS]: Privilege test on pid = %d for PROC_PIDT_SHORTBSDINFO passed\n", pid);
65 return 0;
66 } else {
67 printf("[FAIL]: Privilege test on pid = %d for PROC_PIDT_SHORTBSDINFO failed\n", pid);
68 return 1;
69 }
70 }
71
72
73 int
74 piduniqid_test(int pid, int result)
75 {
76 struct proc_uniqidentifierinfo uniqidinfo;
77 int error;
78
79
80 error = proc_pidinfo(pid, PROC_PIDUNIQIDENTIFIERINFO, 0, &uniqidinfo, sizeof(uniqidinfo));
81 if ((error > 0 && result == TEST_PASS) || (error <= 0 && result == TEST_FAIL)) {
82 printf("[PASS]: Privilege test on pid = %d for PROC_PIDUNIQIDENTIFIERINFO passed\n", pid);
83 return 0;
84 } else {
85 printf("[FAIL]: Privilege test on pid = %d for PROC_PIDUNIQIDENTIFIERINFO failed\n", pid);
86 return 1;
87 }
88
89 }
90
91
92 int main()
93 {
94 int selfpid, launchdpid;
95
96 selfpid = getpid();
97 launchdpid = 1;
98
99 if (bsdinfo_test(selfpid, TEST_PASS))
100 goto fail;
101 if (bsdinfo_test(launchdpid, TEST_FAIL))
102 goto fail;
103
104 if (taskinfo_test(selfpid, TEST_PASS))
105 goto fail;
106 if (taskinfo_test(launchdpid, TEST_FAIL))
107 goto fail;
108
109 if (bsdshortinfo_test(selfpid, TEST_PASS))
110 goto fail;
111 if (bsdshortinfo_test(launchdpid, TEST_PASS))
112 goto fail;
113
114 if (piduniqid_test(selfpid, TEST_PASS))
115 goto fail;
116 if (piduniqid_test(launchdpid, TEST_PASS))
117 goto fail;
118
119
120 printf("Privilege test for libproc passed [PASS] \n");
121 return 0;
122
123 fail:
124 printf("Privilege test for libproc failed [FAIL] \n");
125 return 1;
126 }
127