Skip to contentSkip to conversation

ipaddrlabel: Fix descriptor leak in flush_addrlabel()

PATCH In review Maks Mishin 3 participants 3 messages Feb 7, 2024 – Feb 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipaddrlabel: Fix descriptor leak in flush_addrlabel()

Added closure of descriptor rth2.fd created by rtnl_open() when returning from function.

---
 ip/ipaddrlabel.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ip/ipaddrlabel.c +3 -1
index b045827a..3857fcf5 100644
@@ -201,8 +201,10 @@ static int flush_addrlabel(struct nlmsghdr *n, void *arg)
 		n->nlmsg_type = RTM_DELADDRLABEL;
 		n->nlmsg_flags = NLM_F_REQUEST;
 
-		if (rtnl_open(&rth2, 0) < 0)
+		if (rtnl_open(&rth2, 0) < 0) {
+			rtnl_close(&rth2);
 			return -1;
+		}
 
 		if (rtnl_talk(&rth2, n, NULL) < 0)
 			return -2;
Signature · 1 line
2.30.2
#1 lore raw
Quoted 13 lines · Maks Mishin

Added closure of descriptor rth2.fd created by rtnl_open() when returning from function.

---
 ip/ipaddrlabel.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ip/ipaddrlabel.c +0 -0
index b045827a..3857fcf5 100644

ip/ipaddrlabel.c static int flush_addrlabel(struct nlmsghdr *n, void *arg) · line 205

Quoted 6 lines
ip/ipaddrlabel.c +2 -1
@@ -201,8 +201,10 @@ static int flush_addrlabel(struct nlmsghdr *n, void *arg)
 		n->nlmsg_type = RTM_DELADDRLABEL;
 		n->nlmsg_flags = NLM_F_REQUEST;
 
-		if (rtnl_open(&rth2, 0) < 0)
+		if (rtnl_open(&rth2, 0) < 0) {
+			rtnl_close(&rth2);

This change is unnecessary. You're calling rtnl_close() in the case where rtnl_open() just failed so there's nothing to close.

Quoted 5 lines
ip/ipaddrlabel.c +1 -0
 			return -1;
+		}
 
 		if (rtnl_talk(&rth2, n, NULL) < 0)
 			return -2;
#2 lore raw
In reply to Maks Mishin
Quoted 14 lines · On Wed, 7 Feb 2024 23:25:42 +0300 Maks Mishin <[email protected]> wrote:

Added closure of descriptor rth2.fd created by rtnl_open() when returning from function.

---
 ip/ipaddrlabel.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ip/ipaddrlabel.c +0 -0
index b045827a..3857fcf5 100644
@@ -201,8 +201,10 @@ static int flush_addrlabel(struct nlmsghdr *n, void *arg)
 		n->nlmsg_type = RTM_DELADDRLABEL;

ip/ipaddrlabel.c static int flush_addrlabel(struct nlmsghdr *n, void *arg) · line 210

Quoted 10 lines
ip/ipaddrlabel.c +3 -1
 		n->nlmsg_flags = NLM_F_REQUEST;
 
-		if (rtnl_open(&rth2, 0) < 0)
+		if (rtnl_open(&rth2, 0) < 0) {
+			rtnl_close(&rth2);
 			return -1;
+		}
 
 		if (rtnl_talk(&rth2, n, NULL) < 0)
 			return -2;

This doesn't make sense, the error path in rtnl_open cleans up itself. Do you have a reproducer or is this just some static analyzer?