Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2543018lqp; Mon, 25 Mar 2024 01:55:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyUw4V8vMlkMMU7zkw25/ZTnuV39mPPknDkqQSu8HounrxubkwFPS4t3RmhcRqgQcQ4Rc6BgoisaKQAufc5xPqkLrSaahhMY72GJLPqA== X-Google-Smtp-Source: AGHT+IEAt66byFSrxO0q19TRii+2YtG2VumhMltvT3QCnOIVxbOe2rxM+byIxm6+KyohEzPAlBp+ X-Received: by 2002:a9d:66d0:0:b0:6e5:22d6:71ce with SMTP id t16-20020a9d66d0000000b006e522d671cemr7676854otm.5.1711356907694; Mon, 25 Mar 2024 01:55:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711356907; cv=pass; d=google.com; s=arc-20160816; b=MpWWhsW2Xy68oyCeEY2Z4ieni+ZvH8XaovbKPeP3EPKcWXia3Yj9QESIBMClnlIb7H CkY2HvYJv6iEOPxSnpYqLf8Mb1Ys0z5RMa1mwI/JTGZNIOZXkXjcTziYysHdUpB/CCo+ IS6ycRlfB1nOjyGV8yFeSdYqbgMCvYM+R6cGwFxLw1LVBXF9RMe5uev4rpqyrdb15S5J 0eFl/e4sdy0P2blg7J4MEZAqG94h64t4YIFFaG+c9AUCUb22bDN54721vhm6vgrC5qgi AW6bV5NiHiNvxhhiTI3D9tn5jxWEiqXzPBz9GBzyk4M33U4MEjkV9CSdGrW7YdcYAl83 aF0w== 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:dkim-signature; bh=2djCH1e46BYnAxDNWmtbIGDhEHHSVmvlJUKbGfuNH9A=; fh=M+hGWzUuGxiPBL8ZCFZb3UjwaLWcSpIxU/ACJFpDiog=; b=VwwELFg68wP4EBnKW/kFqNf0Y3q843aoQ6BrflveBbctlwE0y56PGBOAPZy65md4hz y95uwCSCd/BCkAwpvsnHd1yX7VgLYPbQzNNvkYXFrLB0MKcLQ9WgdSB3ct1YwTb2G1LY W+jWigtVaUcsX+4XWJPywpB9j6QB+23k++IJQGiVovP8k4SgBcQAOW7A7aJSwwNvh9Uy V/gDBUAJEuVbgMxOmMS5H9WLcXZRwTUGQHHwTu6eYQBuzxlQ4MjlCzE6W4dJfa8h3fLH mopl0/VOhaqgjuLJAEenzH4nR5VnP+JVrs1H7LorY7/FuspKIoY5BVqObInsEO8LbD9p 6xFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bTXkPs81; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115762-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115762-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id w35-20020a634763000000b005ee3f1a2d57si7288232pgk.885.2024.03.25.01.55.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 01:55:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-115762-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bTXkPs81; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115762-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115762-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 2FD9D299F90 for ; Mon, 25 Mar 2024 08:53:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C4FEB2618C6; Mon, 25 Mar 2024 03:00:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bTXkPs81" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 932381448F3; Sun, 24 Mar 2024 23:12:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711321962; cv=none; b=bsXZoCADA5XYoO4BOPfZieURBvQAnM5ceI9VUM8s6g6ixFt3cPwScaTe7eMQGACGULD2R+pIa/rnMp/KswKQIc87TasPb8FfBSrUGcx4dNh5r9rrQiWIKzkM2Erz3rkIaZIo+6ax6Vp4ug5te/dVLDiC2K8Fs64/Mx2LXqnHr8M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711321962; c=relaxed/simple; bh=Vo3k/UgoRrmyUGzlldwCCqinbenvGMIr4yM+BJI4MQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jEf6EEIxviDzB6cLnnrpZMbcUO/W+mWxU1SV7Xn+fpWmdZ5J4gWvvZTzfh88bMXbwXcB1QRZAd8cBdMxv6+R6DIdOIiVgHRDVaml1WwLO7ZK5HkJF2LZ+ge8ozAiznnFhrlo72V4PLXgBlFEN1UwiZywDgLM/85o10BkNQrxtI4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bTXkPs81; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63977C43390; Sun, 24 Mar 2024 23:12:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711321962; bh=Vo3k/UgoRrmyUGzlldwCCqinbenvGMIr4yM+BJI4MQQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bTXkPs81CXozYOzNZe3xBZn/aaA0c3LUnZjiue/2RVt3KKXaRpf/RQWk7Yb7njyww nP0uj8h2n8X6GILpkMsK3rn3Soj9d8e+9guTV2i7FArwJMl6LZTHbkq5H+EsYcn6IS xEVd8lMYEXEmmCRxrr0+I50HQeboST8hXTID7SiYLZ/SKd5UWT1LXVrEAD1IEkwoBT H3GwuMlqgZaXdlJPC5ZBUKYVsGlNPnsE/+4+Bk+QvZuXyHsn9A/yV1JPV/fiUK+SyH L0AKAd3HXvUsk4fqEayrWSTpBHymbY/UT/bR876Leroa8r4HivLT7Q/SdT5edVYCa5 O96H/aRepgShw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hou Tao , Thomas Gleixner , syzbot+72aa0161922eba61b50e@syzkaller.appspotmail.com, xingwei lee , Sohil Mehta , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 034/451] x86/mm: Disallow vsyscall page read for copy_from_kernel_nofault() Date: Sun, 24 Mar 2024 19:05:10 -0400 Message-ID: <20240324231207.1351418-35-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Hou Tao [ Upstream commit 32019c659ecfe1d92e3bf9fcdfbb11a7c70acd58 ] 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 ]--- The oops is triggered when: 1) A bpf program uses bpf_probe_read_kernel() to read from the vsyscall page and invokes copy_from_kernel_nofault() which in turn calls __get_user_asm(). 2) Because the vsyscall page address is not readable from kernel space, a page fault exception is triggered accordingly. 3) handle_page_fault() considers the vsyscall page address as a user space address instead of a kernel space address. This results in the fix-up setup by bpf not being applied and a page_fault_oops() is invoked due to SMAP. 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 Reviewed-by: Sohil Mehta Acked-by: Thomas Gleixner Link: https://lore.kernel.org/r/20240202103935.3154011-3-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- arch/x86/mm/maccess.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/mm/maccess.c b/arch/x86/mm/maccess.c index 6993f026adec9..42115ac079cfe 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,14 @@ bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size) if (vaddr < TASK_SIZE_MAX + PAGE_SIZE) return false; + /* + * Reading from the vsyscall page may cause an unhandled fault in + * certain cases. Though it is at an address above TASK_SIZE_MAX, it is + * usually considered as a user space address. + */ + 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.43.0