[PATCH v2] scsi: sg: don't return bogus Sg_requests

From: Johannes Thumshirn
Date: Wed May 10 2017 - 03:53:55 EST


If the list search in sg_get_rq_mark() fails to find a valid request, we
return a bogus element. This then can later lead to a GPF in sg_remove_scat().

So don't return bogus Sg_requests in sg_get_rq_mark() but NULL in case the
list search doesn't find a valid request.

Signed-off-by: Johannes Thumshirn <jthumshirn@xxxxxxx>
Reported-by: Andrey Konovalov <andreyknvl@xxxxxxxxxx>
Cc: Hannes Reinecke <hare@xxxxxxx>
Cc: Christoph Hellwig <hch@xxxxxx>
Cc: Doug Gilbert <dgilbert@xxxxxxxxxxxx>
---
Changes to v1:
* Directly return found element within the loop (Hannes)

drivers/scsi/sg.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 0a38ba01b7b4..82c33a6edbea 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -2074,11 +2074,12 @@ sg_get_rq_mark(Sg_fd * sfp, int pack_id)
if ((1 == resp->done) && (!resp->sg_io_owned) &&
((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
resp->done = 2; /* guard against other readers */
- break;
+ write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
+ return resp;
}
}
write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
- return resp;
+ return NULL;
}

/* always adds to end of list */
--
2.12.0