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 390491D04A8; Wed, 2 Oct 2024 13:33:27 +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=1727876007; cv=none; b=Aatd24ncsE99uZUHRr0CfEhPeAc0CBPryKHd20nqypdx+TAnX+xikmQ9TlmIo5aSpXHJfW9s1gEaXPGXF9F4Y15slrJZSPgpWpl+4ieHU32zQHYaleNxlEjoP7mXm6SZ44yFhTz8OJn5+UlUdXANyIgqsQwazTmE40qQ56srmV0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727876007; c=relaxed/simple; bh=G3XwklZq6t+1vLAXNRKPeWYUCLaHBUjovODRW5qjiPY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dT/ZVS4Och8VjDglyOxTPWBHwytEsch4klpRDbgh9ZKCkCqRh7YUpYMcVJq2/ncTqw/+4CVVp6WIB7hC3C43GgxCAPjERR0vnOVxsiTKN64qC1ULZjEDivIRUHtMuqoBP9ajNzLtiJjgh+4SyoH9jiYolkgDtEXCgOzRHHI7Ezc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=JUIy7T43; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="JUIy7T43" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5431C4CEC5; Wed, 2 Oct 2024 13:33:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1727876007; bh=G3XwklZq6t+1vLAXNRKPeWYUCLaHBUjovODRW5qjiPY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JUIy7T43D/2SKTLk0nUkVw58MF1rnVNtDrkTHgOZZXdtfOIkQYj05qlfc/LQF1Dv8 6nWYKMEpk5LpfnJrQpFM+JVwLCYK9DzUeh/qqLJQNFtd2RkO/Bcu9mj/ewhBeEBJKT uPmUeyWunKZefU3IAYVpu9yHSwI7Aqh6QNZhbfnE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Liu RuiTong , Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.11 297/695] bpf: correctly handle malformed BPF_CORE_TYPE_ID_LOCAL relos Date: Wed, 2 Oct 2024 14:54:55 +0200 Message-ID: <20241002125834.296475375@linuxfoundation.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241002125822.467776898@linuxfoundation.org> References: <20241002125822.467776898@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.11-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eduard Zingerman [ Upstream commit 3d2786d65aaa954ebd3fcc033ada433e10da21c4 ] In case of malformed relocation record of kind BPF_CORE_TYPE_ID_LOCAL referencing a non-existing BTF type, function bpf_core_calc_relo_insn would cause a null pointer deference. Fix this by adding a proper check upper in call stack, as malformed relocation records could be passed from user space. Simplest reproducer is a program: r0 = 0 exit With a single relocation record: .insn_off = 0, /* patch first instruction */ .type_id = 100500, /* this type id does not exist */ .access_str_off = 6, /* offset of string "0" */ .kind = BPF_CORE_TYPE_ID_LOCAL, See the link for original reproducer or next commit for a test case. Fixes: 74753e1462e7 ("libbpf: Replace btf__type_by_id() with btf_type_by_id().") Reported-by: Liu RuiTong Closes: https://lore.kernel.org/bpf/CAK55_s6do7C+DVwbwY_7nKfUz0YLDoiA1v6X3Y9+p0sWzipFSA@mail.gmail.com/ Acked-by: Andrii Nakryiko Signed-off-by: Eduard Zingerman Link: https://lore.kernel.org/r/20240822080124.2995724-2-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/btf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 249c94c996150..7783b16b87cfe 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8891,6 +8891,7 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, struct bpf_core_cand_list cands = {}; struct bpf_core_relo_res targ_res; struct bpf_core_spec *specs; + const struct btf_type *type; int err; /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5" @@ -8900,6 +8901,13 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, if (!specs) return -ENOMEM; + type = btf_type_by_id(ctx->btf, relo->type_id); + if (!type) { + bpf_log(ctx->log, "relo #%u: bad type id %u\n", + relo_idx, relo->type_id); + return -EINVAL; + } + if (need_cands) { struct bpf_cand_cache *cc; int i; -- 2.43.0