x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task
PATCH 1/1 x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL.
[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
--- arch/x86/kernel/process_64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/process_64.c +1 -1
@@ -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);
Signature · 1 line
2.28.0.220.ged08abb693-goog
Quoted 7 lines · Eric Dumazet
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL
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.
Quoted 96 lines
[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: 0000000000000400Fixes 07e1d88adaae ("x86/fsgsbase/64: Fix ptrace() to read the FS/GS base accurately")Co-developed-by Jann Horn <[email protected]>Signed-off-by Eric Dumazet <[email protected]>Reported-by syzbot <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Chang S. Bae <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Borislav Petkov <[email protected]>Cc Brian Gerst <[email protected]>Cc Dave Hansen <[email protected]>Cc Denys Vlasenko <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Linus Torvalds <[email protected]>Cc Markus T Metzger <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ravi Shankar <[email protected]>Cc Rik van Riel <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>---
arch/x86/kernel/process_64.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/process_64.c +1
-1@@ -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);Signature · 1 line
2.28.0.220.ged08abb693-goog
Quoted 9 lines · Andy Lutomirski
Quoted 8 lines · Eric Dumazet
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL
Quoted 8 lines
Acked-by Andy Lutomirski <[email protected]>Maybe add something like this to the changelog:
This can happen if
ptrace()orsigreturn()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.
Jann has a repro if needed (and syzbot also had one)
Quoted 6 lines · Andy Lutomirski
Quoted 5 lines · Eric Dumazet
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL
Quoted 7 lines
Acked-by Andy Lutomirski <[email protected]>Maybe add something like this to the changelog:
This can happen if
ptrace()orsigreturn()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.
This is the reproducer I used to test this on 4.20.17:
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/prctl.h>
#include <signal.h>
#include <stddef.h>
#include <errno.h>
#define SEL_LDT 0x4
#define USER_RPL 0x3
#define LDT_SELECTOR(idx) (((idx)<<3) | SEL_LDT | USER_RPL)
int main(void) {
pid_t child = fork();
if (child == -1) err(1, "fork");
if (child == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
while (1) pause(); }
if (ptrace(PTRACE_ATTACH, child, NULL, NULL)) err(1, "PTRACE_ATTACH"); int status;
if (waitpid(child, &status, __WALL) != child) err(1, "waitpid"); if (ptrace( PTRACE_POKEUSER, child, (void*)offsetof(structuser_regs_struct, fs), (void*)LDT_SELECTOR(0) ))
err(1, "PTRACE_POKEUSER");
errno = 0;
unsigned long val = ptrace( PTRACE_PEEKUSER, child,
(void*)offsetof(struct user_regs_struct, fs_base), NULL );
printf("PTRACE_PEEKUSER returns user_regs_struct.fs_base = 0x%lx
(%m)\n", val);
}
Quoted 3 lines · Eric Dumazet
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL.
Thanks for this fix. Linus has picked it up (inclusive the typos to the x86_fsgsbase_read_task() function name ;-), it's now upstream under:
8ab49526b53d: ("x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task")
By the fixes tag it looks like this should probably be backported all the way back to ~v4.20 or so?
Thanks,
Ingo
Quoted 8 lines · Ingo Molnar
Quoted 3 lines · Eric Dumazet
syzbot found its way in 86_fsgsbase_read_task() [1]
Fix is to make sure ldt pointer is not NULL.
Quoted 8 lines
Thanks for this fix. Linus has picked it up (inclusive the typos to the
x86_fsgsbase_read_task()function name ;-), it's now upstream under:
8ab49526b53d: ("x86/fsgsbase/64: Fix NULL deref in 86_fsgsbase_read_task")By the fixes tag it looks like this should probably be backported all the way back to ~v4.20 or so?
This is absolutely right, sorry about the lack of a stable tag.
Most of my patches usually land into David Miller trees, where the stable tag is not welcomed.
We use Fixes: tags to convey the exact information needed for stable backports.
Thanks.