All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2] iplink_vrf: Save device index from response for return code
@ 2018-06-01 15:50 dsahern
  2018-06-01 17:05 ` Phil Sutter
  2018-06-04  1:11 ` Hangbin Liu
  0 siblings, 2 replies; 3+ messages in thread
From: dsahern @ 2018-06-01 15:50 UTC (permalink / raw)
  To: stephen, netdev; +Cc: David Ahern, Hangbin Liu, Phil Sutter

From: David Ahern <dsahern@gmail.com>

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 <liuhangbin@gmail.com>
Cc: Phil Sutter <phil@nwl.cc>
Signed-off-by: David Ahern <dsahern@gmail.com>
---
 ip/iplink_vrf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ip/iplink_vrf.c b/ip/iplink_vrf.c
index e9dd0df98412..6004bb4f305e 100644
--- a/ip/iplink_vrf.c
+++ b/ip/iplink_vrf.c
@@ -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;
 }
-- 
2.11.0

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

* Re: [PATCH iproute2] iplink_vrf: Save device index from response for return code
  2018-06-01 15:50 [PATCH iproute2] iplink_vrf: Save device index from response for return code dsahern
@ 2018-06-01 17:05 ` Phil Sutter
  2018-06-04  1:11 ` Hangbin Liu
  1 sibling, 0 replies; 3+ messages in thread
From: Phil Sutter @ 2018-06-01 17:05 UTC (permalink / raw)
  To: dsahern; +Cc: stephen, netdev, David Ahern, Hangbin Liu

On Fri, Jun 01, 2018 at 08:50:16AM -0700, dsahern@kernel.org wrote:
> From: David Ahern <dsahern@gmail.com>
> 
> 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 <liuhangbin@gmail.com>
> Cc: Phil Sutter <phil@nwl.cc>
> Signed-off-by: David Ahern <dsahern@gmail.com>

Acked-by: Phil Sutter <phil@nwl.cc>

Thanks, Phil

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

* Re: [PATCH iproute2] iplink_vrf: Save device index from response for return code
  2018-06-01 15:50 [PATCH iproute2] iplink_vrf: Save device index from response for return code dsahern
  2018-06-01 17:05 ` Phil Sutter
@ 2018-06-04  1:11 ` Hangbin Liu
  1 sibling, 0 replies; 3+ messages in thread
From: Hangbin Liu @ 2018-06-04  1:11 UTC (permalink / raw)
  To: dsahern; +Cc: stephen, netdev, David Ahern, Phil Sutter

On Fri, Jun 01, 2018 at 08:50:16AM -0700, dsahern@kernel.org wrote:
> From: David Ahern <dsahern@gmail.com>
> 
> 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 <liuhangbin@gmail.com>
> Cc: Phil Sutter <phil@nwl.cc>
> Signed-off-by: David Ahern <dsahern@gmail.com>
> ---
>  ip/iplink_vrf.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/ip/iplink_vrf.c b/ip/iplink_vrf.c
> index e9dd0df98412..6004bb4f305e 100644
> --- a/ip/iplink_vrf.c
> +++ b/ip/iplink_vrf.c
> @@ -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;
>  }
> -- 
> 2.11.0
> 

Thanks for the fix.

Acked-by: Hangbin Liu <liuhangbin@gmail.com>

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

end of thread, other threads:[~2018-06-04  1:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-01 15:50 [PATCH iproute2] iplink_vrf: Save device index from response for return code dsahern
2018-06-01 17:05 ` Phil Sutter
2018-06-04  1:11 ` Hangbin Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.