Skip to contentSkip to conversation

net: openvswitch: fix possible memory leak in ovs_meter_cmd_set()

PATCH Superseded Hangyu Hua 3 participants 4 messages Feb 9, 2023 – Feb 10, 2023

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH v2 1/1 net: openvswitch: fix possible memory leak in ovs_meter_cmd_set()

old_meter needs to be free after it is detached regardless of whether the new meter is successfully attached.

Fixes c7c4c44c9a95 ("net: openvswitch: expand the meters supported number")

---

v2: use goto label and free old_meter outside of ovs lock.

 net/openvswitch/meter.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/openvswitch/meter.c +9 -3
index 6e38f68f88c2..9b680f0894f1 100644
@@ -417,6 +417,7 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	int err;
 	u32 meter_id;
 	bool failed;
+	bool locked = true;
 
 	if (!a[OVS_METER_ATTR_ID])
 		return -EINVAL;
@@ -448,11 +449,13 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 		goto exit_unlock;
 
 	err = attach_meter(meter_tbl, meter);
-	if (err)
-		goto exit_unlock;
 
 	ovs_unlock();
 
+	if (err) {
+		locked = false;
+		goto exit_free_old_meter;
+	}
 	/* Build response with the meter_id and stats from
 	 * the old meter, if any.
 	 */
@@ -472,8 +475,11 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	genlmsg_end(reply, ovs_reply_header);
 	return genlmsg_reply(reply, info);
 
+exit_free_old_meter:
+	ovs_meter_free(old_meter);
 exit_unlock:
-	ovs_unlock();
+	if (locked)
+		ovs_unlock();
 	nlmsg_free(reply);
 exit_free_meter:
 	kfree(meter);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 41 lines · Hangyu Hua

old_meter needs to be free after it is detached regardless of whether the new meter is successfully attached.

Fixes c7c4c44c9a95 ("net: openvswitch: expand the meters supported number")

---

v2: use goto label and free old_meter outside of ovs lock.

 net/openvswitch/meter.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/openvswitch/meter.c +5 -2
index 6e38f68f88c2..9b680f0894f1 100644
@@ -417,6 +417,7 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	int err;
 	u32 meter_id;
 	bool failed;
+	bool locked = true;
 
 	if (!a[OVS_METER_ATTR_ID])
 		return -EINVAL;
@@ -448,11 +449,13 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 		goto exit_unlock;
 
 	err = attach_meter(meter_tbl, meter);
-	if (err)
-		goto exit_unlock;
 
 	ovs_unlock();
 
+	if (err) {
+		locked = false;
+		goto exit_free_old_meter;
+	}
 	/* Build response with the meter_id and stats from
 	 * the old meter, if any.
 	 */

net/openvswitch/meter.c static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info) · line 482

Quoted 9 lines
net/openvswitch/meter.c +4 -1
@@ -472,8 +475,11 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	genlmsg_end(reply, ovs_reply_header);
 	return genlmsg_reply(reply, info);
 
+exit_free_old_meter:
+	ovs_meter_free(old_meter);
 exit_unlock:
-	ovs_unlock();
+	if (locked)
+		ovs_unlock();

I see where you are going here, but is the complexity of the locked variable worth the benefit of freeing old_meter outside the lock?

Quoted 11 lines
net/openvswitch/meter.c +0 -0
 	nlmsg_free(reply);
 exit_free_meter:
 	kfree(meter);
Signature · 6 lines
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev
maintainer
#2 lore raw
Quoted 45 lines · Simon Horman
Quoted 44 lines · Hangyu Hua

old_meter needs to be free after it is detached regardless of whether the new meter is successfully attached.

Fixes c7c4c44c9a95 ("net: openvswitch: expand the meters supported number")

---

v2: use goto label and free old_meter outside of ovs lock.

 net/openvswitch/meter.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/openvswitch/meter.c +5 -2
index 6e38f68f88c2..9b680f0894f1 100644
@@ -417,6 +417,7 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	int err;
 	u32 meter_id;
 	bool failed;
+	bool locked = true;
 
 	if (!a[OVS_METER_ATTR_ID])
 		return -EINVAL;
@@ -448,11 +449,13 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 		goto exit_unlock;
 
 	err = attach_meter(meter_tbl, meter);
-	if (err)
-		goto exit_unlock;
 
 	ovs_unlock();
 
+	if (err) {
+		locked = false;
+		goto exit_free_old_meter;
+	}
 	/* Build response with the meter_id and stats from
 	 * the old meter, if any.
 	 */
@@ -472,8 +475,11 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
 	genlmsg_end(reply, ovs_reply_header);
 	return genlmsg_reply(reply, info);
 
Quoted 10 lines

net/openvswitch/meter.c static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info) · line 482

Quoted 6 lines
net/openvswitch/meter.c +4 -1
+exit_free_old_meter:
+	ovs_meter_free(old_meter);
 exit_unlock:
-	ovs_unlock();
+	if (locked)
+		ovs_unlock();

I see where you are going here, but is the complexity of the locked variable worth the benefit of freeing old_meter outside the lock?

Looking at the error path, I would agree with Simon, and just add an “exit_free_old_meter” label as suggested in v1 and keep the lock in place to make the error path more straightforward.

//Eelco

Quoted 11 lines
Quoted 11 lines
net/openvswitch/meter.c +0 -0
 	nlmsg_free(reply);
 exit_free_meter:
 	kfree(meter);
Signature · 6 lines
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev
author
#3 lore raw
Quoted 60 lines · Eelco Chaudron
Quoted 45 lines · Simon Horman
Quoted 44 lines · Hangyu Hua

old_meter needs to be free after it is detached regardless of whether the new meter is successfully attached.

Fixes c7c4c44c9a95 ("net: openvswitch: expand the meters supported number")

---

v2: use goto label and free old_meter outside of ovs lock.

  net/openvswitch/meter.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/openvswitch/meter.c +5 -2
index 6e38f68f88c2..9b680f0894f1 100644
@@ -417,6 +417,7 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
  	int err;
  	u32 meter_id;
  	bool failed;
+	bool locked = true;
 
  	if (!a[OVS_METER_ATTR_ID])
  		return -EINVAL;
@@ -448,11 +449,13 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
  		goto exit_unlock;
 
  	err = attach_meter(meter_tbl, meter);
-	if (err)
-		goto exit_unlock;
 
  	ovs_unlock();
 
+	if (err) {
+		locked = false;
+		goto exit_free_old_meter;
+	}
  	/* Build response with the meter_id and stats from
  	 * the old meter, if any.
  	 */
@@ -472,8 +475,11 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info)
  	genlmsg_end(reply, ovs_reply_header);
  	return genlmsg_reply(reply, info);
 
Quoted 10 lines

net/openvswitch/meter.c static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info) · line 482

Quoted 6 lines
net/openvswitch/meter.c +4 -1
+exit_free_old_meter:
+	ovs_meter_free(old_meter);
  exit_unlock:
-	ovs_unlock();
+	if (locked)
+		ovs_unlock();

I see where you are going here, but is the complexity of the locked variable worth the benefit of freeing old_meter outside the lock?

Quoted 3 lines

Looking at the error path, I would agree with Simon, and just add an “exit_free_old_meter” label as suggested in v1 and keep the lock in place to make the error path more straightforward.

//Eelco

I see. I will send a v3 later.

Thanks,
Hangyu

Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
net/openvswitch/meter.c +0 -0
  	nlmsg_free(reply);
  exit_free_meter:
  	kfree(meter);
Signature · 6 lines
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev