Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2574151lqp; Mon, 25 Mar 2024 03:09:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX17CNulWUiAgQEnkOj9G0BICXZH3ie6aA6HjzS42PT8m7Af1LgD/EC+mnyURo9BQb9JOtl+Ua6Pm+u+f9yK/olT0pUIZzNdCgmEGlCnQ== X-Google-Smtp-Source: AGHT+IHrtm9DGZqdsMdpsQyYF4tbg74oaS8vn0JT0oxwj+Pq1Tm4grECnTub9OMSOKWheXUjNGaB X-Received: by 2002:a50:954e:0:b0:56b:f2d4:8d9 with SMTP id v14-20020a50954e000000b0056bf2d408d9mr4381245eda.40.1711361391072; Mon, 25 Mar 2024 03:09:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711361391; cv=pass; d=google.com; s=arc-20160816; b=skJokSaCcM8u2y9lgOafqRbj/XPH++T9v6KVwTrHnDkmnIlmUmwIWYtVqM+IFzmp7f SIyUGs7KQ9C4JcTemZHlcFicjabbOicjBk1adlqrfIhA8T245YaQ3BtRRY7NVe0FU3p6 bXrUBNGjiqBZ9lUaFK8/ctSiIxcZZl2UW6QB27lJ2LSHDjK2ZNkHiK6rjL1Gb91Rj6lo F+8mgA2+bSfMkGpZU4mpjUF+W8ooTmTZuRPR4st4+iMBpPz2t9xHWKqqVKsDY9E/r7V3 lxfHT5OIcWCirNj01jHJlclgoQb0GYmFKDUSERfsZm3TbJZtOeYcNIoOIJLqTFiYa9gV EyaQ== 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=y7VgAWGYKIeokzpsxJbmNe4ajOF9ddYtGFSQJudHE6Wr268llImSOtWqBpRA/hNegq VwFf8ykmsr7v8Fr6fywpt2UNQ9+SUfoe5ZxUyeH9UKCCcEzWnsi/CPG3Gb6+9b60GmDq eiyazBwxqQdg+78TCs75uwwHMoFPu6oYz7cdFYZ5C1y7+42xDKUxUwdDorB+AOgDr/Et jb846Hzw15ZRNANpYTSkmN86oRPj0Xvj1jDiD6TYARAsM60a7c8ghvROtf0KbCx+TYfz bpizAjJb4gtLEohV4yQjFs3x11rMumPXbeUiQb22mTto+Wr0fzyT6b+D4BDdv/PlSmmz H2Vw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WdZZWYVa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116118-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116118-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id n12-20020a05640205cc00b0056a2e3a1829si2291184edx.242.2024.03.25.03.09.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 03:09:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116118-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WdZZWYVa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-116118-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116118-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 am.mirrors.kernel.org (Postfix) with ESMTPS id C3A081F2C04B for ; Mon, 25 Mar 2024 10:09:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77C6D3ABE88; Mon, 25 Mar 2024 03:31:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WdZZWYVa" 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 2DA58179FA2; Sun, 24 Mar 2024 23:40:44 +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=1711323644; cv=none; b=sP06XrycjgMX7Hno66Kb2uVj4liiMIjSI1BSml0BA6hcKYhyQa/hRCnhB7pzCwYmsUtibIexYVSecOmtW2zhBuaugakAMKn3t2vF2T5Yz3p8u4dDa+f2dpbxujzAIqvLiiqBECcOMBe/QH9xp/SbKnz/OhMKjFgvTHJvr0Aos9o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323644; c=relaxed/simple; bh=Vo3k/UgoRrmyUGzlldwCCqinbenvGMIr4yM+BJI4MQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ua+BRM5vBx5x4TyEDis89CyZs4DhH4ldDy7cxLNMOOc5VEE0BrxTr+Z9S9+P6GskaJtzFEQh21P5PxWhxyVFrLBRlYOaR4eVdtbO1jy9b6AGzChhD0yf46qQHIH3tfLYW3SaLq2YHxGOF7gWvrlFm88zFPrGQgQ8yuxMMNv36iQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WdZZWYVa; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DA3FC43390; Sun, 24 Mar 2024 23:40:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323644; bh=Vo3k/UgoRrmyUGzlldwCCqinbenvGMIr4yM+BJI4MQQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WdZZWYVa03pnTKSTauywC6ZKM1vs1ORJSDzfD2NgCgP67SOPeWv7IYdgPkFQADqNU W/HfvopHNCnSkTzvcGAuCvBueu2uBEnjjbx4er6QH/ZrXhfsWnDObU/GdBnWEQLjFO VkFiXdPphBLYwMw4YdOFD7BfMOWhXHJWr+80EOkiWGhDTZpBk1qI6qeTV4EvtSPfP3 qgzbF23s4u5mWpzUg3Hr11wzuanRxx6BxDqGjWBrThLEeBZAavbgD5TLmXHvUKlacY vaj+vY2lBHFn5qWLb3XUchU+aMDKwZhLusHi1D6YHs3+tozEa52xhydLddI+NxjIJ2 rw2lBpfi4qdlw== 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 5.10 015/238] x86/mm: Disallow vsyscall page read for copy_from_kernel_nofault() Date: Sun, 24 Mar 2024 19:36:43 -0400 Message-ID: <20240324234027.1354210-16-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324234027.1354210-1-sashal@kernel.org> References: <20240324234027.1354210-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