Skip to content

Commit 7eef5d4

Browse files
jsitnickiKernel Patches Daemon
authored andcommitted
bpf, verifier: Propagate packet access flags to gen_prologue
Change gen_prologue() to accept the packet access flags bitmap. This allows gen_prologue() to inspect multiple access patterns when needed. No functional change. Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent 3876ce1 commit 7eef5d4

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ struct bpf_verifier_ops {
10761076
bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
10771077
const struct bpf_prog *prog,
10781078
struct bpf_insn_access_aux *info);
1079-
int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
1079+
int (*gen_prologue)(struct bpf_insn *insn, u32 pkt_access_flags,
10801080
const struct bpf_prog *prog);
10811081
int (*gen_epilogue)(struct bpf_insn *insn, const struct bpf_prog *prog,
10821082
s16 ctx_stack_off);

kernel/bpf/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
26942694
}
26952695

26962696
static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
2697-
bool direct_write,
2697+
u32 pkt_access_flags,
26982698
const struct bpf_prog *prog)
26992699
{
27002700
/* Nothing to do for sockopt argument. The data is kzalloc'ated.

kernel/bpf/verifier.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21200,7 +21200,6 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
2120021200
struct bpf_prog *new_prog;
2120121201
enum bpf_access_type type;
2120221202
bool is_narrower_load;
21203-
bool seen_direct_write;
2120421203
int epilogue_idx = 0;
2120521204

2120621205
if (ops->gen_epilogue) {
@@ -21228,13 +21227,12 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
2122821227
}
2122921228
}
2123021229

21231-
seen_direct_write = env->seen_packet_access & PA_F_DIRECT_WRITE;
21232-
if (ops->gen_prologue || seen_direct_write) {
21230+
if (ops->gen_prologue || (env->seen_packet_access & PA_F_DIRECT_WRITE)) {
2123321231
if (!ops->gen_prologue) {
2123421232
verifier_bug(env, "gen_prologue is null");
2123521233
return -EFAULT;
2123621234
}
21237-
cnt = ops->gen_prologue(insn_buf, seen_direct_write, env->prog);
21235+
cnt = ops->gen_prologue(insn_buf, env->seen_packet_access, env->prog);
2123821236
if (cnt >= INSN_BUF_SIZE) {
2123921237
verifier_bug(env, "prologue is too long");
2124021238
return -EFAULT;

net/core/filter.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9042,7 +9042,7 @@ static bool sock_filter_is_valid_access(int off, int size,
90429042
prog->expected_attach_type);
90439043
}
90449044

9045-
static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
9045+
static int bpf_noop_prologue(struct bpf_insn *insn_buf, u32 pkt_access_flags,
90469046
const struct bpf_prog *prog)
90479047
{
90489048
/* Neither direct read nor direct write requires any preliminary
@@ -9051,12 +9051,12 @@ static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
90519051
return 0;
90529052
}
90539053

9054-
static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
9054+
static int bpf_unclone_prologue(struct bpf_insn *insn_buf, u32 pkt_access_flags,
90559055
const struct bpf_prog *prog, int drop_verdict)
90569056
{
90579057
struct bpf_insn *insn = insn_buf;
90589058

9059-
if (!direct_write)
9059+
if (!(pkt_access_flags & PA_F_DIRECT_WRITE))
90609060
return 0;
90619061

90629062
/* if (!skb->cloned)
@@ -9125,10 +9125,11 @@ static int bpf_gen_ld_abs(const struct bpf_insn *orig,
91259125
return insn - insn_buf;
91269126
}
91279127

9128-
static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
9128+
static int tc_cls_act_prologue(struct bpf_insn *insn_buf, u32 pkt_access_flags,
91299129
const struct bpf_prog *prog)
91309130
{
9131-
return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
9131+
return bpf_unclone_prologue(insn_buf, pkt_access_flags, prog,
9132+
TC_ACT_SHOT);
91329133
}
91339134

91349135
static bool tc_cls_act_is_valid_access(int off, int size,
@@ -9466,10 +9467,10 @@ static bool sock_ops_is_valid_access(int off, int size,
94669467
return true;
94679468
}
94689469

9469-
static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
9470+
static int sk_skb_prologue(struct bpf_insn *insn_buf, u32 pkt_access_flags,
94709471
const struct bpf_prog *prog)
94719472
{
9472-
return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
9473+
return bpf_unclone_prologue(insn_buf, pkt_access_flags, prog, SK_DROP);
94739474
}
94749475

94759476
static bool sk_skb_is_valid_access(int off, int size,

net/sched/bpf_qdisc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ static int bpf_qdisc_btf_struct_access(struct bpf_verifier_log *log,
132132

133133
BTF_ID_LIST_SINGLE(bpf_qdisc_init_prologue_ids, func, bpf_qdisc_init_prologue)
134134

135-
static int bpf_qdisc_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
135+
static int bpf_qdisc_gen_prologue(struct bpf_insn *insn_buf,
136+
u32 direct_access_flags,
136137
const struct bpf_prog *prog)
137138
{
138139
struct bpf_insn *insn = insn_buf;

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ static int bpf_test_mod_st_ops__test_pro_epilogue(struct st_ops_args *args)
13691369
static int bpf_cgroup_from_id_id;
13701370
static int bpf_cgroup_release_id;
13711371

1372-
static int st_ops_gen_prologue_with_kfunc(struct bpf_insn *insn_buf, bool direct_write,
1372+
static int st_ops_gen_prologue_with_kfunc(struct bpf_insn *insn_buf,
13731373
const struct bpf_prog *prog)
13741374
{
13751375
struct bpf_insn *insn = insn_buf;
@@ -1445,7 +1445,7 @@ static int st_ops_gen_epilogue_with_kfunc(struct bpf_insn *insn_buf, const struc
14451445
}
14461446

14471447
#define KFUNC_PRO_EPI_PREFIX "test_kfunc_"
1448-
static int st_ops_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
1448+
static int st_ops_gen_prologue(struct bpf_insn *insn_buf, u32 pkt_access_flags,
14491449
const struct bpf_prog *prog)
14501450
{
14511451
struct bpf_insn *insn = insn_buf;
@@ -1455,7 +1455,7 @@ static int st_ops_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
14551455
return 0;
14561456

14571457
if (!strncmp(prog->aux->name, KFUNC_PRO_EPI_PREFIX, strlen(KFUNC_PRO_EPI_PREFIX)))
1458-
return st_ops_gen_prologue_with_kfunc(insn_buf, direct_write, prog);
1458+
return st_ops_gen_prologue_with_kfunc(insn_buf, prog);
14591459

14601460
/* r6 = r1[0]; // r6 will be "struct st_ops *args". r1 is "u64 *ctx".
14611461
* r7 = r6->a;

0 commit comments

Comments
 (0)