bpf: fix range arithmetic for bpf map access
Subsystems (4)
PATCH v2 1/1 bpf: fix range arithmetic for bpf map access
I made some invalid assumptions with BPF_AND and BPF_MOD that could result in invalid accesses to bpf map entries. Fix this up by doing a few things
1) Kill BPF_MOD support. This doesn't actually get used by the compiler in real life and just adds extra complexity.
2) Fix the logic for BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's.
3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't.
This fixes the testcase provided by Jann as well as a few other theoretical problems.
---V1->V2:
- set the MIN_RANGE to -1 to essentially disable all negative values for the min value.
- rebased onto net instead of net-next.
include/linux/bpf_verifier.h | 5 ++-- kernel/bpf/verifier.c | 70 +++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 25 deletions(-)
include/linux/bpf_verifier.h +3 -2
@@ -14,7 +14,7 @@* are obviously wrong for any sort of memory access. */ #define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024) -#define BPF_REGISTER_MIN_RANGE -(1024 * 1024 * 1024) +#define BPF_REGISTER_MIN_RANGE -1struct bpf_reg_state {enum bpf_reg_type type;
@@ -22,7 +22,8 @@ struct bpf_reg_state {* Used to determine if any memory access using this register will * result in a bad access. */ -u64 min_value, max_value; +s64 min_value; +u64 max_value;union {/* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */ s64 imm;
kernel/bpf/verifier.c +47 -23
122 lines not on this page · raw message
Signature · 1 line
2.5.5
Quoted 13 lines · Josef Bacik
I made some invalid assumptions with
BPF_ANDandBPF_MODthat could result in invalid accesses to bpf map entries. Fix this up by doing a few things1) Kill
BPF_MODsupport. This doesn't actually get used by the compiler in real life and just adds extra complexity.2) Fix the logic for
BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's.3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't.
Quoted 5 lines
This fixes the testcase provided by Jann as well as a few other theoretical problems.
Reported-by Jann Horn <[email protected]>Signed-off-by Josef Bacik <[email protected]>
lgtm.
Jann, could you please double check the logic.
Thanks!
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Josef Bacik
I made some invalid assumptions with
BPF_ANDandBPF_MODthat could result in invalid accesses to bpf map entries. Fix this up by doing a few things1) Kill
BPF_MODsupport. This doesn't actually get used by the compiler in real life and just adds extra complexity.2) Fix the logic for
BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's.3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't.
This fixes the testcase provided by Jann as well as a few other theoretical problems.
Quoted 8 lines
Quoted 2 lines
Reported-by Jann Horn <[email protected]>Signed-off-by Josef Bacik <[email protected]>lgtm.
Jann, could you please double check the logic.
Thanks!
I found some more potential issues, maybe Josef and you can tell me whether I understood these correctly.
/* If the source register is a random pointer then the
* min_value/max_value values represent the range of the known
* accesses into that value, not the actual min/max value of the
* register itself. In this case we have to reset the reg range
* values so we know it is not safe to look at.
*/
if (regs[insn->src_reg].type != CONST_IMM &&
regs[insn->src_reg].type != UNKNOWN_VALUE) {min_val = BPF_REGISTER_MIN_RANGE;max_val = BPF_REGISTER_MAX_RANGE;
}
Why only the source register? Why not the destination register?
/* We don't know anything about what was done to this register, mark it
* as unknown.
*/
if (min_val == BPF_REGISTER_MIN_RANGE &&
max_val == BPF_REGISTER_MAX_RANGE) {reset_reg_range_values(regs, insn->dst_reg);
return;
}
Why have this special case at all? Since min_val and max_val are basically independent, this code shouldn't be necessary, right?
static void check_reg_overflow(struct bpf_reg_state *reg) {
if (reg->max_value > BPF_REGISTER_MAX_RANGE) reg->max_value = BPF_REGISTER_MAX_RANGE;
if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
reg->min_value > BPF_REGISTER_MAX_RANGE)reg->min_value = BPF_REGISTER_MIN_RANGE;
}
Why is this asymmetric? Why is reg->max_value < BPF_REGISTER_MIN_RANGE not important, but reg->min_value > BPF_REGISTER_MAX_RANGE is?
In states_equal():
if (rold->type == NOT_INIT ||
(rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT)) <------------
continue;
I think this is broken in code like the following:
int value;
if (condition) {
value = 1; // visited first by verifier
} else {
value = 1000000; // visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
value would be an UNKNOWN_VALUE for both paths, right? So states_equal() would decide that the states converge after the conditionally executed code?
Quoted 44 lines · Jann Horn
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Josef Bacik
I made some invalid assumptions with
BPF_ANDandBPF_MODthat could result in invalid accesses to bpf map entries. Fix this up by doing a few things1) Kill
BPF_MODsupport. This doesn't actually get used by the compiler in real life and just adds extra complexity.2) Fix the logic for
BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's.3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't.
This fixes the testcase provided by Jann as well as a few other theoretical problems.
Quoted 8 lines
Quoted 2 lines
Reported-by Jann Horn <[email protected]>Signed-off-by Josef Bacik <[email protected]>lgtm.
Jann, could you please double check the logic.
Thanks!I found some more potential issues, maybe Josef and you can tell me whether I understood these correctly.
/* If the source register is a random pointer then the
* min_value/max_value values represent the range of the known
* accesses into that value, not the actual min/max value of the
* register itself. In this case we have to reset the reg range
* values so we know it is not safe to look at.
*/
if (regs[insn->src_reg].type !=CONST_IMM&&
regs[insn->src_reg].type !=UNKNOWN_VALUE) {min_val=BPF_REGISTER_MIN_RANGE;max_val=BPF_REGISTER_MAX_RANGE;
}
Quoted 2 lines
Why only the source register? Why not the destination register?
The destination register is what we are doing arithmetic to, so we don't actually care about the type of register it is, as we'll interpret the values at a a later point. If the source register however is a MAP or some other pointer, then we know that the min/max values only apply to the range on the actual value of the register, rather than the possible range of values. Said in another way if src register is a MAP then the range is [SRC_REG.imm+SRC_REG.min_value, SRC_REG.imm+SRC_REG.max_value] instead of [SRC_REG.min_value, SRC_REG.max_value].
So this is the same behavior for the destination register for sure, but we don't actually care about it at this point. If the src_reg meets these criteria then we certainly don't know anything about the dest_reg and reset it blow with this
if (min_val == BPF_REGISTER_MIN_RANGE &&
max_val == BPF_REGISTER_MAX_RANGE) {
reset_reg_range_values(regs, insn->dst_reg);
return;
}Then in check_alu_op() if we weren't doing our operation on a MAP_PTR then we set the register to unknown and carry on.
Quoted 10 lines
/* We don't know anything about what was done to this register, mark it
* as unknown.
*/
if (min_val==BPF_REGISTER_MIN_RANGE&&
max_val==BPF_REGISTER_MAX_RANGE) {reset_reg_range_values(regs, insn->dst_reg);
return;
}
Quoted 3 lines
Why have this special case at all? Since
min_valandmax_valare basically independent, this code shouldn't be necessary, right?
They are the value of the source register, as I explained above if we know nothing about the source register then don't even bother doing the math, we also know nothing about the destination register. This is a shortcut to keep us from doing potentially dangerous things when we already know it's invalid.
Quoted 10 lines
static void
check_reg_overflow(struct bpf_reg_state *reg){
if (reg->max_value>BPF_REGISTER_MAX_RANGE)reg->max_value=BPF_REGISTER_MAX_RANGE;
if (reg->min_value<BPF_REGISTER_MIN_RANGE||
reg->min_value>BPF_REGISTER_MAX_RANGE)reg->min_value=BPF_REGISTER_MIN_RANGE;
}
Quoted 3 lines
Why is this asymmetric? Why is
reg->max_value < BPF_REGISTER_MIN_RANGEnot important, butreg->min_value > BPF_REGISTER_MAX_RANGEis?
Because max_value is unsigned, so if we do reg->max_value = BPF_REGISTER_MIN_RANGE; and then do this we'll still get max_value reset to MAX_RANGE.
Quoted 18 lines
In
states_equal():
if (rold->type==NOT_INIT||
(rold->type==UNKNOWN_VALUE&&rcur->type!=NOT_INIT)) <------------
continue;I think this is broken in code like the following:
int value;
if (condition) {
value = 1; // visited first by verifier
} else {
value = 1000000; // visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
Quoted 4 lines
valuewould be anUNKNOWN_VALUEfor both paths, right? Sostates_equal()would decide that the states converge after the conditionally executed code?
Value would be CONST_IMM for both paths, and wouldn't match so they wouldn't converge. I think I understood your question right, let me know if I'm addressing the wrong part of it.
Do my explanations make sense? I'm doing this first thing in the morning so I'm still a little foggy, let me know if things still aren't clear. Thanks,
Josef
From: Josef Bacik <[email protected]>
Date: Mon, 14 Nov 2016 15:45:36 -0500
Quoted 13 lines
I made some invalid assumptions with
BPF_ANDandBPF_MODthat could result in invalid accesses to bpf map entries. Fix this up by doing a few things1) Kill
BPF_MODsupport. This doesn't actually get used by the compiler in real life and just adds extra complexity.2) Fix the logic for
BPF_AND, don't allow AND of negative numbers and set the minimum value to 0 for positive AND's.3) Don't do operations on the ranges if they are set to the limits, as they are by definition undefined, and allowing arithmetic operations on those values could make them appear valid when they really aren't.
Quoted 5 lines
This fixes the testcase provided by Jann as well as a few other theoretical problems.
Reported-by Jann Horn <[email protected]>Signed-off-by Josef Bacik <[email protected]>
Applied, thanks.
Quoted 18 lines · Josef Bacik
Quoted 17 lines · Jann Horn
In
states_equal():
if (rold->type==NOT_INIT||
(rold->type==UNKNOWN_VALUE&&rcur->type!=NOT_INIT))
<------------
continue;I think this is broken in code like the following:
int value;
if (condition) {
value = 1; // visited first by verifier
} else {
value = 1000000; // visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
Quoted 8 lines
Quoted 4 lines
valuewould be anUNKNOWN_VALUEfor both paths, right? Sostates_equal()would decide that the states converge after the conditionally executed code?Value would be
CONST_IMMfor both paths, and wouldn't match so they wouldn't converge. I think I understood your question right, let me know if I'm addressing the wrong part of it.
Okay, true, but what if you load the values from a map and bounds-check them instead of hardcoding them? Then they will be of type UNKNOWN_VALUE, right? Like this:
int value = map[0];
if (condition) {
value &= 0x1; // visited first by verifier
} else {
// nothing; visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
And then rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT will be true in the dummy = 1 line, and the states converge. Am I missing something?
Quoted 41 lines · Jann Horn
Quoted 18 lines · Josef Bacik
Quoted 17 lines · Jann Horn
In
states_equal():
if (rold->type==NOT_INIT||
(rold->type==UNKNOWN_VALUE&&rcur->type!=NOT_INIT))
<------------
continue;I think this is broken in code like the following:
int value;
if (condition) {
value = 1; // visited first by verifier
} else {
value = 1000000; // visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;Quoted 8 lines
Quoted 4 lines
valuewould be anUNKNOWN_VALUEfor both paths, right? Sostates_equal()would decide that the states converge after the conditionally executed code?Value would be
CONST_IMMfor both paths, and wouldn't match so they wouldn't converge. I think I understood your question right, let me know if I'm addressing the wrong part of it.Okay, true, but what if you load the values from a map and bounds-check them instead of hardcoding them? Then they will be of type
UNKNOWN_VALUE, right? Like this:int value = map[0];
if (condition) {
value &=0x1; // visited first by verifier
} else {
// nothing; visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
Quoted 3 lines
And then
rold->type == UNKNOWN_VALUE && rcur->type != NOT_INITwill be true in thedummy = 1line, and the states converge. Am I missing something?
Ah ok yeah I see it now you are right. This is slightly different from this particular problem so I'll send a second patch to address this, sound reasonable? Thanks,
Josef
Quoted 49 lines · Josef Bacik
Quoted 48 lines · Jann Horn
Quoted 25 lines · Josef Bacik
Quoted 18 lines · Jann Horn
In
states_equal():
if (rold->type==NOT_INIT||
(rold->type==UNKNOWN_VALUE&&rcur->type!=NOT_INIT))
<------------
continue;I think this is broken in code like the following:
int value;
if (condition) {
value = 1; // visited first by verifier
} else {
value = 1000000; // visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;Quoted 4 lines
valuewould be anUNKNOWN_VALUEfor both paths, right? Sostates_equal()would decide that the states converge after the conditionally executed code?Quoted 4 lines
Value would be
CONST_IMMfor both paths, and wouldn't match so they wouldn't
converge. I think I understood your question right, let me know if I'm addressing the wrong part of it.Okay, true, but what if you load the values from a map and bounds-check them
instead of hardcoding them? Then they will be of typeUNKNOWN_VALUE, right?
Like this:int value = map[0];
if (condition) {
value &=0x1; // visited first by verifier
} else {
// nothing; visited second by verifier
}
int dummy = 1; // states seem to converge here, but actually don't map[value] = 1234;
Quoted 8 lines
Quoted 4 lines
And then
rold->type == UNKNOWN_VALUE && rcur->type != NOT_INITwill be true in thedummy = 1line, and the states converge. Am I missing something?Ah ok yeah I see it now you are right. This is slightly different from this particular problem so I'll send a second patch to address this, sound reasonable? Thanks,
Sure, makes sense.