Skip to content

Commit a54b2f6

Browse files
liu-song-6Nobody
authored andcommitted
bpf, x86: set header->size properly before freeing it
On do_jit failure path, the header is freed by bpf_jit_binary_pack_free. While bpf_jit_binary_pack_free doesn't require proper ro_header->size, bpf_prog_pack_free still uses it. Set header->size in bpf_int_jit_compile before calling bpf_jit_binary_pack_free. Fixes: 1022a54 ("bpf, x86_64: Use bpf_jit_binary_pack_alloc") Fixes: 33c9805 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]") Reported-by: Kui-Feng Lee <kuifeng@fb.com> Signed-off-by: Song Liu <song@kernel.org>
1 parent 131e52d commit a54b2f6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,8 +2330,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
23302330
if (proglen <= 0) {
23312331
out_image:
23322332
image = NULL;
2333-
if (header)
2333+
if (header) {
2334+
/* set header->size for bpf_arch_text_copy */
2335+
bpf_arch_text_copy(&header->size, &rw_header->size,
2336+
sizeof(rw_header->size));
23342337
bpf_jit_binary_pack_free(header, rw_header);
2338+
}
23352339
prog = orig_prog;
23362340
goto out_addrs;
23372341
}

kernel/bpf/core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,13 +1112,14 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
11121112
* 1) when the program is freed after;
11131113
* 2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
11141114
* For case 2), we need to free both the RO memory and the RW buffer.
1115-
* Also, ro_header->size in 2) is not properly set yet, so rw_header->size
1116-
* is used for uncharge.
1115+
*
1116+
* If bpf_jit_binary_pack_free is called before _finalize (jit error),
1117+
* it is necessary to set ro_header->size properly before freeing it.
11171118
*/
11181119
void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
11191120
struct bpf_binary_header *rw_header)
11201121
{
1121-
u32 size = rw_header ? rw_header->size : ro_header->size;
1122+
u32 size = ro_header->size;
11221123

11231124
bpf_prog_pack_free(ro_header);
11241125
kvfree(rw_header);

0 commit comments

Comments
 (0)