Skip to content

Commit 1980920

Browse files
Jakub KicinskiSomasundaram Krishnasamy
authored andcommitted
bpf: verifier: set reg_type on context accesses in second pass
Use a simplified is_valid_access() callback when verifier is used for program analysis by non-host JITs. This allows us to teach the verifier about packet start and packet end offsets for direct packet access. We can extend the callback as needed but for most packet processing needs there isn't much more the offloads may require. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Simon Horman <[email protected]> Signed-off-by: David S. Miller <[email protected]> (cherry picked from commit 1bdec44) Orabug: 31667601 Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Mark Haywood <[email protected]> Signed-off-by: Somasundaram Krishnasamy <[email protected]>
1 parent 6a82dc5 commit 1980920

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

kernel/bpf/verifier.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,36 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
11021102
return err;
11031103
}
11041104

1105+
static bool analyzer_is_valid_access(struct bpf_verifier_env *env, int off,
1106+
struct bpf_insn_access_aux *info)
1107+
{
1108+
switch (env->prog->type) {
1109+
case BPF_PROG_TYPE_XDP:
1110+
switch (off) {
1111+
case offsetof(struct xdp_buff, data):
1112+
info->reg_type = PTR_TO_PACKET;
1113+
return true;
1114+
case offsetof(struct xdp_buff, data_end):
1115+
info->reg_type = PTR_TO_PACKET_END;
1116+
return true;
1117+
}
1118+
return false;
1119+
case BPF_PROG_TYPE_SCHED_CLS:
1120+
switch (off) {
1121+
case offsetof(struct sk_buff, data):
1122+
info->reg_type = PTR_TO_PACKET;
1123+
return true;
1124+
case offsetof(struct sk_buff, cb) +
1125+
offsetof(struct bpf_skb_data_end, data_end):
1126+
info->reg_type = PTR_TO_PACKET_END;
1127+
return true;
1128+
}
1129+
return false;
1130+
default:
1131+
return false;
1132+
}
1133+
}
1134+
11051135
/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
11061136
static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
11071137
enum bpf_access_type t, enum bpf_reg_type *reg_type)
@@ -1110,12 +1140,13 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
11101140
.reg_type = *reg_type,
11111141
};
11121142

1113-
/* for analyzer ctx accesses are already validated and converted */
1114-
if (env->analyzer_ops)
1115-
return 0;
1116-
1117-
if (env->prog->aux->ops->is_valid_access &&
1118-
env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
1143+
if (env->analyzer_ops) {
1144+
if (analyzer_is_valid_access(env, off, &info)) {
1145+
*reg_type = info.reg_type;
1146+
return 0;
1147+
}
1148+
} else if (env->prog->aux->ops->is_valid_access &&
1149+
env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
11191150
/* A non zero info.ctx_field_size indicates that this field is a
11201151
* candidate for later verifier transformation to load the whole
11211152
* field and then apply a mask when accessed with a narrower

0 commit comments

Comments
 (0)