1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/cpufeature.h>
4 #include <linux/set_memory.h>
5 #include <linux/ptdump.h>
6 #include <linux/seq_file.h>
7 #include <linux/debugfs.h>
8 #include <linux/sort.h>
9 #include <linux/mm.h>
10 #include <linux/kfence.h>
11 #include <linux/kasan.h>
12 #include <asm/kasan.h>
13 #include <asm/abs_lowcore.h>
14 #include <asm/nospec-branch.h>
15 #include <asm/sections.h>
16 #include <asm/maccess.h>
17 
18 static unsigned long max_addr;
19 
20 struct addr_marker {
21 	int is_start;
22 	unsigned long start_address;
23 	unsigned long size;
24 	const char *name;
25 };
26 
27 static struct addr_marker *markers;
28 static unsigned int markers_cnt;
29 
30 struct pg_state {
31 	struct ptdump_state ptdump;
32 	struct seq_file *seq;
33 	int level;
34 	unsigned int current_prot;
35 	bool check_wx;
36 	unsigned long wx_pages;
37 	unsigned long start_address;
38 	const struct addr_marker *marker;
39 };
40 
41 #define pt_dump_seq_printf(m, fmt, args...)	\
42 ({						\
43 	struct seq_file *__m = (m);		\
44 						\
45 	if (__m)				\
46 		seq_printf(__m, fmt, ##args);	\
47 })
48 
49 #define pt_dump_seq_puts(m, fmt)		\
50 ({						\
51 	struct seq_file *__m = (m);		\
52 						\
53 	if (__m)				\
54 		seq_printf(__m, fmt);		\
55 })
56 
print_prot(struct seq_file * m,unsigned int pr,int level)57 static void print_prot(struct seq_file *m, unsigned int pr, int level)
58 {
59 	static const char * const level_name[] =
60 		{ "ASCE", "PGD", "PUD", "PMD", "PTE" };
61 
62 	pt_dump_seq_printf(m, "%s ", level_name[level]);
63 	if (pr & _PAGE_INVALID) {
64 		pt_dump_seq_printf(m, "I\n");
65 		return;
66 	}
67 	pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
68 	pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
69 }
70 
note_prot_wx(struct pg_state * st,unsigned long addr)71 static void note_prot_wx(struct pg_state *st, unsigned long addr)
72 {
73 	if (!st->check_wx)
74 		return;
75 	if (st->current_prot & _PAGE_INVALID)
76 		return;
77 	if (st->current_prot & _PAGE_PROTECT)
78 		return;
79 	if (st->current_prot & _PAGE_NOEXEC)
80 		return;
81 	/*
82 	 * The first lowcore page is W+X if spectre mitigations are using
83 	 * trampolines or the BEAR enhancements facility is not installed,
84 	 * in which case we have two lpswe instructions in lowcore that need
85 	 * to be executable.
86 	 */
87 	if (addr == PAGE_SIZE && (nospec_uses_trampoline() || !cpu_has_bear()))
88 		return;
89 	WARN_ONCE(IS_ENABLED(CONFIG_DEBUG_WX),
90 		  "s390/mm: Found insecure W+X mapping at address %pS\n",
91 		  (void *)st->start_address);
92 	st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
93 }
94 
note_page_update_state(struct pg_state * st,unsigned long addr,unsigned int prot,int level)95 static void note_page_update_state(struct pg_state *st, unsigned long addr, unsigned int prot, int level)
96 {
97 	struct seq_file *m = st->seq;
98 
99 	while (addr >= st->marker[1].start_address) {
100 		st->marker++;
101 		pt_dump_seq_printf(m, "---[ %s %s ]---\n", st->marker->name,
102 				   st->marker->is_start ? "Start" : "End");
103 	}
104 	st->start_address = addr;
105 	st->current_prot = prot;
106 	st->level = level;
107 }
108 
note_page(struct ptdump_state * pt_st,unsigned long addr,int level,u64 val)109 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
110 {
111 	int width = sizeof(unsigned long) * 2;
112 	static const char units[] = "KMGTPE";
113 	const char *unit = units;
114 	unsigned long delta;
115 	struct pg_state *st;
116 	struct seq_file *m;
117 	unsigned int prot;
118 
119 	st = container_of(pt_st, struct pg_state, ptdump);
120 	m = st->seq;
121 	prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC);
122 	if (level == 4 && (val & _PAGE_INVALID))
123 		prot = _PAGE_INVALID;
124 	/* For pmd_none() & friends val gets passed as zero. */
125 	if (level != 4 && !val)
126 		prot = _PAGE_INVALID;
127 	/* Final flush from generic code. */
128 	if (level == -1)
129 		addr = max_addr;
130 	if (st->level == -1) {
131 		pt_dump_seq_puts(m, "---[ Kernel Virtual Address Space ]---\n");
132 		note_page_update_state(st, addr, prot, level);
133 	} else if (prot != st->current_prot || level != st->level ||
134 		   addr >= st->marker[1].start_address) {
135 		note_prot_wx(st, addr);
136 		pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
137 				   width, st->start_address,
138 				   width, addr);
139 		delta = (addr - st->start_address) >> 10;
140 		while (!(delta & 0x3ff) && unit[1]) {
141 			delta >>= 10;
142 			unit++;
143 		}
144 		pt_dump_seq_printf(m, "%9lu%c ", delta, *unit);
145 		print_prot(m, st->current_prot, st->level);
146 		note_page_update_state(st, addr, prot, level);
147 	}
148 }
149 
ptdump_check_wx(void)150 bool ptdump_check_wx(void)
151 {
152 	struct pg_state st = {
153 		.ptdump = {
154 			.note_page = note_page,
155 			.range = (struct ptdump_range[]) {
156 				{.start = 0, .end = max_addr},
157 				{.start = 0, .end = 0},
158 			}
159 		},
160 		.seq = NULL,
161 		.level = -1,
162 		.current_prot = 0,
163 		.check_wx = true,
164 		.wx_pages = 0,
165 		.start_address = 0,
166 		.marker = (struct addr_marker[]) {
167 			{ .start_address =  0, .name = NULL},
168 			{ .start_address = -1, .name = NULL},
169 		},
170 	};
171 
172 	if (!cpu_has_nx())
173 		return true;
174 	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
175 	if (st.wx_pages) {
176 		pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
177 
178 		return false;
179 	} else {
180 		pr_info("Checked W+X mappings: passed, no %sW+X pages found\n",
181 			(nospec_uses_trampoline() || !cpu_has_bear()) ?
182 			"unexpected " : "");
183 
184 		return true;
185 	}
186 }
187 
188 #ifdef CONFIG_PTDUMP_DEBUGFS
ptdump_show(struct seq_file * m,void * v)189 static int ptdump_show(struct seq_file *m, void *v)
190 {
191 	struct pg_state st = {
192 		.ptdump = {
193 			.note_page = note_page,
194 			.range = (struct ptdump_range[]) {
195 				{.start = 0, .end = max_addr},
196 				{.start = 0, .end = 0},
197 			}
198 		},
199 		.seq = m,
200 		.level = -1,
201 		.current_prot = 0,
202 		.check_wx = false,
203 		.wx_pages = 0,
204 		.start_address = 0,
205 		.marker = markers,
206 	};
207 
208 	get_online_mems();
209 	mutex_lock(&cpa_mutex);
210 	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
211 	mutex_unlock(&cpa_mutex);
212 	put_online_mems();
213 	return 0;
214 }
215 DEFINE_SHOW_ATTRIBUTE(ptdump);
216 #endif /* CONFIG_PTDUMP_DEBUGFS */
217 
ptdump_cmp(const void * a,const void * b)218 static int ptdump_cmp(const void *a, const void *b)
219 {
220 	const struct addr_marker *ama = a;
221 	const struct addr_marker *amb = b;
222 
223 	if (ama->start_address > amb->start_address)
224 		return 1;
225 	if (ama->start_address < amb->start_address)
226 		return -1;
227 	/*
228 	 * If the start addresses of two markers are identical sort markers in an
229 	 * order that considers areas contained within other areas correctly.
230 	 */
231 	if (ama->is_start && amb->is_start) {
232 		if (ama->size > amb->size)
233 			return -1;
234 		if (ama->size < amb->size)
235 			return 1;
236 		return 0;
237 	}
238 	if (!ama->is_start && !amb->is_start) {
239 		if (ama->size > amb->size)
240 			return 1;
241 		if (ama->size < amb->size)
242 			return -1;
243 		return 0;
244 	}
245 	if (ama->is_start)
246 		return 1;
247 	if (amb->is_start)
248 		return -1;
249 	return 0;
250 }
251 
add_marker(unsigned long start,unsigned long end,const char * name)252 static int add_marker(unsigned long start, unsigned long end, const char *name)
253 {
254 	size_t oldsize, newsize;
255 
256 	oldsize = markers_cnt * sizeof(*markers);
257 	newsize = oldsize + 2 * sizeof(*markers);
258 	if (!oldsize)
259 		markers = kvmalloc(newsize, GFP_KERNEL);
260 	else
261 		markers = kvrealloc(markers, newsize, GFP_KERNEL);
262 	if (!markers)
263 		goto error;
264 	markers[markers_cnt].is_start = 1;
265 	markers[markers_cnt].start_address = start;
266 	markers[markers_cnt].size = end - start;
267 	markers[markers_cnt].name = name;
268 	markers_cnt++;
269 	markers[markers_cnt].is_start = 0;
270 	markers[markers_cnt].start_address = end;
271 	markers[markers_cnt].size = end - start;
272 	markers[markers_cnt].name = name;
273 	markers_cnt++;
274 	return 0;
275 error:
276 	markers_cnt = 0;
277 	return -ENOMEM;
278 }
279 
pt_dump_init(void)280 static int pt_dump_init(void)
281 {
282 #ifdef CONFIG_KFENCE
283 	unsigned long kfence_start = (unsigned long)__kfence_pool;
284 #endif
285 	unsigned long lowcore = (unsigned long)get_lowcore();
286 	int rc;
287 
288 	/*
289 	 * Figure out the maximum virtual address being accessible with the
290 	 * kernel ASCE. We need this to keep the page table walker functions
291 	 * from accessing non-existent entries.
292 	 */
293 	max_addr = (get_lowcore()->kernel_asce.val & _REGION_ENTRY_TYPE_MASK) >> 2;
294 	max_addr = 1UL << (max_addr * 11 + 31);
295 	/* start + end markers - must be added first */
296 	rc = add_marker(0, -1UL, NULL);
297 	rc |= add_marker((unsigned long)_stext, (unsigned long)_end, "Kernel Image");
298 	rc |= add_marker(lowcore, lowcore + sizeof(struct lowcore), "Lowcore");
299 	rc |= add_marker(__identity_base, __identity_base + ident_map_size, "Identity Mapping");
300 	rc |= add_marker((unsigned long)__samode31, (unsigned long)__eamode31, "Amode31 Area");
301 	rc |= add_marker(MODULES_VADDR, MODULES_END, "Modules Area");
302 	rc |= add_marker(__abs_lowcore, __abs_lowcore + ABS_LOWCORE_MAP_SIZE, "Lowcore Area");
303 	rc |= add_marker(__memcpy_real_area, __memcpy_real_area + MEMCPY_REAL_SIZE, "Real Memory Copy Area");
304 	rc |= add_marker((unsigned long)vmemmap, (unsigned long)vmemmap + vmemmap_size, "vmemmap Area");
305 	rc |= add_marker(VMALLOC_START, VMALLOC_END, "vmalloc Area");
306 #ifdef CONFIG_KFENCE
307 	rc |= add_marker(kfence_start, kfence_start + KFENCE_POOL_SIZE, "KFence Pool");
308 #endif
309 #ifdef CONFIG_KMSAN
310 	rc |= add_marker(KMSAN_VMALLOC_SHADOW_START, KMSAN_VMALLOC_SHADOW_END, "Kmsan vmalloc Shadow");
311 	rc |= add_marker(KMSAN_VMALLOC_ORIGIN_START, KMSAN_VMALLOC_ORIGIN_END, "Kmsan vmalloc Origins");
312 	rc |= add_marker(KMSAN_MODULES_SHADOW_START, KMSAN_MODULES_SHADOW_END, "Kmsan Modules Shadow");
313 	rc |= add_marker(KMSAN_MODULES_ORIGIN_START, KMSAN_MODULES_ORIGIN_END, "Kmsan Modules Origins");
314 #endif
315 #ifdef CONFIG_KASAN
316 	rc |= add_marker(KASAN_SHADOW_START, KASAN_SHADOW_END, "Kasan Shadow");
317 #endif
318 	if (rc)
319 		goto error;
320 	sort(&markers[1], markers_cnt - 1, sizeof(*markers), ptdump_cmp, NULL);
321 #ifdef CONFIG_PTDUMP_DEBUGFS
322 	debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
323 #endif /* CONFIG_PTDUMP_DEBUGFS */
324 	return 0;
325 error:
326 	kvfree(markers);
327 	return -ENOMEM;
328 }
329 device_initcall(pt_dump_init);
330