From mboxrd@z Thu Jan 1 00:00:00 1970 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 A56CB1C03 for ; Mon, 20 Mar 2023 14:57:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23EB7C433D2; Mon, 20 Mar 2023 14:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1679324265; bh=fCzijDuhRGRw/qa4WBR2aDFPO8AGrGYNk1bLYWQdVqc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hBRRLW07aozfRRVJhm3UK7HEFxneccbIHgnhR4x+3ab+RNj9rro+FFPZYmiK5lXY2 fGKdP8V6W5ysfNjz/xSyPv7NCEA25OF6I0O6BPTpt49nQamjcM+5gzcyWhiyREUcq/ YwOSt2OCmxxriIkYl2wrl3edppPT+naMYOYhb3qo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Steven Rostedt (Google)" , Chen Zhongjin Subject: [PATCH 4.14 25/30] ftrace: Fix invalid address access in lookup_rec() when index is 0 Date: Mon, 20 Mar 2023 15:54:49 +0100 Message-Id: <20230320145421.190405652@linuxfoundation.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230320145420.204894191@linuxfoundation.org> References: <20230320145420.204894191@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Chen Zhongjin commit ee92fa443358f4fc0017c1d0d325c27b37802504 upstream. KASAN reported follow problem: BUG: KASAN: use-after-free in lookup_rec Read of size 8 at addr ffff000199270ff0 by task modprobe CPU: 2 Comm: modprobe Call trace: kasan_report __asan_load8 lookup_rec ftrace_location arch_check_ftrace_location check_kprobe_address_safe register_kprobe When checking pg->records[pg->index - 1].ip in lookup_rec(), it can get a pg which is newly added to ftrace_pages_start in ftrace_process_locs(). Before the first pg->index++, index is 0 and accessing pg->records[-1].ip will cause this problem. Don't check the ip when pg->index is 0. Link: https://lore.kernel.org/linux-trace-kernel/20230309080230.36064-1-chenzhongjin@huawei.com Cc: stable@vger.kernel.org Fixes: 9644302e3315 ("ftrace: Speed up search by skipping pages by address") Suggested-by: Steven Rostedt (Google) Signed-off-by: Chen Zhongjin Signed-off-by: Steven Rostedt (Google) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/ftrace.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1646,7 +1646,8 @@ unsigned long ftrace_location_range(unsi key.flags = end; /* overload flags, as it is unsigned long */ for (pg = ftrace_pages_start; pg; pg = pg->next) { - if (end < pg->records[0].ip || + if (pg->index == 0 || + end < pg->records[0].ip || start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) continue; rec = bsearch(&key, pg->records, pg->index,