Skip to content

Commit 00965c2

Browse files
aspskKernel Patches Daemon
authored andcommitted
bpf: force BPF_F_RDONLY_PROG on insn array creation
The original implementation added a hack to check_mem_access() to prevent programs from writing into insn arrays. To get rid of this hack, enforce BPF_F_RDONLY_PROG on map creation. Also fix the corresponding selftest, as the error message changes with this patch. Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Anton Protopopov <[email protected]>
1 parent 8c83cb5 commit 00965c2

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

kernel/bpf/bpf_insn_array.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static struct bpf_map *insn_array_alloc(union bpf_attr *attr)
5555

5656
bpf_map_init_from_attr(&insn_array->map, attr);
5757

58+
/* BPF programs aren't allowed to write to the map */
59+
insn_array->map.map_flags |= BPF_F_RDONLY_PROG;
60+
5861
return &insn_array->map;
5962
}
6063

kernel/bpf/verifier.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7565,11 +7565,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
75657565
verbose(env, "R%d leaks addr into map\n", value_regno);
75667566
return -EACCES;
75677567
}
7568-
if (t == BPF_WRITE && insn_array) {
7569-
verbose(env, "writes into insn_array not allowed\n");
7570-
return -EACCES;
7571-
}
7572-
75737568
err = check_map_access_type(env, regno, off, size, t);
75747569
if (err)
75757570
return err;
@@ -7584,10 +7579,14 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
75847579
} else if (t == BPF_READ && value_regno >= 0) {
75857580
struct bpf_map *map = reg->map_ptr;
75867581

7587-
/* if map is read-only, track its contents as scalars */
7582+
/*
7583+
* If map is read-only, track its contents as scalars,
7584+
* unless it is an insn array (see the special case below)
7585+
*/
75887586
if (tnum_is_const(reg->var_off) &&
75897587
bpf_map_is_rdonly(map) &&
7590-
map->ops->map_direct_value_addr) {
7588+
map->ops->map_direct_value_addr &&
7589+
map->map_type != BPF_MAP_TYPE_INSN_ARRAY) {
75917590
int map_off = off + reg->var_off.value;
75927591
u64 val = 0;
75937592

tools/testing/selftests/bpf/progs/verifier_gotox.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ jt0_%=: \
244244
}
245245

246246
SEC("socket")
247-
__failure __msg("writes into insn_array not allowed")
247+
__failure __msg("write into map forbidden, value_size=16 off=8 size=8")
248248
__naked void jump_table_no_writes(void)
249249
{
250250
asm volatile (" \

0 commit comments

Comments
 (0)