Skip to contentSkip to conversation

netfilter: guard option walkers against 1-byte tail reads

PATCH Superseded David Dull 3 participants 6 messages Mar 7 – Mar 8

Subsystems (2)
lore
Share & tools
lore

David Dull

author
#0 lore raw

PATCH 1/1 netfilter: guard option walkers against 1-byte tail reads

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Add an explicit i == optlen - 1 check before dereferencing op[i + 1] in xt_tcpudp and xt_dccp option walkers.

---
 net/netfilter/xt_dccp.c   | 4 ++--
 net/netfilter/xt_tcpudp.c | 6 ++++--
 2 files changed, 5 insertions(+), 4 deletions(-)
net/netfilter/xt_dccp.c +2 -2
index e5a13ecbe6..037ab93e25 100644
@@ -62,10 +62,10 @@ dccp_find_option(u_int8_t option,
 			return true;
 		}
 
-		if (op[i] < 2)
+		if (op[i] < 2 || i == optlen - 1)
 			i++;
 		else
-			i += op[i+1]?:1;
+			i += op[i + 1] ? : 1;
 	}
 
 	spin_unlock_bh(&dccp_buflock);
net/netfilter/xt_tcpudp.c +4 -2
index e8991130a3..f76cf18f1a 100644
@@ -59,8 +59,10 @@ tcp_find_option(u_int8_t option,
 
 	for (i = 0; i < optlen; ) {
 		if (op[i] == option) return !invert;
-		if (op[i] < 2) i++;
-		else i += op[i+1]?:1;
+		if (op[i] < 2 || i == optlen - 1)
+			i++;
+		else
+			i += op[i + 1] ? : 1;
 	}
 
 	return invert;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 6 lines · David Dull

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Add an explicit i == optlen - 1 check before dereferencing op[i + 1] in xt_tcpudp and xt_dccp option walkers.

Fixes 2e4e6a17af35 ("[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables")
author
#2 lore raw

PATCH v2 1/1 netfilter: guard option walkers against 1-byte tail reads

In reply to David Dull

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Add an explicit i == optlen - 1 check before dereferencing op[i + 1] in xt_tcpudp and xt_dccp option walkers.

Fixes 2e4e6a17af35 ("[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables")
---
 net/netfilter/xt_dccp.c   | 4 ++--
 net/netfilter/xt_tcpudp.c | 6 ++++--
 2 files changed, 5 insertions(+), 4 deletions(-)
net/netfilter/xt_dccp.c +2 -2
index e5a13ecbe6..037ab93e25 100644
@@ -62,10 +62,10 @@ dccp_find_option(u_int8_t option,
 			return true;
 		}
 
-		if (op[i] < 2)
+		if (op[i] < 2 || i == optlen - 1)
 			i++;
 		else
-			i += op[i+1]?:1;
+			i += op[i + 1] ? : 1;
 	}
 
 	spin_unlock_bh(&dccp_buflock);
net/netfilter/xt_tcpudp.c +4 -2
index e8991130a3..f76cf18f1a 100644
@@ -59,8 +59,10 @@ tcp_find_option(u_int8_t option,
 
 	for (i = 0; i < optlen; ) {
 		if (op[i] == option) return !invert;
-		if (op[i] < 2) i++;
-		else i += op[i+1]?:1;
+		if (op[i] < 2 || i == optlen - 1)
+			i++;
+		else
+			i += op[i + 1] ? : 1;
 	}
 
 	return invert;
Signature · 1 line
2.43.0
#3 lore raw
Quoted 21 lines · David Dull

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Add an explicit i == optlen - 1 check before dereferencing op[i + 1] in xt_tcpudp and xt_dccp option walkers.

Fixes 2e4e6a17af35 ("[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables")
---
 net/netfilter/xt_dccp.c   | 4 ++--
 net/netfilter/xt_tcpudp.c | 6 ++++--
 2 files changed, 5 insertions(+), 4 deletions(-)
net/netfilter/xt_dccp.c +0 -0
index e5a13ecbe6..037ab93e25 100644

net/netfilter/xt_dccp.c dccp_find_option(u_int8_t option, · line 69

Quoted 10 lines
net/netfilter/xt_dccp.c +2 -2
@@ -62,10 +62,10 @@ dccp_find_option(u_int8_t option,
 			return true;
 		}
 
-		if (op[i] < 2)
+		if (op[i] < 2 || i == optlen - 1)
 			i++;
 		else
-			i += op[i+1]?:1;
+			i += op[i + 1] ? : 1;
 	}

To improve this area, I'd suggest:

		if (op[i] < 2) {
 			i++;
                        continue;
                }

                if (i + 1 >= optlen)
                        break;

		i += op[i + 1] ? : 1;

}

author
#4 lore raw

PATCH v3 1/1 netfilter: guard option walkers against 1-byte tail reads

In reply to David Dull

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Handle single-byte options first, then check that a length byte is still available before reading op[i + 1] in xt_tcpudp and xt_dccp option walkers.

Fixes 2e4e6a17af35 ("[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables")
---
 net/netfilter/xt_dccp.c   | 8 ++++++--
 net/netfilter/xt_tcpudp.c | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)
net/netfilter/xt_dccp.c +7 -2
index e5a13ecbe6..7f9e2d5c1b 100644
@@ -62,10 +62,14 @@ dccp_find_option(u_int8_t option,
 			return true;
 		}
 
-		if (op[i] < 2 || i == optlen - 1)
+		if (op[i] < 2) {
 			i++;
-		else
+			continue;
+		}
+
+		if (i + 1 >= optlen)
+			break;
+
 			i += op[i + 1] ? : 1;
 	}
 

spin_unlock_bh(&dccp_buflock);

net/netfilter/xt_tcpudp.c +7 -2
index e8991130a3..4f29b1dd0f 100644
@@ -59,10 +59,14 @@ tcp_find_option(u_int8_t option,
 
 	for (i = 0; i < optlen; ) {
 		if (op[i] == option) return !invert;
-		if (op[i] < 2 || i == optlen - 1)
+		if (op[i] < 2) {
 			i++;
-		else
+			continue;
+		}
+
+		if (i + 1 >= optlen)
+			break;
+
 			i += op[i + 1] ? : 1;
 	}
 

return invert;

Signature · 1 line
2.43.0
#5 lore raw
Quoted 25 lines · David Dull

When the last byte of options is a non-single-byte option kind, walkers that advance with i += op[i + 1] ? : 1 can read op[i + 1] past the end of the option area.

Handle single-byte options first, then check that a length byte is still available before reading op[i + 1] in xt_tcpudp and xt_dccp option walkers.

Fixes 2e4e6a17af35 ("[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables")
---
 net/netfilter/xt_dccp.c   | 8 ++++++--
 net/netfilter/xt_tcpudp.c | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)
net/netfilter/xt_dccp.c +0 -1
index e5a13ecbe6..7f9e2d5c1b 100644
@@ -62,10 +62,14 @@ dccp_find_option(u_int8_t option,
 			return true;
 		}
 
-		if (op[i] < 2 || i == optlen - 1)

net/netfilter/xt_dccp.c dccp_find_option(u_int8_t option, · line 73

Quoted 10 lines
net/netfilter/xt_dccp.c +7 -1
+		if (op[i] < 2) {
 			i++;
-		else
+			continue;
+		}
+
+		if (i + 1 >= optlen)
+			break;
+
 			i += op[i + 1] ? : 1;

This indent is wrong, I posted this patch:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/