Skip to content

Commit 0aa7214

Browse files
etsalanakryiko
authored andcommitted
libbpf: Turn relo_core->sym_off unsigned
The symbols' relocation offsets in BPF are stored in an int field, but cannot actually be negative. When in the next patch libbpf relocates globals to the end of the arena, it is also possible to have valid offsets > 2GiB that are used to calculate the final relo offsets. Avoid accidentally interpreting large offsets as negative by turning the sym_off field unsigned. Signed-off-by: Emil Tsalapatis <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 12a1fe6 commit 0aa7214

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ struct reloc_desc {
380380
const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
381381
struct {
382382
int map_idx;
383-
int sym_off;
383+
unsigned int sym_off;
384384
/*
385385
* The following two fields can be unionized, as the
386386
* ext_idx field is used for extern symbols, and the
@@ -763,7 +763,7 @@ struct bpf_object {
763763

764764
struct {
765765
struct bpf_program *prog;
766-
int sym_off;
766+
unsigned int sym_off;
767767
int fd;
768768
} *jumptable_maps;
769769
size_t jumptable_map_cnt;
@@ -6192,7 +6192,7 @@ static void poison_kfunc_call(struct bpf_program *prog, int relo_idx,
61926192
insn->imm = POISON_CALL_KFUNC_BASE + ext_idx;
61936193
}
61946194

6195-
static int find_jt_map(struct bpf_object *obj, struct bpf_program *prog, int sym_off)
6195+
static int find_jt_map(struct bpf_object *obj, struct bpf_program *prog, unsigned int sym_off)
61966196
{
61976197
size_t i;
61986198

@@ -6210,7 +6210,7 @@ static int find_jt_map(struct bpf_object *obj, struct bpf_program *prog, int sym
62106210
return -ENOENT;
62116211
}
62126212

6213-
static int add_jt_map(struct bpf_object *obj, struct bpf_program *prog, int sym_off, int map_fd)
6213+
static int add_jt_map(struct bpf_object *obj, struct bpf_program *prog, unsigned int sym_off, int map_fd)
62146214
{
62156215
size_t cnt = obj->jumptable_map_cnt;
62166216
size_t size = sizeof(obj->jumptable_maps[0]);
@@ -6244,7 +6244,7 @@ static int find_subprog_idx(struct bpf_program *prog, int insn_idx)
62446244
static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struct reloc_desc *relo)
62456245
{
62466246
const __u32 jt_entry_size = 8;
6247-
int sym_off = relo->sym_off;
6247+
unsigned int sym_off = relo->sym_off;
62486248
int jt_size = relo->sym_size;
62496249
__u32 max_entries = jt_size / jt_entry_size;
62506250
__u32 value_size = sizeof(struct bpf_insn_array_value);
@@ -6260,7 +6260,7 @@ static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struc
62606260
return map_fd;
62616261

62626262
if (sym_off % jt_entry_size) {
6263-
pr_warn("map '.jumptables': jumptable start %d should be multiple of %u\n",
6263+
pr_warn("map '.jumptables': jumptable start %u should be multiple of %u\n",
62646264
sym_off, jt_entry_size);
62656265
return -EINVAL;
62666266
}
@@ -6316,7 +6316,7 @@ static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struc
63166316
* should contain values that fit in u32.
63176317
*/
63186318
if (insn_off > UINT32_MAX) {
6319-
pr_warn("map '.jumptables': invalid jump table value 0x%llx at offset %d\n",
6319+
pr_warn("map '.jumptables': invalid jump table value 0x%llx at offset %u\n",
63206320
(long long)jt[i], sym_off + i * jt_entry_size);
63216321
err = -EINVAL;
63226322
goto err_close;

0 commit comments

Comments
 (0)