Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'a-pair-of-follow-ups-for-indirect-jumps'

Anton Protopopov says:

====================
A pair of follow ups for indirect jumps

Two fixes suggested by Alexei in [1]. Resending as a series,
as the second patch depends on the first.

[1] https://lore.kernel.org/bpf/CAADnVQK3piReoo1ja=9hgz7aJ60Y_Jjur_JMOaYV8-Mn_VyE4A@mail.gmail.com/#R
====================

Link: https://patch.msgid.link/20251128063224.1305482-1-a.s.protopopov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

+13 -11
+3
kernel/bpf/bpf_insn_array.c
··· 55 55 56 56 bpf_map_init_from_attr(&insn_array->map, attr); 57 57 58 + /* BPF programs aren't allowed to write to the map */ 59 + insn_array->map.map_flags |= BPF_F_RDONLY_PROG; 60 + 58 61 return &insn_array->map; 59 62 } 60 63
+9 -10
kernel/bpf/verifier.c
··· 6482 6482 break; 6483 6483 case PTR_TO_MAP_VALUE: 6484 6484 pointer_desc = "value "; 6485 + if (reg->map_ptr->map_type == BPF_MAP_TYPE_INSN_ARRAY) 6486 + strict = true; 6485 6487 break; 6486 6488 case PTR_TO_CTX: 6487 6489 pointer_desc = "context "; ··· 7531 7529 { 7532 7530 struct bpf_reg_state *regs = cur_regs(env); 7533 7531 struct bpf_reg_state *reg = regs + regno; 7534 - bool insn_array = reg->type == PTR_TO_MAP_VALUE && 7535 - reg->map_ptr->map_type == BPF_MAP_TYPE_INSN_ARRAY; 7536 7532 int size, err = 0; 7537 7533 7538 7534 size = bpf_size_to_bytes(bpf_size); ··· 7538 7538 return size; 7539 7539 7540 7540 /* alignment checks will add in reg->off themselves */ 7541 - err = check_ptr_alignment(env, reg, off, size, strict_alignment_once || insn_array); 7541 + err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); 7542 7542 if (err) 7543 7543 return err; 7544 7544 ··· 7565 7565 verbose(env, "R%d leaks addr into map\n", value_regno); 7566 7566 return -EACCES; 7567 7567 } 7568 - if (t == BPF_WRITE && insn_array) { 7569 - verbose(env, "writes into insn_array not allowed\n"); 7570 - return -EACCES; 7571 - } 7572 - 7573 7568 err = check_map_access_type(env, regno, off, size, t); 7574 7569 if (err) 7575 7570 return err; ··· 7579 7584 } else if (t == BPF_READ && value_regno >= 0) { 7580 7585 struct bpf_map *map = reg->map_ptr; 7581 7586 7582 - /* if map is read-only, track its contents as scalars */ 7587 + /* 7588 + * If map is read-only, track its contents as scalars, 7589 + * unless it is an insn array (see the special case below) 7590 + */ 7583 7591 if (tnum_is_const(reg->var_off) && 7584 7592 bpf_map_is_rdonly(map) && 7585 - map->ops->map_direct_value_addr) { 7593 + map->ops->map_direct_value_addr && 7594 + map->map_type != BPF_MAP_TYPE_INSN_ARRAY) { 7586 7595 int map_off = off + reg->var_off.value; 7587 7596 u64 val = 0; 7588 7597
+1 -1
tools/testing/selftests/bpf/progs/verifier_gotox.c
··· 244 244 } 245 245 246 246 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") 248 248 __naked void jump_table_no_writes(void) 249 249 { 250 250 asm volatile (" \