Skip to contentSkip to conversation

minor bug in iptables 1.2.9

PATCH Applied Karsten Desler 2 participants 5 messages Jan 30, 2004 – Jan 31, 2004 1 message not in the archive netfilter

lore
Share & tools
lore
#0 lore raw

Hi,

while reading the iptables 1.2.9 source, I found this small bug in iptables.c line 276:

struct in_addr *
dotted_to_addr(const char *dotted)

{
	char buf[20];
	...

	/* copy dotted string, because we need to modify it */
	strncpy(buf, dotted, sizeof(buf) - 1);
	...

strncpy does not \0-terminate strings if strlen(src) == n, thus the following loop can easily run over the end of buf, if a big enough mask is given:
iptables -A INPUT -p tcp -s 1.1.1.1/000000000000000000.

Attached is a patch to fix this problem.

Karsten

iptables.c renamed from iptables~.c +1 -0
@@ -274,6 +274,7 @@
 
 	/* copy dotted string, because we need to modify it */
 	strncpy(buf, dotted, sizeof(buf) - 1);
+	buf[sizeof(buf) - 1] = '\0';
 	addrp = (unsigned char *) &(addr.s_addr);
 
 	p = buf;
#1 lore raw
Quoted 6 lines · Karsten Desler

strncpy does not \0-terminate strings if strlen(src) == n, thus the following loop can easily run over the end of buf, if a big enough mask is given:
iptables -A INPUT -p tcp -s 1.1.1.1/000000000000000000.

Attached is a patch to fix this problem.

Applied, thanks

Signature · 1 line
/Martin
#2 lore raw

Hi,

today I had a little more time to look at the code. A few lines down, there's another incarnation of the same bug. line 641:

	char buf[256];
	...
	strncpy(buf, name, sizeof(buf) - 1);
	if ((p = strrchr(buf, '/')) != NULL) {
	...

Patch attached.
Karsten

iptables.c renamed from iptables~.c +1 -0
@@ -639,6 +640,7 @@
 	int i, j, k, n;
 
 	strncpy(buf, name, sizeof(buf) - 1);
+	buf[sizeof(buf) - 1] = '\0';
 	if ((p = strrchr(buf, '/')) != NULL) {
 		*p = '\0';
 		addrp = parse_mask(p + 1);
#3 lore raw
Quoted 4 lines · Karsten Desler

Hi,

today I had a little more time to look at the code. A few lines down, there's another incarnation of the same bug.

Thanks, applied.

Signature · 1 line
/Martin

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 10 lines · Karsten Desler

Hi,

And a couple more.

Attached are three patches.
iptables.patch - fixes for similar bugs in ip6tables,
iptables- and ip6tables-restore
extensions.patch - fixes for similar bugs in the extensions/
directory
libiptc.patch - simiar fix for libiptc

Applied slightly modified, you introduced some memory-corruption, arrays start at 0...

Thanks for all these fixes.

Signature · 1 line
/Martin