Skip to contentSkip to conversation

usb: isp1760: fix null dereference if kzalloc returns null

PATCH In review Colin King 3 participants 4 messages May 31, 2015 – Jun 2, 2015

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH v2 1/1 usb: isp1760: fix null dereference if kzalloc returns null

From: Colin Ian King <[email protected]>

If kzalloc returns null then isp1760_ep_alloc_request performs a null pointer dereference on req. Check for null to avoid this.

Detected with smatch static analysis:

drivers/usb/isp1760/isp1760-udc.c:816 isp1760_ep_alloc_request()
error: potential null dereference 'req'. (kzalloc returns null)

---
 drivers/usb/isp1760/isp1760-udc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/isp1760/isp1760-udc.c +2 -0
index 3fc4fe7..18ebf5b 100644
@@ -812,6 +812,8 @@ static struct usb_request *isp1760_ep_alloc_request(struct usb_ep *ep,
 	struct isp1760_request *req;
 
 	req = kzalloc(sizeof(*req), gfp_flags);
+	if (!req)
+		return NULL;
 
 	return &req->req;
 }
Signature · 1 line
2.1.4
#1 lore raw

Hello.

Quoted 4 lines · Colin King

From: Colin Ian King <[email protected]>

If kzalloc returns null then isp1760_ep_alloc_request performs a null pointer dereference on req. Check for null to avoid this.

I told you there's no dereference and yet you're repeating it again. :-(

Quoted 6 lines

Detected with smatch static analysis:

drivers/usb/isp1760/isp1760-udc.c:816 isp1760_ep_alloc_request()
error: potential null dereference 'req'. (kzalloc returns null)

WBR, Sergei

#2 lore raw
Quoted 9 lines · On Monday 01 June 2015 16:51:30 Sergei Shtylyov wrote:

Hello.

Quoted 4 lines · Colin King

From: Colin Ian King <[email protected]>

If kzalloc returns null then isp1760_ep_alloc_request performs a null pointer dereference on req. Check for null to avoid this.

I told you there's no dereference and yet you're repeating it again. :-(

How about

"isp1760_ep_alloc_request allocates a structure with kzalloc without checking for NULL and then returns a pointer to one of the structure fields. As the field happens to be the first in the structure the caller can properly check for NULL, but this is risky if the structure layout is changed later. Add an explicit NULL check for the kzalloc return value."

Quoted 9 lines
Quoted 9 lines

a null pointer dereference on req. Check for null to avoid this.

Detected with smatch static analysis:

drivers/usb/isp1760/isp1760-udc.c:816 isp1760_ep_alloc_request()

error: potential null dereference 'req'. (kzalloc returns null)

Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

Hello.

Quoted 7 lines · Laurent Pinchart
Quoted 6 lines
Quoted 4 lines

From: Colin Ian King <[email protected]>

If kzalloc returns null then isp1760_ep_alloc_request performs a null pointer dereference on req. Check for null to avoid this.

I told you there's no dereference and yet you're repeating it again. :-(

Quoted 7 lines

How about

"isp1760_ep_alloc_request allocates a structure with kzalloc without checking for NULL and then returns a pointer to one of the structure fields. As the field happens to be the first in the structure the caller can properly check for NULL, but this is risky if the structure layout is changed later. Add an explicit NULL check for the kzalloc return value."

Fine with me! Thanks, Laurent. :-)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

a null pointer dereference on req. Check for null to avoid this.

Detected with smatch static analysis:

drivers/usb/isp1760/isp1760-udc.c:816 isp1760_ep_alloc_request()

error: potential null dereference 'req'. (kzalloc returns null)

WBR, Sergei