Skip to content

Commit e647815

Browse files
Jiong Wangborkmann
authored andcommitted
bpf: let verifier to calculate and record max_pkt_offset
In check_packet_access, update max_pkt_offset after the offset has passed __check_packet_access. It should be safe to use u32 for max_pkt_offset as explained in code comment. Also, when there is tail call, the max_pkt_offset of the called program is unknown, so conservatively set max_pkt_offset to MAX_PACKET_OFF for such case. Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent bce6a14 commit e647815

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ struct bpf_prog_aux {
293293
atomic_t refcnt;
294294
u32 used_map_cnt;
295295
u32 max_ctx_offset;
296+
u32 max_pkt_offset;
296297
u32 stack_depth;
297298
u32 id;
298299
u32 func_cnt;

kernel/bpf/verifier.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,17 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
14551455
verbose(env, "R%d offset is outside of the packet\n", regno);
14561456
return err;
14571457
}
1458+
1459+
/* __check_packet_access has made sure "off + size - 1" is within u16.
1460+
* reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1461+
* otherwise find_good_pkt_pointers would have refused to set range info
1462+
* that __check_packet_access would have rejected this pkt access.
1463+
* Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1464+
*/
1465+
env->prog->aux->max_pkt_offset =
1466+
max_t(u32, env->prog->aux->max_pkt_offset,
1467+
off + reg->umax_value + size - 1);
1468+
14581469
return err;
14591470
}
14601471

@@ -6138,6 +6149,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
61386149
*/
61396150
prog->cb_access = 1;
61406151
env->prog->aux->stack_depth = MAX_BPF_STACK;
6152+
env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
61416153

61426154
/* mark bpf_tail_call as different opcode to avoid
61436155
* conditional branch in the interpeter for every normal

0 commit comments

Comments
 (0)