kaslr.c (262b45ae3ab4bf8e2caf1fcfd0d8307897519630) kaslr.c (199c8471761273b7e287914cee968ddf21dfbfe0)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * kaslr.c
4 *
5 * This contains the routines needed to generate a reasonable level of
6 * entropy to choose a randomized kernel base address offset in support
7 * of Kernel Address Space Layout Randomization (KASLR). Additionally
8 * handles walking the physical memory maps (and tracking memory regions

--- 118 unchanged lines hidden (view full) ---

127{
128 while (isspace(*str))
129 ++str;
130 return (char *)str;
131}
132#include "../../../../lib/ctype.c"
133#include "../../../../lib/cmdline.c"
134
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * kaslr.c
4 *
5 * This contains the routines needed to generate a reasonable level of
6 * entropy to choose a randomized kernel base address offset in support
7 * of Kernel Address Space Layout Randomization (KASLR). Additionally
8 * handles walking the physical memory maps (and tracking memory regions

--- 118 unchanged lines hidden (view full) ---

127{
128 while (isspace(*str))
129 ++str;
130 return (char *)str;
131}
132#include "../../../../lib/ctype.c"
133#include "../../../../lib/cmdline.c"
134
135enum parse_mode {
136 PARSE_MEMMAP,
137 PARSE_EFI,
138};
139
135static int
140static int
136parse_memmap(char *p, unsigned long long *start, unsigned long long *size)
141parse_memmap(char *p, unsigned long long *start, unsigned long long *size,
142 enum parse_mode mode)
137{
138 char *oldp;
139
140 if (!p)
141 return -EINVAL;
142
143 /* We don't care about this option here */
144 if (!strncmp(p, "exactmap", 8))

--- 6 unchanged lines hidden (view full) ---

151
152 switch (*p) {
153 case '#':
154 case '$':
155 case '!':
156 *start = memparse(p + 1, &p);
157 return 0;
158 case '@':
143{
144 char *oldp;
145
146 if (!p)
147 return -EINVAL;
148
149 /* We don't care about this option here */
150 if (!strncmp(p, "exactmap", 8))

--- 6 unchanged lines hidden (view full) ---

157
158 switch (*p) {
159 case '#':
160 case '$':
161 case '!':
162 *start = memparse(p + 1, &p);
163 return 0;
164 case '@':
159 /* memmap=nn@ss specifies usable region, should be skipped */
160 *size = 0;
165 if (mode == PARSE_MEMMAP) {
166 /*
167 * memmap=nn@ss specifies usable region, should
168 * be skipped
169 */
170 *size = 0;
171 } else {
172 unsigned long long flags;
173
174 /*
175 * efi_fake_mem=nn@ss:attr the attr specifies
176 * flags that might imply a soft-reservation.
177 */
178 *start = memparse(p + 1, &p);
179 if (p && *p == ':') {
180 p++;
181 if (kstrtoull(p, 0, &flags) < 0)
182 *size = 0;
183 else if (flags & EFI_MEMORY_SP)
184 return 0;
185 }
186 *size = 0;
187 }
161 /* Fall through */
162 default:
163 /*
164 * If w/o offset, only size specified, memmap=nn[KMG] has the
165 * same behaviour as mem=nn[KMG]. It limits the max address
166 * system can use. Region above the limit should be avoided.
167 */
168 *start = 0;
169 return 0;
170 }
171
172 return -EINVAL;
173}
174
188 /* Fall through */
189 default:
190 /*
191 * If w/o offset, only size specified, memmap=nn[KMG] has the
192 * same behaviour as mem=nn[KMG]. It limits the max address
193 * system can use. Region above the limit should be avoided.
194 */
195 *start = 0;
196 return 0;
197 }
198
199 return -EINVAL;
200}
201
175static void mem_avoid_memmap(char *str)
202static void mem_avoid_memmap(enum parse_mode mode, char *str)
176{
177 static int i;
178
179 if (i >= MAX_MEMMAP_REGIONS)
180 return;
181
182 while (str && (i < MAX_MEMMAP_REGIONS)) {
183 int rc;
184 unsigned long long start, size;
185 char *k = strchr(str, ',');
186
187 if (k)
188 *k++ = 0;
189
203{
204 static int i;
205
206 if (i >= MAX_MEMMAP_REGIONS)
207 return;
208
209 while (str && (i < MAX_MEMMAP_REGIONS)) {
210 int rc;
211 unsigned long long start, size;
212 char *k = strchr(str, ',');
213
214 if (k)
215 *k++ = 0;
216
190 rc = parse_memmap(str, &start, &size);
217 rc = parse_memmap(str, &start, &size, mode);
191 if (rc < 0)
192 break;
193 str = k;
194
195 if (start == 0) {
196 /* Store the specified memory limit if size > 0 */
197 if (size > 0)
198 mem_limit = size;

--- 34 unchanged lines hidden (view full) ---

233
234 if (!strcmp(param, "hugepages") && gbpage_sz) {
235 p = val;
236 max_gb_huge_pages = simple_strtoull(p, &p, 0);
237 return;
238 }
239}
240
218 if (rc < 0)
219 break;
220 str = k;
221
222 if (start == 0) {
223 /* Store the specified memory limit if size > 0 */
224 if (size > 0)
225 mem_limit = size;

--- 34 unchanged lines hidden (view full) ---

260
261 if (!strcmp(param, "hugepages") && gbpage_sz) {
262 p = val;
263 max_gb_huge_pages = simple_strtoull(p, &p, 0);
264 return;
265 }
266}
267
241
242static void handle_mem_options(void)
243{
244 char *args = (char *)get_cmd_line_ptr();
245 size_t len = strlen((char *)args);
246 char *tmp_cmdline;
247 char *param, *val;
248 u64 mem_size;
249

--- 16 unchanged lines hidden (view full) ---

266 args = next_arg(args, &param, &val);
267 /* Stop at -- */
268 if (!val && strcmp(param, "--") == 0) {
269 warn("Only '--' specified in cmdline");
270 goto out;
271 }
272
273 if (!strcmp(param, "memmap")) {
268static void handle_mem_options(void)
269{
270 char *args = (char *)get_cmd_line_ptr();
271 size_t len = strlen((char *)args);
272 char *tmp_cmdline;
273 char *param, *val;
274 u64 mem_size;
275

--- 16 unchanged lines hidden (view full) ---

292 args = next_arg(args, &param, &val);
293 /* Stop at -- */
294 if (!val && strcmp(param, "--") == 0) {
295 warn("Only '--' specified in cmdline");
296 goto out;
297 }
298
299 if (!strcmp(param, "memmap")) {
274 mem_avoid_memmap(val);
300 mem_avoid_memmap(PARSE_MEMMAP, val);
275 } else if (strstr(param, "hugepages")) {
276 parse_gb_huge_pages(param, val);
277 } else if (!strcmp(param, "mem")) {
278 char *p = val;
279
280 if (!strcmp(p, "nopentium"))
281 continue;
282 mem_size = memparse(p, &p);
283 if (mem_size == 0)
284 goto out;
285
286 mem_limit = mem_size;
301 } else if (strstr(param, "hugepages")) {
302 parse_gb_huge_pages(param, val);
303 } else if (!strcmp(param, "mem")) {
304 char *p = val;
305
306 if (!strcmp(p, "nopentium"))
307 continue;
308 mem_size = memparse(p, &p);
309 if (mem_size == 0)
310 goto out;
311
312 mem_limit = mem_size;
313 } else if (!strcmp(param, "efi_fake_mem")) {
314 mem_avoid_memmap(PARSE_EFI, val);
287 }
288 }
289
290out:
291 free(tmp_cmdline);
292 return;
293}
294

--- 620 unchanged lines hidden ---
315 }
316 }
317
318out:
319 free(tmp_cmdline);
320 return;
321}
322

--- 620 unchanged lines hidden ---