[PATCH] x86/mm: fix overflow in __cpa_addr

Rik van Riel posted 1 patch 2 months, 1 week ago
arch/x86/mm/pat/set_memory.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] x86/mm: fix overflow in __cpa_addr
Posted by Rik van Riel 2 months, 1 week ago
On Sun, 05 Oct 2025 16:30:24 -0700
syzbot <syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com> wrote:


> ==================================================================
> BUG: KASAN: slab-out-of-bounds in __cpa_addr arch/x86/mm/pat/set_memory.c:309 [inline]
> BUG: KASAN: slab-out-of-bounds in __cpa_addr+0x1d3/0x220 arch/x86/mm/pat/set_memory.c:306
> Read of size 8 at addr ffff88801f75e8f8 by task syz.0.17/5978

This should fix it, unless syzbot finds an unexpected new way to
torture things.

---8<---
From e32cd734444d9a95bb2d1067d934f33ab66f050b Mon Sep 17 00:00:00 2001
From: Rik van Riel <riel@surriel.com>
Date: Sun, 5 Oct 2025 23:32:48 -0400
Subject: [PATCH] x86/mm: fix overflow in __cpa_addr

The change to have cpa_flush() call flush_kernel_pages() introduced
a bug where __cpa_addr() can access an address one larger than the
largest one in the cpa->pages array.

KASAN reports the issue like this:

BUG: KASAN: slab-out-of-bounds in __cpa_addr arch/x86/mm/pat/set_memory.c:309 [inline]
BUG: KASAN: slab-out-of-bounds in __cpa_addr+0x1d3/0x220 arch/x86/mm/pat/set_memory.c:306
Read of size 8 at addr ffff88801f75e8f8 by task syz.0.17/5978

This bug could cause cpa_flush() to not properly flush memory,
which somehow never showed any symptoms in my tests, possibly
because cpa_flush() is called so rarely, but could potentially
cause issues for other people.

Fix the issue by directly calculating the flush end address
from the start address.

Signed-off-by: Rik van Riel <riel@surriel.com>
Cc: stable@kernel.org
Reported-by: syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com
Fixes: 86e6815b316e ("x86/mm: Change cpa_flush() to call flush_kernel_range() directly")
---
 arch/x86/mm/pat/set_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index d2d54b8c4dbb..970981893c9b 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -446,7 +446,7 @@ static void cpa_flush(struct cpa_data *cpa, int cache)
 	}
 
 	start = fix_addr(__cpa_addr(cpa, 0));
-	end =   fix_addr(__cpa_addr(cpa, cpa->numpages));
+	end =   start + cpa->numpages * PAGE_SIZE;
 	if (cpa->force_flush_all)
 		end = TLB_FLUSH_ALL;
 
-- 
2.51.0
Re: [PATCH] x86/mm: fix overflow in __cpa_addr
Posted by Dave Hansen 2 months, 1 week ago
On 10/5/25 20:48, Rik van Riel wrote:
> Signed-off-by: Rik van Riel <riel@surriel.com>
> Cc: stable@kernel.org
> Reported-by: syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com
> Fixes: 86e6815b316e ("x86/mm: Change cpa_flush() to call flush_kernel_range() directly")

Hi Folks,

Thanks for the quick fix here!

But this doesn't need to go to stable, right? I think it just went to
Linus during the merge window last week so it never made it to a
release, so won't need a stable backport.
Re: [PATCH] x86/mm: fix overflow in __cpa_addr
Posted by Rik van Riel 2 months, 1 week ago
On Mon, 2025-10-06 at 15:22 -0700, Dave Hansen wrote:
> On 10/5/25 20:48, Rik van Riel wrote:
> > Signed-off-by: Rik van Riel <riel@surriel.com>
> > Cc: stable@kernel.org
> > Reported-by: syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com
> > Fixes: 86e6815b316e ("x86/mm: Change cpa_flush() to call
> > flush_kernel_range() directly")
> 
> Hi Folks,
> 
> Thanks for the quick fix here!
> 
> But this doesn't need to go to stable, right? I think it just went to
> Linus during the merge window last week so it never made it to a
> release, so won't need a stable backport.
> 
Oh good point.

I had totally lost track of when the problem
was merged upstream in the first place.

-- 
All Rights Reversed.
Re: [PATCH] x86/mm: fix overflow in __cpa_addr
Posted by Kiryl Shutsemau 2 months, 1 week ago
On Sun, Oct 05, 2025 at 11:48:05PM -0400, Rik van Riel wrote:
> On Sun, 05 Oct 2025 16:30:24 -0700
> syzbot <syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com> wrote:
> 
> 
> > ==================================================================
> > BUG: KASAN: slab-out-of-bounds in __cpa_addr arch/x86/mm/pat/set_memory.c:309 [inline]
> > BUG: KASAN: slab-out-of-bounds in __cpa_addr+0x1d3/0x220 arch/x86/mm/pat/set_memory.c:306
> > Read of size 8 at addr ffff88801f75e8f8 by task syz.0.17/5978
> 
> This should fix it, unless syzbot finds an unexpected new way to
> torture things.
> 
> ---8<---
> From e32cd734444d9a95bb2d1067d934f33ab66f050b Mon Sep 17 00:00:00 2001
> From: Rik van Riel <riel@surriel.com>
> Date: Sun, 5 Oct 2025 23:32:48 -0400
> Subject: [PATCH] x86/mm: fix overflow in __cpa_addr
> 
> The change to have cpa_flush() call flush_kernel_pages() introduced
> a bug where __cpa_addr() can access an address one larger than the
> largest one in the cpa->pages array.
> 
> KASAN reports the issue like this:
> 
> BUG: KASAN: slab-out-of-bounds in __cpa_addr arch/x86/mm/pat/set_memory.c:309 [inline]
> BUG: KASAN: slab-out-of-bounds in __cpa_addr+0x1d3/0x220 arch/x86/mm/pat/set_memory.c:306
> Read of size 8 at addr ffff88801f75e8f8 by task syz.0.17/5978
> 
> This bug could cause cpa_flush() to not properly flush memory,
> which somehow never showed any symptoms in my tests, possibly
> because cpa_flush() is called so rarely, but could potentially
> cause issues for other people.
> 
> Fix the issue by directly calculating the flush end address
> from the start address.
> 
> Signed-off-by: Rik van Riel <riel@surriel.com>
> Cc: stable@kernel.org
> Reported-by: syzbot+afec6555eef563c66c97@syzkaller.appspotmail.com
> Fixes: 86e6815b316e ("x86/mm: Change cpa_flush() to call flush_kernel_range() directly")

Reviewed-by: Kiryl Shutsemau <kas@kernel.org>

-- 
  Kiryl Shutsemau / Kirill A. Shutemov