Skip to content

Commit afbf21d

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Support readonly/readwrite buffers in verifier
Readonly and readwrite buffer register states are introduced. Totally four states, PTR_TO_RDONLY_BUF[_OR_NULL] and PTR_TO_RDWR_BUF[_OR_NULL] are supported. As suggested by their respective names, PTR_TO_RDONLY_BUF[_OR_NULL] are for readonly buffers and PTR_TO_RDWR_BUF[_OR_NULL] for read/write buffers. These new register states will be used by later bpf map element iterator. New register states share some similarity to PTR_TO_TP_BUFFER as it will calculate accessed buffer size during verification time. The accessed buffer size will be later compared to other metrics during later attach/link_create time. Similar to reg_state PTR_TO_BTF_ID_OR_NULL in bpf iterator programs, PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL reg_types can be set at prog->aux->bpf_ctx_arg_aux, and bpf verifier will retrieve the values during btf_ctx_access(). Later bpf map element iterator implementation will show how such information will be assigned during target registeration time. The verifier is also enhanced such that PTR_TO_RDONLY_BUF can be passed to ARG_PTR_TO_MEM[_OR_NULL] helper argument, and PTR_TO_RDWR_BUF can be passed to ARG_PTR_TO_MEM[_OR_NULL] or ARG_PTR_TO_UNINIT_MEM. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200723184111.590274-1-yhs@fb.com
1 parent f9c7927 commit afbf21d

3 files changed

Lines changed: 104 additions & 6 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ enum bpf_reg_type {
353353
PTR_TO_BTF_ID_OR_NULL, /* reg points to kernel struct or NULL */
354354
PTR_TO_MEM, /* reg points to valid memory region */
355355
PTR_TO_MEM_OR_NULL, /* reg points to valid memory region or NULL */
356+
PTR_TO_RDONLY_BUF, /* reg points to a readonly buffer */
357+
PTR_TO_RDONLY_BUF_OR_NULL, /* reg points to a readonly buffer or NULL */
358+
PTR_TO_RDWR_BUF, /* reg points to a read/write buffer */
359+
PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
356360
};
357361

358362
/* The information passed from prog-specific *_is_valid_access
@@ -694,6 +698,8 @@ struct bpf_prog_aux {
694698
u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
695699
u32 attach_btf_id; /* in-kernel BTF type id to attach to */
696700
u32 ctx_arg_info_size;
701+
u32 max_rdonly_access;
702+
u32 max_rdwr_access;
697703
const struct bpf_ctx_arg_aux *ctx_arg_info;
698704
struct bpf_prog *linked_prog;
699705
bool verifier_zext; /* Zero extensions has been inserted by verifier. */

‎kernel/bpf/btf.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,19 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
38063806
btf_kind_str[BTF_INFO_KIND(t->info)]);
38073807
return false;
38083808
}
3809+
3810+
/* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
3811+
for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
3812+
const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
3813+
3814+
if (ctx_arg_info->offset == off &&
3815+
(ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL ||
3816+
ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) {
3817+
info->reg_type = ctx_arg_info->reg_type;
3818+
return true;
3819+
}
3820+
}
3821+
38093822
if (t->type == 0)
38103823
/* This is a pointer to void.
38113824
* It is the same as scalar from the verifier safety pov.

‎kernel/bpf/verifier.c‎

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ static bool reg_type_may_be_null(enum bpf_reg_type type)
409409
type == PTR_TO_SOCK_COMMON_OR_NULL ||
410410
type == PTR_TO_TCP_SOCK_OR_NULL ||
411411
type == PTR_TO_BTF_ID_OR_NULL ||
412-
type == PTR_TO_MEM_OR_NULL;
412+
type == PTR_TO_MEM_OR_NULL ||
413+
type == PTR_TO_RDONLY_BUF_OR_NULL ||
414+
type == PTR_TO_RDWR_BUF_OR_NULL;
413415
}
414416

415417
static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
@@ -503,6 +505,10 @@ static const char * const reg_type_str[] = {
503505
[PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_",
504506
[PTR_TO_MEM] = "mem",
505507
[PTR_TO_MEM_OR_NULL] = "mem_or_null",
508+
[PTR_TO_RDONLY_BUF] = "rdonly_buf",
509+
[PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null",
510+
[PTR_TO_RDWR_BUF] = "rdwr_buf",
511+
[PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null",
506512
};
507513

508514
static char slot_type_char[] = {
@@ -2173,6 +2179,10 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
21732179
case PTR_TO_XDP_SOCK:
21742180
case PTR_TO_BTF_ID:
21752181
case PTR_TO_BTF_ID_OR_NULL:
2182+
case PTR_TO_RDONLY_BUF:
2183+
case PTR_TO_RDONLY_BUF_OR_NULL:
2184+
case PTR_TO_RDWR_BUF:
2185+
case PTR_TO_RDWR_BUF_OR_NULL:
21762186
return true;
21772187
default:
21782188
return false;
@@ -3052,14 +3062,15 @@ int check_ctx_reg(struct bpf_verifier_env *env,
30523062
return 0;
30533063
}
30543064

3055-
static int check_tp_buffer_access(struct bpf_verifier_env *env,
3056-
const struct bpf_reg_state *reg,
3057-
int regno, int off, int size)
3065+
static int __check_buffer_access(struct bpf_verifier_env *env,
3066+
const char *buf_info,
3067+
const struct bpf_reg_state *reg,
3068+
int regno, int off, int size)
30583069
{
30593070
if (off < 0) {
30603071
verbose(env,
3061-
"R%d invalid tracepoint buffer access: off=%d, size=%d",
3062-
regno, off, size);
3072+
"R%d invalid %s buffer access: off=%d, size=%d",
3073+
regno, buf_info, off, size);
30633074
return -EACCES;
30643075
}
30653076
if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
@@ -3071,12 +3082,45 @@ static int check_tp_buffer_access(struct bpf_verifier_env *env,
30713082
regno, off, tn_buf);
30723083
return -EACCES;
30733084
}
3085+
3086+
return 0;
3087+
}
3088+
3089+
static int check_tp_buffer_access(struct bpf_verifier_env *env,
3090+
const struct bpf_reg_state *reg,
3091+
int regno, int off, int size)
3092+
{
3093+
int err;
3094+
3095+
err = __check_buffer_access(env, "tracepoint", reg, regno, off, size);
3096+
if (err)
3097+
return err;
3098+
30743099
if (off + size > env->prog->aux->max_tp_access)
30753100
env->prog->aux->max_tp_access = off + size;
30763101

30773102
return 0;
30783103
}
30793104

3105+
static int check_buffer_access(struct bpf_verifier_env *env,
3106+
const struct bpf_reg_state *reg,
3107+
int regno, int off, int size,
3108+
bool zero_size_allowed,
3109+
const char *buf_info,
3110+
u32 *max_access)
3111+
{
3112+
int err;
3113+
3114+
err = __check_buffer_access(env, buf_info, reg, regno, off, size);
3115+
if (err)
3116+
return err;
3117+
3118+
if (off + size > *max_access)
3119+
*max_access = off + size;
3120+
3121+
return 0;
3122+
}
3123+
30803124
/* BPF architecture zero extends alu32 ops into 64-bit registesr */
30813125
static void zext_32_to_64(struct bpf_reg_state *reg)
30823126
{
@@ -3427,6 +3471,23 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
34273471
} else if (reg->type == CONST_PTR_TO_MAP) {
34283472
err = check_ptr_to_map_access(env, regs, regno, off, size, t,
34293473
value_regno);
3474+
} else if (reg->type == PTR_TO_RDONLY_BUF) {
3475+
if (t == BPF_WRITE) {
3476+
verbose(env, "R%d cannot write into %s\n",
3477+
regno, reg_type_str[reg->type]);
3478+
return -EACCES;
3479+
}
3480+
err = check_buffer_access(env, reg, regno, off, size, "rdonly",
3481+
false,
3482+
&env->prog->aux->max_rdonly_access);
3483+
if (!err && value_regno >= 0)
3484+
mark_reg_unknown(env, regs, value_regno);
3485+
} else if (reg->type == PTR_TO_RDWR_BUF) {
3486+
err = check_buffer_access(env, reg, regno, off, size, "rdwr",
3487+
false,
3488+
&env->prog->aux->max_rdwr_access);
3489+
if (!err && t == BPF_READ && value_regno >= 0)
3490+
mark_reg_unknown(env, regs, value_regno);
34303491
} else {
34313492
verbose(env, "R%d invalid mem access '%s'\n", regno,
34323493
reg_type_str[reg->type]);
@@ -3668,6 +3729,18 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
36683729
return check_mem_region_access(env, regno, reg->off,
36693730
access_size, reg->mem_size,
36703731
zero_size_allowed);
3732+
case PTR_TO_RDONLY_BUF:
3733+
if (meta && meta->raw_mode)
3734+
return -EACCES;
3735+
return check_buffer_access(env, reg, regno, reg->off,
3736+
access_size, zero_size_allowed,
3737+
"rdonly",
3738+
&env->prog->aux->max_rdonly_access);
3739+
case PTR_TO_RDWR_BUF:
3740+
return check_buffer_access(env, reg, regno, reg->off,
3741+
access_size, zero_size_allowed,
3742+
"rdwr",
3743+
&env->prog->aux->max_rdwr_access);
36713744
default: /* scalar_value|ptr_to_stack or invalid ptr */
36723745
return check_stack_boundary(env, regno, access_size,
36733746
zero_size_allowed, meta);
@@ -3933,6 +4006,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
39334006
else if (!type_is_pkt_pointer(type) &&
39344007
type != PTR_TO_MAP_VALUE &&
39354008
type != PTR_TO_MEM &&
4009+
type != PTR_TO_RDONLY_BUF &&
4010+
type != PTR_TO_RDWR_BUF &&
39364011
type != expected_type)
39374012
goto err_type;
39384013
meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
@@ -6806,6 +6881,10 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
68066881
reg->type = PTR_TO_BTF_ID;
68076882
} else if (reg->type == PTR_TO_MEM_OR_NULL) {
68086883
reg->type = PTR_TO_MEM;
6884+
} else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) {
6885+
reg->type = PTR_TO_RDONLY_BUF;
6886+
} else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) {
6887+
reg->type = PTR_TO_RDWR_BUF;
68096888
}
68106889
if (is_null) {
68116890
/* We don't need id and ref_obj_id from this point

0 commit comments

Comments
 (0)