public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nfc:  Fix potential resource leaks
@ 2022-12-20 13:46 Miaoqian Lin
  2022-12-20 14:18 ` Michal Swiatkowski
  2022-12-21  9:45 ` Krzysztof Kozlowski
  0 siblings, 2 replies; 3+ messages in thread
From: Miaoqian Lin @ 2022-12-20 13:46 UTC (permalink / raw)
  To: Krzysztof Kozlowski, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Samuel Ortiz, Christophe Ricard,
	netdev, linux-kernel
  Cc: linmq006

nfc_get_device() take reference for the device, add missing
nfc_put_device() to release it when not need anymore.
Also fix the style warnning by use error EOPNOTSUPP instead of
ENOTSUPP.

Fixes: 5ce3f32b5264 ("NFC: netlink: SE API implementation")
Fixes: 29e76924cf08 ("nfc: netlink: Add capability to reply to vendor_cmd with data")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 net/nfc/netlink.c | 51 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 14 deletions(-)

diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index 9d91087b9399..d081beaf4828 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -1497,6 +1497,7 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
 	u32 dev_idx, se_idx;
 	u8 *apdu;
 	size_t apdu_len;
+	int error;
 
 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
 	    !info->attrs[NFC_ATTR_SE_INDEX] ||
@@ -1510,25 +1511,37 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
 	if (!dev)
 		return -ENODEV;
 
-	if (!dev->ops || !dev->ops->se_io)
-		return -ENOTSUPP;
+	if (!dev->ops || !dev->ops->se_io) {
+		error = -EOPNOTSUPP;
+		goto put_dev;
+	}
 
 	apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
-	if (apdu_len == 0)
-		return -EINVAL;
+	if (apdu_len == 0) {
+		error = -EINVAL;
+		goto put_dev;
+	}
 
 	apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
-	if (!apdu)
-		return -EINVAL;
+	if (!apdu) {
+		error = -EINVAL;
+		goto put_dev;
+	}
 
 	ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
-	if (!ctx)
-		return -ENOMEM;
+	if (!ctx) {
+		error = -ENOMEM;
+		goto put_dev;
+	}
 
 	ctx->dev_idx = dev_idx;
 	ctx->se_idx = se_idx;
 
-	return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
+	error = nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
+
+put_dev:
+	nfc_put_device(dev);
+	return error;
 }
 
 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
@@ -1551,14 +1564,20 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb,
 	subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
 
 	dev = nfc_get_device(dev_idx);
-	if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
+	if (!dev)
 		return -ENODEV;
+	if (!dev->vendor_cmds || !dev->n_vendor_cmds) {
+		err = -ENODEV;
+		goto put_dev;
+	}
 
 	if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
 		data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
 		data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
-		if (data_len == 0)
-			return -EINVAL;
+		if (data_len == 0) {
+			err = -EINVAL;
+			goto put_dev;
+		}
 	} else {
 		data = NULL;
 		data_len = 0;
@@ -1573,10 +1592,14 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb,
 		dev->cur_cmd_info = info;
 		err = cmd->doit(dev, data, data_len);
 		dev->cur_cmd_info = NULL;
-		return err;
+		goto put_dev;
 	}
 
-	return -EOPNOTSUPP;
+	err = -EOPNOTSUPP;
+
+put_dev:
+	nfc_put_device(dev);
+	return err;
 }
 
 /* message building helper */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nfc:  Fix potential resource leaks
  2022-12-20 13:46 [PATCH] nfc: Fix potential resource leaks Miaoqian Lin
@ 2022-12-20 14:18 ` Michal Swiatkowski
  2022-12-21  9:45 ` Krzysztof Kozlowski
  1 sibling, 0 replies; 3+ messages in thread
From: Michal Swiatkowski @ 2022-12-20 14:18 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: Krzysztof Kozlowski, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Samuel Ortiz, Christophe Ricard,
	netdev, linux-kernel

On Tue, Dec 20, 2022 at 05:46:23PM +0400, Miaoqian Lin wrote:
> nfc_get_device() take reference for the device, add missing
> nfc_put_device() to release it when not need anymore.
> Also fix the style warnning by use error EOPNOTSUPP instead of
> ENOTSUPP.
> 
> Fixes: 5ce3f32b5264 ("NFC: netlink: SE API implementation")
> Fixes: 29e76924cf08 ("nfc: netlink: Add capability to reply to vendor_cmd with data")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
Nice catch
Reviewed-by: Michal Swiatkowski <michal.swiatkowski@linux.intel.com>
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nfc: Fix potential resource leaks
  2022-12-20 13:46 [PATCH] nfc: Fix potential resource leaks Miaoqian Lin
  2022-12-20 14:18 ` Michal Swiatkowski
@ 2022-12-21  9:45 ` Krzysztof Kozlowski
  1 sibling, 0 replies; 3+ messages in thread
From: Krzysztof Kozlowski @ 2022-12-21  9:45 UTC (permalink / raw)
  To: Miaoqian Lin, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Samuel Ortiz, Christophe Ricard, netdev,
	linux-kernel

On 20/12/2022 14:46, Miaoqian Lin wrote:
> nfc_get_device() take reference for the device, add missing
> nfc_put_device() to release it when not need anymore.
> Also fix the style warnning by use error EOPNOTSUPP instead of
> ENOTSUPP.
> 
> Fixes: 5ce3f32b5264 ("NFC: netlink: SE API implementation")
> Fixes: 29e76924cf08 ("nfc: netlink: Add capability to reply to vendor_cmd with data")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  net/nfc/netlink.c | 51 ++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 37 insertions(+), 14 deletions(-)
> 
> diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
> index 9d91087b9399..d081beaf4828 100644
> --- a/net/nfc/netlink.c
> +++ b/net/nfc/netlink.c
> @@ -1497,6 +1497,7 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
>  	u32 dev_idx, se_idx;
>  	u8 *apdu;
>  	size_t apdu_len;
> +	int error;

Let's don't introduce the third or fourth style. Existing code calls it
"rc".

>  
>  	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
>  	    !info->attrs[NFC_ATTR_SE_INDEX] ||
> @@ -1510,25 +1511,37 @@ static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
>  	if (!dev)
>  		return -ENODEV;
>  
> -	if (!dev->ops || !dev->ops->se_io)
> -		return -ENOTSUPP;
> +	if (!dev->ops || !dev->ops->se_io) {
> +		error = -EOPNOTSUPP;
> +		goto put_dev;
> +	}
>  
>  	apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
> -	if (apdu_len == 0)
> -		return -EINVAL;
> +	if (apdu_len == 0) {
> +		error = -EINVAL;
> +		goto put_dev;
> +	}
>  
>  	apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
> -	if (!apdu)
> -		return -EINVAL;
> +	if (!apdu) {
> +		error = -EINVAL;
> +		goto put_dev;
> +	}
>  
>  	ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
> -	if (!ctx)
> -		return -ENOMEM;
> +	if (!ctx) {
> +		error = -ENOMEM;
> +		goto put_dev;
> +	}
>  
>  	ctx->dev_idx = dev_idx;
>  	ctx->se_idx = se_idx;
>  
> -	return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
> +	error = nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
> +
> +put_dev:
> +	nfc_put_device(dev);
> +	return error;
>  }
>  
>  static int nfc_genl_vendor_cmd(struct sk_buff *skb,
> @@ -1551,14 +1564,20 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb,
>  	subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
>  
>  	dev = nfc_get_device(dev_idx);
> -	if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
> +	if (!dev)
>  		return -ENODEV;

Blank line

> +	if (!dev->vendor_cmds || !dev->n_vendor_cmds) {
> +		err = -ENODEV;
> +		goto put_dev;
> +	}
>  
>  	if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
>  		data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
>  		data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
> -		if (data_len == 0)
> -			return -EINVAL;
> +		if (data_len == 0) {
> +			err = -EINVAL;
> +			goto put_dev;
> +		}
>  	} else {
>  		data = NULL;
>  		data_len = 0;
> @@ -1573,10 +1592,14 @@ static int nfc_genl_vendor_cmd(struct sk_buff *skb,
>  		dev->cur_cmd_info = info;
>  		err = cmd->doit(dev, data, data_len);
>  		dev->cur_cmd_info = NULL;
> -		return err;
> +		goto put_dev;
>  	}
>  
> -	return -EOPNOTSUPP;
> +	err = -EOPNOTSUPP;
> +
> +put_dev:
> +	nfc_put_device(dev);
> +	return err;
>  }
>  
>  /* message building helper */

Best regards,
Krzysztof


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-21  9:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-20 13:46 [PATCH] nfc: Fix potential resource leaks Miaoqian Lin
2022-12-20 14:18 ` Michal Swiatkowski
2022-12-21  9:45 ` Krzysztof Kozlowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox