All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yann Droneaud <ydroneaud-RlY5vtjFyJ3QT0dZR+AlfA@public.gmane.org>
To: Bart Van Assche <bvanassche-HInyCGIudOg@public.gmane.org>
Cc: Roland Dreier <roland-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	Alex Chiang <achiang-VXdhtT5mjnY@public.gmane.org>,
	linux-rdma <linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH 2/3] IB/umad: Fix error handling
Date: Mon, 12 May 2014 12:18:10 +0200	[thread overview]
Message-ID: <1399889890.3017.6.camel@localhost.localdomain> (raw)
In-Reply-To: <537086BA.3020807-HInyCGIudOg@public.gmane.org>

Hi,

Le lundi 12 mai 2014 à 10:30 +0200, Bart Van Assche a écrit :
> 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.
> 
> Signed-off-by: Bart Van Assche <bvanassche-HInyCGIudOg@public.gmane.org>
> Cc: <stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
> ---
>  drivers/infiniband/core/user_mad.c | 37 +++++++++++++++++++++++--------------
>  1 file changed, 23 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c
> index e61287c..5c67d80 100644
> --- a/drivers/infiniband/core/user_mad.c
> +++ b/drivers/infiniband/core/user_mad.c
> @@ -780,24 +780,20 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
>  {
>  	struct ib_umad_port *port;
>  	struct ib_umad_file *file;
> -	int ret;
> +	int ret = -ENXIO;
>  

I don't like the way ret is gratuitously set,

>  	port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
>  	kref_get(&port->umad_dev->ref);
>  
>  	mutex_lock(&port->file_mutex);
>  
> -	if (!port->ib_dev) {
> -		ret = -ENXIO;
> +	if (!port->ib_dev)
>  		goto out;
> -	}
>  
> +	ret = -ENOMEM;

especially here: I think it should be moved in the error handling path:

>  	file = kzalloc(sizeof *file, GFP_KERNEL);
> -	if (!file) {
> -		kref_put(&port->umad_dev->ref, ib_umad_release_dev);
> -		ret = -ENOMEM;

keep it here.

> +	if (!file)
>  		goto out;
> -	}
>  
>  	mutex_init(&file->mutex);
>  	spin_lock_init(&file->send_lock);
> @@ -814,6 +810,10 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
>  
>  out:
>  	mutex_unlock(&port->file_mutex);
> +
> +	if (ret)
> +		kref_put(&port->umad_dev->ref, ib_umad_release_dev);
> +
>  	return ret;
>  }
>  
> @@ -892,18 +892,27 @@ static int ib_umad_sm_open(struct inode *inode, struct file *filp)
>  	}
>  
>  	ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
> -	if (ret) {
> -		up(&port->sm_sem);
> -		goto fail;
> -	}
> +	if (ret)
> +		goto up_sem;
>  
>  	filp->private_data = port;
>  
> -	return nonseekable_open(inode, filp);
> +	ret = nonseekable_open(inode, filp);
> +	if (ret)
> +		goto clr_sm_cap;
>  
>  fail:
> -	kref_put(&port->umad_dev->ref, ib_umad_release_dev);
> +	if (ret)
> +		kref_put(&port->umad_dev->ref, ib_umad_release_dev);
>  	return ret;
> +
> +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);
> +
> +up_sem:
> +	up(&port->sm_sem);
> +	goto fail;

I dislike jump backward, why not unconditionally call kref_put() in the
error path and return ret here.

This way you could drop fail label and the test on ret in the default
code path.

>  }
>  
>  static int ib_umad_sm_close(struct inode *inode, struct file *filp)

Regards.

-- 
Yann Droneaud
OPTEYA



--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2014-05-12 10:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-12  8:29 [PATCH 0/3] Fix a use-after-free in ib_umad Bart Van Assche
     [not found] ` <53708666.6060209-HInyCGIudOg@public.gmane.org>
2014-05-12  8:30   ` [PATCH 1/3] IB/umad: Remove container_of() != NULL tests Bart Van Assche
     [not found]     ` <5370869F.5040103-HInyCGIudOg@public.gmane.org>
2014-05-12 10:04       ` Yann Droneaud
     [not found]         ` <1399889097.3017.1.camel-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2014-05-12 10:36           ` Bart Van Assche
     [not found]             ` <5370A41F.8050001-HInyCGIudOg@public.gmane.org>
2014-05-12 12:40               ` Yann Droneaud
2014-05-12  8:30   ` [PATCH 2/3] IB/umad: Fix error handling Bart Van Assche
     [not found]     ` <537086BA.3020807-HInyCGIudOg@public.gmane.org>
2014-05-12 10:18       ` Yann Droneaud [this message]
     [not found]         ` <1399889890.3017.6.camel-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2014-05-12 10:32           ` Bart Van Assche
     [not found]             ` <5370A323.5000504-HInyCGIudOg@public.gmane.org>
2014-05-12 12:35               ` Yann Droneaud
2014-05-16 11:03         ` [PATCH v3 0/2] Fix a use-after-free in ib_umad Bart Van Assche
     [not found]           ` <5375F094.30809-HInyCGIudOg@public.gmane.org>
2014-05-16 11:04             ` [PATCH v3 1/2] IB/umad: Fix error handling Bart Van Assche
     [not found]               ` <5375F0CD.5080809-HInyCGIudOg@public.gmane.org>
2014-05-20  8:33                 ` [PATCH v3.1] " Yann Droneaud
     [not found]                   ` <1400574821-9562-1-git-send-email-ydroneaud-RlY5vtjFyJ3QT0dZR+AlfA@public.gmane.org>
2014-05-20 11:25                     ` Bart Van Assche
     [not found]                       ` <537B3BBE.4040202-HInyCGIudOg@public.gmane.org>
2014-05-20 11:39                         ` Yann Droneaud
2014-05-16 11:05             ` [PATCH v3 2/2] IB/umad: Fix a use-after-free Bart Van Assche
     [not found]               ` <5375F108.20608-HInyCGIudOg@public.gmane.org>
2014-05-16 12:28                 ` Yann Droneaud
2014-06-06 16:25                   ` [RESEND PATCH] " Yann Droneaud
     [not found]                     ` <1402071904-25003-1-git-send-email-ydroneaud-RlY5vtjFyJ3QT0dZR+AlfA@public.gmane.org>
2014-06-06 18:39                       ` Roland Dreier
2014-05-12  8:31   ` [PATCH 3/3] " Bart Van Assche
2014-05-12  9:17   ` [PATCH 1/3] IB/umad: Remove container_of() != NULL tests Bart Van Assche

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=1399889890.3017.6.camel@localhost.localdomain \
    --to=ydroneaud-rly5vtjfyj3qt0dzr+alfa@public.gmane.org \
    --cc=achiang-VXdhtT5mjnY@public.gmane.org \
    --cc=bvanassche-HInyCGIudOg@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=roland-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.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 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.