[v3] check_native: Get rid of alloca

Message ID 20230613140503.1054857-1-josimmon@redhat.com
State Committed
Commit 9a17a193b4ea5730aed0b9e532ae741418dd10d3
Headers
Series [v3] check_native: Get rid of alloca |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Joe Simmons-Talbott June 13, 2023, 2:05 p.m. UTC
  Use malloc rather than alloca to avoid potential stack overflow.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
Changes to v2:
  * add space between free and (.

 sysdeps/unix/sysv/linux/check_native.c | 35 ++++++++------------------
 1 file changed, 11 insertions(+), 24 deletions(-)
  

Comments

Joe Simmons-Talbott June 21, 2023, 5:16 p.m. UTC | #1
On Tue, Jun 13, 2023 at 10:05:03AM -0400, Joe Simmons-Talbott wrote:
> Use malloc rather than alloca to avoid potential stack overflow.
> 
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Can this be applied?

Thanks,
Joe
> ---
> Changes to v2:
>   * add space between free and (.
> 
>  sysdeps/unix/sysv/linux/check_native.c | 35 ++++++++------------------
>  1 file changed, 11 insertions(+), 24 deletions(-)
> 
> diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c
> index 34876ca624..c4aaf4af19 100644
> --- a/sysdeps/unix/sysv/linux/check_native.c
> +++ b/sysdeps/unix/sysv/linux/check_native.c
> @@ -48,11 +48,20 @@ __check_native (uint32_t a1_index, int *a1_native,
>    nladdr.nl_family = AF_NETLINK;
>  
>    socklen_t addr_len = sizeof (nladdr);
> -  bool use_malloc = false;
>  
>    if (fd < 0)
>      return;
>  
> +  /* Netlink requires that user buffer needs to be either 8kb or page size
> +     (whichever is bigger), however this has been changed over time and now
> +     8Kb is sufficient (check NLMSG_DEFAULT_SIZE on Linux
> +     linux/include/linux/netlink.h).  */
> +  const size_t buf_size = 8192;
> +  char *buf = malloc (buf_size);
> +
> +  if (buf == NULL)
> +    goto out;
> +
>    if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0
>        || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0)
>      goto out;
> @@ -81,26 +90,6 @@ __check_native (uint32_t a1_index, int *a1_native,
>    memset (&nladdr, '\0', sizeof (nladdr));
>    nladdr.nl_family = AF_NETLINK;
>  
> -#ifdef PAGE_SIZE
> -  /* Help the compiler optimize out the malloc call if PAGE_SIZE
> -     is constant and smaller or equal to PTHREAD_STACK_MIN/4.  */
> -  const size_t buf_size = PAGE_SIZE;
> -#else
> -  const size_t buf_size = __getpagesize ();
> -#endif
> -  char *buf;
> -
> -  if (__libc_use_alloca (buf_size))
> -    buf = alloca (buf_size);
> -  else
> -    {
> -      buf = malloc (buf_size);
> -      if (buf != NULL)
> -	use_malloc = true;
> -      else
> -	goto out;
> -    }
> -
>    struct iovec iov = { buf, buf_size };
>  
>    if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
> @@ -170,7 +159,5 @@ __check_native (uint32_t a1_index, int *a1_native,
>  
>  out:
>    __close_nocancel_nostatus (fd);
> -
> -  if (use_malloc)
> -    free (buf);
> +  free (buf);
>  }
> -- 
> 2.39.2
>
  

Patch

diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c
index 34876ca624..c4aaf4af19 100644
--- a/sysdeps/unix/sysv/linux/check_native.c
+++ b/sysdeps/unix/sysv/linux/check_native.c
@@ -48,11 +48,20 @@  __check_native (uint32_t a1_index, int *a1_native,
   nladdr.nl_family = AF_NETLINK;
 
   socklen_t addr_len = sizeof (nladdr);
-  bool use_malloc = false;
 
   if (fd < 0)
     return;
 
+  /* Netlink requires that user buffer needs to be either 8kb or page size
+     (whichever is bigger), however this has been changed over time and now
+     8Kb is sufficient (check NLMSG_DEFAULT_SIZE on Linux
+     linux/include/linux/netlink.h).  */
+  const size_t buf_size = 8192;
+  char *buf = malloc (buf_size);
+
+  if (buf == NULL)
+    goto out;
+
   if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0
       || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0)
     goto out;
@@ -81,26 +90,6 @@  __check_native (uint32_t a1_index, int *a1_native,
   memset (&nladdr, '\0', sizeof (nladdr));
   nladdr.nl_family = AF_NETLINK;
 
-#ifdef PAGE_SIZE
-  /* Help the compiler optimize out the malloc call if PAGE_SIZE
-     is constant and smaller or equal to PTHREAD_STACK_MIN/4.  */
-  const size_t buf_size = PAGE_SIZE;
-#else
-  const size_t buf_size = __getpagesize ();
-#endif
-  char *buf;
-
-  if (__libc_use_alloca (buf_size))
-    buf = alloca (buf_size);
-  else
-    {
-      buf = malloc (buf_size);
-      if (buf != NULL)
-	use_malloc = true;
-      else
-	goto out;
-    }
-
   struct iovec iov = { buf, buf_size };
 
   if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
@@ -170,7 +159,5 @@  __check_native (uint32_t a1_index, int *a1_native,
 
 out:
   __close_nocancel_nostatus (fd);
-
-  if (use_malloc)
-    free (buf);
+  free (buf);
 }