Skip to contentSkip to conversation

OSF: code beautification.

PATCH New Pavel A. Nekrasov 2 participants 7 messages May 31, 2005 – Jun 1, 2005 netfilter

lore
Share & tools
lore
#0 lore raw

hello!

Review line 185

Quoted 8 lines
(quoted diff) +1 -2
@@ -186,12 +181,10 @@
 	df = ((ntohs(ip->frag_off) & IP_DF)?1:0);
 	window = ntohs(tcp->window);
 
-	if (tcp->doff*4 > sizeof(struct tcphdr))
-	{
+	if (tcp->doff*4 > sizeof(struct tcphdr)) {
 		optsize = tcp->doff*4 - sizeof(struct tcphdr);

This won't work with fragments, you consider here that the tcp header is linear and that isn't always true.

BTW, fix a deadlock. match() can be called from both bh and process context.

kind regards,
Pavel

#1 lore raw
Quoted 6 lines · On Wed, 1 Jun 2005 00:37:05 +0200 "Pavel A. Nekrasov" <[email protected]> wrote:

hello!

Quoted 4 lines
(quoted diff) +0 -0
@@ -186,12 +181,10 @@
 	df = ((ntohs(ip->frag_off) & IP_DF)?1:0);
 	window = ntohs(tcp->window);
 
Quoted 7 lines

Review line 185

Quoted 4 lines
(quoted diff) +1 -2
-	if (tcp->doff*4 > sizeof(struct tcphdr))
-	{
+	if (tcp->doff*4 > sizeof(struct tcphdr)) {
 		optsize = tcp->doff*4 - sizeof(struct tcphdr);

This won't work with fragments, you consider here that the tcp header is linear and that isn't always true.

It does.
Ip, tcp and options are obtained using fragment aware methods.

Quoted 1 line

BTW, fix a deadlock. match() can be called from both bh and process context.

Neither it suffers rcu lock in nf_hook_slow()?

Quoted 2 lines

kind regards,
Pavel

Evgeniy Polyakov

Only failure makes us experts. -- Theo de Raadt

#2 lore raw
Quoted 2 lines · On Wed, 1 Jun 2005 02:57:31 +0400 Evgeniy Polyakov <[email protected]> wrote:

On Wed, 1 Jun 2005 00:37:05 +0200
"Pavel A. Nekrasov" <[email protected]> wrote:

...

Quoted 3 lines
Quoted 1 line

BTW, fix a deadlock. match() can be called from both bh and process context.

Neither it suffers rcu lock in nf_hook_slow()?

Ugh, it is read lock, but OSF match() method can be safely interrupted and reentered. BH are disabled when broadcasting over netlink, reading finger table is guarded by read_lock, while modification can be done only in process context which uses write_lock_bh().

Evgeniy Polyakov

Only failure makes us experts. -- Theo de Raadt

#6 lore raw

cover OSF: resurect skb fragmentation patch.

In reply to Pavel A. Nekrasov

Patch for fragmented skb was applied half of a year ago against 2.4 sources instead of 2.6.
This one should be applied before others 4 in [*/3] OSF: series.

Thanks to Pavel Nekrasov for pointer.

osf/linux-2.6/net/ipv4/netfilter/ipt_osf.c +25 -7
@@ -158,20 +158,30 @@
       int *hotdrop)
 {
 	struct ipt_osf_info *info = (struct ipt_osf_info *)matchinfo;
-	struct iphdr *ip = skb->nh.iph;
-	struct tcphdr *tcp;
+	struct iphdr _iph, *ip;
+	struct tcphdr _tcph, *tcp;
 	int fmatch = FMATCH_WRONG, fcount = 0;
 	unsigned long totlen, optsize = 0, window;
 	unsigned char df, *optp = NULL, *_optp = NULL;
+	unsigned char opts[MAX_IPOPTLEN];
 	char check_WSS = 0;
 	struct list_head *ent;
 	struct osf_finger *f;
+	int off;
 
-	if (!ip || !info)
+	if (!info)
+		return 0;
+	
+	off = 0;
+	
+	ip = skb_header_pointer(skb, off, sizeof(_iph), &_iph);
+	if (!ip)
 		return 0;
 				
-	tcp = (struct tcphdr *)((u_int32_t *)ip + ip->ihl);
-
+	tcp = skb_header_pointer(skb, off + ip->ihl * 4, sizeof(_tcph), &_tcph);
+	if (!tcp)
+		return 0;
+	
 	if (!tcp->syn)
 		return 0;
 	
@@ -181,8 +191,16 @@
 	
 	if (tcp->doff*4 > sizeof(struct tcphdr))
 	{
-		_optp = optp = (char *)(tcp+1);
 		optsize = tcp->doff*4 - sizeof(struct tcphdr);
+
+		if (optsize > sizeof(opts))
+		{
+			log("%s: BUG: too big options size: optsize=%lu, max=%d.\n",
+					__func__, optsize, sizeof(opts));
+			optsize = sizeof(opts);
+		}
+		
+		_optp = optp = skb_header_pointer(skb, off + ip->ihl*4 + sizeof(_tcph), optsize, opts);
 	}
 
 	/* Actually we can create hash/table of all genres and search
@@ -375,7 +393,7 @@
 		if (optp)
 		{
 			optsize = tcp->doff * 4 - sizeof(struct tcphdr);
-			if (skb_copy_bits(skb, ip->ihl*4 + sizeof(struct tcphdr),
+			if (skb_copy_bits(skb, off + ip->ihl*4 + sizeof(struct tcphdr),
 					  opt, optsize) < 0)
 			{
 				if (info->flags & IPT_OSF_LOG)

Evgeniy Polyakov

Only failure makes us experts. -- Theo de Raadt

#3 lore raw
In reply to Evgeniy Polyakov

it seems that I forgot to reply to the mailling list...

Quoted 8 lines · Evgeniy Polyakov
Quoted 2 lines · On Wed, 1 Jun 2005 02:57:31 +0400 Evgeniy Polyakov <[email protected]> wrote:

On Wed, 1 Jun 2005 00:37:05 +0200
"Pavel A. Nekrasov" <[email protected]> wrote:

...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

BTW, fix a deadlock. match() can be called from both bh and process context.

Neither it suffers rcu lock in nf_hook_slow()?

Ugh, it is read lock, but OSF match() method can be safely interrupted and reentered. BH are disabled when broadcasting over netlink, reading finger table is guarded by read_lock, while modification can be done only in process context which uses write_lock_bh().

not talking about netlink

say a program does send() running on you machine, so some packets can hit match() from process context. While holding read lock, match() is interrupted to handle a bh. now say, a packet coming from bh context hits match() and tries to gets read lock but it's already held by the packet coming from process context. deadlock.

kind regards,
Pavel

#4 lore raw
Quoted 20 lines · On Wed, 1 Jun 2005 02:03:28 +0200 "Pavel A. Nekrasov" <[email protected]> wrote:

it seems that I forgot to reply to the mailling list...

Quoted 8 lines · Evgeniy Polyakov
Quoted 2 lines · On Wed, 1 Jun 2005 02:57:31 +0400 Evgeniy Polyakov <[email protected]> wrote:

On Wed, 1 Jun 2005 00:37:05 +0200
"Pavel A. Nekrasov" <[email protected]> wrote:

...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

BTW, fix a deadlock. match() can be called from both bh and process context.

Neither it suffers rcu lock in nf_hook_slow()?

Ugh, it is read lock, but OSF match() method can be safely interrupted and reentered. BH are disabled when broadcasting over netlink, reading finger table is guarded by read_lock, while modification can be done only in process context which uses write_lock_bh().

Quoted 7 lines

not talking about netlink

say a program does send() running on you machine, so some packets can hit match() from process context. While holding read lock, match() is interrupted to handle a bh. now say, a packet coming from bh context hits match() and tries to gets read lock but it's already held by the packet coming from process context. deadlock.

It is read lock, which can be held by several users.

Modification part, which lives in osf_proc_write(), uses write_lock_bh().

Quoted 2 lines

kind regards,
Pavel

Evgeniy Polyakov

Only failure makes us experts. -- Theo de Raadt

#5 lore raw
Quoted 31 lines · Evgeniy Polyakov
Quoted 20 lines · On Wed, 1 Jun 2005 02:03:28 +0200 "Pavel A. Nekrasov" <[email protected]> wrote:

it seems that I forgot to reply to the mailling list...

Quoted 8 lines · Evgeniy Polyakov
Quoted 2 lines · On Wed, 1 Jun 2005 02:57:31 +0400 Evgeniy Polyakov <[email protected]> wrote:

On Wed, 1 Jun 2005 00:37:05 +0200
"Pavel A. Nekrasov" <[email protected]> wrote:

...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

BTW, fix a deadlock. match() can be called from both bh and process context.

Neither it suffers rcu lock in nf_hook_slow()?

Ugh, it is read lock, but OSF match() method can be safely interrupted and reentered. BH are disabled when broadcasting over netlink, reading finger table is guarded by read_lock, while modification can be done only in process context which uses write_lock_bh().

Quoted 7 lines

not talking about netlink

say a program does send() running on you machine, so some packets can hit match() from process context. While holding read lock, match() is interrupted to handle a bh. now say, a packet coming from bh context hits match() and tries to gets read lock but it's already held by the packet coming from process context. deadlock.

Quoted 3 lines

It is read lock, which can be held by several users.

Modification part, which lives in osf_proc_write(), uses write_lock_bh().

You are right, sorry for the noise.

kind regards,
Pavel