Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp173877lqb; Thu, 14 Mar 2024 08:17:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVYXHGXVMufXV3Ja3EIGVgxDWXe9U/HHVksyY5F34zzGXcpChvbKJEp5SlLQTWzYCS63WWkcrdHKMCYfOmYZzCvgrtEOpZYwLPIyssw5g== X-Google-Smtp-Source: AGHT+IEQfo/+gkfd3w7s6ILlhHvGYn8raq2PC1ddP8OJIgRLZDLICxDzijZWK2Ll3fzupbgH528A X-Received: by 2002:a17:90b:110:b0:29b:a149:2fe4 with SMTP id p16-20020a17090b011000b0029ba1492fe4mr333559pjz.34.1710429464644; Thu, 14 Mar 2024 08:17:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710429464; cv=pass; d=google.com; s=arc-20160816; b=oaYm6o7CJLfj+WuIlWMTU6eV7n7C/gpb9pk8uJofzlN6fFFEZdWNwK4ObZ/N4YYkZQ EyV5oDhBacs1kh3lzh1WEbkY8ehxpG0dtETf0FFy4DnlnU4W1zpNtqWTuoeyO8QDCEX1 oOFT2NsOSeXboqS6E/KUrD93j2rozETIYbxpRkWGezJmdpgO8wEEUjvlUtv81xn02/9O juucPcnJMDRmMASG2KQSNiaOhC62LhY8WTi4sCzZnvSafH1GARNXPzlEcvhQ1Vp2V1ul 6OAi6BtMADJWoFK3qYjPpvgcY/rhPiNezmwKNLD3HBnxJMpJnhLX35shlGG6mZk4O40t FDpg== 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=XVmZst48F+uBploSyrupZToF5HnmbVk1Wp9sBfiE5U0=; fh=Mp+ueS8yaRKk4fUufdq6DdmbLUa0RLVsRR2RgQzSkyc=; b=aCwIBg/GrhWJOXrm/w4+bddjsh5YoKJjJWwYrspodeTcUcUXtulwmZWsMZzqmIgtb0 RfNC61LyUtzgi6WwWk52SfLsdSodb4l0Xf1Qren2NH8hkxpNDAtHCRbtC6j1cDiFGxTy q2Qoxo/u1RRAOzmtqNLe/2fnKNAFyb8cAWcdf0KHiFwwJFj7FyUoCLR+7kF7W3aM5zOd H1LmlHHD+ZlCnfTd/DYiVmx/b/bCDHmXW1xBH+H1/cdNmZXUSXEH3egJ/CJKUR6Zzvrl Glx0GpiHcf2GI9kJuih5OUuNh0L/coKIDywml7zHtKnfh7DIdJNrEBQgFjr5Wx7lTIEo JDEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FrvLywuD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-103466-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103466-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id x8-20020a17090a9dc800b0029dddceef2asi813642pjv.10.2024.03.14.08.17.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 08:17:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103466-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FrvLywuD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-103466-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103466-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 81874B2230D for ; Thu, 14 Mar 2024 15:17:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD2C971B27; Thu, 14 Mar 2024 15:17:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FrvLywuD" 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 10A1171738; Thu, 14 Mar 2024 15:17:35 +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=1710429456; cv=none; b=iWtkAO3UyPjDoIP1tVeTLCoH6lZYULt5amvyOI2jBxrbPTZUhWwiuwzbe6UDXrpEOXgtwYwzBpi1iSnT2D3J15fDiMRy9MD7iaSKtSWCvq4vBs4fgoTxHywTfxPWoupZK0T8/cyqJ+EBta5XPIAsW/IRyBWcckCJim/+J2DlPxY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710429456; c=relaxed/simple; bh=FhmOMqNZWbah0/RhspM9QHHJPs9OQYwnR/APviYefvM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=tlrUFLPA18fVEOz//+GFnk8PHL0pMU6fUW0wBa5O7bqj0mR9eSJlXsmkyn418Bu8WIxR+sqrq9l/HQz7ETFLaWYMDLxCEI0wc4jz+LtzVoLhafzRV+XuiMeh5OVf3oBu4S98ipcTMfaQRuIW8Zghbn0jNPHDn528Cdw0EiyvaxA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FrvLywuD; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71839C43390; Thu, 14 Mar 2024 15:17:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710429455; bh=FhmOMqNZWbah0/RhspM9QHHJPs9OQYwnR/APviYefvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FrvLywuDM/2n2cTmn6OA+qXZViNFe8Rh78EIBwqDZvjtBB14ChvbEn5ATiLTfu7Op JcA2ko1mlUUTXOFk7hEiIgUAdo5f3zCiGv6UJHgwy68Veu8EwARS0BRQceUaLKGY5n paX3JTPYI0C9wNIIob4SkQdlkbeOFTbk2N2vwvhZBh3yEniWB6Bu2vcS+d6NjuE6eC NRpsYN3/VoxHL9XaTuyMKerc9RsfltuxOcRNz1lm9lNyOrOu8RnuIE9TAKO4s5AKjR tT4kkWapmpX1xdlodEdnfbmA6uK/CeI0HbLD1rJWSt9ZpafdygRu12Ub8in2fG8sZR a/Ipwn9FMBhHw== 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 v2] kprobes/x86: Use copy_from_kernel_nofault() to read from unsafe address Date: Fri, 15 Mar 2024 00:17:30 +0900 Message-Id: <171042945004.154897.2221804961882915806.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) --- Changes in v2: - Fix to return NULL if failed to access the address. --- 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..95e4ebe5d514 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 NULL; + + if (is_endbr(insn)) { *on_func_entry = !offset || offset == 4; if (*on_func_entry) offset = 4;