Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btf: Take instruction size into account when handling poison relocation #1351

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ TARGETS := \
btf/testdata/relocs \
btf/testdata/relocs_read \
btf/testdata/relocs_read_tgt \
btf/testdata/relocs_enum \
cmd/bpf2go/testdata/minimal

.PHONY: all clean container-all container-shell generate
Expand Down
10 changes: 9 additions & 1 deletion btf/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ func (f *COREFixup) Apply(ins *asm.Instruction) error {
if f.poison {
const badRelo = 0xbad2310

*ins = asm.BuiltinFunc(badRelo).Call()
// Relocation is poisoned, replace the instruction with an invalid one.

if ins.OpCode.IsDWordLoad() {
// Replace a dword load with a invalid dword load to preserve instruction size.
*ins = asm.LoadImm(asm.R10, badRelo, asm.DWord)
} else {
// Replace all single size instruction with a invalid call instruction.
*ins = asm.BuiltinFunc(badRelo).Call()
}
return nil
}

Expand Down
27 changes: 27 additions & 0 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,30 @@ func TestCORERelocationRead(t *testing.T) {
}
})
}

func TestLD64IMMReloc(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.4", "vmlinux BTF in sysfs")

testutils.Files(t, testutils.Glob(t, "testdata/relocs_enum-*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

if spec.ByteOrder != internal.NativeEndian {
return
}

coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
})
}
Binary file added btf/testdata/relocs_enum-eb.elf
Binary file not shown.
Binary file added btf/testdata/relocs_enum-el.elf
Binary file not shown.
16 changes: 16 additions & 0 deletions btf/testdata/relocs_enum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "bpf_core_read.h"

enum cgroup_subsys_id {
cpuset_cgrp_id,
cpuset_cgrp_id_lublub,
CGROUP_SUBSYS_COUNT,
};

#define __section(NAME) __attribute__((section(NAME), used))

__section("socket/core_ld64imm") int core_ld64imm() {
if (bpf_core_enum_value_exists(enum cgroup_subsys_id, cpuset_cgrp_id_lublub)) {
__attribute__((unused)) const volatile int val = bpf_core_enum_value(enum cgroup_subsys_id, cpuset_cgrp_id_lublub);
}
return 0;
}
Loading