dump_pagetables.c 12.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10
/*
 * Debug helper to dump the current kernel pagetables of the system
 * so that we can see what the various memory ranges are set to.
 *
 * (C) Copyright 2008 Intel Corporation
 *
 * Author: Arjan van de Ven <arjan@linux.intel.com>
 */

11
#include <linux/debugfs.h>
12
#include <linux/kasan.h>
13
#include <linux/mm.h>
14
#include <linux/init.h>
15
#include <linux/sched.h>
16
#include <linux/seq_file.h>
17
#include <linux/highmem.h>
18
#include <linux/pci.h>
19
#include <linux/ptdump.h>
20

21
#include <asm/e820/types.h>
22 23 24 25 26 27 28 29
#include <asm/pgtable.h>

/*
 * The dumper groups pagetable entries of the same type into one, and for
 * that it needs to keep some state when walking, and flush this state
 * when a "break" in the continuity is found.
 */
struct pg_state {
30
	struct ptdump_state ptdump;
31
	int level;
32
	pgprotval_t current_prot;
33
	pgprotval_t effective_prot;
34
	pgprotval_t prot_levels[5];
35
	unsigned long start_address;
36
	const struct addr_marker *marker;
37
	unsigned long lines;
38
	bool to_dmesg;
S
Stephen Smalley 已提交
39 40
	bool check_wx;
	unsigned long wx_pages;
41
	struct seq_file *seq;
42 43
};

44 45 46
struct addr_marker {
	unsigned long start_address;
	const char *name;
47
	unsigned long max_lines;
48 49
};

50 51 52 53
/* Address space markers hints */

#ifdef CONFIG_X86_64

54 55 56
enum address_markers_idx {
	USER_SPACE_NR = 0,
	KERNEL_SPACE_NR,
57
#ifdef CONFIG_MODIFY_LDT_SYSCALL
58 59
	LDT_NR,
#endif
60
	LOW_KERNEL_NR,
61 62
	VMALLOC_START_NR,
	VMEMMAP_START_NR,
63 64 65
#ifdef CONFIG_KASAN
	KASAN_SHADOW_START_NR,
	KASAN_SHADOW_END_NR,
66
#endif
67
	CPU_ENTRY_AREA_NR,
68
#ifdef CONFIG_X86_ESPFIX64
69
	ESPFIX_START_NR,
70 71 72 73
#endif
#ifdef CONFIG_EFI
	EFI_END_NR,
#endif
74 75 76
	HIGH_KERNEL_NR,
	MODULES_VADDR_NR,
	MODULES_END_NR,
77 78 79 80 81 82 83 84 85 86 87
	FIXADDR_START_NR,
	END_OF_SPACE_NR,
};

static struct addr_marker address_markers[] = {
	[USER_SPACE_NR]		= { 0,			"User Space" },
	[KERNEL_SPACE_NR]	= { (1UL << 63),	"Kernel Space" },
	[LOW_KERNEL_NR]		= { 0UL,		"Low Kernel Mapping" },
	[VMALLOC_START_NR]	= { 0UL,		"vmalloc() Area" },
	[VMEMMAP_START_NR]	= { 0UL,		"Vmemmap" },
#ifdef CONFIG_KASAN
88 89 90 91 92 93
	/*
	 * These fields get initialized with the (dynamic)
	 * KASAN_SHADOW_{START,END} values in pt_dump_init().
	 */
	[KASAN_SHADOW_START_NR]	= { 0UL,		"KASAN shadow" },
	[KASAN_SHADOW_END_NR]	= { 0UL,		"KASAN shadow end" },
94 95
#endif
#ifdef CONFIG_MODIFY_LDT_SYSCALL
96
	[LDT_NR]		= { 0UL,		"LDT remap" },
97
#endif
98
	[CPU_ENTRY_AREA_NR]	= { CPU_ENTRY_AREA_BASE,"CPU entry Area" },
99 100 101 102 103 104 105 106 107 108 109 110 111
#ifdef CONFIG_X86_ESPFIX64
	[ESPFIX_START_NR]	= { ESPFIX_BASE_ADDR,	"ESPfix Area", 16 },
#endif
#ifdef CONFIG_EFI
	[EFI_END_NR]		= { EFI_VA_END,		"EFI Runtime Services" },
#endif
	[HIGH_KERNEL_NR]	= { __START_KERNEL_map,	"High Kernel Mapping" },
	[MODULES_VADDR_NR]	= { MODULES_VADDR,	"Modules" },
	[MODULES_END_NR]	= { MODULES_END,	"End Modules" },
	[FIXADDR_START_NR]	= { FIXADDR_START,	"Fixmap Area" },
	[END_OF_SPACE_NR]	= { -1,			NULL }
};

112 113
#define INIT_PGD	((pgd_t *) &init_top_pgt)

114 115 116 117
#else /* CONFIG_X86_64 */

enum address_markers_idx {
	USER_SPACE_NR = 0,
118 119 120
	KERNEL_SPACE_NR,
	VMALLOC_START_NR,
	VMALLOC_END_NR,
121
#ifdef CONFIG_HIGHMEM
122
	PKMAP_BASE_NR,
123 124 125
#endif
#ifdef CONFIG_MODIFY_LDT_SYSCALL
	LDT_NR,
126
#endif
127
	CPU_ENTRY_AREA_NR,
128 129
	FIXADDR_START_NR,
	END_OF_SPACE_NR,
130 131
};

132
static struct addr_marker address_markers[] = {
133 134 135 136 137 138
	[USER_SPACE_NR]		= { 0,			"User Space" },
	[KERNEL_SPACE_NR]	= { PAGE_OFFSET,	"Kernel Mapping" },
	[VMALLOC_START_NR]	= { 0UL,		"vmalloc() Area" },
	[VMALLOC_END_NR]	= { 0UL,		"vmalloc() End" },
#ifdef CONFIG_HIGHMEM
	[PKMAP_BASE_NR]		= { 0UL,		"Persistent kmap() Area" },
139 140 141
#endif
#ifdef CONFIG_MODIFY_LDT_SYSCALL
	[LDT_NR]		= { 0UL,		"LDT remap" },
142
#endif
143
	[CPU_ENTRY_AREA_NR]	= { 0UL,		"CPU entry area" },
144 145
	[FIXADDR_START_NR]	= { 0UL,		"Fixmap area" },
	[END_OF_SPACE_NR]	= { -1,			NULL }
146
};
147

148 149
#define INIT_PGD	(swapper_pg_dir)

150 151
#endif /* !CONFIG_X86_64 */

152 153 154 155
/* Multipliers for offsets within the PTEs */
#define PTE_LEVEL_MULT (PAGE_SIZE)
#define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
#define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
156
#define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
157
#define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT)
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#define pt_dump_seq_printf(m, to_dmesg, fmt, args...)		\
({								\
	if (to_dmesg)					\
		printk(KERN_INFO fmt, ##args);			\
	else							\
		if (m)						\
			seq_printf(m, fmt, ##args);		\
})

#define pt_dump_cont_printf(m, to_dmesg, fmt, args...)		\
({								\
	if (to_dmesg)					\
		printk(KERN_CONT fmt, ##args);			\
	else							\
		if (m)						\
			seq_printf(m, fmt, ##args);		\
})

177 178 179
/*
 * Print a readable form of a pgprot_t to the seq_file
 */
180
static void printk_prot(struct seq_file *m, pgprotval_t pr, int level, bool dmsg)
181
{
182
	static const char * const level_name[] =
183
		{ "pgd", "p4d", "pud", "pmd", "pte" };
184

185
	if (!(pr & _PAGE_PRESENT)) {
186
		/* Not present */
187
		pt_dump_cont_printf(m, dmsg, "                              ");
188 189
	} else {
		if (pr & _PAGE_USER)
190
			pt_dump_cont_printf(m, dmsg, "USR ");
191
		else
192
			pt_dump_cont_printf(m, dmsg, "    ");
193
		if (pr & _PAGE_RW)
194
			pt_dump_cont_printf(m, dmsg, "RW ");
195
		else
196
			pt_dump_cont_printf(m, dmsg, "ro ");
197
		if (pr & _PAGE_PWT)
198
			pt_dump_cont_printf(m, dmsg, "PWT ");
199
		else
200
			pt_dump_cont_printf(m, dmsg, "    ");
201
		if (pr & _PAGE_PCD)
202
			pt_dump_cont_printf(m, dmsg, "PCD ");
203
		else
204
			pt_dump_cont_printf(m, dmsg, "    ");
205

206
		/* Bit 7 has a different meaning on level 3 vs 4 */
207
		if (level <= 3 && pr & _PAGE_PSE)
208 209 210
			pt_dump_cont_printf(m, dmsg, "PSE ");
		else
			pt_dump_cont_printf(m, dmsg, "    ");
211 212
		if ((level == 4 && pr & _PAGE_PAT) ||
		    ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE))
213
			pt_dump_cont_printf(m, dmsg, "PAT ");
214 215
		else
			pt_dump_cont_printf(m, dmsg, "    ");
216
		if (pr & _PAGE_GLOBAL)
217
			pt_dump_cont_printf(m, dmsg, "GLB ");
218
		else
219
			pt_dump_cont_printf(m, dmsg, "    ");
220
		if (pr & _PAGE_NX)
221
			pt_dump_cont_printf(m, dmsg, "NX ");
222
		else
223
			pt_dump_cont_printf(m, dmsg, "x  ");
224
	}
225
	pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
226 227
}

228
static void note_wx(struct pg_state *st, unsigned long addr)
229 230 231
{
	unsigned long npages;

232
	npages = (addr - st->start_address) / PAGE_SIZE;
233 234 235 236 237 238 239

#ifdef CONFIG_PCI_BIOS
	/*
	 * If PCI BIOS is enabled, the PCI BIOS area is forced to WX.
	 * Inform about it, but avoid the warning.
	 */
	if (pcibios_enabled && st->start_address >= PAGE_OFFSET + BIOS_BEGIN &&
240
	    addr <= PAGE_OFFSET + BIOS_END) {
241 242 243 244 245 246
		pr_warn_once("x86/mm: PCI BIOS W+X mapping %lu pages\n", npages);
		return;
	}
#endif
	/* Account the WX pages */
	st->wx_pages += npages;
247 248
	WARN_ONCE(__supported_pte_mask & _PAGE_NX,
		  "x86/mm: Found insecure W+X mapping at address %pS\n",
249 250 251
		  (void *)st->start_address);
}

252 253 254 255 256 257
static inline pgprotval_t effective_prot(pgprotval_t prot1, pgprotval_t prot2)
{
	return (prot1 & prot2 & (_PAGE_USER | _PAGE_RW)) |
	       ((prot1 | prot2) & _PAGE_NX);
}

258 259 260 261 262
/*
 * This function gets called on a break in a continuous series
 * of PTE entries; the next one is different so we need to
 * print what we collected so far.
 */
263 264
static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
		      unsigned long val)
265
{
266 267 268
	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
	pgprotval_t new_prot, new_eff;
	pgprotval_t cur, eff;
269
	static const char units[] = "BKMGTPE";
270
	struct seq_file *m = st->seq;
271

272 273
	new_prot = val & PTE_FLAGS_MASK;

274 275
	if (level > 0) {
		new_eff = effective_prot(st->prot_levels[level - 1],
276 277 278 279 280
					 new_prot);
	} else {
		new_eff = new_prot;
	}

281 282
	if (level >= 0)
		st->prot_levels[level] = new_eff;
283

284 285
	/*
	 * If we have a "break" in the series, we need to flush the state that
286 287
	 * we have now. "break" is either changing perms, levels or
	 * address space marker.
288
	 */
289
	cur = st->current_prot;
290
	eff = st->effective_prot;
291

292
	if (st->level == -1) {
293 294
		/* First entry */
		st->current_prot = new_prot;
295
		st->effective_prot = new_eff;
296 297
		st->level = level;
		st->marker = address_markers;
298
		st->lines = 0;
299 300
		pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
				   st->marker->name);
301 302
	} else if (new_prot != cur || new_eff != eff || level != st->level ||
		   addr >= st->marker[1].start_address) {
303
		const char *unit = units;
304
		unsigned long delta;
305
		int width = sizeof(unsigned long) * 2;
S
Stephen Smalley 已提交
306

307
		if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX))
308
			note_wx(st, addr);
309 310 311 312

		/*
		 * Now print the actual finished series
		 */
313 314 315 316 317
		if (!st->marker->max_lines ||
		    st->lines < st->marker->max_lines) {
			pt_dump_seq_printf(m, st->to_dmesg,
					   "0x%0*lx-0x%0*lx   ",
					   width, st->start_address,
318
					   width, addr);
319

320
			delta = addr - st->start_address;
321 322 323 324 325 326 327 328
			while (!(delta & 1023) && unit[1]) {
				delta >>= 10;
				unit++;
			}
			pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
					    delta, *unit);
			printk_prot(m, st->current_prot, st->level,
				    st->to_dmesg);
329
		}
330
		st->lines++;
331 332 333 334 335 336

		/*
		 * We print markers for special areas of address space,
		 * such as the start of vmalloc space etc.
		 * This helps in the interpretation.
		 */
337
		if (addr >= st->marker[1].start_address) {
338 339 340 341 342 343 344 345 346
			if (st->marker->max_lines &&
			    st->lines > st->marker->max_lines) {
				unsigned long nskip =
					st->lines - st->marker->max_lines;
				pt_dump_seq_printf(m, st->to_dmesg,
						   "... %lu entr%s skipped ... \n",
						   nskip,
						   nskip == 1 ? "y" : "ies");
			}
347
			st->marker++;
348
			st->lines = 0;
349 350
			pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
					   st->marker->name);
351
		}
352

353
		st->start_address = addr;
354
		st->current_prot = new_prot;
355
		st->effective_prot = new_eff;
356
		st->level = level;
357
	}
358 359
}

360 361
static void ptdump_walk_pgd_level_core(struct seq_file *m,
				       struct mm_struct *mm, pgd_t *pgd,
362
				       bool checkwx, bool dmesg)
363
{
364 365 366
	const struct ptdump_range ptdump_ranges[] = {
#ifdef CONFIG_X86_64
	{0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2},
367
	{GUARD_HOLE_END_ADDR, ~0UL},
368
#else
369
	{0, ~0UL},
370
#endif
371 372
	{0, 0}
};
373

374 375 376 377 378
	struct pg_state st = {
		.ptdump = {
			.note_page	= note_page,
			.range		= ptdump_ranges
		},
379
		.level = -1,
380 381 382 383
		.to_dmesg	= dmesg,
		.check_wx	= checkwx,
		.seq		= m
	};
384

385
	ptdump_walk_pgd(&st.ptdump, mm, pgd);
386

S
Stephen Smalley 已提交
387 388 389 390 391 392 393 394 395
	if (!checkwx)
		return;
	if (st.wx_pages)
		pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
			st.wx_pages);
	else
		pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
}

396
void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)
S
Stephen Smalley 已提交
397
{
398
	ptdump_walk_pgd_level_core(m, mm, mm->pgd, false, true);
399 400
}

401 402
void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
				   bool user)
403
{
404
	pgd_t *pgd = mm->pgd;
405
#ifdef CONFIG_PAGE_TABLE_ISOLATION
406
	if (user && boot_cpu_has(X86_FEATURE_PTI))
407 408
		pgd = kernel_to_user_pgdp(pgd);
#endif
409
	ptdump_walk_pgd_level_core(m, mm, pgd, false, false);
410 411 412
}
EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs);

413
void ptdump_walk_user_pgd_level_checkwx(void)
414 415
{
#ifdef CONFIG_PAGE_TABLE_ISOLATION
416
	pgd_t *pgd = INIT_PGD;
417

418
	if (!(__supported_pte_mask & _PAGE_NX) ||
419
	    !boot_cpu_has(X86_FEATURE_PTI))
420 421 422 423
		return;

	pr_info("x86/mm: Checking user space page tables\n");
	pgd = kernel_to_user_pgdp(pgd);
424
	ptdump_walk_pgd_level_core(NULL, &init_mm, pgd, true, false);
425
#endif
426 427
}

S
Stephen Smalley 已提交
428 429
void ptdump_walk_pgd_level_checkwx(void)
{
430
	ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false);
S
Stephen Smalley 已提交
431 432
}

433
static int __init pt_dump_init(void)
434
{
435 436 437 438 439 440 441 442
	/*
	 * Various markers are not compile-time constants, so assign them
	 * here.
	 */
#ifdef CONFIG_X86_64
	address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
	address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
	address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
443 444 445
#ifdef CONFIG_MODIFY_LDT_SYSCALL
	address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
#endif
446 447 448 449
#ifdef CONFIG_KASAN
	address_markers[KASAN_SHADOW_START_NR].start_address = KASAN_SHADOW_START;
	address_markers[KASAN_SHADOW_END_NR].start_address = KASAN_SHADOW_END;
#endif
450
#endif
451
#ifdef CONFIG_X86_32
452 453
	address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
	address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
454
# ifdef CONFIG_HIGHMEM
455
	address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
456
# endif
457
	address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
458
	address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE;
459 460 461
# ifdef CONFIG_MODIFY_LDT_SYSCALL
	address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
# endif
462
#endif
463 464 465
	return 0;
}
__initcall(pt_dump_init);