linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ARC: MM: fix UB and kernel resourse leak in do_page_fault
@ 2018-11-07 12:12 Eugeniy Paltsev
  2018-11-09 17:32 ` Vineet Gupta
  0 siblings, 1 reply; 2+ messages in thread
From: Eugeniy Paltsev @ 2018-11-07 12:12 UTC (permalink / raw)
  To: linux-snps-arc, Vineet Gupta
  Cc: linux-kernel, Alexey Brodkin, Eric W . Biederman, linux-arch,
	Eugeniy Paltsev

Commit 15773ae938d8 ("signal/arc: Use force_sig_fault where
appropriate") adds undefined behaviour and kernel resource leak
to userspace in ARC do_page_fault() implementation.

This happens because we don't initialize si_code variable after we
switch to force_sig_fault using. si_code (as a part of siginfo_t
structure) was previously initialized by clear_siginfo(&info) call
which was removed.

Undefined behaviour path:
-------------------->8---------------------------
}}} a/arch/arc/mm/fault.c
!! -67,6 +67,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
        struct task_struct *tsk = current;
        struct mm_struct *mm = tsk->mm;
+       /// >>> si_code - uninitialized
        int si_code;
        int ret;
        vm_fault_t fault;
        int write = regs->ecr_cause & ECR_C_PROTV_STORE;  /* ST/EX */
!! -81,8 +82,10 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
         * only copy the information from the master page table,
         * nothing more.
         */
+       /// >>> take true branch
        if (address >= VMALLOC_START) {
                ret = handle_kernel_vaddr_fault(address);
+               /// >>> take true branch
                if (unlikely(ret))
+                       /// >>> jump to label "bad_area_nosemaphore"
                        goto bad_area_nosemaphore;
                else
!! -193,10 +196,13 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
 bad_area:
        up_read(&mm->mmap_sem);

+       /// >>> reach label "bad_area_nosemaphore"
 bad_area_nosemaphore:
        /* User mode accesses just cause a SIGSEGV */
+       /// >>> take true branch
        if (user_mode(regs)) {
                tsk->thread.fault_address = address;
+               /// >>> Ooops:
+               /// >>> use uninitialized value "si_code"
+               /// >>> when calling "force_sig_fault"
                force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
                return;
        }
-------------------->8---------------------------

Fixes: 15773ae938d8 ("signal/arc: Use force_sig_fault where appropriate")
Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
---
 arch/arc/mm/fault.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arc/mm/fault.c b/arch/arc/mm/fault.c
index c9da6102eb4f..e2d9fc3fea01 100644
--- a/arch/arc/mm/fault.c
+++ b/arch/arc/mm/fault.c
@@ -66,7 +66,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
 	struct vm_area_struct *vma = NULL;
 	struct task_struct *tsk = current;
 	struct mm_struct *mm = tsk->mm;
-	int si_code;
+	int si_code = 0;
 	int ret;
 	vm_fault_t fault;
 	int write = regs->ecr_cause & ECR_C_PROTV_STORE;  /* ST/EX */
-- 
2.14.5


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ARC: MM: fix UB and kernel resourse leak in do_page_fault
  2018-11-07 12:12 [PATCH] ARC: MM: fix UB and kernel resourse leak in do_page_fault Eugeniy Paltsev
@ 2018-11-09 17:32 ` Vineet Gupta
  0 siblings, 0 replies; 2+ messages in thread
From: Vineet Gupta @ 2018-11-09 17:32 UTC (permalink / raw)
  To: Eugeniy Paltsev, linux-snps-arc
  Cc: linux-kernel, Alexey Brodkin, Eric W . Biederman, linux-arch

On 11/7/18 4:12 AM, Eugeniy Paltsev wrote:
> Commit 15773ae938d8 ("signal/arc: Use force_sig_fault where
> appropriate") adds undefined behaviour and kernel resource leak
> to userspace in ARC do_page_fault() implementation.
> 
> This happens because we don't initialize si_code variable after we
> switch to force_sig_fault using. si_code (as a part of siginfo_t
> structure) was previously initialized by clear_siginfo(&info) call
> which was removed.
> 
> Undefined behaviour path:
> -------------------->8---------------------------
> }}} a/arch/arc/mm/fault.c
> !! -67,6 +67,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
>         struct task_struct *tsk = current;
>         struct mm_struct *mm = tsk->mm;
> +       /// >>> si_code - uninitialized
>         int si_code;
>         int ret;
>         vm_fault_t fault;
>         int write = regs->ecr_cause & ECR_C_PROTV_STORE;  /* ST/EX */
> !! -81,8 +82,10 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
>          * only copy the information from the master page table,
>          * nothing more.
>          */
> +       /// >>> take true branch
>         if (address >= VMALLOC_START) {
>                 ret = handle_kernel_vaddr_fault(address);
> +               /// >>> take true branch
>                 if (unlikely(ret))

While the fix is legit, are you sure this code path was taken. VMALLOC_START is
memory for kernel modules and/or vmalloc. So the fault was induced in kernel space
in which case user_mode)r) will not be true.


> +                       /// >>> jump to label "bad_area_nosemaphore"
>                         goto bad_area_nosemaphore;
>                 else
> !! -193,10 +196,13 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
>  bad_area:
>         up_read(&mm->mmap_sem);
> 
> +       /// >>> reach label "bad_area_nosemaphore"
>  bad_area_nosemaphore:
>         /* User mode accesses just cause a SIGSEGV */
> +       /// >>> take true branch
>         if (user_mode(regs)) {
>                 tsk->thread.fault_address = address;
> +               /// >>> Ooops:
> +               /// >>> use uninitialized value "si_code"
> +               /// >>> when calling "force_sig_fault"
>                 force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
>                 return;
>         }
> -------------------->8---------------------------
> 
> Fixes: 15773ae938d8 ("signal/arc: Use force_sig_fault where appropriate")
> Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>

Added to for-curr after trimming the changelog

Thx,
-Vineet

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-11-09 17:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-07 12:12 [PATCH] ARC: MM: fix UB and kernel resourse leak in do_page_fault Eugeniy Paltsev
2018-11-09 17:32 ` Vineet Gupta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).