Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp548827rdb; Fri, 26 Jan 2024 03:54:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IFs+8zZAyL+V4A3sh0NNQvm7FgP/V/GgN8vhBpnrhiohZip1vZAULJrPBJokKUT7Pz0dnJ3 X-Received: by 2002:a05:620a:2891:b0:783:949f:cbe2 with SMTP id j17-20020a05620a289100b00783949fcbe2mr1664325qkp.150.1706270081914; Fri, 26 Jan 2024 03:54:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706270081; cv=pass; d=google.com; s=arc-20160816; b=KpIziqs5mTbO32b2+DLSILGAf7KZa+4kuWHw1trILmF+M44Zb3cSWemDSItaU2rGxo 0BK8mAW8St02tdX7pVE4rfpamifiweCJOshooJjrvTrPeOi4fIbe11K9n94cxTc6HNwb id3bw6ZXxPP/wsuYuFnMcjokClreyVi4EAOBk8KQucy7KBnAjCSnQIzTNWJZsZjFiofL 85xn3PzQYsm8CIckCyUphxZ74tKQSKOE8du5rKGnoGm3A3RdFgJrhsWHKlpFb3fUgwPl 2//cN/AWHhgWWnkrbFrA7ivgvI0EphX4eVzx3yrl2YC+wkGwJ0RtPB3wqGPIMUZ3fDAi hBoQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=bDewNIKMx+jR4VUFw1+qEHAizhbLJpmFwALi5eJ60q0=; fh=G+80uKrcZQu3HF50WBlLamYOf34JlQTSQytwkjSpE8g=; b=EZmxRMY7lg1x5CRH84vGPyhOrNiL3cN67/HlMC3XwqFn6qTsmwLg/HBotwEgqfVze6 Kt9PQ46t2VPwspKaDTWNdfyHCmfaIH9oAjQza/mlpdu6l+cCAa4BYD33/tfSJvnSY+SI B4MRImrKkSwz0gsL9+E9Wul1/BqmUGuaMc0uRsHbXpPxkV3xA1ON/St0YLlhqSX4LjXu ZyDDC2b8E8OU30/FJAKbyejiHT5ipqKpWtVSOX2BfilbDvCHwKINCvUVkG2DNbZ3/ANg K/OHBgJuyv4sTCCwgFYgiO6yTRCjY+tMKhd1OZYxwpSMomL70u+Cp36+pPxcfZM3a3kZ mPdw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-40071-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40071-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id qr13-20020a05620a390d00b0078154e525besi1183470qkn.485.2024.01.26.03.54.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 03:54:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40071-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-40071-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40071-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AAFDF1C23B96 for ; Fri, 26 Jan 2024 11:54:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 590671B592; Fri, 26 Jan 2024 11:54:02 +0000 (UTC) Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 735FF1B943; Fri, 26 Jan 2024 11:53:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706270041; cv=none; b=pDAW2EA1ZY1k6yJnfSzH6xtmsYcUPTYvZ6sWx531i+Z9J9v006GtcKdmx4NHlye0bgvkxCOaGE4j9Wz8RaofnAO/sHSagXzk8/iqNgEw6Qf737vIUwQp8001m0wROtvKsLaEjp+F40vAYPMIIJa3sHT4WNhCIXPXxF83vSOo67U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706270041; c=relaxed/simple; bh=frgwAcR5N7KIoz7M7+riTCKsR1N7Znj38szEnJrDEXw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MdLd4kUeUTkdmkPRg9cULZjCLoABqbhP/r7UaZ4M9HteEy8GKfXhvEe+w8ACb+ESo7fvXqKCmFNhS4mcuBW0y0YWceoH85a3cDrqdg28yUFdeTzeRzMBZo9esVvLdGgDHBw7mPXxn8N8g4Cc51pdoYs0YtAPLV8eFY14FyeibuI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4TLx1C6zmyz4f3l7f; Fri, 26 Jan 2024 19:53:51 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id EE1A61A0172; Fri, 26 Jan 2024 19:53:55 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.124.27]) by APP1 (Coremail) with SMTP id cCh0CgBHZQ42nbNllmgoCA--.1892S6; Fri, 26 Jan 2024 19:53:55 +0800 (CST) From: Hou Tao To: x86@kernel.org, bpf@vger.kernel.org Cc: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , linux-kernel@vger.kernel.org, xingwei lee , Jann Horn , Sohil Mehta , Yonghong Song , houtao1@huawei.com Subject: [PATCH bpf v2 2/3] x86/mm: Disallow vsyscall page read for copy_from_kernel_nofault() Date: Fri, 26 Jan 2024 19:54:22 +0800 Message-Id: <20240126115423.3943360-3-houtao@huaweicloud.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20240126115423.3943360-1-houtao@huaweicloud.com> References: <20240126115423.3943360-1-houtao@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgBHZQ42nbNllmgoCA--.1892S6 X-Coremail-Antispam: 1UD129KBjvJXoWxCryUKF1fGF45GF1fCFWDtwb_yoW5Kr15pa y5C3y3KFsYyr1UAanFq3409ayrJ3W0kF45Wr1vvry5Za17WFn0yrWkWa4vqrWDAFnFgrWx WwsFyrWqqw1UXaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB0b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IYc2Ij64 vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK 8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I 0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFa9-UUUUU X-CM-SenderInfo: xkrx3t3r6k3tpzhluzxrxghudrp/ From: Hou Tao When trying to use copy_from_kernel_nofault() to read vsyscall page through a bpf program, the following oops was reported: BUG: unable to handle page fault for address: ffffffffff600000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 3231067 P4D 3231067 PUD 3233067 PMD 3235067 PTE 0 Oops: 0000 [#1] PREEMPT SMP PTI CPU: 1 PID: 20390 Comm: test_progs ...... 6.7.0+ #58 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996) ...... RIP: 0010:copy_from_kernel_nofault+0x6f/0x110 ...... Call Trace: ? copy_from_kernel_nofault+0x6f/0x110 bpf_probe_read_kernel+0x1d/0x50 bpf_prog_2061065e56845f08_do_probe_read+0x51/0x8d trace_call_bpf+0xc5/0x1c0 perf_call_bpf_enter.isra.0+0x69/0xb0 perf_syscall_enter+0x13e/0x200 syscall_trace_enter+0x188/0x1c0 do_syscall_64+0xb5/0xe0 entry_SYSCALL_64_after_hwframe+0x6e/0x76 ...... ---[ end trace 0000000000000000 ]--- It seems the occurrence of oops depends on SMAP feature of CPU. It happens as follow: a bpf program uses bpf_probe_read_kernel() to read from vsyscall page, bpf_probe_read_kernel() invokes copy_from_kernel_nofault() in turn and then invokes __get_user_asm(). Because the vsyscall page address is not readable for kernel space, a page fault exception is triggered accordingly, handle_page_fault() considers the vsyscall page address as a userspace address instead of a kernel space address, so the fix-up set-up by bpf isn't applied. Because the CPU has SMAP feature and the access happens in kernel mode, so page_fault_oops() is invoked and an oops happens. If these is no SMAP feature, the fix-up set-up by bpf will be applied and copy_from_kernel_nofault() will return -EFAULT instead. Considering handle_page_fault() has already considered the vsyscall page address as a userspace address, fix the problem by disallowing vsyscall page read for copy_from_kernel_nofault(). Originally-by: Thomas Gleixner Reported-by: syzbot+72aa0161922eba61b50e@syzkaller.appspotmail.com Closes: https://lore.kernel.org/bpf/CAG48ez06TZft=ATH1qh2c5mpS5BT8UakwNkzi6nvK5_djC-4Nw@mail.gmail.com Reported-by: xingwei lee Closes: https://lore.kernel.org/bpf/CABOYnLynjBoFZOf3Z4BhaZkc5hx_kHfsjiW+UWLoB=w33LvScw@mail.gmail.com Signed-off-by: Hou Tao --- arch/x86/mm/maccess.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/mm/maccess.c b/arch/x86/mm/maccess.c index 6993f026adec9..d9272e1db5224 100644 --- a/arch/x86/mm/maccess.c +++ b/arch/x86/mm/maccess.c @@ -3,6 +3,8 @@ #include #include +#include + #ifdef CONFIG_X86_64 bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size) { @@ -15,6 +17,13 @@ bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size) if (vaddr < TASK_SIZE_MAX + PAGE_SIZE) return false; + /* Also consider the vsyscall page as userspace address. Otherwise, + * reading the vsyscall page in copy_from_kernel_nofault() may + * trigger an oops due to an unhandled page fault. + */ + if (is_vsyscall_vaddr(vaddr)) + return false; + /* * Allow everything during early boot before 'x86_virt_bits' * is initialized. Needed for instruction decoding in early -- 2.29.2