Patchwork [3/5] netfilter: nf_nat: fix locking in nf_nat_seq_adjust()

login
register
mail settings
Submitter Patrick McHardy
Date July 28, 2013, 8:54 p.m.
Message ID <1375044851-12471-4-git-send-email-kaber@trash.net>
Download mbox | patch
Permalink /patch/262620/
State Accepted
Headers show

Comments

Patrick McHardy - July 28, 2013, 8:54 p.m.
nf_nat_seq_adjust() needs to grab nf_nat_setofs_lock to protect against
concurrent changes to the sequence adjustment data.

Signed-off-by: Patrick McHardy <kaber@trash.net>
---
 net/netfilter/nf_nat_helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Pablo Neira - July 31, 2013, 4:55 p.m.
On Sun, Jul 28, 2013 at 10:54:09PM +0200, Patrick McHardy wrote:
> nf_nat_seq_adjust() needs to grab nf_nat_setofs_lock to protect against
> concurrent changes to the sequence adjustment data.

Applied, thanks.

I have enqueued this to nf so it hits 3.11. I have to wait to take 4/5
and 5/5 for a while until this patch hits Linus tree.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Patrick McHardy - July 31, 2013, 5:51 p.m.
On Wed, Jul 31, 2013 at 06:55:23PM +0200, Pablo Neira Ayuso wrote:
> On Sun, Jul 28, 2013 at 10:54:09PM +0200, Patrick McHardy wrote:
> > nf_nat_seq_adjust() needs to grab nf_nat_setofs_lock to protect against
> > concurrent changes to the sequence adjustment data.
> 
> Applied, thanks.
> 
> I have enqueued this to nf so it hits 3.11. I have to wait to take 4/5
> and 5/5 for a while until this patch hits Linus tree.

We've had this problem since the beginning of netfilter, so I think
this can go to -next. So far I haven't seen any bugreports related to
this, but its clearly wrong.

A follow up patch will move the entire sequence number adjustment data
to an extend, so the size increase will only affect the quite rare case
of connections using a helper.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pablo Neira - July 31, 2013, 5:57 p.m.
On Wed, Jul 31, 2013 at 07:51:18PM +0200, Patrick McHardy wrote:
> On Wed, Jul 31, 2013 at 06:55:23PM +0200, Pablo Neira Ayuso wrote:
> > On Sun, Jul 28, 2013 at 10:54:09PM +0200, Patrick McHardy wrote:
> > > nf_nat_seq_adjust() needs to grab nf_nat_setofs_lock to protect against
> > > concurrent changes to the sequence adjustment data.
> > 
> > Applied, thanks.
> > 
> > I have enqueued this to nf so it hits 3.11. I have to wait to take 4/5
> > and 5/5 for a while until this patch hits Linus tree.
> 
> We've had this problem since the beginning of netfilter, so I think
> this can go to -next. So far I haven't seen any bugreports related to
> this, but its clearly wrong.

Fair enough, will enqueue this to -next then.

> A follow up patch will move the entire sequence number adjustment data
> to an extend, so the size increase will only affect the quite rare case
> of connections using a helper.

Great to know, thanks.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/net/netfilter/nf_nat_helper.c b/net/netfilter/nf_nat_helper.c
index 85e20a9..a7262ed 100644
--- a/net/netfilter/nf_nat_helper.c
+++ b/net/netfilter/nf_nat_helper.c
@@ -373,6 +373,7 @@  nf_nat_seq_adjust(struct sk_buff *skb,
 	s16 seqoff, ackoff;
 	struct nf_conn_nat *nat = nfct_nat(ct);
 	struct nf_nat_seq *this_way, *other_way;
+	int res;
 
 	dir = CTINFO2DIR(ctinfo);
 
@@ -383,6 +384,7 @@  nf_nat_seq_adjust(struct sk_buff *skb,
 		return 0;
 
 	tcph = (void *)skb->data + protoff;
+	spin_lock_bh(&nf_nat_seqofs_lock);
 	if (after(ntohl(tcph->seq), this_way->correction_pos))
 		seqoff = this_way->offset_after;
 	else
@@ -407,7 +409,10 @@  nf_nat_seq_adjust(struct sk_buff *skb,
 	tcph->seq = newseq;
 	tcph->ack_seq = newack;
 
-	return nf_nat_sack_adjust(skb, protoff, tcph, ct, ctinfo);
+	res = nf_nat_sack_adjust(skb, protoff, tcph, ct, ctinfo);
+	spin_unlock_bh(&nf_nat_seqofs_lock);
+
+	return res;
 }
 
 /* Setup NAT on this expected conntrack so it follows master. */