1*27b03b36SApple OSS Distributions #include <darwintest.h>
2*27b03b36SApple OSS Distributions #include <stdint.h>
3*27b03b36SApple OSS Distributions #include <bsm/libbsm.h>
4*27b03b36SApple OSS Distributions #include <System/sys/codesign.h>
5*27b03b36SApple OSS Distributions #include <sys/errno.h>
6*27b03b36SApple OSS Distributions #include <stdio.h>
7*27b03b36SApple OSS Distributions #include <unistd.h>
8*27b03b36SApple OSS Distributions
9*27b03b36SApple OSS Distributions #define MAXBUFLEN 1024 * 1024
10*27b03b36SApple OSS Distributions
11*27b03b36SApple OSS Distributions T_GLOBAL_META(
12*27b03b36SApple OSS Distributions T_META_NAMESPACE("xnu.codesigning"),
13*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
14*27b03b36SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("codesigning"),
15*27b03b36SApple OSS Distributions T_META_OWNER("rkendallkuppe"));
16*27b03b36SApple OSS Distributions
17*27b03b36SApple OSS Distributions int
get_blob(pid_t pid,int op)18*27b03b36SApple OSS Distributions get_blob(pid_t pid, int op)
19*27b03b36SApple OSS Distributions {
20*27b03b36SApple OSS Distributions uint8_t header[8];
21*27b03b36SApple OSS Distributions unsigned int cnt;
22*27b03b36SApple OSS Distributions int rcent;
23*27b03b36SApple OSS Distributions
24*27b03b36SApple OSS Distributions for (cnt = 0; cnt < sizeof(header); cnt++) {
25*27b03b36SApple OSS Distributions rcent = csops(pid, op, header, cnt);
26*27b03b36SApple OSS Distributions if (rcent != -1 && errno != ERANGE) {
27*27b03b36SApple OSS Distributions T_ASSERT_FAIL("errno != ERANGE for short header");
28*27b03b36SApple OSS Distributions }
29*27b03b36SApple OSS Distributions }
30*27b03b36SApple OSS Distributions
31*27b03b36SApple OSS Distributions rcent = csops(pid, op, header, sizeof(header));
32*27b03b36SApple OSS Distributions if (rcent == -1 && errno == ERANGE) {
33*27b03b36SApple OSS Distributions uint32_t len, bufferlen, bufferlen2;
34*27b03b36SApple OSS Distributions
35*27b03b36SApple OSS Distributions memcpy(&len, &header[4], 4);
36*27b03b36SApple OSS Distributions bufferlen = ntohl(len);
37*27b03b36SApple OSS Distributions
38*27b03b36SApple OSS Distributions T_LOG("Checking bufferlen on blob from kernel for csop %d", op);
39*27b03b36SApple OSS Distributions T_ASSERT_LE_INT(bufferlen, MAXBUFLEN, "Buffer length %zu on blob from kernel can't exceed %zu",
40*27b03b36SApple OSS Distributions (size_t)bufferlen, MAXBUFLEN);
41*27b03b36SApple OSS Distributions T_ASSERT_NE_INT(bufferlen, 0, "Buffer length %zu on blob from kernel can't be zero",
42*27b03b36SApple OSS Distributions (size_t)bufferlen);
43*27b03b36SApple OSS Distributions T_ASSERT_GE_INT(bufferlen, 8, "Buffer length %zu on blob from kernel can't be less than a byte",
44*27b03b36SApple OSS Distributions (size_t)bufferlen);
45*27b03b36SApple OSS Distributions
46*27b03b36SApple OSS Distributions uint8_t buffer[bufferlen + 1];
47*27b03b36SApple OSS Distributions
48*27b03b36SApple OSS Distributions rcent = csops(pid, op, buffer, bufferlen - 1);
49*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(errno, ERANGE, "Performing CS OPS csops with a full buffer - 1");
50*27b03b36SApple OSS Distributions
51*27b03b36SApple OSS Distributions rcent = csops(pid, op, buffer, bufferlen);
52*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Performing CS OPS with full buffer.");
53*27b03b36SApple OSS Distributions
54*27b03b36SApple OSS Distributions memcpy(&len, &buffer[4], 4);
55*27b03b36SApple OSS Distributions bufferlen2 = ntohl(len);
56*27b03b36SApple OSS Distributions
57*27b03b36SApple OSS Distributions if (op == CS_OPS_BLOB) {
58*27b03b36SApple OSS Distributions T_ASSERT_LE_INT(bufferlen2, bufferlen, "Checking %zu is %zu larger on second try",
59*27b03b36SApple OSS Distributions (size_t)bufferlen2, (size_t)bufferlen);
60*27b03b36SApple OSS Distributions
61*27b03b36SApple OSS Distributions /*
62*27b03b36SApple OSS Distributions * CS_OPS_BLOB may want a bigger buffer than the size
63*27b03b36SApple OSS Distributions * of the actual blob. If the blob came in through a
64*27b03b36SApple OSS Distributions * load command for example, then CS_OPS_BLOB will
65*27b03b36SApple OSS Distributions * want to copy out the whole buffer that the load
66*27b03b36SApple OSS Distributions * command points to, which is usually an estimated
67*27b03b36SApple OSS Distributions * size. The actual blob, and therefore the size in
68*27b03b36SApple OSS Distributions * the blob's header, may be smaller.
69*27b03b36SApple OSS Distributions */
70*27b03b36SApple OSS Distributions T_LOG("Blob is smaller (%zu) than expected (%zu). This is fine.",
71*27b03b36SApple OSS Distributions (size_t)bufferlen2, (size_t)bufferlen);
72*27b03b36SApple OSS Distributions } else {
73*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(bufferlen2, bufferlen, "Checking bufferlen sizes are different");
74*27b03b36SApple OSS Distributions }
75*27b03b36SApple OSS Distributions
76*27b03b36SApple OSS Distributions rcent = csops(pid, op, buffer, bufferlen + 1);
77*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Performing CS OPS with a full buffer + 1");
78*27b03b36SApple OSS Distributions
79*27b03b36SApple OSS Distributions return 0;
80*27b03b36SApple OSS Distributions } else if (rcent == 0) {
81*27b03b36SApple OSS Distributions return 0;
82*27b03b36SApple OSS Distributions } else {
83*27b03b36SApple OSS Distributions return 1;
84*27b03b36SApple OSS Distributions }
85*27b03b36SApple OSS Distributions }
86*27b03b36SApple OSS Distributions
87*27b03b36SApple OSS Distributions /*
88*27b03b36SApple OSS Distributions * This source is compiled with different names and build flags.
89*27b03b36SApple OSS Distributions * Makefile has the detail of the TESTNAME.
90*27b03b36SApple OSS Distributions */
91*27b03b36SApple OSS Distributions
92*27b03b36SApple OSS Distributions T_DECL(TESTNAME, "CS OP, code sign operations test")
93*27b03b36SApple OSS Distributions {
94*27b03b36SApple OSS Distributions uint32_t status;
95*27b03b36SApple OSS Distributions int rcent;
96*27b03b36SApple OSS Distributions pid_t pid;
97*27b03b36SApple OSS Distributions
98*27b03b36SApple OSS Distributions pid = getpid();
99*27b03b36SApple OSS Distributions
100*27b03b36SApple OSS Distributions
101*27b03b36SApple OSS Distributions rcent = get_blob(pid, CS_OPS_ENTITLEMENTS_BLOB);
102*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPS entitlements blob");
103*27b03b36SApple OSS Distributions
104*27b03b36SApple OSS Distributions rcent = get_blob(0, CS_OPS_ENTITLEMENTS_BLOB);
105*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPS entitlements blob");
106*27b03b36SApple OSS Distributions
107*27b03b36SApple OSS Distributions rcent = get_blob(pid, CS_OPS_BLOB);
108*27b03b36SApple OSS Distributions
109*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPS blob");
110*27b03b36SApple OSS Distributions
111*27b03b36SApple OSS Distributions rcent = get_blob(0, CS_OPS_BLOB);
112*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPS blob");
113*27b03b36SApple OSS Distributions
114*27b03b36SApple OSS Distributions rcent = get_blob(pid, CS_OPS_IDENTITY);
115*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPs identity");
116*27b03b36SApple OSS Distributions
117*27b03b36SApple OSS Distributions rcent = get_blob(0, CS_OPS_IDENTITY);
118*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPs identity");
119*27b03b36SApple OSS Distributions
120*27b03b36SApple OSS Distributions rcent = csops(pid, CS_OPS_SET_STATUS, &status, sizeof(status) - 1);
121*27b03b36SApple OSS Distributions T_ASSERT_NE_INT(rcent, 0, "Checking CS OPs set status by setting buffer to (status - 1)");
122*27b03b36SApple OSS Distributions
123*27b03b36SApple OSS Distributions status = CS_RESTRICT;
124*27b03b36SApple OSS Distributions rcent = csops(pid, CS_OPS_SET_STATUS, &status, sizeof(status));
125*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Checking CS OPs set status by setting proc RESTRICTED");
126*27b03b36SApple OSS Distributions
127*27b03b36SApple OSS Distributions rcent = csops(pid, CS_OPS_STATUS, &status, sizeof(status));
128*27b03b36SApple OSS Distributions T_ASSERT_EQ_INT(rcent, 0, "Getting CS OPs status of process");
129*27b03b36SApple OSS Distributions
130*27b03b36SApple OSS Distributions /*
131*27b03b36SApple OSS Distributions * Only run the folling tests if not HARD since otherwise
132*27b03b36SApple OSS Distributions * we'll just die when marking ourself invalid.
133*27b03b36SApple OSS Distributions */
134*27b03b36SApple OSS Distributions
135*27b03b36SApple OSS Distributions if ((status & CS_KILL) == 0) {
136*27b03b36SApple OSS Distributions rcent = csops(pid, CS_OPS_MARKINVALID, NULL, 0);
137*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ZERO(rcent, 0, "Setting CS OPs mark proc invalid");
138*27b03b36SApple OSS Distributions
139*27b03b36SApple OSS Distributions status = CS_ENFORCEMENT;
140*27b03b36SApple OSS Distributions rcent = csops(pid, CS_OPS_SET_STATUS, &status, sizeof(status));
141*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(rcent, -1, "Managed to set flags on an INVALID proc");
142*27b03b36SApple OSS Distributions
143*27b03b36SApple OSS Distributions rcent = get_blob(pid, CS_OPS_ENTITLEMENTS_BLOB);
144*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(rcent, 1, "Got entitlements while invalid");
145*27b03b36SApple OSS Distributions
146*27b03b36SApple OSS Distributions rcent = get_blob(pid, CS_OPS_IDENTITY);
147*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(rcent, 1, "Getting CS OPS identity");
148*27b03b36SApple OSS Distributions
149*27b03b36SApple OSS Distributions rcent = get_blob(0, CS_OPS_IDENTITY);
150*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(rcent, 1, "Getting CS OPS identity");
151*27b03b36SApple OSS Distributions
152*27b03b36SApple OSS Distributions rcent = get_blob(0, CS_OPS_BLOB);
153*27b03b36SApple OSS Distributions T_ASSERT_POSIX_ERROR(rcent, 1, "Geting CS OPS blob");
154*27b03b36SApple OSS Distributions }
155*27b03b36SApple OSS Distributions }
156