Skip to contentSkip to conversation

staging: Check for Null return of allocated skb in fw_download_code

PATCH New Nicholas Krause 7 participants 19 messages Aug 13, 2014 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

This patch checks if we are getting a Null allocated skb in the while/do loop of this function. Further more no reference checking is needing as skb_put is only working with the packet defined by skb and not allocating a new one. Further more I am breaking out of the loop in order to make sure we call nic_write_byte and cause no errors with writing the nic byte(s) of private date and then call rt_status which I changed to false in the loop with my additon of checking the allocated skb.

---
 drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c +4 -0
index 1a95d1f..829af66 100644
@@ -61,6 +61,10 @@ static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address,
 		}
 
 		skb  = dev_alloc_skb(frag_length + 4);
+		if (skb == NULL) {
+			rt_status =  false;
+			break;
+		}
 		memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
 		tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
 		tcb_desc->queue_index = TXCMD_QUEUE;
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 32 lines · Nicholas Krause

This patch checks if we are getting a Null allocated skb in the while/do loop of this function. Further more no reference checking is needing as skb_put is only working with the packet defined by skb and not allocating a new one. Further more I am breaking out of the loop in order to make sure we call nic_write_byte and cause no errors with writing the nic byte(s) of private date and then call rt_status which I changed to false in the loop with my additon of checking the allocated skb.

---
 drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c +4 -1
index 1a95d1f..829af66 100644
@@ -61,6 +61,10 @@ static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address,
                }
 
                skb  = dev_alloc_skb(frag_length + 4);
+               if (skb == NULL) {
+                       rt_status =  false;
+                       break;
+               }
                memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
                tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
                tcb_desc->queue_index = TXCMD_QUEUE;
--

1.9.1

Quoted 4 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Sorry about the two patches please read only the second one, I send twice. Nick

#2 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

In reply to Nicholas Krause

drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address, · line 67

Quoted 7 lines · Nicholas Krause

This patch checks if we are getting a Null allocated skb in the while/do loop of this function.

drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c +4 -0
 		skb  = dev_alloc_skb(frag_length + 4);
+		if (skb == NULL) {
+			rt_status =  false;
+			break;
+		}

Nick, it's *STILL* wrong. And although I admit I blew it on the refcount issue, that doesn't change the fact that you're still leaking memory here.

Work this through. Hand simulate it. Pretend that 3 fragments are needed, and that the first two succeed but the third one fails.

Who frees the first two fragments before you return?

For bonus points - explain under what conditions this bug can *possibly* be triggered on an actual system. Consider in your reply both (a) when this code is called and (b) what the system state *should* be at that point, and what it has to be for this bug to trigger. Given your answer to (a) and (b), extrapolate to what the next few hundredths of a second likely hold for this system even after we've fixed the bug you're mis-fixing here.

Once you've done that, you'll hopefully understand why you're trying to fix a bug from 2009.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/5b0c1af4/attachment.bin

#3 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 26 lines · On Tue, Aug 12, 2014 at 10:35 PM, <[email protected]> wrote:

drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address, · line 67

Quoted 7 lines · Nicholas Krause

This patch checks if we are getting a Null allocated skb in the while/do loop of this function.

              skb  = dev_alloc_skb(frag_length + 4);
drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c +4 -0
+             if (skb == NULL) {
+                     rt_status =  false;
+                     break;
+             }

Nick, it's *STILL* wrong. And although I admit I blew it on the refcount issue, that doesn't change the fact that you're still leaking memory here.

Work this through. Hand simulate it. Pretend that 3 fragments are needed, and that the first two succeed but the third one fails.

Who frees the first two fragments before you return?

For bonus points - explain under what conditions this bug can *possibly* be triggered on an actual system. Consider in your reply both (a) when this
code is called and (b) what the system state *should* be at that point, and what it has to be for this bug to trigger. Given your answer to (a) and (b),
extrapolate to what the next few hundredths of a second likely hold for this system even after we've fixed the bug you're mis-fixing here.

Quoted 4 lines

Once you've done that, you'll hopefully understand why you're trying to fix a bug from 2009.

And it may also be a good idea to post the logs regarding how you tested your patch and verified that the fix works as expected. As you are asking someone else to send the patch on behalf of you, anyone who volunteers would like to verify that the patch is indeed tested properly before forwarding.

Thanks -
Manish

Quoted 7 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140812/e9da9a4e/attachment.html

#4 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 2 lines · Manish Katiyar

And it may also be a good idea to post the logs regarding how you tested your patch and verified that the fix works as expected. As you are asking

I want to see how he sets up an environment where he can *trigger* the issue
reliably. :)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/c37064cd/attachment.bin

#5 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 7 lines · On Wed, Aug 13, 2014 at 5:56 PM, <[email protected]> wrote:
Quoted 2 lines · Manish Katiyar

And it may also be a good idea to post the logs regarding how you tested your patch and verified that the fix works as expected. As you are asking

I want to see how he sets up an environment where he can *trigger* the issue reliably. :)

No need to trigger it, faking it would be enough, e.g.:

- skb = dev_alloc_skb(frag_length + 4);
+ {
+ static int i;
+ if (++i < 3)
+ skb = dev_alloc_skb(frag_length + 4);
+ else
+ skb = NULL;
+ }
+ if (skb == NULL) {
+ rt_status = false;
+ break;
+ }

Signature · 2 lines
Thanks.
-- Max
#6 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Review

Quoted 6 lines · Max Filippov

No need to trigger it, faking it would be enough, e.g.:

(quoted diff) +4 -0
+                if (++i < 3)
+                        skb  = dev_alloc_skb(frag_length + 4);
+                else
+                        skb = NULL;

Don't bet on this triggering on a real system without some additional
scaffolding - take a look at what the function is doing, and ask yourself
how many times it will be called on the average system :)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/645e13da/attachment.bin

#7 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 4 lines · On Wed, Aug 13, 2014 at 12:02 PM, <[email protected]> wrote:
Quoted 1 line · Max Filippov

No need to trigger it, faking it would be enough, e.g.:

Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+                if (++i < 3)
+                        skb  = dev_alloc_skb(frag_length + 4);
+                else
+                        skb = NULL;

Don't bet on this triggering on a real system without some additional scaffolding - take a look at what the function is doing, and ask yourself how many times it will be called on the average system :)

Seems to be called a lot. In addition I can only build test this as I don't have the hardware.
Cheers Nick

#8 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 10 lines · Nick Krause
Quoted 9 lines · On Wed, Aug 13, 2014 at 12:02 PM, <[email protected]> wrote:
Quoted 6 lines · Max Filippov

No need to trigger it, faking it would be enough, e.g.:

(quoted diff) +4 -0
+                if (++i < 3)
+                        skb  = dev_alloc_skb(frag_length + 4);
+                else
+                        skb = NULL;
Quoted 6 lines
Quoted 3 lines

Don't bet on this triggering on a real system without some additional scaffolding - take a look at what the function is doing, and ask yourself how many times it will be called on the average system :)

Seems to be called a lot. In addition I can only build test this as I don't have the hardware.
Cheers Nick

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix
this out and send a proper patch :).
By the way thanks for the help guys :).
Nick
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-staging-Check-for-Null-return-of-fw_download_code.patch
Type: text/x-patch
Size: 1380 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/2e6775dd/attachment.bin

#9 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Am 2014-08-13 20:03, schrieb Nick Krause:

Quoted 27 lines · Nick Krause
Quoted 8 lines · On Wed, Aug 13, 2014 at 12:02 PM, <[email protected]> wrote:
Quoted 7 lines · Max Filippov
Quoted 6 lines

No need to trigger it, faking it would be enough, e.g.:

(quoted diff) +4 -0
+                if (++i < 3)
+                        skb  = dev_alloc_skb(frag_length + 4);
+                else
+                        skb = NULL;
Quoted 6 lines
Quoted 3 lines

Don't bet on this triggering on a real system without some additional scaffolding - take a look at what the function is doing, and ask yourself how many times it will be called on the average system :)

Seems to be called a lot. In addition I can only build test this as I don't have the hardware.
Cheers Nick

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function 'skb_quene_purge' [-Werror=implicit-function-declaration]

     skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix this out and send a proper patch :).
By the way thanks for the help guys :).
Nick

Quoted 4 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

This error indicates that you are using a function that is not yet known to the compiler. Check if there's a prototype or function definition BEFORE it's actual use.

br,
phil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/02b2ac73/attachment.html

#10 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

In reply to Nick Krause
Quoted 7 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix this out and send a proper patch :).

Typo, skb_queue_purge.

Signature · 2 lines
Thanks.
-- Max
#11 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 11 lines · Max Filippov
Quoted 7 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix this out and send a proper patch :).

Typo, skb_queue_purge.

I imagine that array index isn't going to go over very well.

Jerry

#12 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 13 lines · Jerry Snitselaar
Quoted 11 lines · Max Filippov
Quoted 7 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix this out and send a proper patch :).

Typo, skb_queue_purge.

Quoted 8 lines

I imagine that array index isn't going to go over very well.

Jerry

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

I known the patch is premature but a NULL pointer and in a device driver needs to be patched no matter what.
If some one reading this has the hardware , it would be great if they can test this :).
Cheers Nick

#13 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 23 lines · Nick Krause
Quoted 13 lines · Jerry Snitselaar
Quoted 11 lines · Max Filippov
Quoted 7 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

I am wondering how do I fix this, I will attach my patch so I can fix this out and send a proper patch :).

Typo, skb_queue_purge.

Quoted 8 lines

I imagine that array index isn't going to go over very well.

Jerry

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Quoted 5 lines

I known the patch is premature but a NULL pointer and in a device driver needs to be patched no matter what.
If some one reading this has the hardware , it would be great if they can test this :).
Cheers Nick

Also just checked and the patch is fixed and builds at least from my testing. Nick

#14 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

In reply to Nick Krause
Quoted 12 lines

-----Original Message-----
From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies- bounces at kernelnewbies.org] On Behalf Of Nick Krause
Sent: Wednesday, August 13, 2014 11:03 AM
To: Valdis Kletnieks
Cc: Max Filippov; kernelnewbies; Manish Katiyar
Subject: Re: [PATCH] staging: Check for Null return of allocated skb in fw_download_code

Quoted 3 lines

Seems to be called a lot. In addition I can only build test this as I don't have the hardware.
Cheers Nick

This code is a device driver. I may be an old fuddy-duddy in this regard, but I don't inflict my code on others until I've tested it.

If you don't have the hardware, you can't have tested it. I think your patch is thus premature.

If you are unable to obtain the hardware for testing, you might want to consider working on parts of the kernel that are hardware independent.

Jeff Haran

#15 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

In reply to Nick Krause
Quoted 5 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ???skb_quene_purge??? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

Well, this is a C 101 problem.

Quoted 1 line

I am wondering how do I fix this,

You fix this by not doing any further kernel hacking until you've gotten a handle on *VERY BASIC* C development concepts.

Also, you're going to have to justify why you're being a total dumbass and coding skb_waitQ[tcb_desc->queue_index] when the *obvious* code is skb_waitQ[TXCMD_QUEUE] - yes, an optimizing compiler will do that substitution, but code clarity is important.

Argh. I may have to break out my +5 Trout of Smacking....

Quoted 2 lines · Nick Krause

This is the fixed patch, I do get an error about uninitialized variables

Are you *trying* to get put in *everybody's* killfile?

Quoted 2 lines

If someone wants to send this out, please do so as this is has been built and applied tested.

And you think that *anybody* wants to upstream a patch from you that even
*you* admit still has trouble??!?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/d83ebb86/attachment.bin

#16 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 26 lines · On Wed, Aug 13, 2014 at 3:17 PM, <[email protected]> wrote:
Quoted 5 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

Well, this is a C 101 problem.

Quoted 1 line

I am wondering how do I fix this,

You fix this by not doing any further kernel hacking until you've gotten a handle on *VERY BASIC* C development concepts.

Also, you're going to have to justify why you're being a total dumbass and coding skb_waitQ[tcb_desc->queue_index] when the *obvious* code is skb_waitQ[TXCMD_QUEUE] - yes, an optimizing compiler will do that substitution, but code clarity is important.

Argh. I may have to break out my +5 Trout of Smacking....

Quoted 2 lines · Nick Krause

This is the fixed patch, I do get an error about uninitialized variables

Quoted 7 lines

Are you *trying* to get put in *everybody's* killfile?

Quoted 2 lines

If someone wants to send this out, please do so as this is has been built and applied tested.

And you think that *anybody* wants to upstream a patch from you that even *you* admit still has trouble??!?

I checked the patched and it worked , uninitialized variables are common and I hit 12 of them.
Nick

#17 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

Quoted 32 lines · Nick Krause
Quoted 31 lines · On Wed, Aug 13, 2014 at 3:17 PM, <[email protected]> wrote:
Quoted 5 lines · Nick Krause

I did test my patch by doing a kernel build and I get this error, drivers/staging/rtl8192e/rtl8192e/r8192E_firmware.c:66:4: error:
implicit declaration of function ?skb_quene_purge? [-Werror=implicit-function-declaration]

    skb_quene_purge(&priv->rtllib->skb_waitQ[tcb_desc->queue_index]);

Well, this is a C 101 problem.

Quoted 1 line

I am wondering how do I fix this,

You fix this by not doing any further kernel hacking until you've gotten a handle on *VERY BASIC* C development concepts.

Also, you're going to have to justify why you're being a total dumbass and coding skb_waitQ[tcb_desc->queue_index] when the *obvious* code is skb_waitQ[TXCMD_QUEUE] - yes, an optimizing compiler will do that substitution, but code clarity is important.

Argh. I may have to break out my +5 Trout of Smacking....

Quoted 2 lines · Nick Krause

This is the fixed patch, I do get an error about uninitialized variables

Are you *trying* to get put in *everybody's* killfile?

Quoted 2 lines

If someone wants to send this out, please do so as this is has been built and applied tested.

Quoted 6 lines
Quoted 2 lines

And you think that *anybody* wants to upstream a patch from you that even *you* admit still has trouble??!?

I checked the patched and it worked , uninitialized variables are common and I hit 12 of them.
Nick

Valdis,
Sorry about the stupid question , I assumed it was something else I was fixing. In addition I fixed up the patch and there are not more errors based on using gcc's optimization features.

Sorry Nick :(

#18 lore raw

PATCH 1/1 staging: Check for Null return of allocated skb in fw_download_code

In reply to Nick Krause
Quoted 10 lines · Nick Krause
Quoted 9 lines · On Wed, Aug 13, 2014 at 12:02 PM, <[email protected]> wrote:
Quoted 6 lines · Max Filippov

No need to trigger it, faking it would be enough, e.g.:

(quoted diff) +4 -0
+                if (++i < 3)
+                        skb  = dev_alloc_skb(frag_length + 4);
+                else
+                        skb = NULL;
Quoted 5 lines
Quoted 3 lines

Don't bet on this triggering on a real system without some additional scaffolding - take a look at what the function is doing, and ask yourself how many times it will be called on the average system :)

Seems to be called a lot.

Oh? Explain to us what this function is actually doing, in general terms,
and then give us a rough estimate of how many times per boot it's called.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140813/030850b6/attachment.bin