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