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