1 /*
2  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3  * Copyright (c) 2020-2021 Petr Vorel <pvorel@suse.cz>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <sys/personality.h>
20 #include <sys/utsname.h>
21 #include <limits.h>
22 
23 #include "test.h"
24 #include "tst_kernel.h"
25 #include "old_safe_stdio.h"
26 
get_kernel_bits_from_uname(struct utsname *buf)27 static int get_kernel_bits_from_uname(struct utsname *buf)
28 {
29 	if (uname(buf)) {
30 		tst_brkm(TBROK | TERRNO, NULL, "uname()");
31 		return -1;
32 	}
33 
34 	return strstr(buf->machine, "64") ? 64 : 32;
35 }
36 
tst_kernel_bits(void)37 int tst_kernel_bits(void)
38 {
39 	struct utsname buf;
40 	static int kernel_bits;
41 
42 	if (kernel_bits)
43 		return kernel_bits;
44 
45 	kernel_bits = get_kernel_bits_from_uname(&buf);
46 
47 	if (kernel_bits == -1)
48 		return -1;
49 
50 	/*
51 	 * ARM64 (aarch64) defines 32-bit compatibility modes as
52 	 * armv8l and armv8b (little and big endian).
53 	 * s390x is 64bit but not contain 64 in the words.
54 	 */
55 	if (!strcmp(buf.machine, "armv8l") || !strcmp(buf.machine, "armv8b")
56 			|| !strcmp(buf.machine, "s390x"))
57 		kernel_bits = 64;
58 
59 #ifdef __ANDROID__
60 	/* Android's bionic libc sets the PER_LINUX32 personality for all 32-bit
61 	 * programs. This will cause buf.machine to report as i686 even though
62 	 * the kernel itself is 64-bit.
63 	 */
64 	if (!strcmp(buf.machine, "i686") &&
65 			(personality(0xffffffff) & PER_MASK) == PER_LINUX32) {
66 		/* Set the personality back to the default. */
67 		if (personality(PER_LINUX) == -1) {
68 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
69 			return -1;
70 		}
71 
72 		/* Redo the uname check without the PER_LINUX32 personality to
73 		 * determine the actual kernel bits value.
74 		 */
75 		kernel_bits = get_kernel_bits_from_uname(&buf);
76 		if (kernel_bits == -1)
77 			return -1;
78 
79 		/* Set the personality back to PER_LINUX32. */
80 		if (personality(PER_LINUX32) == -1) {
81 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
82 			return -1;
83 		}
84 	}
85 #endif  /* __ANDROID__ */
86 
87 	tst_resm(TINFO, "uname.machine=%s kernel is %ibit",
88 		 buf.machine, kernel_bits);
89 
90 	return kernel_bits;
91 }
92 
tst_search_driver_(const char *driver, const char *file)93 static int tst_search_driver_(const char *driver, const char *file)
94 {
95 	struct stat st;
96 	char buf[PATH_MAX];
97 	char *path = NULL, *search = NULL, *sep = NULL;
98 	FILE *f;
99 	int ret = -1;
100 
101 	struct utsname uts;
102 
103 	if (uname(&uts)) {
104 		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
105 		return -1;
106 	}
107 	SAFE_ASPRINTF(NULL, &path, "/lib/modules/%s/%s", uts.release, file);
108 
109 	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
110 		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
111 		return -1;
112 	}
113 
114 	if (access(path, R_OK)) {
115 		tst_resm(TWARN, "file %s cannot be read", path);
116 		return -1;
117 	}
118 
119 	/* always search for x86_64 */
120 	char *fix = strstr(driver, "x86-64");
121 
122 	if (fix)
123 		fix[3] = '_';
124 
125 	SAFE_ASPRINTF(NULL, &search, "/%s.ko", driver);
126 
127 	f = SAFE_FOPEN(NULL, path, "r");
128 
129 	while (fgets(buf, sizeof(buf), f)) {
130 		/* cut dependencies after : */
131 		if ((sep = strchr(buf, ':')))
132 			*sep = 0;
133 
134 		/* driver found */
135 		if (strstr(buf, search) != NULL) {
136 			ret = 0;
137 			break;
138 		}
139 	}
140 
141 	SAFE_FCLOSE(NULL, f);
142 	free(search);
143 	free(path);
144 
145 	return ret;
146 }
147 
tst_search_driver(const char *driver, const char *file)148 static int tst_search_driver(const char *driver, const char *file)
149 {
150 #ifdef __ANDROID__
151 	/*
152 	 * Android may not have properly installed modules.* files. We could
153 	 * search modules in /system/lib/modules, but to determine built-in
154 	 * drivers we need modules.builtin. Therefore assume all drivers are
155 	 * available.
156 	 */
157 	return 0;
158 #endif
159 
160 	if (!tst_search_driver_(driver, file))
161 		return 0;
162 
163 	int ret = -1;
164 
165 	if (strrchr(driver, '-') || strrchr(driver, '_')) {
166 		char *driver2 = strdup(driver);
167 		char *ix = driver2;
168 		char find = '-', replace = '_';
169 
170 		if (strrchr(driver, '_')) {
171 			find = '_';
172 			replace = '-';
173 		}
174 
175 		while ((ix = strchr(ix, find)))
176 			*ix++ = replace;
177 
178 		ret = tst_search_driver_(driver2, file);
179 		free(driver2);
180 	}
181 
182 	return ret;
183 }
184 
tst_check_builtin_driver(const char *driver)185 int tst_check_builtin_driver(const char *driver)
186 {
187 	if (!tst_search_driver(driver, "modules.builtin"))
188 		return 0;
189 
190 	return -1;
191 }
192 
tst_check_driver(const char *driver)193 int tst_check_driver(const char *driver)
194 {
195 	if (!tst_search_driver(driver, "modules.dep") ||
196 		!tst_search_driver(driver, "modules.builtin"))
197 		return 0;
198 
199 	return -1;
200 }
201