Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp169984lqb; Thu, 14 Mar 2024 08:12:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXU+tsEQ4CSuneAx4NaOpNvQzdJJ37TGSvMG4bd9s0gbSwbtYJofXV+570C+9P2JWRBW9uHRd1cXzufpS6ZWtPRUgWdM3GdTSWAe/kI/A== X-Google-Smtp-Source: AGHT+IGlw46pdnLng5iFxpfuc4gLNUR4Z9hWF7y24MWMNgJWWmHT5kU9ErbU0ECPKjWn68wyqIix X-Received: by 2002:a05:6402:14c7:b0:568:2149:329a with SMTP id f7-20020a05640214c700b005682149329amr664189edx.19.1710429175432; Thu, 14 Mar 2024 08:12:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710429175; cv=pass; d=google.com; s=arc-20160816; b=a0HJNGFlegwxa6d3TvuCJ4u0VdD02WiielCRZz0IZLfuUqorLDU7clSzZtBUEQ+uCP WuRP50S/b4Bwl3ND+VsopaHXnyaXPf8z+SyoeyPUVzDCQ2CY5UeKnT9B96l1Osw7gm2b jQT0pPm+EFZL2NUO7/TyDmm2RuAKBiw3Y5/+E7lfz/WQ2WHYsZfJ3FS9QbxPCc+mjOVx WRsthA3ygBbsQ9SV6vc73v1BDXjou3WIoCemJQdvUyqvdMSTGMLqONroC6BvSUjJEJrx mkkvGk15RZ6pljM33TK7VCkhotuhVEvBK1hhVej0umgE34R4xr4oeqsLc7MWfNGMFLDv LJiw== 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:user-agent:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=ykmmwaqUKpX/C9sP1Z+pyRB9ygYDrt+PtaCdjioQ+n4=; fh=Mp+ueS8yaRKk4fUufdq6DdmbLUa0RLVsRR2RgQzSkyc=; b=qvr0C/mGo3k7i0DFPnadMnfRNa9R7mmsMaa75GVYbIiQiQ6h9LjQOG6lfihYDsGQ0X +3XwquNiE25unDhOuTmdwH5slxp10KefhIvxGt1g64vCWoUE48kPY32w0gcrlEwmE6WT OpD4KoE/BS7cw3aS/MDOXjSysZr3+sFAoq8RtCBmm6yhJPkQEmSR6dgZSVIOfjD9OJe3 EU6z9WmlOxfag+aymVpOSOvJqMYauOtB8QiqxtjTp7BbAW8Ze80nCu48oRYHmpTj5DeJ blYWCSIbPnqcYRwbXThz4K0UI4JWdy6okGw0aZn8ZT9kPxGNwMcJeGrpKY3IoFYvbRej ZbBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Oack5M/W"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-103463-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103463-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 t13-20020a056402240d00b00567e1287325si840972eda.445.2024.03.14.08.12.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 08:12:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103463-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="Oack5M/W"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-103463-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103463-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 2BC091F238EB for ; Thu, 14 Mar 2024 15:12:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B929971B4C; Thu, 14 Mar 2024 15:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Oack5M/W" 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 DF360DDD9; Thu, 14 Mar 2024 15:12:36 +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=1710429157; cv=none; b=ZqF6NFL5f7lgovwRmzS+TUQcoXQI3CmMF6mzXAucdWqttzPudb9ijrLMwpYVeazb+JbCgz9ZMS/3RM25D5nAvYt+7vKyHFzT+EnHwtzKJFr1xLfyiqrJw4cpmK/3eKcGStPh50vK1PqYw6LEn6c5T++uFd+ckB0ucEmNIT3zV6w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710429157; c=relaxed/simple; bh=Sq4BX5nCnQbeNMS1RtfpnrqJt/dcI2CBuZ/qi1DiqU4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=shQLooQqpLnbFnn+9AhpVdRqK0eA58lknOiS9FyfwtAb6JMchX4hfFtD8JcdLpWC+7O05C6rBwdpSv0uB0bzrk1PjWpTrmhVRxV4gx/Ns2w3iAW3uXqtRQwnB+Fgq3834S1S7tdLB8XeXk4oyP6pPM6aEEkbNP3sFvGE22yfvXk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Oack5M/W; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 084F6C43390; Thu, 14 Mar 2024 15:12:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710429156; bh=Sq4BX5nCnQbeNMS1RtfpnrqJt/dcI2CBuZ/qi1DiqU4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Oack5M/WdKQ8fmUxvx5L4jrUbRLtqn47gAx6wV+xhim0Uspor2HZgxqimphibTCa7 pLmpGC8UgeN8dk3E8ZEvFWeveoXa/qRj5jkAjVzkxOp4+07/6gevbU+YRhD8P6vN/y Emm59/VXoKAwW5xUqFVP4Ioi6HegcNpAh1916FFCvP55ohO+cKuH9F/JeZB7TlQCCp VGzQNf3tRt5gDz3OGI2S0i0jIAmdNaTEyB5dTygjQrmhND6t3/yDfkF3UhgwHjZ3o8 Pi9HZSZinVwUEDaDtAJNkhY5WzJcyFO3jRy59CGl2U3Em5gEJkhzuflH3v6MQlXzMI 9cvRE2fCcJkNw== From: "Masami Hiramatsu (Google)" To: Linux Trace Kernel , LKML , Steven Rostedt Cc: Qiang Zhang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Masami Hiramatsu , Jinghao Jia , Peter Zijlstra , x86@kernel.org Subject: [PATCH] kprobes/x86: Use copy_from_kernel_nofault() to read from unsafe address Date: Fri, 15 Mar 2024 00:12:30 +0900 Message-Id: <171042914997.151113.2962249725455796854.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240315000753.a448251fce0291e041f76c13@kernel.org> References: <20240315000753.a448251fce0291e041f76c13@kernel.org> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit From: Masami Hiramatsu (Google) Read from an unsafe address with copy_from_kernel_nofault() in arch_adjust_kprobe_addr() because this function is used before checking the address is in text or not. Syzcaller bot found a bug and reported the case if user specifies inaccessible data area, arch_adjust_kprobe_addr() will cause a kernel panic. Reported-by: Qiang Zhang Closes: https://lore.kernel.org/all/CAKHoSAs2rof6vQVBw_Lg_j3QNku0CANZR2qmy4eT7R5Lo8MFbg@mail.gmail.com/ Fixes: cc66bb914578 ("x86/ibt,kprobes: Cure sym+0 equals fentry woes") Signed-off-by: Masami Hiramatsu (Google) --- arch/x86/kernel/kprobes/core.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index a0ce46c0a2d8..a885eea3bd34 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -335,7 +335,16 @@ static int can_probe(unsigned long paddr) kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry) { - if (is_endbr(*(u32 *)addr)) { + u32 insn; + + /* + * Since addr is not guaranteed to be safely accessed yet, use + * copy_from_kernel_nofault() to get the instruction. + */ + if (copy_from_kernel_nofault(&insn, (void *)addr, sizeof(u32))) + return 0; + + if (is_endbr(insn)) { *on_func_entry = !offset || offset == 4; if (*on_func_entry) offset = 4;