From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1791) id 712153858C60; Mon, 26 Jun 2023 13:38:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 712153858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687786687; bh=U3zjz5e33102fKsCky8eWnjhYfVMCW5VZGFIceZ/7qA=; h=From:To:Subject:Date:From; b=EFlwcoRYlzXcwwL5fCJ0XFu51Ofa+3UAZf0SpAFnyVmdXz2bsNr8jcGxxyYVknlDs mYW2Eq4ytL0Xop1P4pUe2BPVgWivBUpo0GO3SjkGt8UXerp/wYoK0UrvKNtF0Yss8w aISSaeOPtr+PqZl3sVhExmqcrIBrRa0oKMwQYcZg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Adhemerval Zanella To: glibc-cvs@sourceware.org Subject: [glibc] ifaddrs: Get rid of alloca X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: 45e2483a6cd920b38d287c51c5363f03a34f92da X-Git-Newrev: 48170127d984f41cd59dfc1b9ee40f729d953bf9 Message-Id: <20230626133807.712153858C60@sourceware.org> Date: Mon, 26 Jun 2023 13:38:07 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=48170127d984f41cd59dfc1b9ee40f729d953bf9 commit 48170127d984f41cd59dfc1b9ee40f729d953bf9 Author: Joe Simmons-Talbott Date: Wed Jun 21 16:00:53 2023 -0400 ifaddrs: Get rid of alloca Use scratch_buffer and malloc rather than alloca to avoid potential stack overflows. Reviewed-by: Adhemerval Zanella Diff: --- sysdeps/unix/sysv/linux/ifaddrs.c | 46 +++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..0db9bb7847 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 . */ -#include #include #include #include #include #include #include +#include #include #include #include @@ -131,26 +131,14 @@ __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 = malloc (buf_size); + if (buf == NULL) + goto out_fail; struct iovec iov = { buf, buf_size }; @@ -229,13 +217,11 @@ __netlink_request (struct netlink_handle *h, int type) h->end_ptr = nlm_next; } - if (use_malloc) - free (buf); + free(buf); return 0; out_fail: - if (use_malloc) - free (buf); + free(buf); return -1; } @@ -324,6 +310,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 +413,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, 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 +813,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }