Linux block layer
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: axboe@kernek.dk
Cc: linux-block@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: [PATCH 2/6] sd: improve TUR handling in sd_check_events
Date: Tue, 14 Feb 2017 20:15:56 +0100	[thread overview]
Message-ID: <20170214191600.17480-3-hch@lst.de> (raw)
In-Reply-To: <20170214191600.17480-1-hch@lst.de>

Remove bogus evaluations of retval and sshdr when the device is offline,
and fix a possible NULL pointer dereference by allocating the 8 byte
sized sense header on stack.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/scsi/sd.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 40b4038c019e..11e290d1dbff 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1425,7 +1425,6 @@ static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
 {
 	struct scsi_disk *sdkp = scsi_disk_get(disk);
 	struct scsi_device *sdp;
-	struct scsi_sense_hdr *sshdr = NULL;
 	int retval;
 
 	if (!sdkp)
@@ -1454,22 +1453,21 @@ static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
 	 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
 	 * sd_revalidate() is called.
 	 */
-	retval = -ENODEV;
-
 	if (scsi_block_when_processing_errors(sdp)) {
-		sshdr  = kzalloc(sizeof(*sshdr), GFP_KERNEL);
+		struct scsi_sense_hdr sshdr = { 0, };
+
 		retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
-					      sshdr);
-	}
+					      &sshdr);
 
-	/* failed to execute TUR, assume media not present */
-	if (host_byte(retval)) {
-		set_media_not_present(sdkp);
-		goto out;
-	}
+		/* failed to execute TUR, assume media not present */
+		if (host_byte(retval)) {
+			set_media_not_present(sdkp);
+			goto out;
+		}
 
-	if (media_not_present(sdkp, sshdr))
-		goto out;
+		if (media_not_present(sdkp, &sshdr))
+			goto out;
+	}
 
 	/*
 	 * For removable scsi disk we have to recognise the presence
@@ -1485,7 +1483,6 @@ static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
 	 *	Medium present state has changed in either direction.
 	 *	Device has indicated UNIT_ATTENTION.
 	 */
-	kfree(sshdr);
 	retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
 	sdp->changed = 0;
 	scsi_disk_put(sdkp);
-- 
2.11.0

  parent reply	other threads:[~2017-02-14 19:16 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-14 19:15 sense handling improvements Christoph Hellwig
2017-02-14 19:15 ` [PATCH 1/6] scsi: always zero sshdr in scsi_normalize_sense Christoph Hellwig
2017-02-15  7:59   ` Hannes Reinecke
2017-02-14 19:15 ` Christoph Hellwig [this message]
2017-02-15  8:00   ` [PATCH 2/6] sd: improve TUR handling in sd_check_events Hannes Reinecke
2017-02-14 19:15 ` [PATCH 3/6] scsi: make the sense header argument to scsi_test_unit_ready mandatory Christoph Hellwig
2017-02-14 19:15 ` [PATCH 4/6] scsi: simplify scsi_execute_req_flags Christoph Hellwig
2017-02-14 19:15 ` [PATCH 5/6] scsi: merge __scsi_execute into scsi_execute Christoph Hellwig
2017-02-14 19:16 ` [PATCH 6/6] scsi: remove scsi_execute_req_flags Christoph Hellwig
2017-02-15  8:19 ` sense handling improvements Hannes Reinecke
2017-02-15 15:04   ` Christoph Hellwig
2017-02-15 15:30     ` Bart Van Assche
2017-02-16  3:42 ` Martin K. Petersen
2017-02-16 14:33   ` Christoph Hellwig
2017-02-22  7:19   ` Christoph Hellwig
2017-02-22 13:52     ` Martin K. Petersen
2017-02-23  0:51     ` Martin K. Petersen
2017-02-23  9:49       ` Christoph Hellwig
2017-02-23 14:28         ` Martin K. Petersen
2017-02-23 14:31           ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170214191600.17480-3-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernek.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox