stable.vger.kernel.org archive mirror
 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, Bart Van Assche <bvanassche@acm.org>,
	Yann Droneaud <ydroneaud@opteya.com>,
	Roland Dreier <roland@purestorage.com>
Subject: [PATCH 3.10 20/46] IB/umad: Fix error handling
Date: Fri,  4 Jul 2014 15:19:24 -0700	[thread overview]
Message-ID: <20140704221603.197571834@linuxfoundation.org> (raw)
In-Reply-To: <20140704221602.280733370@linuxfoundation.org>

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

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

From: Bart Van Assche <bvanassche@acm.org>

commit 8ec0a0e6b58218bdc1db91dd70ebfcd6ad8dd6cd upstream.

Avoid leaking a kref count in ib_umad_open() if port->ib_dev == NULL
or if nonseekable_open() fails.

Avoid leaking a kref count, that sm_sem is kept down and also that the
IB_PORT_SM capability mask is not cleared in ib_umad_sm_open() if
nonseekable_open() fails.

Since container_of() never returns NULL, remove the code that tests
whether container_of() returns NULL.

Moving the kref_get() call from the start of ib_umad_*open() to the
end is safe since it is the responsibility of the caller of these
functions to ensure that the cdev pointer remains valid until at least
when these functions return.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>

[ydroneaud@opteya.com: rework a bit to reduce the amount of code changed]

Signed-off-by: Yann Droneaud <ydroneaud@opteya.com>

[ nonseekable_open() can't actually fail, but....  - Roland ]

Signed-off-by: Roland Dreier <roland@purestorage.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/infiniband/core/user_mad.c |   49 ++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 22 deletions(-)

--- a/drivers/infiniband/core/user_mad.c
+++ b/drivers/infiniband/core/user_mad.c
@@ -780,27 +780,19 @@ static int ib_umad_open(struct inode *in
 {
 	struct ib_umad_port *port;
 	struct ib_umad_file *file;
-	int ret;
+	int ret = -ENXIO;
 
 	port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
-	if (port)
-		kref_get(&port->umad_dev->ref);
-	else
-		return -ENXIO;
 
 	mutex_lock(&port->file_mutex);
 
-	if (!port->ib_dev) {
-		ret = -ENXIO;
+	if (!port->ib_dev)
 		goto out;
-	}
 
+	ret = -ENOMEM;
 	file = kzalloc(sizeof *file, GFP_KERNEL);
-	if (!file) {
-		kref_put(&port->umad_dev->ref, ib_umad_release_dev);
-		ret = -ENOMEM;
+	if (!file)
 		goto out;
-	}
 
 	mutex_init(&file->mutex);
 	spin_lock_init(&file->send_lock);
@@ -814,6 +806,13 @@ static int ib_umad_open(struct inode *in
 	list_add_tail(&file->port_list, &port->file_list);
 
 	ret = nonseekable_open(inode, filp);
+	if (ret) {
+		list_del(&file->port_list);
+		kfree(file);
+		goto out;
+	}
+
+	kref_get(&port->umad_dev->ref);
 
 out:
 	mutex_unlock(&port->file_mutex);
@@ -880,10 +879,6 @@ static int ib_umad_sm_open(struct inode
 	int ret;
 
 	port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
-	if (port)
-		kref_get(&port->umad_dev->ref);
-	else
-		return -ENXIO;
 
 	if (filp->f_flags & O_NONBLOCK) {
 		if (down_trylock(&port->sm_sem)) {
@@ -898,17 +893,27 @@ static int ib_umad_sm_open(struct inode
 	}
 
 	ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
-	if (ret) {
-		up(&port->sm_sem);
-		goto fail;
-	}
+	if (ret)
+		goto err_up_sem;
 
 	filp->private_data = port;
 
-	return nonseekable_open(inode, filp);
+	ret = nonseekable_open(inode, filp);
+	if (ret)
+		goto err_clr_sm_cap;
+
+	kref_get(&port->umad_dev->ref);
+
+	return 0;
+
+err_clr_sm_cap:
+	swap(props.set_port_cap_mask, props.clr_port_cap_mask);
+	ib_modify_port(port->ib_dev, port->port_num, 0, &props);
+
+err_up_sem:
+	up(&port->sm_sem);
 
 fail:
-	kref_put(&port->umad_dev->ref, ib_umad_release_dev);
 	return ret;
 }
 



  parent reply	other threads:[~2014-07-04 22:19 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-04 22:19 [PATCH 3.10 00/46] 3.10.47-stable review Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 01/46] target: Fix left-over se_lun->lun_sep pointer OOPs Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 02/46] iscsi-target: Avoid rejecting incorrect ITT for Data-Out Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 03/46] iscsi-target: Explicily clear login response PDU in exception path Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 04/46] Input: elantech - deal with clickpads reporting right button events Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 05/46] Input: elantech - dont set bit 1 of reg_10 when the no_hw_res quirk is set Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 06/46] PCI: Add new ID for Intel GPU "spurious interrupt" quirk Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 07/46] PCI: Fix incorrect vgaarb conditional in WARN_ON() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 08/46] mtip32xx: Fix ERO and NoSnoop values in PCIe upstream on AMD systems Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 09/46] mtip32xx: Increase timeout for STANDBY IMMEDIATE command Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 10/46] mtip32xx: Remove dfs_parent after pci unregister Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 11/46] recordmcount/MIPS: Fix possible incorrect mcount_loc table entries in modules Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 12/46] MIPS: MSC: Prevent out-of-bounds writes to MIPS SC ioremapd region Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 13/46] UBIFS: fix an mmap and fsync race condition Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 14/46] UBIFS: Remove incorrect assertion in shrink_tnc() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 15/46] watchdog: sp805: Set watchdog_device->timeout from ->set_timeout() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 16/46] watchdog: ath79_wdt: avoid spurious restarts on AR934x Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 17/46] IB/qib: Fix port in pkey change event Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 18/46] IB/ipath: Translate legacy diagpkt into newer extended diagpkt Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 19/46] IB/srp: Fix a sporadic crash triggered by cable pulling Greg Kroah-Hartman
2014-07-04 22:19 ` Greg Kroah-Hartman [this message]
2014-07-04 22:19 ` [PATCH 3.10 21/46] IB/umad: Fix use-after-free on close Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 22/46] SUNRPC: Fix a module reference leak in svc_handle_xprt Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 23/46] pNFS: Handle allocation errors correctly in filelayout_alloc_layout_hdr() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 24/46] nfsd4: fix FREE_STATEID lockowner leak Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 25/46] nfsd: getattr for FATTR4_WORD0_FILES_AVAIL needs the statfs buffer Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 26/46] NFS: Dont declare inode uptodate unless all attributes were checked Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 27/46] nfsd: dont try to reuse an expired DRC entry off the list Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 28/46] nfsd: dont halt scanning the DRC LRU list when theres an RC_INPROG entry Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 29/46] powerpc/pseries: Fix overwritten PE state Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 30/46] powerpc: 64bit sendfile is capped at 2GB Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 31/46] powerpc: fix typo CONFIG_PMAC Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 32/46] powerpc: fix typo CONFIG_PPC_CPU Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 33/46] powerpc: Add AT_HWCAP2 to indicate V.CRYPTO category support Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 34/46] reiserfs: call truncate_setsize under tailpack mutex Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 35/46] MIPS: KVM: Remove redundant NULL checks before kfree() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 36/46] MIPS: KVM: Fix memory leak on VCPU Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 37/46] ipvs: Fix panic due to non-linear skb Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 38/46] ptrace,x86: force IRET path after a ptrace_stop() Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 39/46] mei: me: read H_CSR after asserting reset Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 40/46] mei: me: fix hw ready reset flow Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 41/46] extcon: max77693: Fix two NULL pointer exceptions on missing pdata Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 42/46] netfilter: ipt_ULOG: fix info leaks Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 43/46] Documentation/SubmittingPatches: describe the Fixes: tag Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 44/46] tracing: Try again for saved cmdline if failed due to locking Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 45/46] tracing: Fix syscall_*regfunc() vs copy_process() race Greg Kroah-Hartman
2014-07-04 22:19 ` [PATCH 3.10 46/46] ALSA: hda - Adjust speaker HPF and add LED support for HP Spectre 13 Greg Kroah-Hartman
2014-07-05  5:43 ` [PATCH 3.10 00/46] 3.10.47-stable review Guenter Roeck
2014-07-05  7:04   ` Satoru Takeuchi
2014-07-05 19:11   ` 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=20140704221603.197571834@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=bvanassche@acm.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=roland@purestorage.com \
    --cc=stable@vger.kernel.org \
    --cc=ydroneaud@opteya.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 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).