]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/unit_tests/codesigntests.c
3 #import <System/sys/codesign.h>
11 get_blob(pid_t pid
, int op
)
17 for (cnt
= 0; cnt
< sizeof(header
); cnt
++) {
18 rcent
= csops(pid
, op
, header
, 1);
19 if (rcent
!= -1 && errno
!= ERANGE
)
20 err(1, "errno != ERANGE for short header");
23 rcent
= csops(pid
, op
, header
, sizeof(header
));
24 if (rcent
== -1 && errno
== ERANGE
) {
25 uint32_t len
, bufferlen
, bufferlen2
;
27 memcpy(&len
, &header
[4], 4);
28 bufferlen
= ntohl(len
);
29 if (bufferlen
> 1024 * 1024)
30 errx(1, "invalid length on blob from kernel");
31 else if (bufferlen
== 0)
32 errx(1, "bufferlen == 0");
33 else if (bufferlen
< 8)
34 errx(1, "bufferlen <8 0");
36 uint8_t buffer
[bufferlen
+ 1];
38 rcent
= csops(pid
, op
, buffer
, bufferlen
- 1);
39 if (rcent
!= -1 && errno
!= ERANGE
)
40 errx(1, "csops with full buffer - 1 failed");
42 rcent
= csops(pid
, op
, buffer
, bufferlen
);
44 errx(1, "csops with full buffer failed");
46 memcpy(&len
, &buffer
[4], 4);
47 bufferlen2
= ntohl(len
);
49 if (op
== CS_OPS_BLOB
) {
50 if (bufferlen2
> bufferlen
)
51 errx(1, "buffer larger on second try");
52 if (bufferlen2
!= bufferlen
)
53 warnx("buffer shrunk since codesign can't tell the right size to codesign_allocate");
55 if (bufferlen2
!= bufferlen
)
56 errx(1, "buffer sizes different");
59 rcent
= csops(pid
, op
, buffer
, bufferlen
+ 1);
61 errx(1, "csops with full buffer + 1 didn't pass");
65 } else if (rcent
== 0) {
73 main(int argc
, const char * argv
[])
81 if (get_blob(pid
, CS_OPS_ENTITLEMENTS_BLOB
))
82 errx(1, "failed to get entitlements");
84 if (get_blob(0, CS_OPS_ENTITLEMENTS_BLOB
))
85 errx(1, "failed to get entitlements");
87 if (get_blob(pid
, CS_OPS_BLOB
))
88 errx(1, "failed to get blob");
90 if (get_blob(0, CS_OPS_BLOB
))
91 errx(1, "failed to get blob");
93 if (get_blob(pid
, CS_OPS_IDENTITY
))
94 errx(1, "failed to get identity");
96 if (get_blob(0, CS_OPS_IDENTITY
))
97 errx(1, "failed to get identity");
99 rcent
= csops(pid
, CS_OPS_SET_STATUS
, &status
, sizeof(status
) - 1);
101 err(1, "passed when passed in too short status buffer");
103 status
= htonl(CS_RESTRICT
);
104 rcent
= csops(pid
, CS_OPS_SET_STATUS
, &status
, sizeof(status
));
106 errx(1, "failed to mark proc RESTRICTED");
108 rcent
= csops(pid
, CS_OPS_MARKINVALID
, NULL
, 0);
110 errx(1, "failed to mark proc invalid");
112 status
= htonl(CS_VALID
);
113 rcent
= csops(pid
, CS_OPS_SET_STATUS
, &status
, sizeof(status
));
115 errx(1, "managed set flags on an INVALID proc");
117 if (!get_blob(pid
, CS_OPS_ENTITLEMENTS_BLOB
))
118 errx(1, "got entitlements while invalid");
120 if (!get_blob(pid
, CS_OPS_IDENTITY
))
121 errx(1, "got identity");
123 if (!get_blob(0, CS_OPS_IDENTITY
))
124 errx(1, "got identity");
126 if (!get_blob(pid
, CS_OPS_BLOB
))