Commit 08c8e685 authored by Heiko Carstens's avatar Heiko Carstens Committed by Vasily Gorbik

s390: add ARCH_HAS_DEBUG_WX support

Checks the whole kernel address space for W+X mappings. Note that
currently the first lowcore page unfortunately has to be mapped
W+X. Therefore this not reported as an insecure mapping.

For the very same reason the wording is also different to other
architectures if the test passes:

On s390 it is "no unexpected W+X pages found" instead of
"no W+X pages found".
Tested-by: default avatarVasily Gorbik <gor@linux.ibm.com>
Signed-off-by: default avatarHeiko Carstens <hca@linux.ibm.com>
Signed-off-by: default avatarVasily Gorbik <gor@linux.ibm.com>
parent 6bf9a639
...@@ -60,6 +60,7 @@ config S390 ...@@ -60,6 +60,7 @@ config S390
def_bool y def_bool y
select ARCH_BINFMT_ELF_STATE select ARCH_BINFMT_ELF_STATE
select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_VM_PGTABLE
select ARCH_HAS_DEBUG_WX
select ARCH_HAS_DEVMEM_IS_ALLOWED select ARCH_HAS_DEVMEM_IS_ALLOWED
select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_ELF_RANDOMIZE
select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_FORTIFY_SOURCE
......
...@@ -774,6 +774,7 @@ CONFIG_MAGIC_SYSRQ=y ...@@ -774,6 +774,7 @@ CONFIG_MAGIC_SYSRQ=y
CONFIG_DEBUG_PAGEALLOC=y CONFIG_DEBUG_PAGEALLOC=y
CONFIG_PAGE_OWNER=y CONFIG_PAGE_OWNER=y
CONFIG_DEBUG_RODATA_TEST=y CONFIG_DEBUG_RODATA_TEST=y
CONFIG_DEBUG_WX=y
CONFIG_PTDUMP_DEBUGFS=y CONFIG_PTDUMP_DEBUGFS=y
CONFIG_DEBUG_OBJECTS=y CONFIG_DEBUG_OBJECTS=y
CONFIG_DEBUG_OBJECTS_SELFTEST=y CONFIG_DEBUG_OBJECTS_SELFTEST=y
......
...@@ -758,6 +758,7 @@ CONFIG_GDB_SCRIPTS=y ...@@ -758,6 +758,7 @@ CONFIG_GDB_SCRIPTS=y
CONFIG_FRAME_WARN=1024 CONFIG_FRAME_WARN=1024
CONFIG_DEBUG_SECTION_MISMATCH=y CONFIG_DEBUG_SECTION_MISMATCH=y
CONFIG_MAGIC_SYSRQ=y CONFIG_MAGIC_SYSRQ=y
CONFIG_DEBUG_WX=y
CONFIG_PTDUMP_DEBUGFS=y CONFIG_PTDUMP_DEBUGFS=y
CONFIG_DEBUG_MEMORY_INIT=y CONFIG_DEBUG_MEMORY_INIT=y
CONFIG_PANIC_ON_OOPS=y CONFIG_PANIC_ON_OOPS=y
......
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_S390_PTDUMP_H
#define _ASM_S390_PTDUMP_H
void ptdump_check_wx(void);
static inline void debug_checkwx(void)
{
if (IS_ENABLED(CONFIG_DEBUG_WX))
ptdump_check_wx();
}
#endif /* _ASM_S390_PTDUMP_H */
...@@ -8,7 +8,7 @@ obj-y += page-states.o pageattr.o pgtable.o pgalloc.o ...@@ -8,7 +8,7 @@ obj-y += page-states.o pageattr.o pgtable.o pgalloc.o
obj-$(CONFIG_CMM) += cmm.o obj-$(CONFIG_CMM) += cmm.o
obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
obj-$(CONFIG_PTDUMP_DEBUGFS) += dump_pagetables.o obj-$(CONFIG_PTDUMP_CORE) += dump_pagetables.o
obj-$(CONFIG_PGSTE) += gmap.o obj-$(CONFIG_PGSTE) += gmap.o
KASAN_SANITIZE_kasan_init.o := n KASAN_SANITIZE_kasan_init.o := n
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/kasan.h> #include <linux/kasan.h>
#include <asm/ptdump.h>
#include <asm/kasan.h> #include <asm/kasan.h>
#include <asm/sections.h> #include <asm/sections.h>
...@@ -47,6 +48,8 @@ struct pg_state { ...@@ -47,6 +48,8 @@ struct pg_state {
struct seq_file *seq; struct seq_file *seq;
int level; int level;
unsigned int current_prot; unsigned int current_prot;
bool check_wx;
unsigned long wx_pages;
unsigned long start_address; unsigned long start_address;
const struct addr_marker *marker; const struct addr_marker *marker;
}; };
...@@ -81,6 +84,26 @@ static void print_prot(struct seq_file *m, unsigned int pr, int level) ...@@ -81,6 +84,26 @@ static void print_prot(struct seq_file *m, unsigned int pr, int level)
pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n"); pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
} }
static void note_prot_wx(struct pg_state *st, unsigned long addr)
{
#ifdef CONFIG_DEBUG_WX
if (!st->check_wx)
return;
if (st->current_prot & _PAGE_INVALID)
return;
if (st->current_prot & _PAGE_PROTECT)
return;
if (st->current_prot & _PAGE_NOEXEC)
return;
/* The first lowcore page is currently still W+X. */
if (addr == PAGE_SIZE)
return;
WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n",
(void *)st->start_address);
st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
#endif /* CONFIG_DEBUG_WX */
}
static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val) static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
{ {
int width = sizeof(unsigned long) * 2; int width = sizeof(unsigned long) * 2;
...@@ -109,6 +132,7 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, ...@@ -109,6 +132,7 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
st->level = level; st->level = level;
} else if (prot != st->current_prot || level != st->level || } else if (prot != st->current_prot || level != st->level ||
addr >= st->marker[1].start_address) { addr >= st->marker[1].start_address) {
note_prot_wx(st, addr);
pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ", pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
width, st->start_address, width, st->start_address,
width, addr); width, addr);
...@@ -129,6 +153,40 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, ...@@ -129,6 +153,40 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
} }
} }
#ifdef CONFIG_DEBUG_WX
void ptdump_check_wx(void)
{
struct pg_state st = {
.ptdump = {
.note_page = note_page,
.range = (struct ptdump_range[]) {
{.start = 0, .end = max_addr},
{.start = 0, .end = 0},
}
},
.seq = NULL,
.level = -1,
.current_prot = 0,
.check_wx = true,
.wx_pages = 0,
.start_address = 0,
.marker = (struct addr_marker[]) {
{ .start_address = 0, .name = NULL},
{ .start_address = -1, .name = NULL},
},
};
if (!MACHINE_HAS_NX)
return;
ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
if (st.wx_pages)
pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
else
pr_info("Checked W+X mappings: passed, no unexpected W+X pages found\n");
}
#endif /* CONFIG_DEBUG_WX */
#ifdef CONFIG_PTDUMP_DEBUGFS
static int ptdump_show(struct seq_file *m, void *v) static int ptdump_show(struct seq_file *m, void *v)
{ {
struct pg_state st = { struct pg_state st = {
...@@ -142,6 +200,8 @@ static int ptdump_show(struct seq_file *m, void *v) ...@@ -142,6 +200,8 @@ static int ptdump_show(struct seq_file *m, void *v)
.seq = m, .seq = m,
.level = -1, .level = -1,
.current_prot = 0, .current_prot = 0,
.check_wx = false,
.wx_pages = 0,
.start_address = 0, .start_address = 0,
.marker = address_markers, .marker = address_markers,
}; };
...@@ -154,6 +214,7 @@ static int ptdump_show(struct seq_file *m, void *v) ...@@ -154,6 +214,7 @@ static int ptdump_show(struct seq_file *m, void *v)
return 0; return 0;
} }
DEFINE_SHOW_ATTRIBUTE(ptdump); DEFINE_SHOW_ATTRIBUTE(ptdump);
#endif /* CONFIG_PTDUMP_DEBUGFS */
static int pt_dump_init(void) static int pt_dump_init(void)
{ {
...@@ -167,7 +228,8 @@ static int pt_dump_init(void) ...@@ -167,7 +228,8 @@ static int pt_dump_init(void)
address_markers[MODULES_NR].start_address = MODULES_VADDR; address_markers[MODULES_NR].start_address = MODULES_VADDR;
address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap; address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
address_markers[VMALLOC_NR].start_address = VMALLOC_START; address_markers[VMALLOC_NR].start_address = VMALLOC_START;
debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops); if (IS_ENABLED(CONFIG_PTDUMP_DEBUGFS))
debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
return 0; return 0;
} }
device_initcall(pt_dump_init); device_initcall(pt_dump_init);
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <asm/processor.h> #include <asm/processor.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <asm/pgalloc.h> #include <asm/pgalloc.h>
#include <asm/ptdump.h>
#include <asm/dma.h> #include <asm/dma.h>
#include <asm/lowcore.h> #include <asm/lowcore.h>
#include <asm/tlb.h> #include <asm/tlb.h>
...@@ -129,6 +130,7 @@ void mark_rodata_ro(void) ...@@ -129,6 +130,7 @@ void mark_rodata_ro(void)
set_memory_ro((unsigned long)__start_ro_after_init, size >> PAGE_SHIFT); set_memory_ro((unsigned long)__start_ro_after_init, size >> PAGE_SHIFT);
pr_info("Write protected read-only-after-init data: %luk\n", size >> 10); pr_info("Write protected read-only-after-init data: %luk\n", size >> 10);
debug_checkwx();
} }
int set_memory_encrypted(unsigned long addr, int numpages) int set_memory_encrypted(unsigned long addr, int numpages)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment