[v4] ifaddrs: Get rid of alloca

Message ID 20230531183933.2007586-1-josimmon@redhat.com
State Superseded
Headers
Series [v4] ifaddrs: 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-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm 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 May 31, 2023, 6:39 p.m. UTC
  Use scratch_buffer and malloc rather than alloca to avoid potential stack
overflows.
---
Changes to v3:
  * Get rid of the 'out_fail' label and its respective gotos since we're
    using a cleanup handler for the malloc'ed space.
 sysdeps/unix/sysv/linux/ifaddrs.c | 64 +++++++++++++++----------------
 1 file changed, 30 insertions(+), 34 deletions(-)
  

Patch

diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c
index 184ee224cb..ef87b00423 100644
--- a/sysdeps/unix/sysv/linux/ifaddrs.c
+++ b/sysdeps/unix/sysv/linux/ifaddrs.c
@@ -16,13 +16,13 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <alloca.h>
 #include <assert.h>
 #include <errno.h>
 #include <ifaddrs.h>
 #include <net/if.h>
 #include <netinet/in.h>
 #include <netpacket/packet.h>
+#include <scratch_buffer.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -122,6 +122,13 @@  __netlink_sendreq (struct netlink_handle *h, int type)
 }
 
 
+static void
+ifree (char **ptr)
+{
+  free (*ptr);
+}
+
+
 int
 __netlink_request (struct netlink_handle *h, int type)
 {
@@ -131,31 +138,19 @@  __netlink_request (struct netlink_handle *h, int type)
   ssize_t read_len;
   bool done = false;
 
-#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
-  bool use_malloc = false;
-  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_fail;
-    }
+  /* 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 __attribute__ ((__cleanup__ (ifree))) = malloc (buf_size);
+  if (buf == NULL)
+    return -1;
 
   struct iovec iov = { buf, buf_size };
 
   if (__netlink_sendreq (h, type) < 0)
-    goto out_fail;
+    return -1;
 
   while (! done)
     {
@@ -173,13 +168,13 @@  __netlink_request (struct netlink_handle *h, int type)
       read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
       __netlink_assert_response (h->fd, read_len);
       if (read_len < 0)
-	goto out_fail;
+	return -1;
 
       if (nladdr.nl_pid != 0)
 	continue;
 
       if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC))
-	goto out_fail;
+	return -1;
 
       size_t count = 0;
       size_t remaining_len = read_len;
@@ -205,7 +200,7 @@  __netlink_request (struct netlink_handle *h, int type)
 		errno = EIO;
 	      else
 		errno = -nlerr->error;
-	      goto out_fail;
+	      return -1;
 	    }
 	}
 
@@ -217,7 +212,7 @@  __netlink_request (struct netlink_handle *h, int type)
       nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
 						+ read_len);
       if (nlm_next == NULL)
-	goto out_fail;
+	return -1;
       nlm_next->next = NULL;
       nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
       nlm_next->size = read_len;
@@ -229,14 +224,7 @@  __netlink_request (struct netlink_handle *h, int type)
       h->end_ptr = nlm_next;
     }
 
-  if (use_malloc)
-    free (buf);
   return 0;
-
-out_fail:
-  if (use_malloc)
-    free (buf);
-  return -1;
 }
 
 
@@ -324,6 +312,8 @@  getifaddrs_internal (struct ifaddrs **ifap)
   char *ifa_data_ptr;	/* Pointer to the unused part of memory for
 				ifa_data.  */
   int result = 0;
+  struct scratch_buffer buf;
+  scratch_buffer_init (&buf);
 
   *ifap = NULL;
 
@@ -425,7 +415,12 @@  getifaddrs_internal (struct ifaddrs **ifap)
     }
 
   /* Table for mapping kernel index to entry in our list.  */
-  map_newlink_data = alloca (newlink * sizeof (int));
+  if (!scratch_buffer_set_array_size (&buf, 1, newlink * sizeof (int)))
+    {
+      result = -1;
+      goto exit_free;
+    }
+  map_newlink_data = buf.data;
   memset (map_newlink_data, '\xff', newlink * sizeof (int));
 
   ifa_data_ptr = (char *) &ifas[newlink + newaddr];
@@ -820,6 +815,7 @@  getifaddrs_internal (struct ifaddrs **ifap)
  exit_free:
   __netlink_free_handle (&nh);
   __netlink_close (&nh);
+  scratch_buffer_free (&buf);
 
   return result;
 }