Two x86 BPF JIT fixes
Subsystems (5)
Daniel Borkmann
maintainercover Two x86 BPF JIT fixes
Fix two memory leaks in x86 JIT. For details, please see individual patches in this series. Thanks!
Daniel Borkmann (2):
bpf, x64: fix memleak when not converging after image bpf, x64: fix memleak when not converging on calls
arch/x86/net/bpf_jit_comp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 1/2 bpf, x64: fix memleak when not converging after image
While reviewing x64 JIT code, I noticed that we leak the prior allocated JIT image in the case where proglen != oldproglen during the JIT passes. Prior to the commit e0ee9c12157d ("x86: bpf_jit: fix two bugs in eBPF JIT compiler") we would just break out of the loop, and using the image as the JITed prog since it could only shrink in size anyway. After e0ee9c12157d, we would bail out to out_addrs label where we free addrs and jit_data but not the image coming from bpf_jit_binary_alloc().
--- arch/x86/net/bpf_jit_comp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +2 -2
@@ -1236,6 +1236,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)for (pass =0 ; pass <20 || image; pass++) { proglen =do_jit (prog, addrs, image, oldproglen, &ctx);if (proglen <=0 ) { +out_image: image =NULL ;if (header)bpf_jit_binary_free (header);
@@ -1246,8 +1247,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)if (proglen != oldproglen) {pr_err ("bpf_jit: proglen=%d != oldproglen=%d\n" , proglen, oldproglen); - prog = orig_prog; - goto out_addrs; + goto out_image; }break ; }
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 2/2 bpf, x64: fix memleak when not converging on calls
In reply to Daniel BorkmannThe JIT logic in jit_subprogs() is as follows: for all subprogs we allocate a bpf_prog_alloc(), populate it (prog->is_func = 1 here), and pass it to bpf_int_jit_compile(). If a failure occurred during JIT and prog->jited is not set, then we bail out from attempting to JIT the whole program, and punt to the interpreter instead. In case JITing went successful, we fixup BPF call offsets and do another pass to bpf_int_jit_compile() (extra_pass is true at that point) to complete JITing calls. Given that requires to pass JIT context around addrs and jit_data from x86 JIT are freed in the extra_pass in bpf_int_jit_compile() when calls are involved (if not, they can be freed immediately). However, if in the original pass, the JIT image didn't converge then we leak addrs and jit_data since image itself is NULL, the prog->is_func is set and extra_pass is false in that case, meaning both will become unreachable and are never cleaned up, therefore we need to free as well on !image. Only x64 JIT is affected.
--- arch/x86/net/bpf_jit_comp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/net/bpf_jit_comp.c +1 -1
@@ -1283,7 +1283,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) prog = orig_prog; } -if (!prog->is_func || extra_pass) { +if (!image || !prog->is_func || extra_pass) { out_addrs:kfree (addrs);kfree (jit_data);
Signature · 1 line
2.9.5
Re: [PATCH bpf 1/2] bpf, x64: fix memleak when not converging after image
In reply to Daniel BorkmannFrom: Daniel Borkmann <[email protected]>
Date: Wed, 2 May 2018 20:12:22 +0200
Quoted 11 lines
While reviewing x64 JIT code, I noticed that we leak the prior allocated JIT image in the case where proglen != oldproglen during the JIT passes. Prior to the commit
e0ee9c12157d("x86:bpf_jit: fix two bugs in eBPF JIT compiler") we would just break out of the loop, and using the image as the JITed prog since it could only shrink in size anyway. Aftere0ee9c12157d, we would bail out toout_addrslabel where we free addrs andjit_databut not the image coming frombpf_jit_binary_alloc().Fixes e0ee9c12157d ("x86: bpf_jit: fix two bugs in eBPF JIT compiler")Signed-off-by Daniel Borkmann <[email protected]>
Re: [PATCH bpf 2/2] bpf, x64: fix memleak when not converging on calls
In reply to Daniel BorkmannFrom: Daniel Borkmann <[email protected]>
Date: Wed, 2 May 2018 20:12:23 +0200
Quoted 17 lines
The JIT logic in
jit_subprogs()is as follows: for all subprogs we allocate abpf_prog_alloc(), populate it (prog->is_func= 1 here), and pass it tobpf_int_jit_compile(). If a failure occurred during JIT andprog->jitedis not set, then we bail out from attempting to JIT the whole program, and punt to the interpreter instead. In case JITing went successful, we fixup BPF call offsets and do another pass tobpf_int_jit_compile()(extra_passis true at that point) to complete JITing calls. Given that requires to pass JIT context around addrs andjit_datafrom x86 JIT are freed in theextra_passinbpf_int_jit_compile()when calls are involved (if not, they can be freed immediately). However, if in the original pass, the JIT image didn't converge then we leak addrs andjit_datasince image itself is NULL, theprog->is_funcis set andextra_passis false in that case, meaning both will become unreachable and are never cleaned up, therefore we need to free as well on !image. Only x64 JIT is affected.
Quoted 3 lines
Fixes 1c2a088a6626 ("bpf: x64: add JIT support for multi-function programs")Signed-off-by Daniel Borkmann <[email protected]>
Alexei Starovoitov
AppliedQuoted 2 lines · Daniel Borkmann
Fix two memory leaks in x86 JIT. For details, please see individual patches in this series. Thanks!
Applied to bpf tree, Thanks Daniel.