Skip to contentSkip to conversation

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

PATCH Applied Eric Dumazet 3 participants 3 messages Jun 24, 2021

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

PATCH v2 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 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")
Cc Paolo Abeni <[email protected]>
Cc Tom Herbert <[email protected]>

---
v2: Removed not needed optlen assignment for IPV6_TLV_PAD1 handling,
added the Fixes: tag for first problem, feedback from Paolo, thanks !

 net/ipv6/exthdrs.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
net/ipv6/exthdrs.c +13 -14
index 6f7da8f3e2e5849f917853984c69bf02a0f1e27c..26882e165c9e37a105f988020031f03d6b1a5cf9 100644
@@ -135,18 +135,23 @@ 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:
-			optlen = 1;
+		if (nh[off] == IPV6_TLV_PAD1) {
 			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 +168,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 +188,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
#1 lore raw
Quoted 73 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.

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 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes c1412fce7ecc ("net/ipv6/exthdrs.c: Strict PadN option checking")
Cc Paolo Abeni <[email protected]>
Cc Tom Herbert <[email protected]>

---
v2: Removed not needed optlen assignment for IPV6_TLV_PAD1 handling,
added the Fixes: tag for first problem, feedback from Paolo, thanks !

 net/ipv6/exthdrs.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
net/ipv6/exthdrs.c +13 -13
index 6f7da8f3e2e5849f917853984c69bf02a0f1e27c..26882e165c9e37a105f988020031f03d6b1a5cf9 100644
@@ -135,18 +135,23 @@ 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:
-			optlen = 1;
+		if (nh[off] == IPV6_TLV_PAD1) {
 			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 +168,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;

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

Quoted 7 lines
net/ipv6/exthdrs.c +0 -1
@@ -188,7 +188,6 @@ static bool ip6_parse_tlv(const struct tlvtype_proc *procs,
 				return false;
 
 			padlen = 0;
-			break;
 		}
 		off += optlen;
 		len -= optlen;

LGTM!

#2 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 9 lines · On Thu, 24 Jun 2021 03:07:20 -0700 you 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)

[...]

Here is the summary with links:
- [v2,net] ipv6: fix out-of-bound access in ip6_parse_tlv()

    https://git.kernel.org/netdev/net/c/624085a31c1a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html