linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities
@ 2022-04-13 10:00 Enze Li
  2022-04-26 12:46 ` Martin K. Petersen
  2022-04-27  2:56 ` [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities Enze Li
  0 siblings, 2 replies; 6+ messages in thread
From: Enze Li @ 2022-04-13 10:00 UTC (permalink / raw)
  To: jejb, martin.petersen; +Cc: linux-scsi, linux-kernel

The function get_capabilities() has the possibility of failing to
allocate transfer buffer, but it does not currently handle this, which
can lead to exceptions when accessing the buffer.

This patch adds handling when memory allocation fails.

Signed-off-by: Enze Li <lienze@kylinos.cn>
---
 drivers/scsi/sr.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index cbd92891a762..868f88b1f4d4 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -113,7 +113,7 @@ static int sr_open(struct cdrom_device_info *, int);
 static void sr_release(struct cdrom_device_info *);
 
 static void get_sectorsize(struct scsi_cd *);
-static void get_capabilities(struct scsi_cd *);
+static int get_capabilities(struct scsi_cd *);
 
 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
 				    unsigned int clearing, int slot);
@@ -669,8 +669,9 @@ static int sr_probe(struct device *dev)
 
 	sdev->sector_size = 2048;	/* A guess, just in case */
 
-	/* FIXME: need to handle a get_capabilities failure properly ?? */
-	get_capabilities(cd);
+	error = -ENOMEM;
+	if (get_capabilities(cd))
+		goto fail_put;
 	sr_vendor_init(cd);
 
 	set_capacity(disk, cd->capacity);
@@ -794,7 +795,7 @@ static void get_sectorsize(struct scsi_cd *cd)
 	return;
 }
 
-static void get_capabilities(struct scsi_cd *cd)
+static int get_capabilities(struct scsi_cd *cd)
 {
 	unsigned char *buffer;
 	struct scsi_mode_data data;
@@ -819,7 +820,7 @@ static void get_capabilities(struct scsi_cd *cd)
 	buffer = kmalloc(512, GFP_KERNEL);
 	if (!buffer) {
 		sr_printk(KERN_ERR, cd, "out of memory.\n");
-		return;
+		return -ENOMEM;
 	}
 
 	/* eat unit attentions */
@@ -839,7 +840,7 @@ static void get_capabilities(struct scsi_cd *cd)
 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
 		kfree(buffer);
 		sr_printk(KERN_INFO, cd, "scsi-1 drive");
-		return;
+		return 0;
 	}
 
 	n = data.header_length + data.block_descriptor_length;
@@ -898,6 +899,7 @@ static void get_capabilities(struct scsi_cd *cd)
 	}
 
 	kfree(buffer);
+	return 0;
 }
 
 /*
-- 
2.25.1


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

* Re: [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities
  2022-04-13 10:00 [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities Enze Li
@ 2022-04-26 12:46 ` Martin K. Petersen
  2022-04-27  3:03   ` Enze Li
  2022-04-27  2:56 ` [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities Enze Li
  1 sibling, 1 reply; 6+ messages in thread
From: Martin K. Petersen @ 2022-04-26 12:46 UTC (permalink / raw)
  To: Enze Li; +Cc: jejb, martin.petersen, linux-scsi, linux-kernel


Enze,

> @@ -669,8 +669,9 @@ static int sr_probe(struct device *dev)
>  
>  	sdev->sector_size = 2048;	/* A guess, just in case */
>  
> -	/* FIXME: need to handle a get_capabilities failure properly ?? */
> -	get_capabilities(cd);
> +	error = -ENOMEM;
> +	if (get_capabilities(cd))
> +		goto fail_put;

Shouldn't this be 'goto fail_minor;'?

-- 
Martin K. Petersen	Oracle Linux Engineering

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

* [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities
  2022-04-13 10:00 [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities Enze Li
  2022-04-26 12:46 ` Martin K. Petersen
@ 2022-04-27  2:56 ` Enze Li
  2022-04-28  2:30   ` Martin K. Petersen
  2022-05-03  0:51   ` Martin K. Petersen
  1 sibling, 2 replies; 6+ messages in thread
From: Enze Li @ 2022-04-27  2:56 UTC (permalink / raw)
  To: lienze, jejb, martin.petersen; +Cc: linux-kernel, linux-scsi

The function get_capabilities() has the possibility of failing to
allocate transfer buffer, but it does not currently handle this, which
may lead to exceptions when accessing the buffer.

This patch adds handling when memory allocation fails.

Signed-off-by: Enze Li <lienze@kylinos.cn>
---
 drivers/scsi/sr.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index cbd92891a762..32d3b8274f14 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -113,7 +113,7 @@ static int sr_open(struct cdrom_device_info *, int);
 static void sr_release(struct cdrom_device_info *);
 
 static void get_sectorsize(struct scsi_cd *);
-static void get_capabilities(struct scsi_cd *);
+static int get_capabilities(struct scsi_cd *);
 
 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
 				    unsigned int clearing, int slot);
@@ -669,8 +669,9 @@ static int sr_probe(struct device *dev)
 
 	sdev->sector_size = 2048;	/* A guess, just in case */
 
-	/* FIXME: need to handle a get_capabilities failure properly ?? */
-	get_capabilities(cd);
+	error = -ENOMEM;
+	if (get_capabilities(cd))
+		goto fail_minor;
 	sr_vendor_init(cd);
 
 	set_capacity(disk, cd->capacity);
@@ -794,7 +795,7 @@ static void get_sectorsize(struct scsi_cd *cd)
 	return;
 }
 
-static void get_capabilities(struct scsi_cd *cd)
+static int get_capabilities(struct scsi_cd *cd)
 {
 	unsigned char *buffer;
 	struct scsi_mode_data data;
@@ -819,7 +820,7 @@ static void get_capabilities(struct scsi_cd *cd)
 	buffer = kmalloc(512, GFP_KERNEL);
 	if (!buffer) {
 		sr_printk(KERN_ERR, cd, "out of memory.\n");
-		return;
+		return -ENOMEM;
 	}
 
 	/* eat unit attentions */
@@ -839,7 +840,7 @@ static void get_capabilities(struct scsi_cd *cd)
 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
 		kfree(buffer);
 		sr_printk(KERN_INFO, cd, "scsi-1 drive");
-		return;
+		return 0;
 	}
 
 	n = data.header_length + data.block_descriptor_length;
@@ -898,6 +899,7 @@ static void get_capabilities(struct scsi_cd *cd)
 	}
 
 	kfree(buffer);
+	return 0;
 }
 
 /*
-- 
2.25.1


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

* Re: [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities
  2022-04-26 12:46 ` Martin K. Petersen
@ 2022-04-27  3:03   ` Enze Li
  0 siblings, 0 replies; 6+ messages in thread
From: Enze Li @ 2022-04-27  3:03 UTC (permalink / raw)
  To: martin.petersen; +Cc: jejb, lienze, linux-kernel, linux-scsi

Hi Martin,

Thank you for the review.  I've fixed the issue you pointed out.
v2 of this patch is here:
https://lore.kernel.org/all/20220427025647.298358-1-lienze@kylinos.cn/

Thanks,
Enze

>Enze,
>
>> @@ -669,8 +669,9 @@ static int sr_probe(struct device *dev)
>>  
>>  	sdev->sector_size = 2048;	/* A guess, just in case */
>>  
>> -	/* FIXME: need to handle a get_capabilities failure properly
>> ?? */
>> -	get_capabilities(cd);
>> +	error = -ENOMEM;
>> +	if (get_capabilities(cd))
>> +		goto fail_put;
>
>Shouldn't this be 'goto fail_minor;'?
>
>-- 
>Martin K. Petersen	Oracle Linux Engineering

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

* Re: [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities
  2022-04-27  2:56 ` [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities Enze Li
@ 2022-04-28  2:30   ` Martin K. Petersen
  2022-05-03  0:51   ` Martin K. Petersen
  1 sibling, 0 replies; 6+ messages in thread
From: Martin K. Petersen @ 2022-04-28  2:30 UTC (permalink / raw)
  To: Enze Li; +Cc: jejb, martin.petersen, linux-kernel, linux-scsi


Enze,

> The function get_capabilities() has the possibility of failing to
> allocate transfer buffer, but it does not currently handle this, which
> may lead to exceptions when accessing the buffer.

Applied to 5.19/scsi-staging, thanks!

-- 
Martin K. Petersen	Oracle Linux Engineering

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

* Re: [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities
  2022-04-27  2:56 ` [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities Enze Li
  2022-04-28  2:30   ` Martin K. Petersen
@ 2022-05-03  0:51   ` Martin K. Petersen
  1 sibling, 0 replies; 6+ messages in thread
From: Martin K. Petersen @ 2022-05-03  0:51 UTC (permalink / raw)
  To: Enze Li, jejb; +Cc: Martin K . Petersen, linux-kernel, linux-scsi

On Wed, 27 Apr 2022 10:56:47 +0800, Enze Li wrote:

> The function get_capabilities() has the possibility of failing to
> allocate transfer buffer, but it does not currently handle this, which
> may lead to exceptions when accessing the buffer.
> 
> This patch adds handling when memory allocation fails.
> 
> 
> [...]

Applied to 5.19/scsi-queue, thanks!

[1/1] scsi: sr: add handling of memory allocation failures when calling get_capabilities
      https://git.kernel.org/mkp/scsi/c/ebc95c790653

-- 
Martin K. Petersen	Oracle Linux Engineering

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

end of thread, other threads:[~2022-05-05 16:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-13 10:00 [PATCH] scsi: sr: add handling of memory allocation failure in get_capabilities Enze Li
2022-04-26 12:46 ` Martin K. Petersen
2022-04-27  3:03   ` Enze Li
2022-04-27  2:56 ` [PATCH v2] scsi: sr: add handling of memory allocation failures when calling get_capabilities Enze Li
2022-04-28  2:30   ` Martin K. Petersen
2022-05-03  0:51   ` Martin K. Petersen

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 NNTP newsgroup(s).