fix correct_cache() in libiptc.c
PATCH 1/1 fix correct_cache() in libiptc.c
Hi Harald,
I got a report on irc that iptables -X didn't work like it used to. Seems related to your libiptc work... you touched it last, you get to check all new patches against it :)
Test-cases:
filter table is empty.
# ./iptables -N HELLO # ./iptables -N world # ./iptables -X #
Nothing wrong here.
Just switching the order of chains...
# ./iptables -N world # ./iptables -N HELLO # ./iptables -X iptables: No chain/target/match by that name #
Huh?
The cause of this is that chains aren't removed in the order they were created, they are removed in sorted order and the code for deletion of a chain is currently somewhat busted.
Now I've added
printf("Deleting chain: %s\n", cc->name);
in libiptc/libiptc.c:481
Lets go back to the first testcase which seemed to work.
# ./iptables -N HELLO # ./iptables -N world # ./iptables -X Deleting chain: FORWARD Deleting chain: OUTPUT Deleting chain: HELLO #
That's not right.
Let's see what it does in the testcase that really failed...
# ./iptables -N world # ./iptables -N HELLO # ./iptables -X Deleting chain: world iptables: No chain/target/match by that name #
Ok this is weird.
If you add more printf's you see that TC_DELETE_CHAIN() gets called for the HELLO chain and find_label() finds it and then delete_rules() is called. delete_rules() calls correct_cache() which thinks it should delete the chain called world instead.
And then TC_DELETE_CHAIN() is called for the world chain and now cache_num_chains has been decremented by one so it doesn't find the world chain which is last.
I've found two errors in the code, patch below.
libiptc/libiptc.c renamed from libiptc/libiptc.c.orig +3 -3
@@ -471,15 +471,15 @@if (delta <0 ) {/* take care about deleted chains */ -if (cc->start_off >= offset+delta - && cc->end_off <= offset) { +if (cc->start_off > offset + && cc->end_off < offset-delta) {/* this chain is within the deleted range, * let's remove it from the cache */ void *start;unsigned int size; h->cache_num_chains--; -if (i+1 >= h->cache_num_chains) +if (i == h->cache_num_chains)continue ; start = &h->cache_chain_heads[i+1 ]; size = (h->cache_num_chains-i)
With this patch it becomes:
# ./iptables -N HELLO # ./iptables -N world # ./iptables -X Deleting chain: HELLO Deleting chain: world #
Much better.
# ./iptables -N world # ./iptables -N HELLO # ./iptables -X Deleting chain: HELLO Deleting chain: world #
Ahh. Both cases work like they should.
Now it selects the correct chain to delete.
And the off-by-one bug causes it to do nothing when it comes to the second-last chain which is wrong, it should do the memory-copies for all deleted chains except when it's the last chain that's beeing deleted.
I'm not sure if my new limits in the first if-statement are 100% correct. I don't know the code that well, and honestly I never want to, I got enough exposure for several lifetimes last night. The code is absolutely horrible...
Oh yes, this fix actually makes iptables-restore work again...
filter table is once again empty:
Without patch:
# ./iptables -A INPUT # ./iptables -A FORWARD # ./iptables -A OUTPUT # ./iptables-save > file # ./iptables-restore < file Deleting chain: INPUT Deleting chain: FORWARD iptables-restore: line 13 failed #
With the patch above it works fine. And it only deletes user-defined chains that exist before iptables-restore is run. (which is none in this simple testcase)
I have one more thing I want you to look at, once again in correct_cache()...
if (cc->start_off > offset)
cc->start_off += delta;
if (cc->end_off >= offset)
cc->end_off += delta;Shouldn't that really be moved up into the part where chains are deleted and turned into an unconditional loop that adds delta (which is negative) for all moved chains? What if several chains are deleted at once? Currently that isn't done but correct_cache() implies that it supports it because it doesn't stop looking for deleted chains after the first one is found.
Signature · 1 line
/Martin
Quoted 5 lines · Martin Josefsson
Hi Harald,
I got a report on irc that iptables -X didn't work like it used to. Seems related to your libiptc work... you touched it last, you get to check all new patches against it :)
Yes, as stated on the mailinglist, I even expect it to still be broken a bit.
Expect it to break even more in the future, I'm currently working on an
almost total reimplementation of the whole library. The ruleset load
time ist still unbearable.
Quoted 9 lines
# ./iptables -N HELLO # ./iptables -N world # ./iptables -X Deleting chain: FORWARD Deleting chain: OUTPUT Deleting chain: HELLO #That's not right.
ouch. I didn't test chain deletion as well as insertion/creation. Believe me, I was happy that something was working again at all ;)
Quoted 9 lines
Ok this is weird.
If you add more printf's you see that
TC_DELETE_CHAIN()gets called for the HELLO chain andfind_label()finds it and thendelete_rules()is called.delete_rules()callscorrect_cache()which thinks it should delete the chain called world instead.
And thenTC_DELETE_CHAIN()is called for the world chain and nowcache_num_chainshas been decremented by one so it doesn't find the world chain which is last.
well spotted.
Quoted 23 lines
libiptc/libiptc.c renamed from libiptc/libiptc.c.orig +3
-3@@ -471,15 +471,15 @@if (delta <0 ) {/* take care about deleted chains */ -if (cc->start_off >= offset+delta - && cc->end_off <= offset) { +if (cc->start_off > offset + && cc->end_off < offset-delta) {/* this chain is within the deleted range, * let's remove it from the cache */ void *start;unsigned int size; h->cache_num_chains--; -if (i+1 >= h->cache_num_chains) +if (i == h->cache_num_chains)continue ; start = &h->cache_chain_heads[i+1 ]; size = (h->cache_num_chains-i)
Quoted 4 lines
Now it selects the correct chain to delete.
And the off-by-one bug causes it to do nothing when it comes to the second-last chain which is wrong, it should do the memory-copies for all deleted chains except when it's the last chain that's beeing deleted.
true.
Quoted 4 lines
I'm not sure if my new limits in the first if-statement are 100% correct. I don't know the code that well, and honestly I never want to, I got enough exposure for several lifetimes last night. The code is absolutely horrible...
*agreed*. I vote for presenting Rusty with an award for obfuscated code at the workshop ;)
Quoted 9 lines
I have one more thing I want you to look at, once again in
correct_cache()...if (cc->start_off > offset) cc->start_off += delta; if (cc->end_off >= offset) cc->end_off += delta;
Quoted 6 lines
Shouldn't that really be moved up into the part where chains are deleted and turned into an unconditional loop that adds delta (which is negative) for all moved chains? What if several chains are deleted at once? Currently that isn't done but
correct_cache()implies that it supports it because it doesn't stop looking for deleted chains after the first one is found.
Oh well, I undestand. This was a mistake. correct_cache() was first written for inserting entries, and the additional code part for rule deletion turns out to be broken in several ways.
I'm going to apply your patch now, and then deal with this issue.
Let's hope this is the last thing I have to do in the old libiptc.
Quoted 1 line
/Martin
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
Quoted 6 lines · Harald Welte
Yes, as stated on the mailinglist, I even expect it to still be broken a bit.
Expect it to break even more in the future, I'm currently working on an
almost total reimplementation of the whole library. The ruleset load
time ist still unbearable.
I hope you are abandoning the "API" the current library has? It will break a lot of "third-party" stuff but IMHO it's worth it.
Quoted 7 lines
Quoted 4 lines
I'm not sure if my new limits in the first if-statement are 100% correct. I don't know the code that well, and honestly I never want to, I got enough exposure for several lifetimes last night. The code is absolutely horrible...
*agreed*. I vote for presenting Rusty with an award for obfuscated code at the workshop ;)
Yes we really have to. I still can't understand how it's possible to write code like that. Is there a special school you can go to in order to learn? :)
Quoted 5 lines
Oh well, I undestand. This was a mistake.
correct_cache()was first written for inserting entries, and the additional code part for rule deletion turns out to be broken in several ways.I'm going to apply your patch now, and then deal with this issue.
Good, thanks. You'll commit it so people can delete their chains until your rewrite is finished?
The patch has had some testing by the person who reported the problem and it seemed to work fine.
Quoted 1 line
Let's hope this is the last thing I have to do in the old libiptc.
Lets hope this is the last thing _anyone_ has to do in the old libiptc :) It has to be a violation of human rights to read that code (forced or not)
Signature · 1 line
/Martin
Quoted 10 lines · Martin Josefsson
Quoted 6 lines · Harald Welte
Yes, as stated on the mailinglist, I even expect it to still be broken a bit.
Expect it to break even more in the future, I'm currently working on an
almost total reimplementation of the whole library. The ruleset load
time ist still unbearable.I hope you are abandoning the "API" the current library has?
no, not at all. It has to be 100% compatible.
Quoted 1 line
It will break a lot of "third-party" stuff but IMHO it's worth it.
No. That's for pkttables. It's not worth changing the old api.
Quoted 1 line
/Martin
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
What about this one? I think it is a clener fix...
Index: libiptc.c
===================================================================
RCS file: /cvsroot/netfilter/userspace/libiptc/libiptc.c,v retrieving revision 1.40
libiptc.c +6 -2
@@ -479,8 +479,12 @@unsigned int size; h->cache_num_chains--; -if (i+1 >= h->cache_num_chains) + +/* no need for memmove since we are +* removing the last entry */ +if (i >= h->cache_num_chains)continue ; + start = &h->cache_chain_heads[i+1 ]; size = (h->cache_num_chains-i) *sizeof (struct chain_cache );
@@ -943,7 +947,7 @@ (*handle)->entries.size -= rules_size;/* Fix the chain cache */ -if (!correct_cache (*handle, offset, -(int )rules_size)) +if (!correct_cache (*handle, offset+rules_size, -(int )rules_size))return 0 ;return set_verdict (offset, -(int )rules_size, handle);
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie