Re: [PATCH] x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task
From: Andy Lutomirski
Date: Fri Aug 14 2020 - 15:03:36 EST
> On Aug 14, 2020, at 11:16 AM, Eric Dumazet <edumazet@xxxxxxxxxx> wrote:
>
> syzbot found its way in 86_fsgsbase_read_task() [1]
>
> Fix is to make sure ldt pointer is not NULL
Acked-by: Andy Lutomirski <luto@xxxxxxxxxx>
Maybe add something like this to the changelog:
This can happen if ptrace() or sigreturn() pokes an LDT selector into FS or GS for a task with no LDT and something tries to read the base before a return to usermode notices the bad selector and fixes it.
I’ll see if I can whip up a test case too.
>
> [1]
> general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] PREEMPT SMP KASAN
> KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
> CPU: 0 PID: 6866 Comm: syz-executor262 Not tainted 5.8.0-syzkaller #0
> Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
> RIP: 0010:x86_fsgsbase_read_task+0x16d/0x310 arch/x86/kernel/process_64.c:393
> Code: 3c 02 00 0f 85 7c 01 00 00 4c 8b a5 80 04 00 00 44 0f b7 eb 48 b8 00 00 00 00 00 fc ff df 49 8d 7c 24 08 48 89 fa 48 c1 ea 03 <0f> b6 04 02 84 c0 74 08 3c 03 0f 8e 30 01 00 00 45 3b 6c 24 08 0f
> RSP: 0018:ffffc90001677db8 EFLAGS: 00010202
> RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000
> RDX: 0000000000000001 RSI: 0000000000000008 RDI: 0000000000000008
> RBP: ffff88809eaf9780 R08: 0000000000000000 R09: ffff88809eaf9c17
> R10: ffffed1013d5f382 R11: 0000000000000000 R12: 0000000000000000
> R13: 0000000000000000 R14: 0000000000000000 R15: 0000000020000080
> FS: 0000000000000000(0000) GS:ffff8880ae600000(0063) knlGS:0000000009cf4840
> CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033
> CR2: 0000000020000080 CR3: 000000009fa0d000 CR4: 00000000001506f0
> DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
> Call Trace:
> putreg32+0x3ab/0x530 arch/x86/kernel/ptrace.c:876
> genregs32_set arch/x86/kernel/ptrace.c:1026 [inline]
> genregs32_set+0xa4/0x100 arch/x86/kernel/ptrace.c:1006
> copy_regset_from_user include/linux/regset.h:326 [inline]
> ia32_arch_ptrace arch/x86/kernel/ptrace.c:1061 [inline]
> compat_arch_ptrace+0x36c/0xd90 arch/x86/kernel/ptrace.c:1198
> __do_compat_sys_ptrace kernel/ptrace.c:1420 [inline]
> __se_compat_sys_ptrace kernel/ptrace.c:1389 [inline]
> __ia32_compat_sys_ptrace+0x220/0x2f0 kernel/ptrace.c:1389
> do_syscall_32_irqs_on arch/x86/entry/common.c:84 [inline]
> __do_fast_syscall_32+0x57/0x80 arch/x86/entry/common.c:126
> do_fast_syscall_32+0x2f/0x70 arch/x86/entry/common.c:149
> entry_SYSENTER_compat_after_hwframe+0x4d/0x5c
> RIP: 0023:0xf7fe9569
> Code: 10 05 03 74 b8 01 10 06 03 74 b4 01 10 07 03 74 b0 01 10 08 03 74 d8 01 00 00 00 00 00 00 00 00 00 51 52 55 89 e5 0f 34 cd 80 <5d> 5a 59 c3 90 90 90 90 eb 0d 90 90 90 90 90 90 90 90 90 90 90 90
> RSP: 002b:00000000ffd99e7c EFLAGS: 00000217 ORIG_RAX: 000000000000001a
> RAX: ffffffffffffffda RBX: 000000000000000d RCX: 0000000000001ad1
> RDX: 0000000000000000 RSI: 0000000020000080 RDI: 0000000000001ad1
> RBP: 000000000000003c R08: 0000000000000000 R09: 0000000000000000
> R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
> R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
> Modules linked in:
> ---[ end trace f79c8042700af7c5 ]---
> RIP: 0010:x86_fsgsbase_read_task+0x16d/0x310 arch/x86/kernel/process_64.c:393
> Code: 3c 02 00 0f 85 7c 01 00 00 4c 8b a5 80 04 00 00 44 0f b7 eb 48 b8 00 00 00 00 00 fc ff df 49 8d 7c 24 08 48 89 fa 48 c1 ea 03 <0f> b6 04 02 84 c0 74 08 3c 03 0f 8e 30 01 00 00 45 3b 6c 24 08 0f
> RSP: 0018:ffffc90001677db8 EFLAGS: 00010202
> RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000
> RDX: 0000000000000001 RSI: 0000000000000008 RDI: 0000000000000008
> RBP: ffff88809eaf9780 R08: 0000000000000000 R09: ffff88809eaf9c17
> R10: ffffed1013d5f382 R11: 0000000000000000 R12: 0000000000000000
> R13: 0000000000000000 R14: 0000000000000000 R15: 0000000020000080
> FS: 0000000000000000(0000) GS:ffff8880ae600000(0063) knlGS:0000000009cf4840
> CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033
> CR2: 0000000020000080 CR3: 000000009fa0d000 CR4: 00000000001506f0
> DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
>
> Fixes: 07e1d88adaae ("x86/fsgsbase/64: Fix ptrace() to read the FS/GS base accurately")
> Co-developed-by: Jann Horn <jannh@xxxxxxxxxx>
> Signed-off-by: Eric Dumazet <edumazet@xxxxxxxxxx>
> Reported-by: syzbot <syzkaller@xxxxxxxxxxxxxxxx>
> Cc: Andy Lutomirski <luto@xxxxxxxxxx>
> Cc: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> Cc: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
> Cc: Borislav Petkov <bp@xxxxxxxxx>
> Cc: Brian Gerst <brgerst@xxxxxxxxx>
> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
> Cc: Denys Vlasenko <dvlasenk@xxxxxxxxxx>
> Cc: H. Peter Anvin <hpa@xxxxxxxxx>
> Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
> Cc: Markus T Metzger <markus.t.metzger@xxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Ravi Shankar <ravi.v.shankar@xxxxxxxxx>
> Cc: Rik van Riel <riel@xxxxxxxxxxx>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> ---
> arch/x86/kernel/process_64.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> index d6f946707270e41d4aa04e4069b1b4eb82afd687..8393ee1aa9c89b3712b7aef510149d73762ef314 100644
> --- a/arch/x86/kernel/process_64.c
> +++ b/arch/x86/kernel/process_64.c
> @@ -390,7 +390,7 @@ unsigned long x86_fsgsbase_read_task(struct task_struct *task,
> */
> mutex_lock(&task->mm->context.lock);
> ldt = task->mm->context.ldt;
> - if (unlikely(idx >= ldt->nr_entries))
> + if (unlikely(ldt == NULL || idx >= ldt->nr_entries))
> base = 0;
> else
> base = get_desc_base(ldt->entries + idx);
> --
> 2.28.0.220.ged08abb693-goog
>