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 <houtao1@huawei.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20211025064025.2567443-3-houtao1@huawei.com
This commit is contained in:
parent
31a645aea4
commit
35346ab641
@ -1650,6 +1650,29 @@ bool bpf_prog_test_check_kfunc_call(u32 kfunc_id, struct module *owner);
|
|||||||
bool btf_ctx_access(int off, int size, enum bpf_access_type type,
|
bool btf_ctx_access(int off, int size, enum bpf_access_type type,
|
||||||
const struct bpf_prog *prog,
|
const struct bpf_prog *prog,
|
||||||
struct bpf_insn_access_aux *info);
|
struct bpf_insn_access_aux *info);
|
||||||
|
|
||||||
|
static inline bool bpf_tracing_ctx_access(int off, int size,
|
||||||
|
enum bpf_access_type type)
|
||||||
|
{
|
||||||
|
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
|
||||||
|
return false;
|
||||||
|
if (type != BPF_READ)
|
||||||
|
return false;
|
||||||
|
if (off % size != 0)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool bpf_tracing_btf_ctx_access(int off, int size,
|
||||||
|
enum bpf_access_type type,
|
||||||
|
const struct bpf_prog *prog,
|
||||||
|
struct bpf_insn_access_aux *info)
|
||||||
|
{
|
||||||
|
if (!bpf_tracing_ctx_access(off, size, type))
|
||||||
|
return false;
|
||||||
|
return btf_ctx_access(off, size, type, prog, info);
|
||||||
|
}
|
||||||
|
|
||||||
int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
|
int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
|
||||||
const struct btf_type *t, int off, int size,
|
const struct btf_type *t, int off, int size,
|
||||||
enum bpf_access_type atype,
|
enum bpf_access_type atype,
|
||||||
|
@ -1646,13 +1646,7 @@ static bool raw_tp_prog_is_valid_access(int off, int size,
|
|||||||
const struct bpf_prog *prog,
|
const struct bpf_prog *prog,
|
||||||
struct bpf_insn_access_aux *info)
|
struct bpf_insn_access_aux *info)
|
||||||
{
|
{
|
||||||
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
|
return bpf_tracing_ctx_access(off, size, type);
|
||||||
return false;
|
|
||||||
if (type != BPF_READ)
|
|
||||||
return false;
|
|
||||||
if (off % size != 0)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool tracing_prog_is_valid_access(int off, int size,
|
static bool tracing_prog_is_valid_access(int off, int size,
|
||||||
@ -1660,13 +1654,7 @@ static bool tracing_prog_is_valid_access(int off, int size,
|
|||||||
const struct bpf_prog *prog,
|
const struct bpf_prog *prog,
|
||||||
struct bpf_insn_access_aux *info)
|
struct bpf_insn_access_aux *info)
|
||||||
{
|
{
|
||||||
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
|
return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
|
||||||
return false;
|
|
||||||
if (type != BPF_READ)
|
|
||||||
return false;
|
|
||||||
if (off % size != 0)
|
|
||||||
return false;
|
|
||||||
return btf_ctx_access(off, size, type, prog, info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
|
int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
|
||||||
|
@ -81,14 +81,7 @@ static bool bpf_tcp_ca_is_valid_access(int off, int size,
|
|||||||
const struct bpf_prog *prog,
|
const struct bpf_prog *prog,
|
||||||
struct bpf_insn_access_aux *info)
|
struct bpf_insn_access_aux *info)
|
||||||
{
|
{
|
||||||
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
|
if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
|
||||||
return false;
|
|
||||||
if (type != BPF_READ)
|
|
||||||
return false;
|
|
||||||
if (off % size != 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!btf_ctx_access(off, size, type, prog, info))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
|
if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
|
||||||
|
Loading…
Reference in New Issue
Block a user