riscv: Assorted bug fixes
Subsystems (2)
cover riscv: Assorted bug fixes
This series contains five independent bug fixes across the RISC-V architecture code, found with the help of Claude AI (claude-4.6-opus):
1. riscv: errata: Fix bitwise vs logical AND in MIPS errata patching
- Logical AND (&&) was used instead of bitwise AND (&) when checking errata flags, causing all errata patches to be applied when any single one was detected.
2. riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on error
- On copyin failure, uninitialized stack data was written into the target task's pt_regs, corrupting registers and potentially leaking kernel stack contents.
3. riscv: mm: Fix NULL pointer dereference in __set_memory
- find_vm_area() can return NULL but was dereferenced without a check.
4. riscv: mm: Fix NULL dereferences in napot hugetlb functions
- huge_pte_offset() can return NULL in the napot path but was used without NULL checks in huge_ptep_set_access_flags() and huge_ptep_set_wrprotect().
5. riscv: mm: Fix TOCTOU race in remove_pte_mapping
- The PTE was read twice (once via ptep_get, once via *ptep), creating a race window where another CPU could modify the PTE between reads.
All patches are small and self-contained. These bugs were identified through AI-assisted code review using Claude, which also assisted in writing the fixes. Each patch has been manually reviewed for correctness.
Michael Neuling (5):
riscv: errata: Fix bitwise vs logical AND in MIPS errata patching
riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on
error
riscv: mm: Fix NULL pointer dereference in __set_memory
riscv: mm: Fix NULL dereferences in napot hugetlb functions
riscv: mm: Fix TOCTOU race in remove_pte_mapping
arch/riscv/errata/mips/errata.c | 2 +- arch/riscv/kernel/ptrace.c | 4 ++-- arch/riscv/mm/hugetlbpage.c | 4 ++++ arch/riscv/mm/init.c | 2 +- arch/riscv/mm/pageattr.c | 4 ++++ 5 files changed, 12 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 riscv: errata: Fix bitwise vs logical AND in MIPS errata patching
The condition checking whether a specific errata needs patching uses logical AND (&&) instead of bitwise AND (&). Since logical AND only checks that both operands are non-zero, this causes all errata patches to be applied whenever any single errata is detected, rather than only applying the matching one.
The SiFive errata implementation correctly uses bitwise AND for the same check.
--- arch/riscv/errata/mips/errata.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/errata/mips/errata.c +1 -1
@@ -57,7 +57,7 @@ void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, } tmp = (1U << alt->patch_id); -if (cpu_req_errata && tmp) { +if (cpu_req_errata & tmp) {mutex_lock (&text_mutex);patch_text_nosync (ALT_OLD_PTR (alt),ALT_ALT_PTR (alt), alt->alt_len);
Signature · 1 line
2.43.0
Paul Walmsley
maintainerRe: [PATCH 1/5] riscv: errata: Fix bitwise vs logical AND in MIPS errata patching
Quoted 6 lines · Michael Neuling
The condition checking whether a specific errata needs patching uses logical AND (&&) instead of bitwise AND (&). Since logical AND only checks that both operands are non-zero, this causes all errata patches to be applied whenever any single errata is detected, rather than only applying the matching one.
Quoted 6 lines
The SiFive errata implementation correctly uses bitwise AND for the same check.
Fixes 0b0ca959d2 ("riscv: errata: Fix the PAUSE Opcode for MIPS P8700")Signed-off-by Michael Neuling <[email protected]>Assisted-by Cursor:claude-4.6-opus-high-thinking
Thanks, after fixing the checkpatch warning regarding the Fixes: line in the patch description, queued for v7.1-rc.
- Paul
PATCH 2/5 riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on error
In reply to Michael Neulingcompat_riscv_gpr_set() calls cregs_to_regs() unconditionally, even when user_regset_copyin() fails. Since cregs is an uninitialized stack variable, a copyin failure causes uninitialized stack data to be written into the target task's pt_regs, corrupting its register state and potentially leaking kernel stack contents.
Only call cregs_to_regs() when user_regset_copyin() succeeds.
--- arch/riscv/kernel/ptrace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/ptrace.c +2 -2
@@ -577,8 +577,8 @@ static int compat_riscv_gpr_set(struct task_struct *target,struct compat_user_regs_struct cregs; ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, &cregs,0 ,-1 ); - -cregs_to_regs (&cregs,task_pt_regs (target)); +if (!ret) +cregs_to_regs (&cregs,task_pt_regs (target));return ret; }
Signature · 1 line
2.43.0
Paul Walmsley
maintainerRe: [PATCH 2/5] riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on error
Hi Mikey,
Quoted 11 lines · Michael Neuling
compat_riscv_gpr_set()callscregs_to_regs()unconditionally, even whenuser_regset_copyin()fails. Since cregs is an uninitialized stack variable, a copyin failure causes uninitialized stack data to be written into the target task'spt_regs, corrupting its register state and potentially leaking kernel stack contents.Only call
cregs_to_regs()whenuser_regset_copyin()succeeds.Fixes 4608c15959 ("riscv: compat: ptrace: Add compat_arch_ptrace implement")Signed-off-by Michael Neuling <[email protected]>Assisted-by Cursor:claude-4.6-opus-high-thinking
Thanks. I'm wondering if we have the same issue in compat_restore_sigcontext(). Care to add that to your patch if you agree?
- Paul
Re: [PATCH 2/5] riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on error
Quoted 2 lines
Thanks. I'm wondering if we have the same issue in
compat_restore_sigcontext(). Care to add that to your patch if you agree?
Agreed, resending.
Mikey
PATCH v2 1/1 riscv: Fix register corruption from uninitialized cregs on error
In reply to Paul Walmsleycompat_riscv_gpr_set() calls cregs_to_regs() unconditionally, even when user_regset_copyin() fails. Since cregs is an uninitialized stack variable, a copyin failure causes uninitialized stack data to be written into the target task's pt_regs, corrupting its register state and potentially leaking kernel stack contents.
compat_restore_sigcontext() has the same issue: it calls cregs_to_regs() even when __copy_from_user() fails, leading to the same corruption of the signal-returning task's register state on error.
Only call cregs_to_regs() when the user copy succeeds.
--- arch/riscv/kernel/compat_signal.c | 2 ++ arch/riscv/kernel/ptrace.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
arch/riscv/kernel/compat_signal.c +2 -0
@@ -107,6 +107,8 @@ static long compat_restore_sigcontext(struct pt_regs *regs,/* sc_regs is structured the same as the start of pt_regs */ err =__copy_from_user (&cregs, &sc->sc_regs,sizeof (sc->sc_regs)); +if (unlikely (err)) +return err;cregs_to_regs (&cregs, regs);
arch/riscv/kernel/ptrace.c +2 -2
@@ -577,8 +577,8 @@ static int compat_riscv_gpr_set(struct task_struct *target,struct compat_user_regs_struct cregs; ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, &cregs,0 ,-1 ); - -cregs_to_regs (&cregs,task_pt_regs (target)); +if (!ret) +cregs_to_regs (&cregs,task_pt_regs (target));return ret; }
Signature · 1 line
2.43.0
Paul Walmsley
maintainerRe: [PATCH v2] riscv: Fix register corruption from uninitialized cregs on error
Quoted 10 lines · Michael Neuling
compat_riscv_gpr_set()callscregs_to_regs()unconditionally, even whenuser_regset_copyin()fails. Since cregs is an uninitialized stack variable, a copyin failure causes uninitialized stack data to be written into the target task'spt_regs, corrupting its register state and potentially leaking kernel stack contents.
compat_restore_sigcontext()has the same issue: it callscregs_to_regs()even when__copy_from_user()fails, leading to the same corruption of the signal-returning task's register state on error.
Quoted 6 lines
Only call
cregs_to_regs()when the user copy succeeds.Fixes 4608c159594f ("riscv: compat: ptrace: Add compat_arch_ptrace implement")Fixes 7383ee05314b ("riscv: compat: signal: Add rt_frame implementation")Signed-off-by Michael Neuling <[email protected]>Assisted-by Cursor:claude-4.6-opus-high-thinking
Thanks very much; queued for v7.1-rc.
- Paul
PATCH 3/5 riscv: mm: Fix NULL pointer dereference in __set_memory
In reply to Michael Neulingfind_vm_area() can return NULL if no vm_struct covers the given address. The code immediately dereferences area->addr without a NULL check. While is_vmalloc_or_module_addr() confirms the address falls within the vmalloc/module address range, it does not guarantee the address belongs to an active allocation, so find_vm_area() may still return NULL.
Add the missing NULL check.
--- arch/riscv/mm/pageattr.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/mm/pageattr.c +4 -0
@@ -289,6 +289,10 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,int i, page_start; area =find_vm_area ((void *)start); +if (!area) { + ret = -EINVAL ; + goto unlock; + } page_start = (start - (unsigned long )area->addr) >>PAGE_SHIFT ;for (i = page_start; i < page_start + numpages; ++i) {
Signature · 1 line
2.43.0
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 20 lines · Michael Neuling
find_vm_area()can return NULL if novm_structcovers the given address. The code immediately dereferencesarea->addrwithout a NULL check. Whileis_vmalloc_or_module_addr()confirms the address falls within the vmalloc/module address range, it does not guarantee the address belongs to an active allocation, sofind_vm_area()may still return NULL.Add the missing NULL check.
Fixes 311cd2f6e2 ("riscv: Fix set_memory_XX() and set_direct_map_XX() by splitting huge linear mappings")Signed-off-by Michael Neuling <[email protected]>Assisted-by Cursor:claude-4.6-opus-high-thinking--- arch/riscv/mm/pageattr.c | 4 ++++ 1 file changed, 4 insertions(+)arch/riscv/mm/pageattr.c +0
-0
arch/riscv/mm/pageattr.c static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, · line 298
Quoted 10 lines
arch/riscv/mm/pageattr.c +4
-0@@ -289,6 +289,10 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,int i, page_start; area =find_vm_area ((void *)start); +if (!area) { + ret = -EINVAL ; + goto unlock; + } page_start = (start - (unsigned long )area->addr) >>PAGE_SHIFT ;for (i = page_start; i < page_start + numpages; ++i) {
Which caller would end up calling __set_memory() in such a way?
Signature · 3 lines
Cheers, David
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 10 lines
arch/riscv/mm/pageattr.c static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, · line 298
Quoted 8 lines
area = find_vm_area((void *)start);arch/riscv/mm/pageattr.c +4
-0+if (!area) { + ret = -EINVAL ; + goto unlock; + } page_start = (start - (unsigned long )area->addr) >>PAGE_SHIFT ;for (i = page_start; i < page_start + numpages; ++i) {Which caller would end up calling
__set_memory()in such a way?
It wouldn't. You're right on this and the other two. Sorry for the noise.
Mikey
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 7 lines · Michael Neuling
Quoted 7 lines
Quoted 7 lines
area = find_vm_area((void *)start);arch/riscv/mm/pageattr.c +4
-0+if (!area) { + ret = -EINVAL ; + goto unlock; + } page_start = (start - (unsigned long )area->addr) >>PAGE_SHIFT ;
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
for (i = page_start; i < page_start + numpages; ++i) {Which caller would end up calling
__set_memory()in such a way?It wouldn't. You're right on this and the other two. Sorry for the noise.
Slopped-by: Cursor:claude-4.6-opus-high-thinking
:)
Patch #1 seems reasonable (although non-critical); not sure about patch #2, leaving both to Risc-V people!
Signature · 3 lines
Cheers, David
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 9 lines · Michael Neuling
Quoted 9 lines
Quoted 8 lines
area = find_vm_area((void *)start);arch/riscv/mm/pageattr.c +4
-0+if (!area) { + ret = -EINVAL ; + goto unlock; + } page_start = (start - (unsigned long )area->addr) >>PAGE_SHIFT ;for (i = page_start; i < page_start + numpages; ++i) {
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Which caller would end up calling
__set_memory()in such a way?It wouldn't. You're right on this and the other two. Sorry for the noise.
Slopped-by: Cursor:claude-4.6-opus-high-thinking
:)
It's a general tendency of LLMs to generate overly defensive code and raise concerns about theoretical issues.
Maybe it's worth taking into account when writing prompts specific to kernel development.
Quoted 7 lines
Patch #1 seems reasonable (although non-critical); not sure about patch #2, leaving both to Risc-V people!
Signature · 3 lines
Cheers, David
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 5 lines · Mike Rapoport
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Michael Neuling
It wouldn't. You're right on this and the other two. Sorry for the noise.
Quoted 8 lines
Quoted 3 lines
Slopped-by: Cursor:claude-4.6-opus-high-thinking
:)
It's a general tendency of LLMs to generate overly defensive code and raise concerns about theoretical issues.
Maybe it's worth taking into account when writing prompts specific to kernel development.
Yes, and I can only urge people to actually check what the LLM spits out, so they won't lose their credibility with maintainers ;)
Signature · 3 lines
Cheers, David
Re: [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 5 lines · Mike Rapoport
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Michael Neuling
It wouldn't. You're right on this and the other two. Sorry for the noise.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Slopped-by: Cursor:claude-4.6-opus-high-thinking
:)
It's a general tendency of LLMs to generate overly defensive code and raise concerns about theoretical issues.
Maybe it's worth taking into account when writing prompts specific to kernel development.
We've had some success using it to review some patches for maintainers.
I was attempting to use it on existing code but I agree this was overly defensive
and didn't consider the broader context.
Quoted 2 lines
Yes, and I can only urge people to actually check what the LLM spits out, so they won't lose their credibility with maintainers ;)
I did actually dismiss more than half of what it produced. Maybe that makes it worse :-)
I'm not as familiar with mm so it was a mistake on my part to send these.
Mikey
PATCH 4/5 riscv: mm: Fix NULL dereferences in napot hugetlb functions
In reply to Michael Neulinghuge_pte_offset() can return NULL when any level of the page table walk encounters a non-present entry. Both huge_ptep_set_access_flags() and huge_ptep_set_wrprotect() re-derive ptep via huge_pte_offset() in the napot path but use the result without a NULL check, leading to NULL pointer dereferences in get_clear_contig_flush() and set_pte_at().
Add NULL checks after huge_pte_offset() in both functions.
--- arch/riscv/mm/hugetlbpage.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/mm/hugetlbpage.c +4 -0
@@ -288,6 +288,8 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, order =napot_cont_order (pte); pte_num =napot_pte_num (order); ptep =huge_pte_offset (mm, addr,napot_cont_size (order)); +if (!ptep) +return 0 ; orig_pte =get_clear_contig_flush (mm, addr, ptep, pte_num);if (pte_dirty (orig_pte))
@@ -335,6 +337,8 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, order =napot_cont_order (pte); pte_num =napot_pte_num (order); ptep =huge_pte_offset (mm, addr,napot_cont_size (order)); +if (!ptep) +return ; orig_pte =get_clear_contig_flush (mm, addr, ptep, pte_num); orig_pte =pte_wrprotect (orig_pte);
Signature · 1 line
2.43.0
Re: [PATCH 4/5] riscv: mm: Fix NULL dereferences in napot hugetlb functions
Quoted 29 lines · Michael Neuling
huge_pte_offset()can return NULL when any level of the page table walk encounters a non-present entry. Bothhuge_ptep_set_access_flags()andhuge_ptep_set_wrprotect()re-derive ptep viahuge_pte_offset()in the napot path but use the result without a NULL check, leading to NULL pointer dereferences inget_clear_contig_flush()andset_pte_at().Add NULL checks after
huge_pte_offset()in both functions.Fixes 82a1a1f3bf ("riscv: mm: support Svnapot in hugetlb page")Signed-off-by Michael Neuling <[email protected]>Assisted-by Cursor:claude-4.6-opus-high-thinking--- arch/riscv/mm/hugetlbpage.c | 4 ++++ 1 file changed, 4 insertions(+)arch/riscv/mm/hugetlbpage.c +2
-0@@ -288,6 +288,8 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, order =napot_cont_order (pte); pte_num =napot_pte_num (order); ptep =huge_pte_offset (mm, addr,napot_cont_size (order)); +if (!ptep) +return 0 ; orig_pte =get_clear_contig_flush (mm, addr, ptep, pte_num);if (pte_dirty (orig_pte))
arch/riscv/mm/hugetlbpage.c void huge_ptep_set_wrprotect(struct mm_struct *mm, · line 344
Quoted 8 lines
arch/riscv/mm/hugetlbpage.c +2
-0@@ -335,6 +337,8 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, order =napot_cont_order (pte); pte_num =napot_pte_num (order); ptep =huge_pte_offset (mm, addr,napot_cont_size (order)); +if (!ptep) +return ; orig_pte =get_clear_contig_flush (mm, addr, ptep, pte_num); orig_pte =pte_wrprotect (orig_pte);
These functions are called when we previously verified that there is indeed a pte mapped. And while holding the PTL to protect concurrent unmapping.
So how should this possibly trigger?
Signature · 3 lines
Cheers, David
PATCH 5/5 riscv: mm: Fix TOCTOU race in remove_pte_mapping
In reply to Michael Neulingremove_pte_mapping() reads the PTE via ptep_get() (a READ_ONCE) into a local variable, but then checks pte_present(*ptep) by dereferencing the pointer directly, reading the PTE a second time. If another CPU modifies the PTE between the two reads, pte_present may check a different value than what was captured, and the subsequent pte_page() could derive the wrong page to free.
Use the already-captured local pte variable for the pte_present check.
--- arch/riscv/mm/init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/mm/init.c +1 -1
@@ -1562,7 +1562,7 @@ static void __meminit remove_pte_mapping(pte_t *pte_base, unsigned long addr, un ptep = pte_base +pte_index (addr); pte =ptep_get (ptep); -if (!pte_present (*ptep)) +if (!pte_present (pte))continue ;pte_clear (&init_mm, addr, ptep);
Signature · 1 line
2.43.0
Re: [PATCH 5/5] riscv: mm: Fix TOCTOU race in remove_pte_mapping
Quoted 4 lines · Michael Neuling
remove_pte_mapping()reads the PTE viaptep_get()(aREAD_ONCE) into a local variable, but then checkspte_present(*ptep)by dereferencing the pointer directly, reading the PTE a second time. If another CPU modifies the PTE between the two reads
Is that even possible?
The code does not use any locking, so nothing would be safe here if races could happen, no?
Signature · 3 lines
Cheers, David