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