From patchwork Fri Nov 4 21:24:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 15807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp651042wru; Fri, 4 Nov 2022 14:38:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ARb7137387AZNmnLFK7N60JZK1VgP8rN/d0zUfO9BGRdAFJowmEEQLKCEiISG+DuaEYXv X-Received: by 2002:a17:907:7fa4:b0:791:9307:9d6a with SMTP id qk36-20020a1709077fa400b0079193079d6amr36196882ejc.464.1667597910493; Fri, 04 Nov 2022 14:38:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667597910; cv=none; d=google.com; s=arc-20160816; b=JS4U8+30KVpHOac2QQfm9poV1dcXomVwXyzbNE2MkfkWziFL+8gmWirjo9GbiPKREd 2egK9d6Y5qRZOhh9n9FIVWX6iDS44tPzsPYLUbvkNZvr7Yx4No5lawlPWD9qbXxvsTAv uwIP9Ao53Ts7buQ1v+4eqaP3UapKyPIRzh39OWHQkyuWXry+I531fOt9lDbX7j8n/nfr FmNqXScRkWVRrRd+TDSEryHp8SYyQgEE2HgyclVEjGgP0zIk62Gd/grqfKBDL+OG5v88 Z+jW2cdiLm1qmo6flavl8EfgiENjlMI6QXE0Lx5UsUKW+5g8BfMQXnt29Dg0p1S37Obx 3nNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :reply-to:dkim-signature; bh=CG1mlrDYCYVQ66+gGflTWD0vEgGOvioVVAQzm046g0w=; b=0J/ue0fxVHlwd/HIAUfx5ahdcKjiPjC+56Xm1kWS71HZ6pUwf02elTnFn8AIXO9plI wHo7Yr/DuQyVVQhKsI2pwHun6WjrI+lrC11zSbolcJUWe+nSkoIUeBpa3ODeChMHWORT 4Q1il2GQ88t61D1SMXYPU9+H7kxOFPCmCh3M4UM9FiZLhFtHlGt1XJaMgUESI2wi6oaQ bNUybxyK6ejSAJSZulr+s5kaOJIKjoL3xlk5Sdxfdsyy+HKpCjjUwkf2CiHL++mh5A5X VMTyoLpYHVgMU0zd06UfPWF5hALI7RQYOghBInNO20h7GFQN4HfwSCEOHIVMIIgoOH0C sBrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ArHTBmvC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k25-20020a17090627d900b00781d302d5e9si146018ejc.166.2022.11.04.14.38.07; Fri, 04 Nov 2022 14:38:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ArHTBmvC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229717AbiKDVYw (ORCPT + 99 others); Fri, 4 Nov 2022 17:24:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229496AbiKDVYo (ORCPT ); Fri, 4 Nov 2022 17:24:44 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DED4E4E42F for ; Fri, 4 Nov 2022 14:24:35 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id n1-20020a170902f60100b00179c0a5c51fso4322882plg.7 for ; Fri, 04 Nov 2022 14:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=CG1mlrDYCYVQ66+gGflTWD0vEgGOvioVVAQzm046g0w=; b=ArHTBmvCIKJmEkGvU+CZmdhiyP8UqJKmOJqB9oJvmfgv6zudO+mQRBa9XH+f6XfJFV OKc3s5OzvclNBHKkw/hmysyh/TN9Vsl3VnReicavvBJNLojL8WMd5cJz978mzvkV7yUc rYBC2LRtheyP+eOVNNMS+9Xqq3fuGtPKtm/RzmANDWuJg+IKutvIA3TzOhLBzNzU4vLy q9HSGs2DXqZC3MnuG5eTGwxz3BkxaKGaqx0RyHHPEC7nd12TKJLkYnkkzWxlq/9KGXGz gZb/pNQgql4Ul7Mprq7oGKnA2KMaWBY7ltUYsWQ1kDQHNMzWh5bX+ETOPgJa4tTN088L HHhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CG1mlrDYCYVQ66+gGflTWD0vEgGOvioVVAQzm046g0w=; b=r1rjqggFSg8mXuOWj6d+J2fV5qOQ8qDFVzi02rKTblsKfJzLhw/xxUmONSHFaw8PJ2 NbB7CCgrm5gfHMbUVnG/S0SwYX0pPV8NJCJWUynKjWv2RBVLAUEdB3HAl/SGLe52RT9q 9uBj8qJPvklZkHgtwhNwSUhDmbANwUaEp0RGTu6I09XRbv7UU/rK1aE508ufITyrew+8 h6NDkJ1GYstxWxfH+Mj/X2Z7nPXZfby8lhsGiyL3MLjxmscSsCpxmV1tcdLLznOoitmM AazzbnzdPdElIGRQx2DJeNGfl23Su9r+qbc/yNp2Z9kINCG7x8Vd4Vem5cF8sLmudYq2 ppnw== X-Gm-Message-State: ACrzQf3rG6n6GewX1TSIA9jJ2aZsBatm4sXDP3/xysK/dbaVcArS5/N7 9wnG+2/anHYzzUggT+udUHUFQ9vnRrM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f304:b0:182:2589:db21 with SMTP id c4-20020a170902f30400b001822589db21mr394567ple.151.1667597075466; Fri, 04 Nov 2022 14:24:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Nov 2022 21:24:33 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221104212433.1339826-1-seanjc@google.com> Subject: [PATCH] x86/mm: Populate KASAN shadow for per-CPU GDT mapping in CPU entry area From: Sean Christopherson To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org Cc: "H. Peter Anvin" , linux-kernel@vger.kernel.org, syzbot+ffb4f000dc2872c93f62@syzkaller.appspotmail.com, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com, Sean Christopherson X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748603146582240430?= X-GMAIL-MSGID: =?utf-8?q?1748603146582240430?= Bounce through cea_map_percpu_pages() when setting protections for the per-CPU GDT mapping so that KASAN populates a shadow for said mapping. Failure to populate the shadow will result in a not-present #PF during KASAN validation if the kernel performs a software lookup into the GDT. The bug is most easily reproduced by doing a sigreturn with a garbage CS in the sigcontext, e.g. int main(void) { struct sigcontext regs; syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); memset(®s, 0, sizeof(regs)); regs.cs = 0x1d0; syscall(__NR_rt_sigreturn); return 0; } to coerce the kernel into doing a GDT lookup to compute CS.base when reading the instruction bytes on the subsequent #GP to determine whether or not the #GP is something the kernel should handle, e.g. to fixup UMIP violations or to emulate CLI/STI for IOPL=3 applications. BUG: unable to handle page fault for address: fffffbc8379ace00 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 16c03a067 P4D 16c03a067 PUD 15b990067 PMD 15b98f067 PTE 0 Oops: 0000 [#1] PREEMPT SMP KASAN CPU: 3 PID: 851 Comm: r2 Not tainted 6.1.0-rc3-next-20221103+ #432 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:kasan_check_range+0xdf/0x190 Call Trace: get_desc+0xb0/0x1d0 insn_get_seg_base+0x104/0x270 insn_fetch_from_user+0x66/0x80 fixup_umip_exception+0xb1/0x530 exc_general_protection+0x181/0x210 asm_exc_general_protection+0x22/0x30 RIP: 0003:0x0 Code: Unable to access opcode bytes at 0xffffffffffffffd6. RSP: 0003:0000000000000000 EFLAGS: 00000202 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 00000000000001d0 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 Fixes: 9fd429c28073 ("x86/kasan: Map shadow for percpu pages on demand") Reported-by: syzbot+ffb4f000dc2872c93f62@syzkaller.appspotmail.com Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Vincenzo Frascino Cc: kasan-dev@googlegroups.com Signed-off-by: Sean Christopherson --- arch/x86/mm/cpu_entry_area.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) base-commit: 81214a573d19ae2fa5b528286ba23cd1cb17feec diff --git a/arch/x86/mm/cpu_entry_area.c b/arch/x86/mm/cpu_entry_area.c index dff9001e5e12..4a6440461c10 100644 --- a/arch/x86/mm/cpu_entry_area.c +++ b/arch/x86/mm/cpu_entry_area.c @@ -195,7 +195,7 @@ static void __init setup_cpu_entry_area(unsigned int cpu) pgprot_t tss_prot = PAGE_KERNEL; #endif - cea_set_pte(&cea->gdt, get_cpu_gdt_paddr(cpu), gdt_prot); + cea_map_percpu_pages(&cea->gdt, get_cpu_gdt_rw(cpu), 1, gdt_prot); cea_map_percpu_pages(&cea->entry_stack_page, per_cpu_ptr(&entry_stack_storage, cpu), 1,