Skip to contentSkip to conversation

bpf: fix states equal logic for varlen access

PATCH Superseded Josef Bacik 3 participants 5 messages Nov 28, 2016 – Nov 29, 2016

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix states equal logic for varlen access

If we have a branch that looks something like this

int foo = map->value;
if (condition) {
foo += blah;
} else {
foo = bar;
}
map->array[foo] = baz;

We will incorrectly assume that the !condition branch is equal to the condition branch as the register for foo will be UNKNOWN_VALUE in both cases. We need to adjust this logic to only do this if we didn't do a varlen access after we processed the !condition branch, otherwise we have different ranges and need to check the other branch as well.

---
 kernel/bpf/verifier.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +8 -2
index 89f787c..2c8a688 100644
@@ -2478,6 +2478,7 @@ static bool states_equal(struct bpf_verifier_env *env,
 {
 	struct bpf_reg_state *rold, *rcur;
 	int i;
+	bool map_access = env->varlen_map_value_access;
 
 	for (i = 0; i < MAX_BPF_REG; i++) {
 		rold = &old->regs[i];
@@ -2489,12 +2490,17 @@ static bool states_equal(struct bpf_verifier_env *env,
 		/* If the ranges were not the same, but everything else was and
 		 * we didn't do a variable access into a map then we are a-ok.
 		 */
-		if (!env->varlen_map_value_access &&
+		if (!map_access &&
 		    rold->type == rcur->type && rold->imm == rcur->imm)
 			continue;
 
+		/* If we didn't map access then again we don't care about the
+		 * mismatched range values and it's ok if our old type was
+		 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
+		 */
 		if (rold->type == NOT_INIT ||
-		    (rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT))
+		    (!map_access && (rold->type == UNKNOWN_VALUE &&
+				     rcur->type != NOT_INIT)))
 			continue;
 
 		if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
Signature · 1 line
2.7.4
#1 lore raw
Quoted 26 lines · Josef Bacik

If we have a branch that looks something like this

int foo = map->value;
if (condition) {
foo += blah;
} else {
foo = bar;
}
map->array[foo] = baz;

We will incorrectly assume that the !condition branch is equal to the condition branch as the register for foo will be UNKNOWN_VALUE in both cases. We need to adjust this logic to only do this if we didn't do a varlen access after we processed the !condition branch, otherwise we have different ranges and need to check the other branch as well.

---
 kernel/bpf/verifier.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -0
index 89f787c..2c8a688 100644

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2481

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -2478,6 +2478,7 @@ static bool states_equal(struct bpf_verifier_env *env,
 {
 	struct bpf_reg_state *rold, *rcur;
 	int i;
+	bool map_access = env->varlen_map_value_access;

that's a bit misleading name for the variable.
Pls call it varlen_map_access.

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2494

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
 	for (i = 0; i < MAX_BPF_REG; i++) {
 		rold = &old->regs[i];
@@ -2489,12 +2490,17 @@ static bool states_equal(struct bpf_verifier_env *env,
 		/* If the ranges were not the same, but everything else was and
 		 * we didn't do a variable access into a map then we are a-ok.
 		 */
-		if (!env->varlen_map_value_access &&
+		if (!map_access &&
 		    rold->type == rcur->type && rold->imm == rcur->imm)

just noticed that this one is missing comparing rold->id == rcur->id

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2503

Quoted 10 lines
kernel/bpf/verifier.c +6 -1
 			continue;
 
+		/* If we didn't map access then again we don't care about the
+		 * mismatched range values and it's ok if our old type was
+		 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
+		 */
 		if (rold->type == NOT_INIT ||
-		    (rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT))
+		    (!map_access && (rold->type == UNKNOWN_VALUE &&
+				     rcur->type != NOT_INIT)))

please drop unnecessary ( )

maintainer
#2 lore raw
Quoted 11 lines · Alexei Starovoitov
Quoted 10 lines · Josef Bacik

If we have a branch that looks something like this

int foo = map->value;

if (condition) {
   foo += blah;
} else {

foo = bar;
}
map->array[foo] = baz;

Quoted 5 lines
Quoted 5 lines

We will incorrectly assume that the !condition branch is equal to the condition branch as the register for foo will be UNKNOWN_VALUE in both cases. We need to adjust this logic to only do this if we didn't do a varlen access after we processed the !condition branch, otherwise we have different ranges and need to check the other branch as well.

Fixes 484611357c19 ("bpf: allow access into map value arrays")
Quoted 1 line
Quoted 1 line

Please also add a test case to tools/testing/selftests/bpf/test_verifier.c for net-next tree, so we can make sure we catch this in future. Test could look like that after this fix it fails by the verifier, but before it doesn't due to false state pruning.

Quoted 15 lines

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2481

Quoted 12 lines
  kernel/bpf/verifier.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +1 -0
index 89f787c..2c8a688 100644
@@ -2478,6 +2478,7 @@ static bool states_equal(struct bpf_verifier_env *env,
  {
  	struct bpf_reg_state *rold, *rcur;
  	int i;
+	bool map_access = env->varlen_map_value_access;

that's a bit misleading name for the variable.
Pls call it varlen_map_access.

Nit: while at it, please also move it up as first var (netdev has reverse xmas tree style).

Quoted 24 lines

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2494

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
  	for (i = 0; i < MAX_BPF_REG; i++) {
  		rold = &old->regs[i];
@@ -2489,12 +2490,17 @@ static bool states_equal(struct bpf_verifier_env *env,
  		/* If the ranges were not the same, but everything else was and
  		 * we didn't do a variable access into a map then we are a-ok.
  		 */
-		if (!env->varlen_map_value_access &&
+		if (!map_access &&
  		    rold->type == rcur->type && rold->imm == rcur->imm)

just noticed that this one is missing comparing rold->id == rcur->id

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2503

Quoted 10 lines
kernel/bpf/verifier.c +6 -1
  			continue;
 
+		/* If we didn't map access then again we don't care about the
+		 * mismatched range values and it's ok if our old type was
+		 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
+		 */
  		if (rold->type == NOT_INIT ||
-		    (rold->type == UNKNOWN_VALUE && rcur->type != NOT_INIT))
+		    (!map_access && (rold->type == UNKNOWN_VALUE &&
+				     rcur->type != NOT_INIT)))

please drop unnecessary ( )

#3 lore raw
In reply to Alexei Starovoitov
Quoted 35 lines · Alexei Starovoitov
Quoted 26 lines · Josef Bacik

If we have a branch that looks something like this

int foo = map->value;
if (condition) {
foo += blah;
} else {
foo = bar;
}
map->array[foo] = baz;

We will incorrectly assume that the !condition branch is equal to the condition branch as the register for foo will be UNKNOWN_VALUE in both cases. We need to adjust this logic to only do this if we didn't do a varlen access after we processed the !condition branch, otherwise we have different ranges and need to check the other branch as well.

---
 kernel/bpf/verifier.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -0
index 89f787c..2c8a688 100644

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2481

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -2478,6 +2478,7 @@ static bool states_equal(struct bpf_verifier_env *env,
 {
 	struct bpf_reg_state *rold, *rcur;
 	int i;
+	bool map_access = env->varlen_map_value_access;

that's a bit misleading name for the variable.
Pls call it varlen_map_access.

Quoted 12 lines

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2494

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
 	for (i = 0; i < MAX_BPF_REG; i++) {
 		rold = &old->regs[i];
@@ -2489,12 +2490,17 @@ static bool states_equal(struct bpf_verifier_env *env,
 		/* If the ranges were not the same, but everything else was and
 		 * we didn't do a variable access into a map then we are a-ok.
 		 */
-		if (!env->varlen_map_value_access &&
+		if (!map_access &&
 		    rold->type == rcur->type && rold->imm == rcur->imm)

just noticed that this one is missing comparing rold->id == rcur->id

Do you want me to fix that here? I'll fix up the rest of the stuff, and Daniels things as well. Thanks,

Josef

#4 lore raw
Quoted 49 lines · Josef Bacik
Quoted 35 lines · Alexei Starovoitov
Quoted 26 lines · Josef Bacik

If we have a branch that looks something like this

int foo = map->value;
if (condition) {
foo += blah;
} else {
foo = bar;
}
map->array[foo] = baz;

We will incorrectly assume that the !condition branch is equal to the condition branch as the register for foo will be UNKNOWN_VALUE in both cases. We need to adjust this logic to only do this if we didn't do a varlen access after we processed the !condition branch, otherwise we have different ranges and need to check the other branch as well.

---
kernel/bpf/verifier.c | 10 ++++++++--

1 file changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -0
index 89f787c..2c8a688 100644

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2481

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -2478,6 +2478,7 @@ static bool states_equal(struct bpf_verifier_env *env,
 {
 	struct bpf_reg_state *rold, *rcur;
 	int i;
+	bool map_access = env->varlen_map_value_access;

that's a bit misleading name for the variable.
Pls call it varlen_map_access.

Quoted 12 lines

kernel/bpf/verifier.c static bool states_equal(struct bpf_verifier_env *env, · line 2494

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
 	for (i = 0; i < MAX_BPF_REG; i++) {
 		rold = &old->regs[i];
@@ -2489,12 +2490,17 @@ static bool states_equal(struct bpf_verifier_env *env,
 		/* If the ranges were not the same, but everything else was and
 		 * we didn't do a variable access into a map then we are a-ok.
 		 */
-		if (!env->varlen_map_value_access &&
+		if (!map_access &&
 		    rold->type == rcur->type && rold->imm == rcur->imm)

just noticed that this one is missing comparing rold->id == rcur->id

Quoted 2 lines

Do you want me to fix that here? I'll fix up the rest of the stuff, and Daniels things as well. Thanks,

Nevermind. Comparing 'id' is not needed in net, only in net-next.