Skip to content

Commit 1bdec44

Browse files
Jakub Kicinskidavem330
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]>
1 parent 40d0af5 commit 1bdec44

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
@@ -813,6 +813,36 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
813813
return err;
814814
}
815815

816+
static bool analyzer_is_valid_access(struct bpf_verifier_env *env, int off,
817+
struct bpf_insn_access_aux *info)
818+
{
819+
switch (env->prog->type) {
820+
case BPF_PROG_TYPE_XDP:
821+
switch (off) {
822+
case offsetof(struct xdp_buff, data):
823+
info->reg_type = PTR_TO_PACKET;
824+
return true;
825+
case offsetof(struct xdp_buff, data_end):
826+
info->reg_type = PTR_TO_PACKET_END;
827+
return true;
828+
}
829+
return false;
830+
case BPF_PROG_TYPE_SCHED_CLS:
831+
switch (off) {
832+
case offsetof(struct sk_buff, data):
833+
info->reg_type = PTR_TO_PACKET;
834+
return true;
835+
case offsetof(struct sk_buff, cb) +
836+
offsetof(struct bpf_skb_data_end, data_end):
837+
info->reg_type = PTR_TO_PACKET_END;
838+
return true;
839+
}
840+
return false;
841+
default:
842+
return false;
843+
}
844+
}
845+
816846
/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
817847
static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
818848
enum bpf_access_type t, enum bpf_reg_type *reg_type)
@@ -821,12 +851,13 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
821851
.reg_type = *reg_type,
822852
};
823853

824-
/* for analyzer ctx accesses are already validated and converted */
825-
if (env->analyzer_ops)
826-
return 0;
827-
828-
if (env->prog->aux->ops->is_valid_access &&
829-
env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
854+
if (env->analyzer_ops) {
855+
if (analyzer_is_valid_access(env, off, &info)) {
856+
*reg_type = info.reg_type;
857+
return 0;
858+
}
859+
} else if (env->prog->aux->ops->is_valid_access &&
860+
env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
830861
/* A non zero info.ctx_field_size indicates that this field is a
831862
* candidate for later verifier transformation to load the whole
832863
* field and then apply a mask when accessed with a narrower

0 commit comments

Comments
 (0)