xref: /xnu-8792.41.9/tests/correct_kernel_booted.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions // Copyright (c) 2020 Apple, Inc.  All rights reserved.
2*5c2921b0SApple OSS Distributions 
3*5c2921b0SApple OSS Distributions #include <darwintest.h>
4*5c2921b0SApple OSS Distributions #include <dirent.h>
5*5c2921b0SApple OSS Distributions #include <fcntl.h>
6*5c2921b0SApple OSS Distributions #include <libkern/OSByteOrder.h>
7*5c2921b0SApple OSS Distributions #include <mach-o/loader.h>
8*5c2921b0SApple OSS Distributions #include <stdbool.h>
9*5c2921b0SApple OSS Distributions #include <sys/mman.h>
10*5c2921b0SApple OSS Distributions #include <sys/stat.h>
11*5c2921b0SApple OSS Distributions #include <sys/sysctl.h>
12*5c2921b0SApple OSS Distributions #include <TargetConditionals.h>
13*5c2921b0SApple OSS Distributions #include <unistd.h>
14*5c2921b0SApple OSS Distributions #include <uuid/uuid.h>
15*5c2921b0SApple OSS Distributions 
16*5c2921b0SApple OSS Distributions static bool
get_macho_uuid(const char * cwd,const char * path,uuid_t uuid)17*5c2921b0SApple OSS Distributions get_macho_uuid(const char *cwd, const char *path, uuid_t uuid)
18*5c2921b0SApple OSS Distributions {
19*5c2921b0SApple OSS Distributions 	bool found = false;
20*5c2921b0SApple OSS Distributions 	void *mapped = MAP_FAILED;
21*5c2921b0SApple OSS Distributions 	size_t mapped_len = 0;
22*5c2921b0SApple OSS Distributions 
23*5c2921b0SApple OSS Distributions 	T_SETUPBEGIN;
24*5c2921b0SApple OSS Distributions 
25*5c2921b0SApple OSS Distributions 	// Skip irregular files (directories, devices, etc.).
26*5c2921b0SApple OSS Distributions 	struct stat stbuf = {};
27*5c2921b0SApple OSS Distributions 	int ret = stat(path, &stbuf);
28*5c2921b0SApple OSS Distributions 	if (ret < 0 && errno == ENOENT) {
29*5c2921b0SApple OSS Distributions 		goto out;
30*5c2921b0SApple OSS Distributions 	}
31*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "should stat %s%s", cwd, path);
32*5c2921b0SApple OSS Distributions 	if ((stbuf.st_mode & S_IFREG) == 0) {
33*5c2921b0SApple OSS Distributions 		goto out;
34*5c2921b0SApple OSS Distributions 	}
35*5c2921b0SApple OSS Distributions 	if (stbuf.st_size < (off_t)sizeof(struct mach_header)) {
36*5c2921b0SApple OSS Distributions 		goto out;
37*5c2921b0SApple OSS Distributions 	}
38*5c2921b0SApple OSS Distributions 
39*5c2921b0SApple OSS Distributions 	int fd = open(path, O_RDONLY);
40*5c2921b0SApple OSS Distributions 	if (fd < 0 && (errno == EPERM || errno == EACCES || errno == ENOENT)) {
41*5c2921b0SApple OSS Distributions 		goto out;
42*5c2921b0SApple OSS Distributions 	}
43*5c2921b0SApple OSS Distributions 	T_QUIET;
44*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "should open file at %s%s", cwd, path);
45*5c2921b0SApple OSS Distributions 
46*5c2921b0SApple OSS Distributions 	mapped = mmap(NULL, (size_t)stbuf.st_size, PROT_READ, MAP_PRIVATE,
47*5c2921b0SApple OSS Distributions 	    fd, 0);
48*5c2921b0SApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
49*5c2921b0SApple OSS Distributions 	T_ASSERT_NE(mapped, MAP_FAILED, "should map Mach-O binary at %s%s",
50*5c2921b0SApple OSS Distributions 	    cwd, path);
51*5c2921b0SApple OSS Distributions 	(void)close(fd);
52*5c2921b0SApple OSS Distributions 
53*5c2921b0SApple OSS Distributions 	// Mach-O parsing boilerplate.
54*5c2921b0SApple OSS Distributions 	uint32_t magic = *(uint32_t *)mapped;
55*5c2921b0SApple OSS Distributions 	bool should_swap = false;
56*5c2921b0SApple OSS Distributions 	bool b32 = false;
57*5c2921b0SApple OSS Distributions 	// XXX This does not handle fat binaries.
58*5c2921b0SApple OSS Distributions 	switch (magic) {
59*5c2921b0SApple OSS Distributions 	case MH_CIGAM:
60*5c2921b0SApple OSS Distributions 		should_swap = true;
61*5c2921b0SApple OSS Distributions 		OS_FALLTHROUGH;
62*5c2921b0SApple OSS Distributions 	case MH_MAGIC:
63*5c2921b0SApple OSS Distributions 		b32 = true;
64*5c2921b0SApple OSS Distributions 		break;
65*5c2921b0SApple OSS Distributions 	case MH_CIGAM_64:
66*5c2921b0SApple OSS Distributions 		should_swap = true;
67*5c2921b0SApple OSS Distributions 		break;
68*5c2921b0SApple OSS Distributions 	case MH_MAGIC_64:
69*5c2921b0SApple OSS Distributions 		break;
70*5c2921b0SApple OSS Distributions 	default:
71*5c2921b0SApple OSS Distributions 		goto out;
72*5c2921b0SApple OSS Distributions 	}
73*5c2921b0SApple OSS Distributions 	const struct load_command *lcmd = NULL;
74*5c2921b0SApple OSS Distributions 	unsigned int ncmds = 0;
75*5c2921b0SApple OSS Distributions 	if (b32) {
76*5c2921b0SApple OSS Distributions 		const struct mach_header *hdr = mapped;
77*5c2921b0SApple OSS Distributions 		ncmds = hdr->ncmds;
78*5c2921b0SApple OSS Distributions 		lcmd = (const void *)((const char *)mapped + sizeof(*hdr));
79*5c2921b0SApple OSS Distributions 	} else {
80*5c2921b0SApple OSS Distributions 		const struct mach_header_64 *hdr = mapped;
81*5c2921b0SApple OSS Distributions 		ncmds = hdr->ncmds;
82*5c2921b0SApple OSS Distributions 		lcmd = (const void *)((const char *)mapped + sizeof(*hdr));
83*5c2921b0SApple OSS Distributions 	}
84*5c2921b0SApple OSS Distributions 	ncmds = should_swap ? OSSwapInt32(ncmds) : ncmds;
85*5c2921b0SApple OSS Distributions 
86*5c2921b0SApple OSS Distributions 	// Scan through load commands to find LC_UUID.
87*5c2921b0SApple OSS Distributions 	for (unsigned int i = 0; i < ncmds; i++) {
88*5c2921b0SApple OSS Distributions 		if ((should_swap ? OSSwapInt32(lcmd->cmd) : lcmd->cmd) == LC_UUID) {
89*5c2921b0SApple OSS Distributions 			const struct uuid_command *uuid_cmd = (const void *)lcmd;
90*5c2921b0SApple OSS Distributions 			uuid_copy(uuid, uuid_cmd->uuid);
91*5c2921b0SApple OSS Distributions 			found = true;
92*5c2921b0SApple OSS Distributions 			break;
93*5c2921b0SApple OSS Distributions 		}
94*5c2921b0SApple OSS Distributions 
95*5c2921b0SApple OSS Distributions 		uint32_t cmdsize = should_swap ? OSSwapInt32(lcmd->cmdsize) :
96*5c2921b0SApple OSS Distributions 		    lcmd->cmdsize;
97*5c2921b0SApple OSS Distributions 		lcmd = (const void *)((const char *)lcmd + cmdsize);
98*5c2921b0SApple OSS Distributions 	}
99*5c2921b0SApple OSS Distributions 
100*5c2921b0SApple OSS Distributions 	if (!found) {
101*5c2921b0SApple OSS Distributions 		T_LOG("could not find LC_UUID in Mach-O at %s%s", cwd, path);
102*5c2921b0SApple OSS Distributions 	}
103*5c2921b0SApple OSS Distributions 
104*5c2921b0SApple OSS Distributions out:
105*5c2921b0SApple OSS Distributions 	T_SETUPEND;
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions 	if (mapped != MAP_FAILED) {
108*5c2921b0SApple OSS Distributions 		munmap(mapped, mapped_len);
109*5c2921b0SApple OSS Distributions 	}
110*5c2921b0SApple OSS Distributions 	return found;
111*5c2921b0SApple OSS Distributions }
112*5c2921b0SApple OSS Distributions 
113*5c2921b0SApple OSS Distributions T_DECL(correct_kernel_booted,
114*5c2921b0SApple OSS Distributions     "Make sure the kernel on disk matches the running kernel, by UUID.",
115*5c2921b0SApple OSS Distributions     T_META_RUN_CONCURRENTLY(true),
116*5c2921b0SApple OSS Distributions     T_META_CHECK_LEAKS(false))
117*5c2921b0SApple OSS Distributions {
118*5c2921b0SApple OSS Distributions 	T_SETUPBEGIN;
119*5c2921b0SApple OSS Distributions 
120*5c2921b0SApple OSS Distributions 	uuid_t kern_uuid;
121*5c2921b0SApple OSS Distributions 	uuid_string_t kern_uuid_str;
122*5c2921b0SApple OSS Distributions 	size_t kern_uuid_size = sizeof(kern_uuid_str);
123*5c2921b0SApple OSS Distributions 	int ret = sysctlbyname("kern.uuid", &kern_uuid_str, &kern_uuid_size, NULL,
124*5c2921b0SApple OSS Distributions 	    0);
125*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ret, "should get running kernel UUID");
126*5c2921b0SApple OSS Distributions 	T_LOG("%s: running kernel", kern_uuid_str);
127*5c2921b0SApple OSS Distributions 
128*5c2921b0SApple OSS Distributions 	ret = uuid_parse(kern_uuid_str, kern_uuid);
129*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "should parse kernel UUID into bytes");
130*5c2921b0SApple OSS Distributions 
131*5c2921b0SApple OSS Distributions #if TARGET_OS_OSX
132*5c2921b0SApple OSS Distributions 	const char *kernels_path = "/System/Library/Kernels/";
133*5c2921b0SApple OSS Distributions #else // TARGET_OS_OSX
134*5c2921b0SApple OSS Distributions 	const char *kernels_path = "/";
135*5c2921b0SApple OSS Distributions #endif // !TARGET_OS_OSX
136*5c2921b0SApple OSS Distributions 	T_LOG("searching for kernels at %s", kernels_path);
137*5c2921b0SApple OSS Distributions 
138*5c2921b0SApple OSS Distributions 	ret = chdir(kernels_path);
139*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "should change directory to %s",
140*5c2921b0SApple OSS Distributions 	    kernels_path);
141*5c2921b0SApple OSS Distributions 
142*5c2921b0SApple OSS Distributions 	DIR *kernels_dir = opendir(kernels_path);
143*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(kernels_dir, "should open directory at %s",
144*5c2921b0SApple OSS Distributions 	    kernels_path);
145*5c2921b0SApple OSS Distributions 
146*5c2921b0SApple OSS Distributions 	T_SETUPEND;
147*5c2921b0SApple OSS Distributions 
148*5c2921b0SApple OSS Distributions 	bool found = false;
149*5c2921b0SApple OSS Distributions 	struct dirent *entry = NULL;
150*5c2921b0SApple OSS Distributions 	while ((entry = readdir(kernels_dir)) != NULL) {
151*5c2921b0SApple OSS Distributions 		uuid_t bin_uuid;
152*5c2921b0SApple OSS Distributions 		bool ok = get_macho_uuid(kernels_path, entry->d_name, bin_uuid);
153*5c2921b0SApple OSS Distributions 		if (ok) {
154*5c2921b0SApple OSS Distributions 			uuid_string_t bin_uuid_str;
155*5c2921b0SApple OSS Distributions 			uuid_unparse(bin_uuid, bin_uuid_str);
156*5c2921b0SApple OSS Distributions 			T_LOG("%s: from %s%s", bin_uuid_str, kernels_path, entry->d_name);
157*5c2921b0SApple OSS Distributions 			if (uuid_compare(bin_uuid, kern_uuid) == 0) {
158*5c2921b0SApple OSS Distributions 				found = true;
159*5c2921b0SApple OSS Distributions 				T_PASS("UUID from %s%s matches kernel UUID", kernels_path,
160*5c2921b0SApple OSS Distributions 				    entry->d_name);
161*5c2921b0SApple OSS Distributions 			}
162*5c2921b0SApple OSS Distributions 		}
163*5c2921b0SApple OSS Distributions 	}
164*5c2921b0SApple OSS Distributions 	if (!found) {
165*5c2921b0SApple OSS Distributions 		T_FAIL("failed to find kernel binary with UUID of the running kernel, "
166*5c2921b0SApple OSS Distributions 		    "wrong kernel is booted");
167*5c2921b0SApple OSS Distributions 	}
168*5c2921b0SApple OSS Distributions 	(void)closedir(kernels_dir);
169*5c2921b0SApple OSS Distributions }
170