diff mbox

[3/5] ipv6: Fix memory leak in set_ipv6_address() / ip6_create_ll_address()

Message ID 1462218931-4573-4-git-send-email-thuth@redhat.com
State Accepted
Headers show

Commit Message

Thomas Huth May 2, 2016, 7:55 p.m. UTC
The  set_ipv6_address() function calls ip6_create_ll_address() to
get a link-local address. The latter function uses malloc to create
a buffer for composing that address, and returns the corresponding
poniter to the caller. However, set_ipv6_address() does not free
that buffer again, so the allocated memory is lost.
Since set_ipv6_address() already allocated space for the new IPv6
address anyway, let's fix this issue by passing the buffer from
set_ipv6_address() to ip6_create_ll_address() instead, so that
ip6_create_ll_address() does not have to allocate memory at all.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 clients/net-snk/app/netlib/ipv6.c | 18 +++++-------------
 clients/net-snk/app/netlib/ipv6.h |  2 +-
 2 files changed, 6 insertions(+), 14 deletions(-)

Comments

Andrew Jones May 3, 2016, 5:28 a.m. UTC | #1
On Mon, May 02, 2016 at 09:55:29PM +0200, Thomas Huth wrote:
> The  set_ipv6_address() function calls ip6_create_ll_address() to
> get a link-local address. The latter function uses malloc to create
> a buffer for composing that address, and returns the corresponding
> poniter to the caller. However, set_ipv6_address() does not free
> that buffer again, so the allocated memory is lost.
> Since set_ipv6_address() already allocated space for the new IPv6
> address anyway, let's fix this issue by passing the buffer from
> set_ipv6_address() to ip6_create_ll_address() instead, so that
> ip6_create_ll_address() does not have to allocate memory at all.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  clients/net-snk/app/netlib/ipv6.c | 18 +++++-------------
>  clients/net-snk/app/netlib/ipv6.h |  2 +-
>  2 files changed, 6 insertions(+), 14 deletions(-)

We could probably just get rid of ip6_create_ll_address, opencoding
the two-liner at its single callsite. Anyway,

Reviewed-by: Andrew Jones <drjones@redhat.com>

> 
> diff --git a/clients/net-snk/app/netlib/ipv6.c b/clients/net-snk/app/netlib/ipv6.c
> index baa5034..220fd36 100644
> --- a/clients/net-snk/app/netlib/ipv6.c
> +++ b/clients/net-snk/app/netlib/ipv6.c
> @@ -77,9 +77,7 @@ void set_ipv6_address(int fd, ip6_addr_t *_own_ip6)
>  	/* If no address was passed as a parameter generate a link-local
>  	 * address from our MAC address.*/
>  	if (_own_ip6 == NULL)
> -		memcpy(&(own_ip6->addr.addr),
> -			ip6_create_ll_address(get_mac_address()),
> -		       IPV6_ADDR_LENGTH);
> +		ip6_create_ll_address(get_mac_address(), &own_ip6->addr);
>  	else
>  		memcpy (&(own_ip6->addr.addr), _own_ip6, 16);
>  
> @@ -225,18 +223,12 @@ uint64_t mac2eui64(const uint8_t *mac)
>   * NET: create link-local IPv6 address
>   *
>   * @param  own_mac    MAC of NIC
> - * @return ll_addr    pointer to newly created link-local address
> + * @param ll_addr     pointer to link-local address which should be created
>   */
> -ip6_addr_t *ip6_create_ll_address(const uint8_t *own_mac)
> +void ip6_create_ll_address(const uint8_t *own_mac, ip6_addr_t *ll_addr)
>  {
> -	ip6_addr_t *ll_addr;
> -
> -	ll_addr = malloc (sizeof (struct ip6addr_list_entry));
> -	memset (ll_addr, 0, IPV6_ADDR_LENGTH);
> -	ll_addr->part.prefix       |= IPV6_LL_PREFIX;
> -	ll_addr->part.interface_id |= mac2eui64((uint8_t *) own_mac);
> -
> -	return ll_addr;
> +	ll_addr->part.prefix = IPV6_LL_PREFIX;
> +	ll_addr->part.interface_id = mac2eui64((uint8_t *) own_mac);
>  }
>  
>  /*
> diff --git a/clients/net-snk/app/netlib/ipv6.h b/clients/net-snk/app/netlib/ipv6.h
> index 72c6ee2..6565a88 100644
> --- a/clients/net-snk/app/netlib/ipv6.h
> +++ b/clients/net-snk/app/netlib/ipv6.h
> @@ -150,7 +150,7 @@ void set_ipv6_address(int fd, ip6_addr_t *own_ip6);
>  ip6_addr_t *get_ipv6_address(void);
>  
>  /* Create link-local address from a given Mac Address */
> -ip6_addr_t * ip6_create_ll_address (const uint8_t *own_mac);
> +void ip6_create_ll_address (const uint8_t *own_mac, ip6_addr_t *ll_addr);
>  
>  /* For a given MAC calculates EUI64-Identifier.*/
>  uint64_t mac2eui64 (const uint8_t *mac);
> -- 
> 1.8.3.1
>
diff mbox

Patch

diff --git a/clients/net-snk/app/netlib/ipv6.c b/clients/net-snk/app/netlib/ipv6.c
index baa5034..220fd36 100644
--- a/clients/net-snk/app/netlib/ipv6.c
+++ b/clients/net-snk/app/netlib/ipv6.c
@@ -77,9 +77,7 @@  void set_ipv6_address(int fd, ip6_addr_t *_own_ip6)
 	/* If no address was passed as a parameter generate a link-local
 	 * address from our MAC address.*/
 	if (_own_ip6 == NULL)
-		memcpy(&(own_ip6->addr.addr),
-			ip6_create_ll_address(get_mac_address()),
-		       IPV6_ADDR_LENGTH);
+		ip6_create_ll_address(get_mac_address(), &own_ip6->addr);
 	else
 		memcpy (&(own_ip6->addr.addr), _own_ip6, 16);
 
@@ -225,18 +223,12 @@  uint64_t mac2eui64(const uint8_t *mac)
  * NET: create link-local IPv6 address
  *
  * @param  own_mac    MAC of NIC
- * @return ll_addr    pointer to newly created link-local address
+ * @param ll_addr     pointer to link-local address which should be created
  */
-ip6_addr_t *ip6_create_ll_address(const uint8_t *own_mac)
+void ip6_create_ll_address(const uint8_t *own_mac, ip6_addr_t *ll_addr)
 {
-	ip6_addr_t *ll_addr;
-
-	ll_addr = malloc (sizeof (struct ip6addr_list_entry));
-	memset (ll_addr, 0, IPV6_ADDR_LENGTH);
-	ll_addr->part.prefix       |= IPV6_LL_PREFIX;
-	ll_addr->part.interface_id |= mac2eui64((uint8_t *) own_mac);
-
-	return ll_addr;
+	ll_addr->part.prefix = IPV6_LL_PREFIX;
+	ll_addr->part.interface_id = mac2eui64((uint8_t *) own_mac);
 }
 
 /*
diff --git a/clients/net-snk/app/netlib/ipv6.h b/clients/net-snk/app/netlib/ipv6.h
index 72c6ee2..6565a88 100644
--- a/clients/net-snk/app/netlib/ipv6.h
+++ b/clients/net-snk/app/netlib/ipv6.h
@@ -150,7 +150,7 @@  void set_ipv6_address(int fd, ip6_addr_t *own_ip6);
 ip6_addr_t *get_ipv6_address(void);
 
 /* Create link-local address from a given Mac Address */
-ip6_addr_t * ip6_create_ll_address (const uint8_t *own_mac);
+void ip6_create_ll_address (const uint8_t *own_mac, ip6_addr_t *ll_addr);
 
 /* For a given MAC calculates EUI64-Identifier.*/
 uint64_t mac2eui64 (const uint8_t *mac);