Skip to contentSkip to conversation

ipv6: fix out-of-bound access in ip6_parse_tlv()

PATCH Superseded Eric Dumazet 4 participants 4 messages Jun 23, 2021 – Jun 24, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv6: fix out-of-bound access in ip6_parse_tlv()

From: Eric Dumazet <[email protected]>

First problem is that optlen is fetched without checking there is more than one byte to parse.

Fix this by taking care of IPV6_TLV_PAD1 before fetching optlen (under appropriate sanity checks against len)

Second problem is that IPV6_TLV_PADN checks of zero padding are performed before the check of remaining length.

Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")
Cc Paolo Abeni <[email protected]>
Cc Tom Herbert <[email protected]>

---

Only compiled, I would appreciate a solid review of this patch before merging it, thanks !

 net/ipv6/exthdrs.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
net/ipv6/exthdrs.c +13 -13
index 6f7da8f3e2e5849f917853984c69bf02a0f1e27c..007959d4d3748f1e21f83946024a9967d08b25b6 100644
@@ -135,18 +135,24 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs,
 	len -= 2;
 
 	while (len > 0) {
-		int optlen = nh[off + 1] + 2;
-		int i;
+		int optlen, i;
 
-		switch (nh[off]) {
-		case IPV6_TLV_PAD1:
+		if (nh[off] == IPV6_TLV_PAD1) {
 			optlen = 1;
 			padlen++;
 			if (padlen > 7)
 				goto bad;
-			break;
+			off++;
+			len--;
+			continue;
+		}
+		if (len < 2)
+			goto bad;
+		optlen = nh[off + 1] + 2;
+		if (optlen > len)
+			goto bad;
 
-		case IPV6_TLV_PADN:
+		if (nh[off] == IPV6_TLV_PADN) {
 			/* RFC 2460 states that the purpose of PadN is
 			 * to align the containing header to multiples
 			 * of 8. 7 is therefore the highest valid value.
@@ -163,12 +169,7 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs,
 				if (nh[off + i] != 0)
 					goto bad;
 			}
-			break;
-
-		default: /* Other TLV code so scan list */
-			if (optlen > len)
-				goto bad;
-
+		} else {
 			tlv_count++;
 			if (tlv_count > max_count)
 				goto bad;
@@ -188,7 +189,6 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs,
 				return false;
 
 			padlen = 0;
-			break;
 		}
 		off += optlen;
 		len -= optlen;
Signature · 1 line
2.32.0.288.g62a8d224e6-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · On Wed, 23 Jun 2021 12:43:53 -0700 Eric Dumazet wrote:

From: Eric Dumazet <[email protected]>

First problem is that optlen is fetched without checking there is more than one byte to parse.

Fix this by taking care of IPV6_TLV_PAD1 before fetching optlen (under appropriate sanity checks against len)

Second problem is that IPV6_TLV_PADN checks of zero padding are performed before the check of remaining length.

Quoted 7 lines
Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")
Cc Paolo Abeni <[email protected]>
Cc Tom Herbert <[email protected]>

---

Only compiled, I would appreciate a solid review of this patch before merging it, thanks !

for what that's worth

maintainer
#2 lore raw
In reply to Eric Dumazet
Quoted 5 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

First problem is that optlen is fetched without checking there is more than one byte to parse.

Quoted 7 lines

Fix this by taking care of IPV6_TLV_PAD1 before fetching optlen (under appropriate sanity checks against len)

Second problem is that IPV6_TLV_PADN checks of zero padding are performed before the check of remaining length.

Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")

Perhaps even:

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

for the first issue?

Quoted 16 lines
Cc Paolo Abeni <[email protected]>
Cc Tom Herbert <[email protected]>

---

Only compiled, I would appreciate a solid review of this patch before merging it, thanks !

 net/ipv6/exthdrs.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
net/ipv6/exthdrs.c +0 -0
index 6f7da8f3e2e5849f917853984c69bf02a0f1e27c..007959d4d3748f1e21f83946024a9967d08b25b6 100644
@@ -135,18 +135,24 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs,
 	len -= 2;

net/ipv6/exthdrs.c static bool ip6_parse_tlv(const struct tlvtype_proc *procs, · line 141

Quoted 10 lines
net/ipv6/exthdrs.c +2 -4
 
 	while (len > 0) {
-		int optlen = nh[off + 1] + 2;
-		int i;
+		int optlen, i;
 
-		switch (nh[off]) {
-		case IPV6_TLV_PAD1:
+		if (nh[off] == IPV6_TLV_PAD1) {
 			optlen = 1;

It looks like the above assignment is not needed anymore.

Other than that LGTM,

Thanks!

Paolo

#3 lore raw
Quoted 22 lines · Paolo Abeni
Quoted 5 lines · Eric Dumazet

From: Eric Dumazet <[email protected]>

First problem is that optlen is fetched without checking there is more than one byte to parse.

Quoted 7 lines

Fix this by taking care of IPV6_TLV_PAD1 before fetching optlen (under appropriate sanity checks against len)

Second problem is that IPV6_TLV_PADN checks of zero padding are performed before the check of remaining length.

Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")

Perhaps even:

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

for the first issue?

Quoted 7 lines

+ if (nh[off] == IPV6_TLV_PAD1) {

Quoted 1 line
                      optlen = 1;

It looks like the above assignment is not needed anymore.

Other than that LGTM,

Thanks for the review, I am sending the v2.