Skip to content

Commit 35346ab

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Factor out helpers for ctx access checking
Factor out two helpers to check the read access of ctx for raw tp and BTF function. bpf_tracing_ctx_access() is used to check the read access to argument is valid, and bpf_tracing_btf_ctx_access() checks whether the btf type of argument is valid besides the checking of argument read. bpf_tracing_btf_ctx_access() will be used by the following patch. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 31a645a commit 35346ab

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

include/linux/bpf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,29 @@ bool bpf_prog_test_check_kfunc_call(u32 kfunc_id, struct module *owner);
16501650
bool btf_ctx_access(int off, int size, enum bpf_access_type type,
16511651
const struct bpf_prog *prog,
16521652
struct bpf_insn_access_aux *info);
1653+
1654+
static inline bool bpf_tracing_ctx_access(int off, int size,
1655+
enum bpf_access_type type)
1656+
{
1657+
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1658+
return false;
1659+
if (type != BPF_READ)
1660+
return false;
1661+
if (off % size != 0)
1662+
return false;
1663+
return true;
1664+
}
1665+
1666+
static inline bool bpf_tracing_btf_ctx_access(int off, int size,
1667+
enum bpf_access_type type,
1668+
const struct bpf_prog *prog,
1669+
struct bpf_insn_access_aux *info)
1670+
{
1671+
if (!bpf_tracing_ctx_access(off, size, type))
1672+
return false;
1673+
return btf_ctx_access(off, size, type, prog, info);
1674+
}
1675+
16531676
int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
16541677
const struct btf_type *t, int off, int size,
16551678
enum bpf_access_type atype,

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,27 +1646,15 @@ static bool raw_tp_prog_is_valid_access(int off, int size,
16461646
const struct bpf_prog *prog,
16471647
struct bpf_insn_access_aux *info)
16481648
{
1649-
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1650-
return false;
1651-
if (type != BPF_READ)
1652-
return false;
1653-
if (off % size != 0)
1654-
return false;
1655-
return true;
1649+
return bpf_tracing_ctx_access(off, size, type);
16561650
}
16571651

16581652
static bool tracing_prog_is_valid_access(int off, int size,
16591653
enum bpf_access_type type,
16601654
const struct bpf_prog *prog,
16611655
struct bpf_insn_access_aux *info)
16621656
{
1663-
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1664-
return false;
1665-
if (type != BPF_READ)
1666-
return false;
1667-
if (off % size != 0)
1668-
return false;
1669-
return btf_ctx_access(off, size, type, prog, info);
1657+
return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
16701658
}
16711659

16721660
int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,

net/ipv4/bpf_tcp_ca.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ static bool bpf_tcp_ca_is_valid_access(int off, int size,
8181
const struct bpf_prog *prog,
8282
struct bpf_insn_access_aux *info)
8383
{
84-
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
85-
return false;
86-
if (type != BPF_READ)
87-
return false;
88-
if (off % size != 0)
89-
return false;
90-
91-
if (!btf_ctx_access(off, size, type, prog, info))
84+
if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
9285
return false;
9386

9487
if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)

0 commit comments

Comments
 (0)