All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Bill Kuzeja <william.kuzeja@stratus.com>,
	Himanshu Madhani <himanshu.madhani@cavium.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Subject: [PATCH 4.16 52/68] scsi: qla2xxx: Fix small memory leak in qla2x00_probe_one on probe failure
Date: Tue, 17 Apr 2018 17:58:05 +0200	[thread overview]
Message-ID: <20180417155751.463768844@linuxfoundation.org> (raw)
In-Reply-To: <20180417155749.341779147@linuxfoundation.org>

4.16-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Bill Kuzeja <William.Kuzeja@stratus.com>

commit 6d6340672ba3a99c4cf7af79c2edf7aa25595c84 upstream.

The code that fixes the crashes in the following commit introduced a small
memory leak:

commit 6a2cf8d3663e ("scsi: qla2xxx: Fix crashes in qla2x00_probe_one on probe failure")

Fixing this requires a bit of reworking, which I've explained. Also provide
some code cleanup.

There is a small window in qla2x00_probe_one where if qla2x00_alloc_queues
fails, we end up never freeing req and rsp and leak 0xc0 and 0xc8 bytes
respectively (the sizes of req and rsp).

I originally put in checks to test for this condition which were based on
the incorrect assumption that if ha->rsp_q_map and ha->req_q_map were
allocated, then rsp and req were allocated as well. This is incorrect.
There is a window between these allocations:

       ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
                goto probe_hw_failed;

[if successful, both rsp and req allocated]

       base_vha = qla2x00_create_host(sht, ha);
                goto probe_hw_failed;

       ret = qla2x00_request_irqs(ha, rsp);
                goto probe_failed;

       if (qla2x00_alloc_queues(ha, req, rsp)) {
                goto probe_failed;

[if successful, now ha->rsp_q_map and ha->req_q_map allocated]

To simplify this, we should just set req and rsp to NULL after we free
them. Sounds simple enough? The problem is that req and rsp are pointers
defined in the qla2x00_probe_one and they are not always passed by reference
to the routines that free them.

Here are paths which can free req and rsp:

PATH 1:
qla2x00_probe_one
   ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
   [req and rsp are passed by reference, but if this fails, we currently
    do not NULL out req and rsp. Easily fixed]

PATH 2:
qla2x00_probe_one
   failing in qla2x00_request_irqs or qla2x00_alloc_queues
      probe_failed:
         qla2x00_free_device(base_vha);
            qla2x00_free_req_que(ha, req)
            qla2x00_free_rsp_que(ha, rsp)

PATH 3:
qla2x00_probe_one:
   failing in qla2x00_mem_alloc or qla2x00_create_host
      probe_hw_failed:
         qla2x00_free_req_que(ha, req)
         qla2x00_free_rsp_que(ha, rsp)

PATH 1: This should currently work, but it doesn't because rsp and rsp are
not set to NULL in qla2x00_mem_alloc. Easily remedied.

PATH 2: req and rsp aren't passed in at all to qla2x00_free_device but are
derived from ha->req_q_map[0] and ha->rsp_q_map[0]. These are only set up if
qla2x00_alloc_queues succeeds.

In qla2x00_free_queues, we are protected from crashing if these don't exist
because req_qid_map and rsp_qid_map are only set on their allocation. We are
guarded in this way:

        for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
                if (!test_bit(cnt, ha->req_qid_map))
                        continue;

PATH 3: This works. We haven't freed req or rsp yet (or they were never
allocated if qla2x00_mem_alloc failed), so we'll attempt to free them here.

To summarize, there are a few small changes to make this work correctly and
(and for some cleanup):

1) (For PATH 1) Set *rsp and *req to NULL in case of failure in
qla2x00_mem_alloc so these are correctly set to NULL back in
qla2x00_probe_one

2) After jumping to probe_failed: and calling qla2x00_free_device,
explicitly set rsp and req to NULL so further calls with these pointers do
not crash, i.e. the free queue calls in the probe_hw_failed section we fall
through to.

3) Fix return code check in the call to qla2x00_alloc_queues. We currently
drop the return code on the floor. The probe fails but the caller of the
probe doesn't have an error code, so it attaches to pci. This can result in
a crash on module shutdown.

4) Remove unnecessary NULL checks in qla2x00_free_req_que,
qla2x00_free_rsp_que, and the egregious NULL checks before kfrees and vfrees
in qla2x00_mem_free.

I tested this out running a scenario where the card breaks at various times
during initialization. I made sure I forced every error exit path in
qla2x00_probe_one.

Cc: <stable@vger.kernel.org> # v4.16
Fixes: 6a2cf8d3663e ("scsi: qla2xxx: Fix crashes in qla2x00_probe_one on probe failure")
Signed-off-by: Bill Kuzeja <william.kuzeja@stratus.com>
Acked-by: Himanshu Madhani <himanshu.madhani@cavium.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/scsi/qla2xxx/qla_os.c |   44 ++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 23 deletions(-)

--- a/drivers/scsi/qla2xxx/qla_os.c
+++ b/drivers/scsi/qla2xxx/qla_os.c
@@ -471,9 +471,6 @@ fail_req_map:
 
 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
 {
-	if (!ha->req_q_map)
-		return;
-
 	if (IS_QLAFX00(ha)) {
 		if (req && req->ring_fx00)
 			dma_free_coherent(&ha->pdev->dev,
@@ -484,17 +481,14 @@ static void qla2x00_free_req_que(struct
 		(req->length + 1) * sizeof(request_t),
 		req->ring, req->dma);
 
-	if (req) {
+	if (req)
 		kfree(req->outstanding_cmds);
-		kfree(req);
-	}
+
+	kfree(req);
 }
 
 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
 {
-	if (!ha->rsp_q_map)
-		return;
-
 	if (IS_QLAFX00(ha)) {
 		if (rsp && rsp->ring)
 			dma_free_coherent(&ha->pdev->dev,
@@ -505,8 +499,7 @@ static void qla2x00_free_rsp_que(struct
 		(rsp->length + 1) * sizeof(response_t),
 		rsp->ring, rsp->dma);
 	}
-	if (rsp)
-		kfree(rsp);
+	kfree(rsp);
 }
 
 static void qla2x00_free_queues(struct qla_hw_data *ha)
@@ -3107,7 +3100,8 @@ qla2x00_probe_one(struct pci_dev *pdev,
 		goto probe_failed;
 
 	/* Alloc arrays of request and response ring ptrs */
-	if (qla2x00_alloc_queues(ha, req, rsp)) {
+	ret = qla2x00_alloc_queues(ha, req, rsp);
+	if (ret) {
 		ql_log(ql_log_fatal, base_vha, 0x003d,
 		    "Failed to allocate memory for queue pointers..."
 		    "aborting.\n");
@@ -3408,8 +3402,15 @@ probe_failed:
 	}
 
 	qla2x00_free_device(base_vha);
-
 	scsi_host_put(base_vha->host);
+	/*
+	 * Need to NULL out local req/rsp after
+	 * qla2x00_free_device => qla2x00_free_queues frees
+	 * what these are pointing to. Or else we'll
+	 * fall over below in qla2x00_free_req/rsp_que.
+	 */
+	req = NULL;
+	rsp = NULL;
 
 probe_hw_failed:
 	qla2x00_mem_free(ha);
@@ -4115,6 +4116,7 @@ fail_npiv_info:
 	(*rsp)->dma = 0;
 fail_rsp_ring:
 	kfree(*rsp);
+	*rsp = NULL;
 fail_rsp:
 	dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
 		sizeof(request_t), (*req)->ring, (*req)->dma);
@@ -4122,6 +4124,7 @@ fail_rsp:
 	(*req)->dma = 0;
 fail_req_ring:
 	kfree(*req);
+	*req = NULL;
 fail_req:
 	dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
 		ha->ct_sns, ha->ct_sns_dma);
@@ -4509,16 +4512,11 @@ qla2x00_mem_free(struct qla_hw_data *ha)
 		dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
 			ha->init_cb, ha->init_cb_dma);
 
-	if (ha->optrom_buffer)
-		vfree(ha->optrom_buffer);
-	if (ha->nvram)
-		kfree(ha->nvram);
-	if (ha->npiv_info)
-		kfree(ha->npiv_info);
-	if (ha->swl)
-		kfree(ha->swl);
-	if (ha->loop_id_map)
-		kfree(ha->loop_id_map);
+	vfree(ha->optrom_buffer);
+	kfree(ha->nvram);
+	kfree(ha->npiv_info);
+	kfree(ha->swl);
+	kfree(ha->loop_id_map);
 
 	ha->srb_mempool = NULL;
 	ha->ctx_mempool = NULL;

  parent reply	other threads:[~2018-04-17 15:58 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-17 15:57 [PATCH 4.16 00/68] 4.16.3-stable review Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 01/68] cdc_ether: flag the Cinterion AHS8 modem by gemalto as WWAN Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 02/68] rds: MP-RDS may use an invalid c_path Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 03/68] slip: Check if rstate is initialized before uncompressing Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 04/68] vhost: fix vhost_vq_access_ok() log check Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 05/68] l2tp: fix races in tunnel creation Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 06/68] l2tp: fix race in duplicate tunnel detection Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 07/68] ip_gre: clear feature flags when incompatible o_flags are set Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 08/68] vhost: Fix vhost_copy_to_user() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 09/68] lan78xx: Correctly indicate invalid OTP Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 10/68] [PATCH] sparc64: Properly range check DAX completion index Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 11/68] media: v4l2-core: fix size of devnode_nums[] bitarray Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 12/68] media: v4l2-compat-ioctl32: dont oops on overlay Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 13/68] media: v4l: vsp1: Fix header display list status check in continuous mode Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 14/68] ipmi: Fix some error cleanup issues Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 15/68] parisc: Fix out of array access in match_pci_device() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 16/68] parisc: Fix HPMC handler by increasing size to multiple of 16 bytes Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 17/68] iwlwifi: add a bunch of new 9000 PCI IDs Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 18/68] Drivers: hv: vmbus: do not mark HV_PCIE as perf_device Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 19/68] PCI: hv: Serialize the present and eject work items Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 20/68] PCI: hv: Fix 2 hang issues in hv_compose_msi_msg() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 21/68] KVM: PPC: Book3S HV: trace_tlbie must not be called in realmode Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 22/68] perf intel-pt: Fix overlap detection to identify consecutive buffers correctly Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 23/68] perf intel-pt: Fix sync_switch Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 24/68] perf intel-pt: Fix error recovery from missing TIP packet Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 25/68] perf intel-pt: Fix timestamp following overflow Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 26/68] perf/core: Fix use-after-free in uprobe_perf_close() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 27/68] radeon: hide pointless #warning when compile testing Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 28/68] x86/mce/AMD: Pass the bank number to smca_get_bank_type() Greg Kroah-Hartman
2018-04-17 15:57   ` [4.16,28/68] " Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 29/68] x86/mce/AMD, EDAC/mce_amd: Enumerate Reserved SMCA bank type Greg Kroah-Hartman
2018-04-17 15:57   ` [4.16,29/68] " Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 30/68] x86/mce/AMD: Get address from already initialized block Greg Kroah-Hartman
2018-04-17 15:57   ` [4.16,30/68] " Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 31/68] ath9k: Protect queue draining by rcu_read_lock() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 32/68] x86/uapi: Fix asm/bootparam.h userspace compilation errors Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 33/68] x86/apic: Fix signedness bug in APIC ID validity checks Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 34/68] sunrpc: remove incorrect HMAC request initialization Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 35/68] f2fs: fix heap mode to reset it back Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 36/68] block: Change a rcu_read_{lock,unlock}_sched() pair into rcu_read_{lock,unlock}() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 37/68] nvme: Skip checking heads without namespaces Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 38/68] lib: fix stall in __bitmap_parselist() Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 39/68] zboot: fix stack protector in compressed boot phase Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 40/68] blk-mq: Directly schedule q->timeout_work when aborting a request Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 41/68] blk-mq: order getting budget and driver tag Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 42/68] blk-mq: make sure that correct hctx->next_cpu is set Greg Kroah-Hartman
2018-04-17 15:57   ` Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 43/68] blk-mq: dont keep offline CPUs mapped to hctx 0 Greg Kroah-Hartman
2018-04-17 15:57   ` Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 44/68] ovl: Set d->last properly during lookup Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 45/68] ovl: fix lookup with middle layer opaque dir and absolute path redirects Greg Kroah-Hartman
2018-04-17 15:57 ` [PATCH 4.16 46/68] ovl: set i_ino to the value of st_ino for NFS export Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 47/68] ovl: set lower layer st_dev only if setting lower st_ino Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 48/68] xen: xenbus_dev_frontend: Fix XS_TRANSACTION_END handling Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 49/68] hugetlbfs: fix bug in pgoff overflow checking Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 50/68] nfsd: fix incorrect umasks Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 51/68] scsi: scsi_dh: Dont look for NULL devices handlers by name Greg Kroah-Hartman
2018-04-17 15:58 ` Greg Kroah-Hartman [this message]
2018-04-17 15:58 ` [PATCH 4.16 53/68] Revert "scsi: core: return BLK_STS_OK for DID_OK in __scsi_error_from_host_byte()" Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 54/68] apparmor: fix logging of the existence test for signals Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 55/68] apparmor: fix display of .ns_name for containers Greg Kroah-Hartman
2018-04-18 15:05   ` Serge E. Hallyn
2018-04-17 15:58 ` [PATCH 4.16 56/68] apparmor: fix resource audit messages when auditing peer Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 57/68] block/loop: fix deadlock after loop_set_status Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 58/68] nfit: fix region registration vs block-data-window ranges Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 59/68] s390/qdio: dont retry EQBS after CCQ 96 Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 60/68] s390/qdio: dont merge ERROR output buffers Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 61/68] s390/ipl: ensure loadparm valid flag is set Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 62/68] s390/compat: fix setup_frame32 Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 63/68] get_user_pages_fast(): return -EFAULT on access_ok failure Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 64/68] mm/gup_benchmark: handle gup failures Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 65/68] getname_kernel() needs to make sure that ->name != ->iname in long case Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 66/68] Bluetooth: Fix connection if directed advertising and privacy is used Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 67/68] Bluetooth: hci_bcm: Treat Interrupt ACPI resources as always being active-low Greg Kroah-Hartman
2018-04-17 15:58 ` [PATCH 4.16 68/68] rtl8187: Fix NULL pointer dereference in priv->conf_mutex Greg Kroah-Hartman
2018-04-17 21:03 ` [PATCH 4.16 00/68] 4.16.3-stable review Shuah Khan
2018-04-18  6:58   ` Greg Kroah-Hartman
2018-04-17 23:43 ` kernelci.org bot
2018-04-18  5:13 ` Naresh Kamboju
2018-04-18  6:59   ` Greg Kroah-Hartman
2018-04-18 15:41 ` Guenter Roeck
2018-04-19  6:40   ` Greg Kroah-Hartman

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=20180417155751.463768844@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=himanshu.madhani@cavium.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=stable@vger.kernel.org \
    --cc=william.kuzeja@stratus.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.