Re: [PATCH 1/2] x86: mm: ptdump: Calculate effective permissions correctly

From: Steven Price
Date: Tue May 26 2020 - 06:41:49 EST


On 22/05/2020 19:07, Qian Cai wrote:
On Thu, May 21, 2020 at 04:23:07PM +0100, Steven Price wrote:
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -249,10 +249,22 @@ static void note_wx(struct pg_state *st, unsigned long addr)
@@ -270,16 +282,10 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
struct seq_file *m = st->seq;
new_prot = val & PTE_FLAGS_MASK;
+ new_eff = st->prot_levels[level];

This will trigger,

.config (if ever matters):
https://raw.githubusercontent.com/cailca/linux-mm/master/x86.config

[ 104.532621] UBSAN: array-index-out-of-bounds in arch/x86/mm/dump_pagetables.c:284:27
[ 104.542620] index -1 is out of range for type 'pgprotval_t [5]'

Doh! In this case the result (new_eff) is never actually used: the -1 is used just to flush the last proper entry out, so in this case val == 0 which means the following statement sets new_eff = 0. But obviously an out-of-bounds access isn't great. Thanks for testing!

The following patch should fix it up by making the assignment conditional on val != 0.

Andrew: Would you like me to resend, or can you squash in the below?

Steve

-----8<----
diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c
index 17aa7ac94a34..ea9010113f69 100644
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -282,10 +282,10 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
struct seq_file *m = st->seq;

new_prot = val & PTE_FLAGS_MASK;
- new_eff = st->prot_levels[level];
-
if (!val)
new_eff = 0;
+ else
+ new_eff = st->prot_levels[level];

/*
* If we have a "break" in the series, we need to flush the state that