Skip to contentSkip to conversation

iplink_vrf: Save device index from response for return code

PATCH In review [email protected] 3 participants 3 messages Jun 1, 2018 – Jun 4, 2018

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

PATCH 1/1 iplink_vrf: Save device index from response for return code

From: David Ahern <[email protected]>

A recent commit changed rtnl_talk_* to return the response message in allocated memory so callers need to free it. The change to name_is_vrf did not save the device index which is pointing to a struct inside the now allocated and freed memory resulting in garbage getting returned in some cases.

Fix by using a stack variable to save the return value and only set it to ifi->ifi_index after all checks are done and before the answer buffer is freed.

Fixes 86bf43c7c2fdc ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")
Cc Hangbin Liu <[email protected]>
Cc Phil Sutter <[email protected]>
---
 ip/iplink_vrf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ip/iplink_vrf.c +3 -1
index e9dd0df98412..6004bb4f305e 100644
@@ -191,6 +191,7 @@ int name_is_vrf(const char *name)
 	struct rtattr *tb[IFLA_MAX+1];
 	struct rtattr *li[IFLA_INFO_MAX+1];
 	struct ifinfomsg *ifi;
+	int ifindex = 0;
 	int len;
 
 	addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
@@ -218,7 +219,8 @@ int name_is_vrf(const char *name)
 	if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
 		goto out;
 
+	ifindex = ifi->ifi_index;
 out:
 	free(answer);
-	return ifi->ifi_index;
+	return ifindex;
 }
Signature · 1 line
2.11.0
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · On Fri, Jun 01, 2018 at 08:50:16AM -0700, [email protected] wrote:

From: David Ahern <[email protected]>

A recent commit changed rtnl_talk_* to return the response message in allocated memory so callers need to free it. The change to name_is_vrf did not save the device index which is pointing to a struct inside the now allocated and freed memory resulting in garbage getting returned in some cases.

Quoted 8 lines

Fix by using a stack variable to save the return value and only set it to ifi->ifi_index after all checks are done and before the answer buffer is freed.

Fixes 86bf43c7c2fdc ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")
Cc Hangbin Liu <[email protected]>
Cc Phil Sutter <[email protected]>

Thanks, Phil

Acked-by on v1 1/1
#2 lore raw
In reply to [email protected]
Quoted 45 lines · On Fri, Jun 01, 2018 at 08:50:16AM -0700, [email protected] wrote:

From: David Ahern <[email protected]>

A recent commit changed rtnl_talk_* to return the response message in allocated memory so callers need to free it. The change to name_is_vrf did not save the device index which is pointing to a struct inside the now allocated and freed memory resulting in garbage getting returned in some cases.

Fix by using a stack variable to save the return value and only set it to ifi->ifi_index after all checks are done and before the answer buffer is freed.

Fixes 86bf43c7c2fdc ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")
Cc Hangbin Liu <[email protected]>
Cc Phil Sutter <[email protected]>
---
 ip/iplink_vrf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ip/iplink_vrf.c +3 -1
index e9dd0df98412..6004bb4f305e 100644
@@ -191,6 +191,7 @@ int name_is_vrf(const char *name)
 	struct rtattr *tb[IFLA_MAX+1];
 	struct rtattr *li[IFLA_INFO_MAX+1];
 	struct ifinfomsg *ifi;
+	int ifindex = 0;
 	int len;
 
 	addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
@@ -218,7 +219,8 @@ int name_is_vrf(const char *name)
 	if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
 		goto out;
 
+	ifindex = ifi->ifi_index;
 out:
 	free(answer);
-	return ifi->ifi_index;
+	return ifindex;
 }
Signature · 1 line
2.11.0

Thanks for the fix.