Skip to content

Commit 453d87f

Browse files
ruscurmpe
authored andcommitted
powerpc/mm: Warn if W+X pages found on boot
Implement code to walk all pages and warn if any are found to be both writable and executable. Depends on STRICT_KERNEL_RWX enabled, and is behind the DEBUG_WX config option. This only runs on boot and has no runtime performance implications. Very heavily influenced (and in some cases copied verbatim) from the ARM64 code written by Laura Abbott (thanks!), since our ptdump infrastructure is similar. Signed-off-by: Russell Currey <ruscur@russell.cc> [mpe: Fixup build error when disabled] Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 5f18cbd commit 453d87f

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎arch/powerpc/Kconfig.debug‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,25 @@ config PPC_PTDUMP
361361

362362
If you are unsure, say N.
363363

364+
config PPC_DEBUG_WX
365+
bool "Warn on W+X mappings at boot"
366+
depends on PPC_PTDUMP
367+
help
368+
Generate a warning if any W+X mappings are found at boot.
369+
370+
This is useful for discovering cases where the kernel is leaving
371+
W+X mappings after applying NX, as such mappings are a security risk.
372+
373+
Note that even if the check fails, your kernel is possibly
374+
still fine, as W+X mappings are not a security hole in
375+
themselves, what they do is that they make the exploitation
376+
of other unfixed kernel bugs easier.
377+
378+
There is no runtime or memory usage effect of this option
379+
once the kernel has booted up - it's a one time check.
380+
381+
If in doubt, say "Y".
382+
364383
config PPC_FAST_ENDIAN_SWITCH
365384
bool "Deprecated fast endian-switch syscall"
366385
depends on DEBUG_KERNEL && PPC_BOOK3S_64

‎arch/powerpc/include/asm/pgtable.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ void mark_initmem_nx(void);
105105
static inline void mark_initmem_nx(void) { }
106106
#endif
107107

108+
#ifdef CONFIG_PPC_DEBUG_WX
109+
void ptdump_check_wx(void);
110+
#else
111+
static inline void ptdump_check_wx(void) { }
112+
#endif
113+
108114
/*
109115
* When used, PTE_FRAG_NR is defined in subarch pgtable.h
110116
* so we are sure it is included when arriving here.

‎arch/powerpc/mm/pgtable_32.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ void mark_rodata_ro(void)
396396
PFN_DOWN((unsigned long)__start_rodata);
397397

398398
change_page_attr(page, numpages, PAGE_KERNEL_RO);
399+
400+
// mark_initmem_nx() should have already run by now
401+
ptdump_check_wx();
399402
}
400403
#endif
401404

‎arch/powerpc/mm/pgtable_64.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ void mark_rodata_ro(void)
332332
radix__mark_rodata_ro();
333333
else
334334
hash__mark_rodata_ro();
335+
336+
// mark_initmem_nx() should have already run by now
337+
ptdump_check_wx();
335338
}
336339

337340
void mark_initmem_nx(void)

‎arch/powerpc/mm/ptdump/ptdump.c‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "ptdump.h"
3232

3333
#ifdef CONFIG_PPC32
34-
#define KERN_VIRT_START 0
34+
#define KERN_VIRT_START PAGE_OFFSET
3535
#endif
3636

3737
/*
@@ -68,6 +68,8 @@ struct pg_state {
6868
unsigned long last_pa;
6969
unsigned int level;
7070
u64 current_flags;
71+
bool check_wx;
72+
unsigned long wx_pages;
7173
};
7274

7375
struct addr_marker {
@@ -181,6 +183,20 @@ static void dump_addr(struct pg_state *st, unsigned long addr)
181183

182184
}
183185

186+
static void note_prot_wx(struct pg_state *st, unsigned long addr)
187+
{
188+
if (!st->check_wx)
189+
return;
190+
191+
if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X)))
192+
return;
193+
194+
WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n",
195+
(void *)st->start_address, (void *)st->start_address);
196+
197+
st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
198+
}
199+
184200
static void note_page(struct pg_state *st, unsigned long addr,
185201
unsigned int level, u64 val)
186202
{
@@ -210,6 +226,7 @@ static void note_page(struct pg_state *st, unsigned long addr,
210226

211227
/* Check the PTE flags */
212228
if (st->current_flags) {
229+
note_prot_wx(st, addr);
213230
dump_addr(st, addr);
214231

215232
/* Dump all the flags */
@@ -387,6 +404,30 @@ static void build_pgtable_complete_mask(void)
387404
pg_level[i].mask |= pg_level[i].flag[j].mask;
388405
}
389406

407+
#ifdef CONFIG_PPC_DEBUG_WX
408+
void ptdump_check_wx(void)
409+
{
410+
struct pg_state st = {
411+
.seq = NULL,
412+
.marker = address_markers,
413+
.check_wx = true,
414+
};
415+
416+
if (radix_enabled())
417+
st.start_address = PAGE_OFFSET;
418+
else
419+
st.start_address = KERN_VIRT_START;
420+
421+
walk_pagetables(&st);
422+
423+
if (st.wx_pages)
424+
pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
425+
st.wx_pages);
426+
else
427+
pr_info("Checked W+X mappings: passed, no W+X pages found\n");
428+
}
429+
#endif
430+
390431
static int ptdump_init(void)
391432
{
392433
struct dentry *debugfs_file;

0 commit comments

Comments
 (0)