net: fix for a race condition in the inet frag code
Subsystems (3)
PATCH 1/1 net: fix for a race condition in the inet frag code
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between inet_frag_evictor, inet_frag_intern and the IPv4/6 frag_queue and expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to the lru_list (inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by the fraq_queue function for different reasons) before it's added to the lru_list, then after it gets added it's a matter of time for the evictor to get to a piece of memory which has been freed leading to a number of different bugs depending on what's left there. I've been able to trigger this on both IPv4 and IPv6 (which is normal as the frag code is the same), but it's been much more difficult to trigger on IPv4 due to the protocol differences about how fragments are treated. The setup I used to reproduce this is:
2 machines with 4 x 10G bonded in a RR bond, so the same flow can be seen on multiple cards at the same time. Then I used multiple instances of ping/ping6 to generate fragmented packets and flood the machines with them while running other processes to load the attacked machine. *It is very important to have the _same flow_ coming in on multiple CPUs concurrently. Usually the attacked machine would die in less than 30 minutes, if configured properly to have many evictor calls and timeouts it could happen in 10 minutes or so.
The fix is simple, just move the lru_add under the hash chain locked region so when a removing function is called it'll have to wait for the fragment to be added to the lru_list, and then it'll remove it (it works because the hash chain removal is done before the lru_list one and there's no window between the two list adds when the frag can get dropped). With this fix applied I couldn't kill the same machine in 24 hours with the same setup.
rwlock")
---
I'm new to this code, so I'm not sure if this is the best approach to fix the issue and am open to other suggestions, since I consider the issue quite serious (remotely triggerable) I'll be closely monitoring this thread to get it fixed asap.
net/ipv4/inet_fragment.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/inet_fragment.c +2 -1
@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock); -inet_frag_lru_add (nf, qp); +return qp; }
Signature · 1 line
1.8.4.2
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_list
Sorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);
If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the frag_expire timer fires instantly it should not free qp.
What am I missing?
Thanks,
Florian
Quoted 29 lines · Florian Westphal
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_listSorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
Quoted 9 lines
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the
frag_expiretimer fires instantly it should not free qp.What am I missing?
Thanks,
Florian
inet_frag_kill when called from the IPv4/6 frag_queue function will remove the timer refcount, then inet_frag_put afterwards will drop it to 0 and free it and all of this could happen before the frag was ever added to the LRU list, then it gets added. This happens much easier for IPv6 because of the dropping of overlapping fragments in its frag_queue function, the point is we need to have the timer's refcount removed in any way (it could be the timer itself - there's an inet_frag_put in the end, or much easier by the frag_queue function). I think I've explained it badly, I hope this makes it clearer :-)
Cheers,
Nik
Quoted 29 lines · Florian Westphal
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_listSorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
Quoted 9 lines
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the
frag_expiretimer fires instantly it should not free qp.What am I missing?
Thanks,
Florian
An important point is that inet_frag_kill removes both the timer's refcnt and has an unconditional atomic_dec to remove the original/guarding refcnt, so it basically removes everything that's in the way.
Quoted 34 lines · On Mon, 03 Mar 2014 15:49:56 +0100 Nikolay Aleksandrov <[email protected]>…
Quoted 33 lines · Florian Westphal
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_listSorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the
frag_expiretimer fires instantly it should not free qp.
Quoted 8 lines
Quoted 5 lines
What am I missing?
Thanks,
FlorianAn important point is that
inet_frag_killremoves both the timer's refcnt and has an unconditionalatomic_decto remove the original/guarding refcnt, so it basically removes everything that's in the way.
It sound like we might have a refcnt problem...
Do we need an extra refcnt for maintaining elements the LRU list?
Signature · 5 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Sr. Network Kernel Developer at Red Hat Author of http://www.iptv-analyzer.org LinkedIn: http://www.linkedin.com/in/brouer
Quoted 42 lines · Jesper Dangaard Brouer
Quoted 39 lines · On Mon, 03 Mar 2014 15:49:56 +0100 Nikolay Aleksandrov <[email protected]>…
Quoted 38 lines · Florian Westphal
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_listSorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the
frag_expiretimer fires instantly it should not free qp.What am I missing?
Thanks,
Florian
Quoted 7 lines
Quoted 3 lines
An important point is that
inet_frag_killremoves both the timer's refcnt and has an unconditionalatomic_decto remove the original/guarding refcnt, so it basically removes everything that's in the way.It sound like we might have a refcnt problem...
Do we need an extra refcnt for maintaining elements the LRU list?
I don't think so, if you keep the lru_add separated from the chain addition as before you still got a race condition when frags are seen by the fq_find functions but are not in the LRU list yet unless you add the refcnt before adding to the lru list while holding the chain lock (or before that) which will alter the behaviour, but still if inet_frag_kill gets called it should remove that refcnt and thus put us in the same position as now if we want to keep the behaviour as it's now.
Nik
Quoted 2 lines · On Mon, 03 Mar 2014 15:43:00 +0100 Nikolay Aleksandrov <[email protected]>…
Quoted 1 line · Florian Westphal
Nikolay Aleksandrov <[email protected]> wrote:
[...]
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);
[...]
Quoted 9 lines
Quoted 1 line
inet_frag_killwhen called from the IPv4/6frag_queuefunction will remove the timer refcount, theninet_frag_putafterwards will drop it to 0 and free it and all of this could happen before the frag was ever added to the LRU list, then it gets added. This happens much easier for IPv6 because of the dropping of overlapping fragments in itsfrag_queuefunction, the point is we need to have the timer's refcount removed in any way (it could be the timer itself - there's aninet_frag_putin the end, or much easier by thefrag_queuefunction). I think I've explained it badly, I hope this makes it clearer :-)
I like this desc better.
After some IRC discussions with Nik and Florian, I acknowledge this is real race condition.
The real solution is the remove the LRU list system (which will also solve a scalability problem), but short-term we need Nik's fix, which I guess should be a stable fix.
Thanks Nik!
Signature · 5 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Sr. Network Kernel Developer at Red Hat Author of http://www.iptv-analyzer.org LinkedIn: http://www.linkedin.com/in/brouer
Quoted 34 lines · Nikolay Aleksandrov
Quoted 33 lines · Florian Westphal
Quoted 9 lines · Nikolay Aleksandrov
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_listSorry. Not following here, see below.
net/ipv4/inet_fragment.c static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf, · line 283
Quoted 11 lines
net/ipv4/inet_fragment.c +1
-0@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock);If I understand correctly your're saying that qp can be free'd on another/cpu timer right after dropping the locks. But how is it possible?
->refcnt is bumped above when arming the timer (before dropping chain lock), so even if the
frag_expiretimer fires instantly it should not free qp.
Quoted 8 lines
Quoted 5 lines
What am I missing?
Thanks,
FlorianAn important point is that
inet_frag_killremoves both the timer's refcnt and has an unconditionalatomic_decto remove the original/guarding refcnt, so it basically removes everything that's in the way.
You're right.
Problem is that when we return from inet_frag_intern() we can end up with a qp that is no longer in the hash (inet_frag_kill was invoked) but has been added to the lru list _after_ inet_frag_kill supposedly removed it.
The refcnt is not 0 (yet) by the time inet_frag_intern returns but it turns to 0 soon after on the next _put event.
Your fix makes 'in hash table but not on lru list' impossible and thus avoids the problem.
Thanks for explaining!
David Miller
maintainerFrom: Nikolay Aleksandrov <[email protected]>
Date: Mon, 3 Mar 2014 15:05:20 +0100
Quoted 32 lines
I stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between
inet_frag_evictor,inet_frag_internand the IPv4/6frag_queueand expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to thelru_list(inet_frag_lru_add), it may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by thefraq_queuefunction for different reasons) before it's added to thelru_list, then after it gets added it's a matter of time for the evictor to get to a piece of memory which has been freed leading to a number of different bugs depending on what's left there. I've been able to trigger this on both IPv4 and IPv6 (which is normal as the frag code is the same), but it's been much more difficult to trigger on IPv4 due to the protocol differences about how fragments are treated. The setup I used to reproduce this is:
2 machines with 4 x 10G bonded in a RR bond, so the same flow can be seen on multiple cards at the same time. Then I used multiple instances of ping/ping6 to generate fragmented packets and flood the machines with them while running other processes to load the attacked machine. *It is very important to have the_sameflow_coming in on multiple CPUs concurrently. Usually the attacked machine would die in less than 30 minutes, if configured properly to have many evictor calls and timeouts it could happen in 10 minutes or so.The fix is simple, just move the
lru_addunder the hash chain locked region so when a removing function is called it'll have to wait for the fragment to be added to thelru_list, and then it'll remove it (it works because the hash chain removal is done before thelru_listone and there's no window between the two list adds when the frag can get dropped). With this fix applied I couldn't kill the same machine in 24 hours with the same setup.
Quoted 7 lines
Fixes 3ef0eb0db4bf ("net: frag, move LRU list maintenance outside ofrwlock")
CC Jesper Dangaard Brouer <[email protected]>CC David S. Miller <[email protected]>Signed-off-by Nikolay Aleksandrov <[email protected]>
Nik, please beef up the commit message a bit and resubmit.
Some of your replies explained the situation a bit better. Don't be afraid of making the commit message too long or too verbose :-)
Thanks!
PATCH v2 1/1 net: fix for a race condition in the inet frag code
In reply to Nikolay AleksandrovI stumbled upon this very serious bug while hunting for another one, it's a very subtle race condition between inet_frag_evictor, inet_frag_intern and the IPv4/6 frag_queue and expire functions (basically the users of inet_frag_kill/inet_frag_put).
What happens is that after a fragment has been added to the hash chain but before it's been added to the lru_list (inet_frag_lru_add) in inet_frag_intern, may get deleted (either by an expired timer if the system load is high or the timer sufficiently low, or by the fraq_queue function for different reasons) before it's added to the lru_list, then after it gets added it's a matter of time for the evictor to get to a piece of memory which has been freed leading to a number of different bugs depending on what's left there. I've been able to trigger this on both IPv4 and IPv6 (which is normal as the frag code is the same), but it's been much more difficult to trigger on IPv4 due to the protocol differences about how fragments are treated. The setup I used to reproduce this is:
2 machines with 4 x 10G bonded in a RR bond, so the same flow can be seen on multiple cards at the same time. Then I used multiple instances of ping/ping6 to generate fragmented packets and flood the machines with them while running other processes to load the attacked machine. *It is very important to have the _same flow_ coming in on multiple CPUs concurrently. Usually the attacked machine would die in less than 30 minutes, if configured properly to have many evictor calls and timeouts it could happen in 10 minutes or so.
An important point to make is that any caller (frag_queue or timer) of inet_frag_kill will remove both the timer refcount and the original/guarding refcount thus removing everything that's keeping the frag from being freed at the next inet_frag_put.
All of this could happen before the frag was ever added to the LRU list, then it gets added and the evictor uses a freed fragment.
An example for IPv6 would be if a fragment is being added and is at the stage of being inserted in the hash after the hash lock is released, but before inet_frag_lru_add executes (or is able to obtain the lru lock) another overlapping fragment for the same flow arrives at a different CPU which finds it in the hash, but since it's overlapping it drops it invoking inet_frag_kill and thus removing all guarding refcounts, and afterwards freeing it by invoking inet_frag_put which removes the last refcount added previously by inet_frag_find, then inet_frag_lru_add gets executed by inet_frag_intern and we have a freed fragment in the lru_list.
The fix is simple, just move the lru_add under the hash chain locked region so when a removing function is called it'll have to wait for the fragment to be added to the lru_list, and then it'll remove it (it works because the hash chain removal is done before the lru_list one and there's no window between the two list adds when the frag can get dropped). With this fix applied I couldn't kill the same machine in 24 hours with the same setup.
rwlock")
---
v2: Add more information on the flow leading to the race and an example. I hope this is better :-)
net/ipv4/inet_fragment.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/inet_fragment.c +2 -1
@@ -278,9 +278,10 @@ static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,atomic_inc (&qp->refcnt);hlist_add_head (&qp->list, &hb->chain); +inet_frag_lru_add (nf, qp);spin_unlock (&hb->chain_lock);read_unlock (&f->lock); -inet_frag_lru_add (nf, qp); +return qp; }
Signature · 1 line
1.8.4.2
Quoted 2 lines · Nikolay Aleksandrov
v2: Add more information on the flow leading to the race and an example. I hope this is better :-)
It is, thanks Nik!
Quoted 11 lines · On Mon, 3 Mar 2014 23:19:18 +0100 Nikolay Aleksandrov <[email protected]>…
Fixes 3ef0eb0db4bf ("net: frag, move LRU list maintenance outside ofrwlock")
CC Florian Westphal <[email protected]>CC Jesper Dangaard Brouer <[email protected]>CC David S. Miller <[email protected]>Signed-off-by Nikolay Aleksandrov <[email protected]>---
v2: Add more information on the flow leading to the race and an example. I hope this is better :-)
Good work, thanks a lot for finding this race bug!
Signature · 5 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Sr. Network Kernel Developer at Red Hat Author of http://www.iptv-analyzer.org LinkedIn: http://www.linkedin.com/in/brouer
David Miller
maintainerFrom: Nikolay Aleksandrov <[email protected]>
Date: Mon, 3 Mar 2014 23:19:18 +0100
Quoted 10 lines
The fix is simple, just move the
lru_addunder the hash chain locked region so when a removing function is called it'll have to wait for the fragment to be added to thelru_list, and then it'll remove it (it works because the hash chain removal is done before thelru_listone and there's no window between the two list adds when the frag can get dropped). With this fix applied I couldn't kill the same machine in 24 hours with the same setup.Fixes 3ef0eb0db4bf ("net: frag, move LRU list maintenance outside ofrwlock")
Applied and queued up for -stable, thanks.