Skip to contentSkip to conversation

staging: keucr: check kmalloc() return value

PATCH Superseded Vasiliy Kulikov 3 participants 4 messages Sep 12, 2010 – Sep 19, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: keucr: check kmalloc() return value

kmalloc() may fail, if so return error code.
Also don't alloc 16 bytes from the heap, use stack instead.

---
Compile tested.

 drivers/staging/keucr/init.c            |    2 +
 drivers/staging/keucr/msscsi.c          |    4 ++
 drivers/staging/keucr/smilsub.c         |    4 +--
 drivers/staging/keucr/smscsi.c          |    4 ++
 5 files changed, 71 insertions(+), 3 deletions(-)
 create mode 100644 drivers/staging/keucr/check-return.list
drivers/staging/keucr/init.c +2 -0
index 2ae129b..1934805 100644
@@ -300,6 +300,8 @@ int ENE_LoadBinCode(struct us_data *us, BYTE flag)
 		return USB_STOR_TRANSPORT_GOOD;
 
 	buf = kmalloc(0x800, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	switch ( flag )
 	{
 		// For SD
drivers/staging/keucr/msscsi.c +4 -0
index b908a23..ad0c5c6 100644
@@ -168,6 +168,8 @@ int MS_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
 		DWORD	blkno;
 
 		buf = kmalloc(blenByte, GFP_KERNEL);
+		if (buf = NULL)
+			return USB_STOR_TRANSPORT_ERROR;
 
 		result = ENE_LoadBinCode(us, MS_RW_PATTERN);
 		if (result != USB_STOR_XFER_GOOD)
@@ -271,6 +273,8 @@ int MS_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
 		WORD	len, oldphy, newphy;
 
 		buf = kmalloc(blenByte, GFP_KERNEL);
+		if (buf = NULL)
+			return USB_STOR_TRANSPORT_ERROR;
 		usb_stor_set_xfer_buf(us, buf, blenByte, srb, FROM_XFER_BUF);
 
 		result = ENE_LoadBinCode(us, MS_RW_PATTERN);
drivers/staging/keucr/smilsub.c +1 -3
index 844b659..293ea09 100644
@@ -815,7 +815,7 @@ int Ssfdc_D_ReadRedtData(struct us_data *us, BYTE *redundant)
 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
 	int	result;
 	WORD	addr;
-	BYTE  *buf;
+	BYTE buf[0x10];
 
 	result = ENE_LoadBinCode(us, SM_RW_PATTERN);
 	if (result != USB_STOR_XFER_GOOD)
@@ -839,11 +839,9 @@ int Ssfdc_D_ReadRedtData(struct us_data *us, BYTE *redundant)
 	bcb->CDB[8]			= 0;
 	bcb->CDB[9]			= 1;
 
-	buf = kmalloc(0x10, GFP_KERNEL);
 	//result = ENE_SendScsiCmd(us, FDIR_READ, redundant, 0);
 	result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
 	memcpy(redundant, buf, 0x10);
-	kfree(buf);
 	if (result != USB_STOR_XFER_GOOD)
 		return USB_STOR_TRANSPORT_ERROR;
 
drivers/staging/keucr/smscsi.c +4 -0
index 43e32c6..6211686 100644
@@ -145,6 +145,8 @@ int SM_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
 		return USB_STOR_TRANSPORT_ERROR;
 
 	buf = kmalloc(blenByte, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	result = Media_D_ReadSector(us, bn, blen, buf);
 	usb_stor_set_xfer_buf(us, buf, blenByte, srb, TO_XFER_BUF);
 	kfree(buf);
@@ -175,6 +177,8 @@ int SM_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
 		return USB_STOR_TRANSPORT_ERROR;
 
 	buf = kmalloc(blenByte, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	usb_stor_set_xfer_buf(us, buf, blenByte, srb, FROM_XFER_BUF);
 	result = Media_D_CopySector(us, bn, blen, buf);
 	kfree(buf);
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 2 lines · Vasiliy Kulikov

kmalloc() may fail, if so return error code.
Also don't alloc 16 bytes from the heap, use stack instead.

I don't have ENE_SendScsiCmd() in my repo, but I think it's a DMA function. You're not supposed to use stack memory for DMA transfers. This is documented in: Documentation/DMA-API-HOWTO.txt

So the original code is correct here (except that there is no error handling).

regards,
dan carpenter

#2 lore raw
In reply to Vasiliy Kulikov
Quoted 2 lines · Vasiliy Kulikov

kmalloc() may fail, if so return error code.
Also don't alloc 16 bytes from the heap, use stack instead.

Nope, you can't do that for USB drivers, sorry. Please read the documentation, it states this very clearly.

thanks,

greg k-h

#3 lore raw

PATCH v2 1/1 staging: keucr: check kmalloc() return value

kmalloc() may fail, if so return error code.

---
Compile tested.

 drivers/staging/keucr/init.c   |    2 ++
 drivers/staging/keucr/msscsi.c |    4 ++++
 drivers/staging/keucr/smscsi.c |    4 ++++
 3 files changed, 10 insertions(+), 0 deletions(-)
drivers/staging/keucr/init.c +2 -0
index 2ae129b..1934805 100644
@@ -300,6 +300,8 @@ int ENE_LoadBinCode(struct us_data *us, BYTE flag)
 		return USB_STOR_TRANSPORT_GOOD;
 
 	buf = kmalloc(0x800, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	switch ( flag )
 	{
 		// For SD
drivers/staging/keucr/msscsi.c +4 -0
index b908a23..ad0c5c6 100644
@@ -168,6 +168,8 @@ int MS_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
 		DWORD	blkno;
 
 		buf = kmalloc(blenByte, GFP_KERNEL);
+		if (buf = NULL)
+			return USB_STOR_TRANSPORT_ERROR;
 
 		result = ENE_LoadBinCode(us, MS_RW_PATTERN);
 		if (result != USB_STOR_XFER_GOOD)
@@ -271,6 +273,8 @@ int MS_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
 		WORD	len, oldphy, newphy;
 
 		buf = kmalloc(blenByte, GFP_KERNEL);
+		if (buf = NULL)
+			return USB_STOR_TRANSPORT_ERROR;
 		usb_stor_set_xfer_buf(us, buf, blenByte, srb, FROM_XFER_BUF);
 
 		result = ENE_LoadBinCode(us, MS_RW_PATTERN);
drivers/staging/keucr/smscsi.c +4 -0
index 43e32c6..6211686 100644
@@ -145,6 +145,8 @@ int SM_SCSI_Read(struct us_data *us, struct scsi_cmnd *srb)
 		return USB_STOR_TRANSPORT_ERROR;
 
 	buf = kmalloc(blenByte, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	result = Media_D_ReadSector(us, bn, blen, buf);
 	usb_stor_set_xfer_buf(us, buf, blenByte, srb, TO_XFER_BUF);
 	kfree(buf);
@@ -175,6 +177,8 @@ int SM_SCSI_Write(struct us_data *us, struct scsi_cmnd *srb)
 		return USB_STOR_TRANSPORT_ERROR;
 
 	buf = kmalloc(blenByte, GFP_KERNEL);
+	if (buf = NULL)
+		return USB_STOR_TRANSPORT_ERROR;
 	usb_stor_set_xfer_buf(us, buf, blenByte, srb, FROM_XFER_BUF);
 	result = Media_D_CopySector(us, bn, blen, buf);
 	kfree(buf);
Signature · 1 line
1.7.0.4