Skip to content

Commit

Permalink
fix: support bpf_core_type_exists()
Browse files Browse the repository at this point in the history
In the previous version, when no target is found in BTF, coreCalculateFixups()
will mark its relocation as poison and cause failure when verified by Linux verifier.

This issue is fixed by using right fixup for "checksForExistence" type of relocations.

Signed-off-by: Shang-Wen Wang (Sam Wang) <sam_s_wang@trendmicro.com>
  • Loading branch information
holyspectral authored and ti-mo committed May 5, 2022
1 parent 422a008 commit c8d1cae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/btf/core.go
Expand Up @@ -315,10 +315,16 @@ func coreCalculateFixups(byteOrder binary.ByteOrder, local Type, targets []Type,

if bestFixups == nil {
// Nothing at all matched, probably because there are no suitable
// targets at all. Poison everything!
// targets at all.
//
// Poison everything except checksForExistence.
bestFixups = make([]COREFixup, len(relos))
for i, relo := range relos {
bestFixups[i] = COREFixup{kind: relo.kind, poison: true}
if relo.kind.checksForExistence() {
bestFixups[i] = COREFixup{kind: relo.kind, local: 1, target: 0}
} else {
bestFixups[i] = COREFixup{kind: relo.kind, poison: true}
}
}
}

Expand Down
Binary file modified internal/btf/testdata/relocs_read-eb.elf
Binary file not shown.
Binary file modified internal/btf/testdata/relocs_read-el.elf
Binary file not shown.
15 changes: 15 additions & 0 deletions internal/btf/testdata/relocs_read.c
Expand Up @@ -26,6 +26,12 @@ struct bits {
u64 f : 16, g : 30;
};

struct nonexist {
int non_exist;
};

enum nonexist_enum { NON_EXIST = 1 };

// Perform a read from a subprog to ensure CO-RE relocations
// occurring there are tracked and executed in the final linked program.
__attribute__((noinline)) int read_subprog() {
Expand Down Expand Up @@ -89,6 +95,15 @@ __attribute__((noinline)) int read_subprog() {
if (BPF_CORE_READ_BITFIELD(&bar, g) != 0x15443322)
return __LINE__;

if (bpf_core_type_exists(struct nonexist) != 0)
return __LINE__;

if (bpf_core_field_exists(((struct nonexist *)0)->non_exist) != 0)
return __LINE__;

if (bpf_core_enum_value_exists(enum nonexist_enum, NON_EXIST) != 0)
return __LINE__;

return 0;
}

Expand Down

0 comments on commit c8d1cae

Please sign in to comment.