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