Skip to content

Commit 137df11

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Don't assume SHT_GNU_verdef presence for SHT_GNU_versym section
Fix too eager assumption that SHT_GNU_verdef ELF section is going to be present whenever binary has SHT_GNU_versym section. It seems like either SHT_GNU_verdef or SHT_GNU_verneed can be used, so failing on missing SHT_GNU_verdef actually breaks use cases in production. One specific reported issue, which was used to manually test this fix, was trying to attach to `readline` function in BASH binary. Fixes: bb7fa09 ("libbpf: Support symbol versioning for uprobe") Reported-by: Liam Wisehart <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Manu Bretelle <[email protected]> Reviewed-by: Fangrui Song <[email protected]> Acked-by: Hengqi Chen <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a3c2dd9 commit 137df11

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tools/lib/bpf/elf.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ static int elf_sym_iter_new(struct elf_sym_iter *iter,
141141
iter->versyms = elf_getdata(scn, 0);
142142

143143
scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
144-
if (!scn) {
145-
pr_debug("elf: failed to find verdef ELF sections in '%s'\n", binary_path);
146-
return -ENOENT;
147-
}
148-
if (!gelf_getshdr(scn, &sh))
144+
if (!scn)
145+
return 0;
146+
147+
iter->verdefs = elf_getdata(scn, 0);
148+
if (!iter->verdefs || !gelf_getshdr(scn, &sh)) {
149+
pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
149150
return -EINVAL;
151+
}
150152
iter->verdef_strtabidx = sh.sh_link;
151-
iter->verdefs = elf_getdata(scn, 0);
152153

153154
return 0;
154155
}
@@ -199,6 +200,9 @@ static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
199200
GElf_Verdef verdef;
200201
int offset;
201202

203+
if (!iter->verdefs)
204+
return NULL;
205+
202206
offset = 0;
203207
while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
204208
if (verdef.vd_ndx != ver) {

0 commit comments

Comments
 (0)