Skip to contentSkip to conversation

selftest: bpf: Correct mssind comparison in test_tcp_custom_syncookie.c.

PATCH In review Kuniyuki Iwashima 2 participants 3 messages Aug 19, 2024 – Aug 21, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftest: bpf: Correct mssind comparison in test_tcp_custom_syncookie.c.

Smatch reported a possible off-by-one in tcp_validate_cookie().

However, it's false positive because the possible range of mssind is limited from 0 to 3 by the preceding calculation.

  mssind = (cookie & (3 << 6)) >> 6;

There's no real issue, but let's make Smatch happy to suppress the same reports.

---
 tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +2 -2
index 44ee0d037f95..36b842133033 100644
@@ -487,12 +487,12 @@ static int tcp_validate_cookie(struct tcp_syncookie *ctx)
 
 	mssind = (cookie & (3 << 6)) >> 6;
 	if (ctx->ipv4) {
-		if (mssind > ARRAY_SIZE(msstab4))
+		if (mssind >= ARRAY_SIZE(msstab4))
 			goto err;
 
 		ctx->attrs.mss = msstab4[mssind];
 	} else {
-		if (mssind > ARRAY_SIZE(msstab6))
+		if (mssind >= ARRAY_SIZE(msstab6))
 			goto err;
 
 		ctx->attrs.mss = msstab6[mssind];
Signature · 1 line
2.30.2
#1 lore raw
Quoted 22 lines · Kuniyuki Iwashima

Smatch reported a possible off-by-one in tcp_validate_cookie().

However, it's false positive because the possible range of mssind is limited from 0 to 3 by the preceding calculation.

   mssind = (cookie & (3 << 6)) >> 6;

There's no real issue, but let's make Smatch happy to suppress the same reports.

---
  tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +0 -0
index 44ee0d037f95..36b842133033 100644

tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c static int tcp_validate_cookie(struct tcp_syncookie *ctx) · line 488

Quoted 2 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +0 -0
@@ -487,12 +487,12 @@ static int tcp_validate_cookie(struct tcp_syncookie *ctx)
 
  	mssind = (cookie & (3 << 6)) >> 6;

This should have bound the mssind.

tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c static int tcp_validate_cookie(struct tcp_syncookie *ctx) · line 490

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +1 -1
  	if (ctx->ipv4) {
-		if (mssind > ARRAY_SIZE(msstab4))
+		if (mssind >= ARRAY_SIZE(msstab4))

Does the verifier complain without this if check?

Quoted 9 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +1 -1
  			goto err;
 
  		ctx->attrs.mss = msstab4[mssind];
  	} else {
-		if (mssind > ARRAY_SIZE(msstab6))
+		if (mssind >= ARRAY_SIZE(msstab6))
  			goto err;
 
  		ctx->attrs.mss = msstab6[mssind];
#2 lore raw

From: Martin KaFai Lau <[email protected]>
Date: Tue, 20 Aug 2024 17:11:48 -0700

Quoted 28 lines · Kuniyuki Iwashima
Quoted 22 lines

Smatch reported a possible off-by-one in tcp_validate_cookie().

However, it's false positive because the possible range of mssind is limited from 0 to 3 by the preceding calculation.

   mssind = (cookie & (3 << 6)) >> 6;

There's no real issue, but let's make Smatch happy to suppress the same reports.

---
  tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +0 -0
index 44ee0d037f95..36b842133033 100644

tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c static int tcp_validate_cookie(struct tcp_syncookie *ctx) · line 488

Quoted 2 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +0 -0
@@ -487,12 +487,12 @@ static int tcp_validate_cookie(struct tcp_syncookie *ctx)
 
  	mssind = (cookie & (3 << 6)) >> 6;

This should have bound the mssind.

Quoted 5 lines

tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c static int tcp_validate_cookie(struct tcp_syncookie *ctx) · line 490

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +1 -1
  	if (ctx->ipv4) {
-		if (mssind > ARRAY_SIZE(msstab4))
+		if (mssind >= ARRAY_SIZE(msstab4))

Does the verifier complain without this if check?

It didn't :)

I'll remove the checks in v2.

Thanks!