scsi: qla2xxx: Warn if done() or free() are called on an already freed srb
authorDaniel Wagner <dwagner@suse.de>
Tue, 8 Sep 2020 08:15:13 +0000 (10:15 +0200)
committerMartin K. Petersen <martin.petersen@oracle.com>
Thu, 10 Sep 2020 02:01:42 +0000 (22:01 -0400)
Emit a warning when ->done or ->free are called on an already freed
srb. There is a hidden use-after-free bug in the driver which corrupts
the srb memory pool which originates from the cleanup callbacks.

An extensive search didn't bring any lights on the real problem. The
initial fix was to set both pointers to NULL and try to catch invalid
accesses. But instead the memory corruption was gone and the driver
didn't crash. Since not all calling places check for NULL pointer, add
explicitly default handlers. With this we workaround the memory
corruption and add a debug help.

Link: https://lore.kernel.org/r/20200908081516.8561-2-dwagner@suse.de
Reviewed-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Arun Easi <aeasi@marvell.com>
Signed-off-by: Daniel Wagner <dwagner@suse.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/qla2xxx/qla_init.c
drivers/scsi/qla2xxx/qla_inline.h

index 57a2d76aa691d6707c180f6e72a4ffe4b1ab63dd..fb7d57dc4e69fc0b1b48df8274a3f53bd565cc82 100644 (file)
@@ -63,6 +63,16 @@ void qla2x00_sp_free(srb_t *sp)
        qla2x00_rel_sp(sp);
 }
 
+void qla2xxx_rel_done_warning(srb_t *sp, int res)
+{
+       WARN_ONCE(1, "Calling done() of an already freed srb %p object\n", sp);
+}
+
+void qla2xxx_rel_free_warning(srb_t *sp)
+{
+       WARN_ONCE(1, "Calling free() of an already freed srb %p object\n", sp);
+}
+
 /* Asynchronous Login/Logout Routines -------------------------------------- */
 
 unsigned long
index 861dc522723ce1d8341abb844f814a5d1ca62204..2aa6f81f87c4340b87f4ba5c7021e99ef2fed192 100644 (file)
@@ -207,10 +207,15 @@ qla2xxx_get_qpair_sp(scsi_qla_host_t *vha, struct qla_qpair *qpair,
        return sp;
 }
 
+void qla2xxx_rel_done_warning(srb_t *sp, int res);
+void qla2xxx_rel_free_warning(srb_t *sp);
+
 static inline void
 qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
 {
        sp->qpair = NULL;
+       sp->done = qla2xxx_rel_done_warning;
+       sp->free = qla2xxx_rel_free_warning;
        mempool_free(sp, qpair->srb_mempool);
        QLA_QPAIR_MARK_NOT_BUSY(qpair);
 }