All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Nicholas Carlini <npc@anthropic.com>,
	kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf v4 11/11] selftests/bpf: Add truncated CO-RE ldimm64 test
Date: Tue, 15 Sep 2026 00:25:08 +0200	[thread overview]
Message-ID: <20260914222514.1635018-12-memxor@gmail.com> (raw)
In-Reply-To: <20260914222514.1635018-1-memxor@gmail.com>

Exercise CO-RE relocation of an ldimm64 instruction whose second slot is
missing. Build a valid BPF object with a compiler-emitted relocation, then
truncate the program symbol to retain only the first instruction slot.

Without bounds validation, libbpf reads or writes beyond the exact-sized
instruction allocation, depending on whether the relocation resolves.
Exercise both paths and require bpf_object__prepare() to reject the
malformed object with a precise diagnostic.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../prog_tests/core_reloc_truncated_ldimm64.c | 82 +++++++++++++++++++
 .../bpf/progs/core_reloc_truncated_ldimm64.c  | 27 ++++++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/core_reloc_truncated_ldimm64.c
 create mode 100644 tools/testing/selftests/bpf/progs/core_reloc_truncated_ldimm64.c

diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc_truncated_ldimm64.c b/tools/testing/selftests/bpf/prog_tests/core_reloc_truncated_ldimm64.c
new file mode 100644
index 000000000000..b70c8874241d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc_truncated_ldimm64.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <elf.h>
+#include <test_progs.h>
+
+#include "core_reloc_truncated_ldimm64.skel.h"
+
+static bool truncate_ldimm64_prog(void *elf_data, size_t elf_sz, const char *prog_name)
+{
+	Elf64_Ehdr *ehdr = elf_data;
+	Elf64_Shdr *shdrs;
+	Elf64_Sym *syms;
+	const char *strs;
+	size_t i, j;
+
+	if (elf_sz < sizeof(*ehdr) || ehdr->e_shoff > elf_sz ||
+	    ehdr->e_shnum > (elf_sz - ehdr->e_shoff) / sizeof(*shdrs))
+		return false;
+
+	shdrs = elf_data + ehdr->e_shoff;
+	for (i = 0; i < ehdr->e_shnum; i++) {
+		if (shdrs[i].sh_type != SHT_SYMTAB)
+			continue;
+		syms = elf_data + shdrs[i].sh_offset;
+		strs = elf_data + shdrs[shdrs[i].sh_link].sh_offset;
+		for (j = 0; j < shdrs[i].sh_size / sizeof(*syms); j++) {
+			if (ELF64_ST_TYPE(syms[j].st_info) == STT_FUNC &&
+			    !strcmp(strs + syms[j].st_name, prog_name)) {
+				syms[j].st_size = sizeof(struct bpf_insn);
+				return true;
+			}
+		}
+	}
+
+	return false;
+}
+
+static void run_test(const char *prog_name)
+{
+	const void *elf_bytes;
+	size_t elf_sz;
+	struct bpf_object *obj = NULL;
+	void *elf_data = NULL;
+	char *log = NULL;
+	int err;
+
+	elf_bytes = core_reloc_truncated_ldimm64__elf_bytes(&elf_sz);
+	if (!ASSERT_OK_PTR(elf_bytes, "elf_bytes"))
+		return;
+
+	elf_data = malloc(elf_sz);
+	if (!ASSERT_OK_PTR(elf_data, "elf_data"))
+		return;
+	memcpy(elf_data, elf_bytes, elf_sz);
+	if (!ASSERT_TRUE(truncate_ldimm64_prog(elf_data, elf_sz, prog_name), "truncate_prog"))
+		goto cleanup;
+
+	obj = bpf_object__open_mem(elf_data, elf_sz, NULL);
+	if (!ASSERT_OK_PTR(obj, "obj_open"))
+		goto cleanup;
+
+	if (start_libbpf_log_capture())
+		goto cleanup;
+	err = bpf_object__prepare(obj);
+	log = stop_libbpf_log_capture();
+	ASSERT_EQ(err, -EINVAL, "obj_prepare");
+	ASSERT_HAS_SUBSTR(log, "insn #0 (LDIMM64) is truncated", "libbpf_log");
+
+cleanup:
+	free(log);
+	bpf_object__close(obj);
+	free(elf_data);
+}
+
+void test_core_reloc_truncated_ldimm64(void)
+{
+	if (test__start_subtest("resolved"))
+		run_test("core_reloc_truncated_ldimm64_resolved");
+	if (test__start_subtest("poison"))
+		run_test("core_reloc_truncated_ldimm64_poison");
+}
diff --git a/tools/testing/selftests/bpf/progs/core_reloc_truncated_ldimm64.c b/tools/testing/selftests/bpf/progs/core_reloc_truncated_ldimm64.c
new file mode 100644
index 000000000000..d8d9a3650fe0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/core_reloc_truncated_ldimm64.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+
+enum core_reloc_truncated_ldimm64 {
+	CORE_RELOC_TRUNCATED_LDIMM64_VALUE,
+};
+
+SEC("raw_tracepoint/sys_enter")
+int core_reloc_truncated_ldimm64_resolved(void *ctx)
+{
+	return bpf_core_enum_value_exists(enum core_reloc_truncated_ldimm64,
+					  CORE_RELOC_TRUNCATED_LDIMM64_VALUE);
+}
+
+SEC("raw_tracepoint/sys_exit")
+int core_reloc_truncated_ldimm64_poison(void *ctx)
+{
+	return bpf_core_enum_value(enum core_reloc_truncated_ldimm64,
+				   CORE_RELOC_TRUNCATED_LDIMM64_VALUE);
+}
+
+char LICENSE[] SEC("license") = "GPL";
-- 
2.53.0


  parent reply	other threads:[~2026-09-14 22:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14 22:24 [PATCH bpf v4 00/11] Misc bug fixes - part 5 Kumar Kartikeya Dwivedi
2026-09-14 22:24 ` [PATCH bpf v4 01/11] bpf: Make post-verification instruction rewrites killable Kumar Kartikeya Dwivedi
2026-09-14 22:24 ` [PATCH bpf v4 02/11] bpf: Preserve packet pointer class displacement in regsafe() Kumar Kartikeya Dwivedi
2026-09-14 22:25 ` [PATCH bpf v4 03/11] selftests/bpf: Test packet pointer class displacement pruning Kumar Kartikeya Dwivedi
2026-09-14 22:25 ` [PATCH bpf v4 04/11] bpf: Apply CO-RE relocations before subprogram validation Kumar Kartikeya Dwivedi
2026-09-14 23:15   ` bot+bpf-ci
2026-09-16 18:22   ` Eduard Zingerman
2026-09-14 22:25 ` [PATCH bpf v4 05/11] selftests/bpf: Test early in-kernel CO-RE relocation Kumar Kartikeya Dwivedi
2026-09-14 23:28   ` bot+bpf-ci
2026-09-16 18:23   ` Eduard Zingerman
2026-09-16 18:26     ` Kumar Kartikeya Dwivedi
2026-09-14 22:25 ` [PATCH bpf v4 06/11] bpf: Restrict CO-RE poisoning to relocatable instructions Kumar Kartikeya Dwivedi
2026-09-14 23:28   ` bot+bpf-ci
2026-09-14 22:25 ` [PATCH bpf v4 07/11] selftests/bpf: Test CO-RE instruction poisoning restrictions Kumar Kartikeya Dwivedi
2026-09-16 18:27   ` Eduard Zingerman
2026-09-14 22:25 ` [PATCH bpf v4 08/11] bpf: Assign lock identity to callback map values Kumar Kartikeya Dwivedi
2026-09-14 22:25 ` [PATCH bpf v4 09/11] selftests/bpf: Check callback map value lock identity Kumar Kartikeya Dwivedi
2026-09-16 19:04   ` Eduard Zingerman
2026-09-14 22:25 ` [PATCH bpf v4 10/11] libbpf: Reject truncated ldimm64 CO-RE relocations Kumar Kartikeya Dwivedi
2026-09-16 18:37   ` Eduard Zingerman
2026-09-14 22:25 ` Kumar Kartikeya Dwivedi [this message]
2026-09-14 23:15   ` [PATCH bpf v4 11/11] selftests/bpf: Add truncated CO-RE ldimm64 test bot+bpf-ci
2026-09-16 18:43   ` Eduard Zingerman
2026-09-16 19:02     ` Kumar Kartikeya Dwivedi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260914222514.1635018-12-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=npc@anthropic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.