Skip to contentSkip to conversation

net: bpf: arm64: fix module memory leak when JIT image build fails

PATCH Applied Daniel Borkmann 4 participants 4 messages Sep 11, 2014 – Sep 12, 2014

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

PATCH 1/1 net: bpf: arm64: fix module memory leak when JIT image build fails

On ARM64, when the BPF JIT compiler fills the JIT image body with opcodes during translation of eBPF into ARM64 opcodes, we may fail for several reasons during that phase: one being that we jump to the notyet label for not yet supported eBPF instructions such as BPF_ST. In that case we only free offsets, but not the actual allocated target image where opcodes are being stored. Fix it by calling module_free() on dismantle time in case of errors.

Cc Zi Shen Lim <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Will Deacon <[email protected]>

---
[ Compile-tested only. ]

 arch/arm64/net/bpf_jit_comp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +3 -1
index 38c4296..7ae3354 100644
@@ -651,8 +651,10 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
 	build_prologue(&ctx);
 
 	ctx.body_offset = ctx.idx;
-	if (build_body(&ctx))
+	if (build_body(&ctx)) {
+		module_free(NULL, ctx.image);
 		goto out;
+	}
 
 	build_epilogue(&ctx);
 
Signature · 1 line
1.9.3
Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Daniel Borkmann

On ARM64, when the BPF JIT compiler fills the JIT image body with opcodes during translation of eBPF into ARM64 opcodes, we may fail for several reasons during that phase: one being that we jump to the notyet label for not yet supported eBPF instructions such as BPF_ST. In that case we only free offsets, but not the actual allocated target image where opcodes are being stored. Fix it by calling module_free() on dismantle time in case of errors.

Cc Zi Shen Lim <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Will Deacon <[email protected]>

---
[ Compile-tested only. ]

 arch/arm64/net/bpf_jit_comp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index 38c4296..7ae3354 100644

arch/arm64/net/bpf_jit_comp.c void bpf_int_jit_compile(struct bpf_prog *prog) · line 658

Quoted 9 lines
arch/arm64/net/bpf_jit_comp.c +3 -1
@@ -651,8 +651,10 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
 	build_prologue(&ctx);
 
 	ctx.body_offset = ctx.idx;
-	if (build_body(&ctx))
+	if (build_body(&ctx)) {
+		module_free(NULL, ctx.image);
 		goto out;
+	}
 

Looks good to me:

Catalin, can you apply this on the for-next/core branch, please?

Will

Acked-by
#2 lore raw
Quoted 36 lines · Will Deacon
Quoted 23 lines · Daniel Borkmann

On ARM64, when the BPF JIT compiler fills the JIT image body with opcodes during translation of eBPF into ARM64 opcodes, we may fail for several reasons during that phase: one being that we jump to the notyet label for not yet supported eBPF instructions such as BPF_ST. In that case we only free offsets, but not the actual allocated target image where opcodes are being stored. Fix it by calling module_free() on dismantle time in case of errors.

Cc Zi Shen Lim <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Will Deacon <[email protected]>

---
[ Compile-tested only. ]

 arch/arm64/net/bpf_jit_comp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index 38c4296..7ae3354 100644

arch/arm64/net/bpf_jit_comp.c void bpf_int_jit_compile(struct bpf_prog *prog) · line 658

Quoted 9 lines
arch/arm64/net/bpf_jit_comp.c +3 -1
@@ -651,8 +651,10 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
      build_prologue(&ctx);
 
      ctx.body_offset = ctx.idx;
-     if (build_body(&ctx))
+     if (build_body(&ctx)) {
+             module_free(NULL, ctx.image);
              goto out;
+     }
 

Looks good to me:

Quoted 1 line

Same here:

Thank you Daniel.

Quoted 4 lines

Catalin, can you apply this on the for-next/core branch, please?

Will

maintainer Applied
#3 lore raw
In reply to Will Deacon
Quoted 36 lines · Will Deacon
Quoted 23 lines · Daniel Borkmann

On ARM64, when the BPF JIT compiler fills the JIT image body with opcodes during translation of eBPF into ARM64 opcodes, we may fail for several reasons during that phase: one being that we jump to the notyet label for not yet supported eBPF instructions such as BPF_ST. In that case we only free offsets, but not the actual allocated target image where opcodes are being stored. Fix it by calling module_free() on dismantle time in case of errors.

Cc Zi Shen Lim <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Will Deacon <[email protected]>

---
[ Compile-tested only. ]

 arch/arm64/net/bpf_jit_comp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index 38c4296..7ae3354 100644

arch/arm64/net/bpf_jit_comp.c void bpf_int_jit_compile(struct bpf_prog *prog) · line 658

Quoted 9 lines
arch/arm64/net/bpf_jit_comp.c +3 -1
@@ -651,8 +651,10 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
 	build_prologue(&ctx);
 
 	ctx.body_offset = ctx.idx;
-	if (build_body(&ctx))
+	if (build_body(&ctx)) {
+		module_free(NULL, ctx.image);
 		goto out;
+	}
 

Looks good to me:

Quoted 3 lines

Catalin, can you apply this on the for-next/core branch, please?

Applied, thanks.

Signature · 1 line
Catalin