mirror of https://gitee.com/openkylin/linux.git
Merge branch 'improve-branch_taken'
John Fastabend says: ==================== This series adds logic to the verifier to handle the case where a pointer is known to be non-null but then the verifier encountesr a instruction, such as 'if ptr == 0 goto X' or 'if ptr != 0 goto X', where the pointer is compared against 0. Because the verifier tracks if a pointer may be null in many cases we can improve the branch tracking by following the case known to be true. The first patch adds the verifier logic and patches 2-4 add the test cases. v1->v2: fix verifier logic to return -1 indicating both paths must still be walked if value is not zero. Move mark_precision skip for this case into caller of mark_precision to ensure mark_precision can still catch other misuses. And add PTR_TYPE_BTF_ID to our list of supported types. Finally add one more test to catch the value not equal zero case. Thanks to Andrii for original review. Also fixed up commit messages hopefully its better now. ==================== Acked-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
commit
29ae90d221
|
@ -393,6 +393,15 @@ static bool type_is_sk_pointer(enum bpf_reg_type type)
|
|||
type == PTR_TO_XDP_SOCK;
|
||||
}
|
||||
|
||||
static bool reg_type_not_null(enum bpf_reg_type type)
|
||||
{
|
||||
return type == PTR_TO_SOCKET ||
|
||||
type == PTR_TO_TCP_SOCK ||
|
||||
type == PTR_TO_MAP_VALUE ||
|
||||
type == PTR_TO_SOCK_COMMON ||
|
||||
type == PTR_TO_BTF_ID;
|
||||
}
|
||||
|
||||
static bool reg_type_may_be_null(enum bpf_reg_type type)
|
||||
{
|
||||
return type == PTR_TO_MAP_VALUE_OR_NULL ||
|
||||
|
@ -6308,8 +6317,25 @@ static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode)
|
|||
static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
|
||||
bool is_jmp32)
|
||||
{
|
||||
if (__is_pointer_value(false, reg))
|
||||
return -1;
|
||||
if (__is_pointer_value(false, reg)) {
|
||||
if (!reg_type_not_null(reg->type))
|
||||
return -1;
|
||||
|
||||
/* If pointer is valid tests against zero will fail so we can
|
||||
* use this to direct branch taken.
|
||||
*/
|
||||
if (val != 0)
|
||||
return -1;
|
||||
|
||||
switch (opcode) {
|
||||
case BPF_JEQ:
|
||||
return 0;
|
||||
case BPF_JNE:
|
||||
return 1;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_jmp32)
|
||||
return is_branch32_taken(reg, val, opcode);
|
||||
|
@ -6808,7 +6834,11 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
|
|||
}
|
||||
|
||||
if (pred >= 0) {
|
||||
err = mark_chain_precision(env, insn->dst_reg);
|
||||
/* If we get here with a dst_reg pointer type it is because
|
||||
* above is_branch_taken() special cased the 0 comparison.
|
||||
*/
|
||||
if (!__is_pointer_value(false, dst_reg))
|
||||
err = mark_chain_precision(env, insn->dst_reg);
|
||||
if (BPF_SRC(insn->code) == BPF_X && !err)
|
||||
err = mark_chain_precision(env, insn->src_reg);
|
||||
if (err)
|
||||
|
|
|
@ -73,6 +73,7 @@ int bpf_sk_lookup_test0(struct __sk_buff *skb)
|
|||
|
||||
tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
|
||||
sk = bpf_sk_lookup_tcp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
|
||||
bpf_printk("sk=%d\n", sk ? 1 : 0);
|
||||
if (sk)
|
||||
bpf_sk_release(sk);
|
||||
return sk ? TC_ACT_OK : TC_ACT_UNSPEC;
|
||||
|
|
|
@ -821,3 +821,36 @@
|
|||
.result = REJECT,
|
||||
.errstr = "invalid mem access",
|
||||
},
|
||||
{
|
||||
"reference tracking: branch tracking valid pointer null comparison",
|
||||
.insns = {
|
||||
BPF_SK_LOOKUP(sk_lookup_tcp),
|
||||
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
|
||||
BPF_MOV64_IMM(BPF_REG_3, 1),
|
||||
BPF_JMP_IMM(BPF_JNE, BPF_REG_6, 0, 1),
|
||||
BPF_MOV64_IMM(BPF_REG_3, 0),
|
||||
BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 2),
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
|
||||
BPF_EMIT_CALL(BPF_FUNC_sk_release),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = ACCEPT,
|
||||
},
|
||||
{
|
||||
"reference tracking: branch tracking valid pointer value comparison",
|
||||
.insns = {
|
||||
BPF_SK_LOOKUP(sk_lookup_tcp),
|
||||
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
|
||||
BPF_MOV64_IMM(BPF_REG_3, 1),
|
||||
BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 4),
|
||||
BPF_MOV64_IMM(BPF_REG_3, 0),
|
||||
BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 1234, 2),
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
|
||||
BPF_EMIT_CALL(BPF_FUNC_sk_release),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.errstr = "Unreleased reference",
|
||||
.result = REJECT,
|
||||
},
|
||||
|
|
|
@ -150,3 +150,22 @@
|
|||
.result_unpriv = REJECT,
|
||||
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
|
||||
},
|
||||
{
|
||||
"map lookup and null branch prediction",
|
||||
.insns = {
|
||||
BPF_MOV64_IMM(BPF_REG_1, 10),
|
||||
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
|
||||
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
|
||||
BPF_LD_MAP_FD(BPF_REG_1, 0),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
|
||||
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
|
||||
BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 2),
|
||||
BPF_JMP_IMM(BPF_JNE, BPF_REG_6, 0, 1),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_10, 10),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.fixup_map_hash_8b = { 4 },
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = ACCEPT,
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue