Skip to contentSkip to conversation

Fix memory leakage if kzalloc fails

PATCH In review Souptick Joarder 3 participants 4 messages Oct 5, 2016 – Oct 7, 2016 TI wilink wireless drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory leakage if kzalloc fails

This patch is added to properly handle memory leak if kzalloc fails in wl18xx_scan_send() and wl18xx_scan_sched_scan_config()

---
 drivers/net/wireless/ti/wl18xx/scan.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/net/wireless/ti/wl18xx/scan.c +10 -10
index 4e522154..aed22e1 100644
@@ -41,14 +41,13 @@ static void wl18xx_adjust_channels(struct wl18xx_cmd_scan_params *cmd,
 static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 			    struct cfg80211_scan_request *req)
 {
-	struct wl18xx_cmd_scan_params *cmd;
+	struct wl18xx_cmd_scan_params *cmd = NULL;
 	struct wlcore_scan_channels *cmd_channels = NULL;
 	int ret;
 
 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 	if (!cmd) {
-		ret = -ENOMEM;
-		goto out;
+		return -ENOMEM;
 	}
 
 	/* scan on the dev role if the regular one is not started */
@@ -59,7 +58,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
 	if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
 		ret = -EINVAL;
-		goto out;
+		goto err_cmd_free;
 	}
 
 	cmd->scan_type = SCAN_TYPE_SEARCH;
@@ -84,7 +83,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 	cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
 	if (!cmd_channels) {
 		ret = -ENOMEM;
-		goto out;
+		goto err_cmd_free;
 	}
 
 	wlcore_set_scan_chan_params(wl, cmd_channels, req->channels,
@@ -153,6 +152,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
 out:
 	kfree(cmd_channels);
+err_cmd_free:
 	kfree(cmd);
 	return ret;
 }
@@ -171,7 +171,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 				  struct cfg80211_sched_scan_request *req,
 				  struct ieee80211_scan_ies *ies)
 {
-	struct wl18xx_cmd_scan_params *cmd;
+	struct wl18xx_cmd_scan_params *cmd = NULL;
 	struct wlcore_scan_channels *cmd_channels = NULL;
 	struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
 	int ret;
@@ -185,15 +185,14 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 	if (!cmd) {
-		ret = -ENOMEM;
-		goto out;
+		return -ENOMEM;
 	}
 
 	cmd->role_id = wlvif->role_id;
 
 	if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
 		ret = -EINVAL;
-		goto out;
+		goto err_cmd_free;
 	}
 
 	cmd->scan_type = SCAN_TYPE_PERIODIC;
@@ -218,7 +217,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 	cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
 	if (!cmd_channels) {
 		ret = -ENOMEM;
-		goto out;
+		goto err_cmd_free;
 	}
 
 	/* configure channels */
@@ -296,6 +295,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
 out:
 	kfree(cmd_channels);
+err_cmd_free:
 	kfree(cmd);
 	return ret;
 }
Signature · 1 line
1.9.1
#1 lore raw

Hi,

Quoted 2 lines · Souptick Joarder

This patch is added to properly handle memory leak if kzalloc fails in wl18xx_scan_send() and wl18xx_scan_sched_scan_config()

What memory leak?

Quoted 2 lines

Why two signed-off-bys?

Quoted 44 lines
---
 drivers/net/wireless/ti/wl18xx/scan.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/net/wireless/ti/wl18xx/scan.c +4 -5
index 4e522154..aed22e1 100644
@@ -41,14 +41,13 @@ static void wl18xx_adjust_channels(struct wl18xx_cmd_scan_params *cmd,
 static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
                            struct cfg80211_scan_request *req)
 {
-       struct wl18xx_cmd_scan_params *cmd;
+       struct wl18xx_cmd_scan_params *cmd = NULL;
        struct wlcore_scan_channels *cmd_channels = NULL;
        int ret;
 
        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
        if (!cmd) {
-               ret = -ENOMEM;
-               goto out;
+               return -ENOMEM;
        }
 
        /* scan on the dev role if the regular one is not started */
@@ -59,7 +58,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
        if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
                ret = -EINVAL;
-               goto out;
+               goto err_cmd_free;
        }
 
        cmd->scan_type = SCAN_TYPE_SEARCH;
@@ -84,7 +83,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
        cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
        if (!cmd_channels) {
                ret = -ENOMEM;
-               goto out;
+               goto err_cmd_free;
        }
 
        wlcore_set_scan_chan_params(wl, cmd_channels, req->channels,

drivers/net/wireless/ti/wl18xx/scan.c static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif, · line 155

Quoted 4 lines
drivers/net/wireless/ti/wl18xx/scan.c +1 -0
@@ -153,6 +152,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
 out:
        kfree(cmd_channels);
+err_cmd_free:

kfree(NULL) is valid, so therefore the out: and err_cmd_free: labels are equivalent from a memory freeing perspective, so where exactly are we leaking memory in this function?

Quoted 40 lines
drivers/net/wireless/ti/wl18xx/scan.c +4 -5
        kfree(cmd);
        return ret;
 }
@@ -171,7 +171,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
                                  struct cfg80211_sched_scan_request *req,
                                  struct ieee80211_scan_ies *ies)
 {
-       struct wl18xx_cmd_scan_params *cmd;
+       struct wl18xx_cmd_scan_params *cmd = NULL;
        struct wlcore_scan_channels *cmd_channels = NULL;
        struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
        int ret;
@@ -185,15 +185,14 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
        if (!cmd) {
-               ret = -ENOMEM;
-               goto out;
+               return -ENOMEM;
        }
 
        cmd->role_id = wlvif->role_id;
 
        if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
                ret = -EINVAL;
-               goto out;
+               goto err_cmd_free;
        }
 
        cmd->scan_type = SCAN_TYPE_PERIODIC;
@@ -218,7 +217,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
        cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
        if (!cmd_channels) {
                ret = -ENOMEM;
-               goto out;
+               goto err_cmd_free;
        }
 
        /* configure channels */

drivers/net/wireless/ti/wl18xx/scan.c int wl18xx_scan_sched_scan_config(struct wl1271 *wl, · line 298

Quoted 4 lines
drivers/net/wireless/ti/wl18xx/scan.c +1 -0
@@ -296,6 +295,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
 out:
        kfree(cmd_channels);
+err_cmd_free:

Same question here.

Quoted 6 lines
drivers/net/wireless/ti/wl18xx/scan.c +0 -1
        kfree(cmd);
        return ret;
 }
--

1.9.1

Signature · 4 lines
Julian Calaby

Email: [email protected]
Profile: http://www.google.com/profiles/julian.calaby/
#2 lore raw

Hi Julian,

Quoted 7 lines · Julian Calaby

Hi,

Quoted 2 lines · Souptick Joarder

This patch is added to properly handle memory leak if kzalloc fails in wl18xx_scan_send() and wl18xx_scan_sched_scan_config()

What memory leak?

My Apologies here. I was addressing here about freeing the invalid memories. But I put wrong description. I will resend this patch with proper descriptions and addressing your comments.

Quoted 5 lines
Quoted 3 lines

Why two signed-off-bys?

We both were involved in addressing this.

Quoted 43 lines
Quoted 43 lines
---
 drivers/net/wireless/ti/wl18xx/scan.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/net/wireless/ti/wl18xx/scan.c +4 -5
index 4e522154..aed22e1 100644
@@ -41,14 +41,13 @@ static void wl18xx_adjust_channels(struct wl18xx_cmd_scan_params *cmd,
 static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
                            struct cfg80211_scan_request *req)
 {
-       struct wl18xx_cmd_scan_params *cmd;
+       struct wl18xx_cmd_scan_params *cmd = NULL;
        struct wlcore_scan_channels *cmd_channels = NULL;
        int ret;
 
        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
        if (!cmd) {
-               ret = -ENOMEM;
-               goto out;
+               return -ENOMEM;
        }
 
        /* scan on the dev role if the regular one is not started */
@@ -59,7 +58,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
        if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
                ret = -EINVAL;
-               goto out;
+               goto err_cmd_free;
        }
 
        cmd->scan_type = SCAN_TYPE_SEARCH;
@@ -84,7 +83,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
        cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
        if (!cmd_channels) {
                ret = -ENOMEM;
-               goto out;
+               goto err_cmd_free;
        }
 
Quoted 8 lines

drivers/net/wireless/ti/wl18xx/scan.c static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif, · line 155

Quoted 6 lines
drivers/net/wireless/ti/wl18xx/scan.c +1 -0
        wlcore_set_scan_chan_params(wl, cmd_channels, req->channels,
@@ -153,6 +152,7 @@ static int wl18xx_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 
 out:
        kfree(cmd_channels);
+err_cmd_free:

kfree(NULL) is valid,

I agree with you. As *cmd not initialized with NULL, so it can hold a garbage address.
In case of memory allocation failure, kernel may enter in abnormal behavior while freeing this memory.

so therefore the out: and err_cmd_free: labels

Quoted 2 lines

are equivalent from a memory freeing perspective, so where exactly are we leaking memory in this function?

I want to avoid kfree(cmd_channels) calls when we have not allocated the memory.

Quoted 39 lines
Quoted 39 lines
drivers/net/wireless/ti/wl18xx/scan.c +4 -5
        kfree(cmd);
        return ret;
 }
@@ -171,7 +171,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
                                  struct cfg80211_sched_scan_request *req,
                                  struct ieee80211_scan_ies *ies)
 {
-       struct wl18xx_cmd_scan_params *cmd;
+       struct wl18xx_cmd_scan_params *cmd = NULL;
        struct wlcore_scan_channels *cmd_channels = NULL;
        struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
        int ret;
@@ -185,15 +185,14 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
        if (!cmd) {
-               ret = -ENOMEM;
-               goto out;
+               return -ENOMEM;
        }
 
        cmd->role_id = wlvif->role_id;
 
        if (WARN_ON(cmd->role_id == WL12XX_INVALID_ROLE_ID)) {
                ret = -EINVAL;
-               goto out;
+               goto err_cmd_free;
        }
 
        cmd->scan_type = SCAN_TYPE_PERIODIC;
@@ -218,7 +217,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
        cmd_channels = kzalloc(sizeof(*cmd_channels), GFP_KERNEL);
        if (!cmd_channels) {
                ret = -ENOMEM;
-               goto out;
+               goto err_cmd_free;
        }
 
Quoted 8 lines

drivers/net/wireless/ti/wl18xx/scan.c int wl18xx_scan_sched_scan_config(struct wl1271 *wl, · line 298

Quoted 6 lines
drivers/net/wireless/ti/wl18xx/scan.c +1 -0
        /* configure channels */
@@ -296,6 +295,7 @@ int wl18xx_scan_sched_scan_config(struct wl1271 *wl,
 
 out:
        kfree(cmd_channels);
+err_cmd_free:

Same question here.

Please refer above comment for the same.

Quoted 15 lines
Quoted 7 lines
drivers/net/wireless/ti/wl18xx/scan.c +0 -1
        kfree(cmd);
        return ret;
 }
--

1.9.1

--
Julian Calaby

Email: [email protected]
Profile: http://www.google.com/profiles/julian.calaby/

#3 lore raw
Quoted 7 lines · Souptick Joarder

Hi Julian,

Quoted 3 lines · Julian Calaby

Hi,

On Wed, Oct 5, 2016 at 10:50 PM, Souptick Joarder <[email protected]> wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

This patch is added to properly handle memory leak if kzalloc fails in wl18xx_scan_send() and wl18xx_scan_sched_scan_config()

What memory leak?

My Apologies here. I was addressing here about freeing the invalid memories. But I put wrong description. I will resend this patch with proper descriptions and addressing your comments.

Also the prefix in the title should be "wl18xx: ", not "[wl18xx]". See:

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches#subject_name

Signature · 1 line
Kalle Valo