LKML Archive mirror
 help / color / mirror / Atom feed
* potential null pointer dereference in drivers/isdn/hisax/isdnl2.c
@ 2011-02-03 20:27 Jesper Juhl
  2011-02-14  0:53 ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Jesper Juhl @ 2011-02-03 20:27 UTC (permalink / raw
  To: linux-kernel; +Cc: netdev, Tejun Heo, Karsten Keil

Hi,

In drivers/isdn/hisax/isdnl2.c:l2_pull_iqueue() we have this:

	...
		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
		memcpy(skb_put(skb, i), header, i);
	...

If alloc_skb() fails and returns NULL then the second line will cause a 
NULL pointer dereference - skb_put() gives the pointer to 
skb_tail_pointer() which dereferences it.

I'm not quite sure how this should be dealt with, so I'll just report it 
rather than submit a patch. Happy bug fixing :-)


-- 
Jesper Juhl <jj@chaosbits.net>            http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: potential null pointer dereference in drivers/isdn/hisax/isdnl2.c
  2011-02-03 20:27 potential null pointer dereference in drivers/isdn/hisax/isdnl2.c Jesper Juhl
@ 2011-02-14  0:53 ` David Miller
  2011-02-15 20:09   ` Milton Miller
  0 siblings, 1 reply; 4+ messages in thread
From: David Miller @ 2011-02-14  0:53 UTC (permalink / raw
  To: jj; +Cc: linux-kernel, netdev, tj, isdn

From: Jesper Juhl <jj@chaosbits.net>
Date: Thu, 3 Feb 2011 21:27:56 +0100 (CET)

> In drivers/isdn/hisax/isdnl2.c:l2_pull_iqueue() we have this:
> 
> 	...
> 		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
> 		memcpy(skb_put(skb, i), header, i);
> 	...
> 
> If alloc_skb() fails and returns NULL then the second line will cause a 
> NULL pointer dereference - skb_put() gives the pointer to 
> skb_tail_pointer() which dereferences it.
> 
> I'm not quite sure how this should be dealt with, so I'll just report it 
> rather than submit a patch. Happy bug fixing :-)

Thanks Jesper, I'll fix this like so:

--------------------
hisax: Fix unchecked alloc_skb() return.

Jesper Juhl noticed that l2_pull_iqueue() does not
check to see if alloc_skb() fails.

Fix this by first trying to reallocate the headroom
if necessary, rather than later after we've made hard
to undo state changes.

Reported-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
---
 drivers/isdn/hisax/isdnl2.c |   35 ++++++++++++++++++++---------------
 1 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/drivers/isdn/hisax/isdnl2.c b/drivers/isdn/hisax/isdnl2.c
index 0858791..98ac835 100644
--- a/drivers/isdn/hisax/isdnl2.c
+++ b/drivers/isdn/hisax/isdnl2.c
@@ -1243,14 +1243,21 @@ l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
 	st->l2.rc = 0;
 }
 
+static int l2_hdr_space_needed(struct Layer2 *l2)
+{
+	int len = test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
+
+	return len + (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
+}
+
 static void
 l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
 {
 	struct PStack *st = fi->userdata;
-	struct sk_buff *skb, *oskb;
+	struct sk_buff *skb;
 	struct Layer2 *l2 = &st->l2;
 	u_char header[MAX_HEADER_LEN];
-	int i;
+	int i, hdr_space_needed;
 	int unsigned p1;
 	u_long flags;
 
@@ -1261,6 +1268,16 @@ l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
 	if (!skb)
 		return;
 
+	hdr_space_needed = l2_hdr_space_needed(l2);
+	if (hdr_space_needed > skb_headroom(skb)) {
+		struct sk_buff *orig_skb = skb;
+
+		skb = skb_realloc_headroom(skb, hdr_space_needed);
+		if (!skb) {
+			dev_kfree_skb(orig_skb);
+			return;
+		}
+	}
 	spin_lock_irqsave(&l2->lock, flags);
 	if(test_bit(FLG_MOD128, &l2->flag))
 		p1 = (l2->vs - l2->va) % 128;
@@ -1285,19 +1302,7 @@ l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
 		l2->vs = (l2->vs + 1) % 8;
 	}
 	spin_unlock_irqrestore(&l2->lock, flags);
-	p1 = skb->data - skb->head;
-	if (p1 >= i)
-		memcpy(skb_push(skb, i), header, i);
-	else {
-		printk(KERN_WARNING
-		"isdl2 pull_iqueue skb header(%d/%d) too short\n", i, p1);
-		oskb = skb;
-		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
-		memcpy(skb_put(skb, i), header, i);
-		skb_copy_from_linear_data(oskb,
-					  skb_put(skb, oskb->len), oskb->len);
-		dev_kfree_skb(oskb);
-	}
+	memcpy(skb_push(skb, i), header, i);
 	st->l2.l2l1(st, PH_PULL | INDICATION, skb);
 	test_and_clear_bit(FLG_ACK_PEND, &st->l2.flag);
 	if (!test_and_set_bit(FLG_T200_RUN, &st->l2.flag)) {
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: potential null pointer dereference in drivers/isdn/hisax/isdnl2.c
  2011-02-14  0:53 ` David Miller
@ 2011-02-15 20:09   ` Milton Miller
  2011-02-15 20:15     ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Milton Miller @ 2011-02-15 20:09 UTC (permalink / raw
  To: David Miller; +Cc: linux-kernel, netdev, tj, isdn, jj

On Mon, 14 Feb 2011 00:53:09 -0000, Dave Miler wrote:
> From: Jesper Juhl <jj@chaosbits.net>
> 
> > In drivers/isdn/hisax/isdnl2.c:l2_pull_iqueue() we have this:
> > 
> > 	...
> > 		skb = alloc_skb(oskb->len + i, GFP_ATOMIC);
> > 		memcpy(skb_put(skb, i), header, i);
> > 	...
> > 
> > If alloc_skb() fails and returns NULL then the second line will cause a 
> > NULL pointer dereference - skb_put() gives the pointer to 
> > skb_tail_pointer() which dereferences it.
> > 
> > I'm not quite sure how this should be dealt with, so I'll just report it 
> > rather than submit a patch. Happy bug fixing :-)
> 
> Thanks Jesper, I'll fix this like so:
> 
> --------------------
> hisax: Fix unchecked alloc_skb() return.
> 
> Jesper Juhl noticed that l2_pull_iqueue() does not
> check to see if alloc_skb() fails.
> 
> Fix this by first trying to reallocate the headroom
> if necessary, rather than later after we've made hard
> to undo state changes.
> 
> Reported-by: Jesper Juhl <jj@chaosbits.net>
> Signed-off-by: David S. Miller <davem@davemloft.net>
> 
> ---
> drivers/isdn/hisax/isdnl2.c |   35 ++++++++++++++++++++---------------
>  1 files changed, 20 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/isdn/hisax/isdnl2.c b/drivers/isdn/hisax/isdnl2.c
> index 0858791..98ac835 100644
> --- a/drivers/isdn/hisax/isdnl2.c
> +++ b/drivers/isdn/hisax/isdnl2.c
> @@ -1243,14 +1243,21 @@ l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
>  	st->l2.rc = 0;
>  }
>  
> +static int l2_hdr_space_needed(struct Layer2 *l2)
> +{
> +	int len = test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
> +
> +	return len + (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
> +}
> +

That struck me as an funny way to write 2 * len, so I finally looked
at the code.  I think one of those should be FLG_MOD128, but then
at that point why not use the existing l2headersize(l2, ui) with
ui = 0?

I see this is in linux-next of Feb 15, 2011.

milton

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: potential null pointer dereference in drivers/isdn/hisax/isdnl2.c
  2011-02-15 20:09   ` Milton Miller
@ 2011-02-15 20:15     ` David Miller
  0 siblings, 0 replies; 4+ messages in thread
From: David Miller @ 2011-02-15 20:15 UTC (permalink / raw
  To: miltonm; +Cc: linux-kernel, netdev, tj, isdn, jj

From: Milton Miller <miltonm@bga.com>
Date: Tue, 15 Feb 2011 14:09:16 -0600

> On Mon, 14 Feb 2011 00:53:09 -0000, Dave Miler wrote:
>> diff --git a/drivers/isdn/hisax/isdnl2.c b/drivers/isdn/hisax/isdnl2.c
>> index 0858791..98ac835 100644
>> --- a/drivers/isdn/hisax/isdnl2.c
>> +++ b/drivers/isdn/hisax/isdnl2.c
>> @@ -1243,14 +1243,21 @@ l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
>>  	st->l2.rc = 0;
>>  }
>>  
>> +static int l2_hdr_space_needed(struct Layer2 *l2)
>> +{
>> +	int len = test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
>> +
>> +	return len + (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
>> +}
>> +
> 
> That struck me as an funny way to write 2 * len, so I finally looked
> at the code.  I think one of those should be FLG_MOD128, but then
> at that point why not use the existing l2headersize(l2, ui) with
> ui = 0?
> 
> I see this is in linux-next of Feb 15, 2011.

Good catch, thanks.  I'll fix this.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-02-15 20:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-03 20:27 potential null pointer dereference in drivers/isdn/hisax/isdnl2.c Jesper Juhl
2011-02-14  0:53 ` David Miller
2011-02-15 20:09   ` Milton Miller
2011-02-15 20:15     ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).