dump_pagetables.c 12.2 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
static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
253
{
254 255 256 257 258 259 260 261 262 263 264 265 266 267
	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
	pgprotval_t prot = val & PTE_FLAGS_MASK;
	pgprotval_t effective;

	if (level > 0) {
		pgprotval_t higher_prot = st->prot_levels[level - 1];

		effective = (higher_prot & prot & (_PAGE_USER | _PAGE_RW)) |
			    ((higher_prot | prot) & _PAGE_NX);
	} else {
		effective = prot;
	}

	st->prot_levels[level] = effective;
268 269
}

270 271 272 273 274
/*
 * 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.
 */
275 276
static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
		      unsigned long val)
277
{
278 279 280
	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
	pgprotval_t new_prot, new_eff;
	pgprotval_t cur, eff;
281
	static const char units[] = "BKMGTPE";
282
	struct seq_file *m = st->seq;
283

284
	new_prot = val & PTE_FLAGS_MASK;
285 286 287 288
	if (!val)
		new_eff = 0;
	else
		new_eff = st->prot_levels[level];
289

290 291
	/*
	 * If we have a "break" in the series, we need to flush the state that
292 293
	 * we have now. "break" is either changing perms, levels or
	 * address space marker.
294
	 */
295
	cur = st->current_prot;
296
	eff = st->effective_prot;
297

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

313
		if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX))
314
			note_wx(st, addr);
315 316 317 318

		/*
		 * Now print the actual finished series
		 */
319 320 321 322 323
		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,
324
					   width, addr);
325

326
			delta = addr - st->start_address;
327 328 329 330 331 332 333 334
			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);
335
		}
336
		st->lines++;
337 338 339 340 341 342

		/*
		 * We print markers for special areas of address space,
		 * such as the start of vmalloc space etc.
		 * This helps in the interpretation.
		 */
343
		if (addr >= st->marker[1].start_address) {
344 345 346 347 348 349 350 351 352
			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");
			}
353
			st->marker++;
354
			st->lines = 0;
355 356
			pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
					   st->marker->name);
357
		}
358

359
		st->start_address = addr;
360
		st->current_prot = new_prot;
361
		st->effective_prot = new_eff;
362
		st->level = level;
363
	}
364 365
}

366 367
static void ptdump_walk_pgd_level_core(struct seq_file *m,
				       struct mm_struct *mm, pgd_t *pgd,
368
				       bool checkwx, bool dmesg)
369
{
370 371 372
	const struct ptdump_range ptdump_ranges[] = {
#ifdef CONFIG_X86_64
	{0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2},
373
	{GUARD_HOLE_END_ADDR, ~0UL},
374
#else
375
	{0, ~0UL},
376
#endif
377 378
	{0, 0}
};
379

380 381 382
	struct pg_state st = {
		.ptdump = {
			.note_page	= note_page,
383
			.effective_prot = effective_prot,
384 385
			.range		= ptdump_ranges
		},
386
		.level = -1,
387 388 389 390
		.to_dmesg	= dmesg,
		.check_wx	= checkwx,
		.seq		= m
	};
391

392
	ptdump_walk_pgd(&st.ptdump, mm, pgd);
393

S
Stephen Smalley 已提交
394 395 396 397 398 399 400 401 402
	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");
}

403
void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)
S
Stephen Smalley 已提交
404
{
405
	ptdump_walk_pgd_level_core(m, mm, mm->pgd, false, true);
406 407
}

408 409
void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
				   bool user)
410
{
411
	pgd_t *pgd = mm->pgd;
412
#ifdef CONFIG_PAGE_TABLE_ISOLATION
413
	if (user && boot_cpu_has(X86_FEATURE_PTI))
414 415
		pgd = kernel_to_user_pgdp(pgd);
#endif
416
	ptdump_walk_pgd_level_core(m, mm, pgd, false, false);
417 418 419
}
EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs);

420
void ptdump_walk_user_pgd_level_checkwx(void)
421 422
{
#ifdef CONFIG_PAGE_TABLE_ISOLATION
423
	pgd_t *pgd = INIT_PGD;
424

425
	if (!(__supported_pte_mask & _PAGE_NX) ||
426
	    !boot_cpu_has(X86_FEATURE_PTI))
427 428 429 430
		return;

	pr_info("x86/mm: Checking user space page tables\n");
	pgd = kernel_to_user_pgdp(pgd);
431
	ptdump_walk_pgd_level_core(NULL, &init_mm, pgd, true, false);
432
#endif
433 434
}

S
Stephen Smalley 已提交
435 436
void ptdump_walk_pgd_level_checkwx(void)
{
437
	ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false);
S
Stephen Smalley 已提交
438 439
}

440
static int __init pt_dump_init(void)
441
{
442 443 444 445 446 447 448 449
	/*
	 * 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;
450 451 452
#ifdef CONFIG_MODIFY_LDT_SYSCALL
	address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
#endif
453 454 455 456
#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
457
#endif
458
#ifdef CONFIG_X86_32
459 460
	address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
	address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
461
# ifdef CONFIG_HIGHMEM
462
	address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
463
# endif
464
	address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
465
	address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE;
466 467 468
# ifdef CONFIG_MODIFY_LDT_SYSCALL
	address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
# endif
469
#endif
470 471 472
	return 0;
}
__initcall(pt_dump_init);