Skip to contentSkip to conversation

bpftool: Don't crash on missing jited insns or ksyms

PATCH Applied Toke Høiland-Jørgensen 5 participants 12 messages Dec 10, 2019 – Dec 11, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpftool: Don't crash on missing jited insns or ksyms

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

---
v2:
- The sysctl causing this is kptr_restrict, not bpf_jit_harden; update commit msg to get this right (Martin).

 tools/bpf/bpftool/prog.c          | 2 +-
 tools/bpf/bpftool/xlated_dumper.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/prog.c +1 -1
index 4535c863d2cd..2ce9c5ba1934 100644
@@ -493,7 +493,7 @@ static int do_dump(int argc, char **argv)
 
 	info = &info_linear->info;
 	if (mode == DUMP_JITED) {
-		if (info->jited_prog_len == 0) {
+		if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
 			p_info("no instructions returned");
 			goto err_free;
 		}
tools/bpf/bpftool/xlated_dumper.c +1 -1
index 494d7ae3614d..5b91ee65a080 100644
@@ -174,7 +174,7 @@ static const char *print_call(void *private_data,
 	struct kernel_sym *sym;
 
 	if (insn->src_reg == BPF_PSEUDO_CALL &&
-	    (__u32) insn->imm < dd->nr_jited_ksyms)
+	    (__u32) insn->imm < dd->nr_jited_ksyms && dd->jited_ksyms)
 		address = dd->jited_ksyms[insn->imm];
 
 	sym = kernel_syms_search(dd, address);
Signature · 1 line
2.24.0
Acked-by on v2 1/1
#1 lore raw
Quoted 7 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

#2 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

#3 lore raw
Quoted 11 lines · Jakub Kicinski
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Quoted 7 lines
Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

Yeah, guess so? Although I must admit it's not quite clear to me whether bpftool gets stable backports, or if it follows the "only moving forward" credo of libbpf?

Anyhow, I don't suppose it'll hurt to have the Fixes: tag(s) in there; does Patchwork pick these up (or can you guys do that when you apply this?), or should I resend?

-Toke

#4 lore raw
Quoted 16 lines · Toke Høiland-Jørgensen
Quoted 15 lines · Jakub Kicinski
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Quoted 7 lines
Quoted 3 lines
Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

Yeah, guess so? Although I must admit it's not quite clear to me whether bpftool gets stable backports, or if it follows the "only moving forward" credo of libbpf?

bpftool does not have a GH repo, and seeing strength of Alexei's arguments in the recent discussion - I don't think it will. So no reason for bpftool to be "special" ❄️.

Then again seeing Andrii's zeal for pushing the codegen stuff into bpftool, maybe Facebook's intention is to make it so.

Hard to tell what to do when standard practices don't apply, sigh.

Quoted 3 lines

Anyhow, I don't suppose it'll hurt to have the Fixes: tag(s) in there; does Patchwork pick these up (or can you guys do that when you apply this?), or should I resend?

I don't think it does, but perhaps Daniel's scripts do.

Either way I don't think it's worth a resend.

#5 lore raw
Quoted 21 lines · Jakub Kicinski
Quoted 20 lines · Toke Høiland-Jørgensen
Quoted 18 lines · Jakub Kicinski
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

Quoted 7 lines
Quoted 3 lines

Yeah, guess so? Although I must admit it's not quite clear to me whether bpftool gets stable backports, or if it follows the "only moving forward" credo of libbpf?

bpftool does not have a GH repo, and seeing strength of Alexei's arguments in the recent discussion - I don't think it will. So no reason for bpftool to be "special"

bpftool always was and will be a special user of libbpf.

#6 lore raw
Quoted 26 lines · Alexei Starovoitov
Quoted 25 lines · Jakub Kicinski
Quoted 23 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Jakub Kicinski
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Quoted 7 lines
Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

Yeah, guess so? Although I must admit it's not quite clear to me whether bpftool gets stable backports, or if it follows the "only moving forward" credo of libbpf?

Quoted 5 lines
Quoted 3 lines

bpftool does not have a GH repo, and seeing strength of Alexei's arguments in the recent discussion - I don't think it will. So no reason for bpftool to be "special"

bpftool always was and will be a special user of libbpf.

There we go again. Making proclamations without any justification or explanation.

Maybe there is a language barrier between us, but I wrote the initial bpftool code, so I don't see how you (who authored one patch) can say what it was or is. Do you mean to say what you intend to make it?

bpftool was intended to be a CLI to BPF _kernel_ interface. libbpf was just the library that we all agreed to use moving forward for ELF loading.

I'm not going to argue with you again. You kept bad mouthing iproute2 and then your only argument was the reviews sometimes take longer than 24 hours. Which I'm sure you have a lot of experience with:

iproute2$ git log --author=Starov --oneline
4bfe68253670 iptnl: add support for collect_md flag in IPv4 and IPv6 tunnels

iproute2$ git log --author=fb.com --oneline
3da6d055d93f bpf: add btf func and func_proto kind support 7a04dd84a7f9 bpf: check map symbol type properly with newer llvm compiler 73451259daaa tc: fix ipv6 filter selector attribute for some prefix lengths 0b4ea60b5a48 bpf: Add support for IFLA_XDP_PROG_ID
4bfe68253670 iptnl: add support for collect_md flag in IPv4 and IPv6 tunnels 414aeec90f82 ss: Add tcp_info fields data_segs_in/out 409998c5a4eb iproute: ip-gue/ip-fou manpages

Upstreaming bpftool was a big mistake, but we live and we learn.

#7 lore raw
Quoted 31 lines · Jakub Kicinski
Quoted 30 lines · Alexei Starovoitov
Quoted 28 lines · Jakub Kicinski
Quoted 16 lines · Toke Høiland-Jørgensen
Quoted 15 lines · Jakub Kicinski
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Fixes 71bb428fe2c1 ("tools: bpf: add bpftool")

and

Quoted 7 lines
Quoted 3 lines
Fixes f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")

?

Yeah, guess so? Although I must admit it's not quite clear to me whether bpftool gets stable backports, or if it follows the "only moving forward" credo of libbpf?

bpftool does not have a GH repo, and seeing strength of Alexei's arguments in the recent discussion - I don't think it will. So no reason for bpftool to be "special"

Quoted 8 lines
Quoted 1 line

bpftool always was and will be a special user of libbpf.

There we go again. Making proclamations without any justification or explanation.

Maybe there is a language barrier between us, but I wrote the initial bpftool code, so I don't see how you (who authored one patch) can say what it was or is. Do you mean to say what you intend to make it?

When code lands it becomes the part of the code that maintainers keep in the best shape possible considering contributions from many parties. Original author of the code is equal to everyone else who submits patches. The job of the maintainer is to mediate folks who contribute the patches. Sounds like you expected to own bpftool as a single owner. I don't think kernel is such place. In most projects I'm aware of the OWNERS file is discouraged. The kernel is such project. The kernel has MAINTAINERS file which lists people most knowledgeable in the area and who's job is to keep the code in good shape, consider long term growth, etc. Maintainers are not owners of the code.

bpftool became way more than what it was when initially landed. Just like libbpf was implemented mainly by huawei folks and used in perf only. Now perf is a minority and original authors are not contributing any more. I'm sure you thought of bpftool as cli for sys_bpf only. Well thankfully it outgrew this narrow view point. bpftool btf dump file doesn't call sys_bpf at all. bpftool perf | net using several other kernel apis. I think it's a good growth trajectory for bpftool. More people use it and like it.

Quoted 1 line

Upstreaming bpftool was a big mistake, but we live and we learn

ok. Not going count on your help anymore.
Since you're not interesting in helping please don't stall it either.

maintainer
#8 lore raw
In reply to Toke Høiland-Jørgensen

On Tue, Dec 10, 2019 at 10:09:55PM +0100, Toke Høiland-Jørgensen wrote:
[...]

Quoted 3 lines

Anyhow, I don't suppose it'll hurt to have the Fixes: tag(s) in there; does Patchwork pick these up (or can you guys do that when you apply this?), or should I resend?

Fixes tags should /always/ be present if possible, since they help to provide more context even if the buggy commit was in bpf-next, for example.

#9 lore raw
Quoted 8 lines · Daniel Borkmann

On Tue, Dec 10, 2019 at 10:09:55PM +0100, Toke Høiland-Jørgensen wrote:
[...]

Quoted 3 lines

Anyhow, I don't suppose it'll hurt to have the Fixes: tag(s) in there; does Patchwork pick these up (or can you guys do that when you apply this?), or should I resend?

Fixes tags should /always/ be present if possible, since they help to provide more context even if the buggy commit was in bpf-next, for example.

ACK, will do. Thank you for picking them up for this patch (did you do that manually, or is this part of your scripts?)

-Toke

maintainer
#10 lore raw
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 6 lines · Daniel Borkmann

On Tue, Dec 10, 2019 at 10:09:55PM +0100, Toke Høiland-Jørgensen wrote:
[...]

Quoted 3 lines

Anyhow, I don't suppose it'll hurt to have the Fixes: tag(s) in there; does Patchwork pick these up (or can you guys do that when you apply this?), or should I resend?

Quoted 5 lines
Quoted 2 lines

Fixes tags should /always/ be present if possible, since they help to provide more context even if the buggy commit was in bpf-next, for example.

ACK, will do. Thank you for picking them up for this patch (did you do that manually, or is this part of your scripts?)

Manually right now, but if you have some cycles, happy to pick up a patch [0]. :)

[0] https://git.kernel.org/pub/scm/linux/kernel/git/dborkman/pw.git/

maintainer Applied
#11 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 9 lines · Toke Høiland-Jørgensen

When the kptr_restrict sysctl is set, the kernel can fail to return jited_ksyms or jited_prog_insns, but still have positive values in nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when trying to dump the program because it only checks the len fields not the actual pointers to the instructions and ksyms.

Fix this by adding the missing checks.

Applied, thanks!