ifaddrs: Get rid of alloca

Use scratch_buffer and malloc rather than alloca to avoid potential stack
overflows.
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
This commit is contained in:
Joe Simmons-Talbott 2023-06-21 16:00:53 -04:00 committed by Adhemerval Zanella
parent 45e2483a6c
commit 48170127d9

View file

@ -16,13 +16,13 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <alloca.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <net/if.h> #include <net/if.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netpacket/packet.h> #include <netpacket/packet.h>
#include <scratch_buffer.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -131,26 +131,14 @@ __netlink_request (struct netlink_handle *h, int type)
ssize_t read_len; ssize_t read_len;
bool done = false; bool done = false;
#ifdef PAGE_SIZE /* Netlink requires that user buffer needs to be either 8kb or page size
/* Help the compiler optimize out the malloc call if PAGE_SIZE (whichever is bigger), however this has been changed over time and now
is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ 8Kb is sufficient (check NLMSG_DEFAULT_SIZE on Linux
const size_t buf_size = PAGE_SIZE; linux/include/linux/netlink.h). */
#else const size_t buf_size = 8192;
const size_t buf_size = __getpagesize (); char *buf = malloc (buf_size);
#endif if (buf == NULL)
bool use_malloc = false; goto out_fail;
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;
}
struct iovec iov = { buf, buf_size }; struct iovec iov = { buf, buf_size };
@ -229,13 +217,11 @@ __netlink_request (struct netlink_handle *h, int type)
h->end_ptr = nlm_next; h->end_ptr = nlm_next;
} }
if (use_malloc) free(buf);
free (buf);
return 0; return 0;
out_fail: out_fail:
if (use_malloc) free(buf);
free (buf);
return -1; return -1;
} }
@ -324,6 +310,8 @@ getifaddrs_internal (struct ifaddrs **ifap)
char *ifa_data_ptr; /* Pointer to the unused part of memory for char *ifa_data_ptr; /* Pointer to the unused part of memory for
ifa_data. */ ifa_data. */
int result = 0; int result = 0;
struct scratch_buffer buf;
scratch_buffer_init (&buf);
*ifap = NULL; *ifap = NULL;
@ -425,7 +413,12 @@ getifaddrs_internal (struct ifaddrs **ifap)
} }
/* Table for mapping kernel index to entry in our list. */ /* 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)); memset (map_newlink_data, '\xff', newlink * sizeof (int));
ifa_data_ptr = (char *) &ifas[newlink + newaddr]; ifa_data_ptr = (char *) &ifas[newlink + newaddr];
@ -820,6 +813,7 @@ getifaddrs_internal (struct ifaddrs **ifap)
exit_free: exit_free:
__netlink_free_handle (&nh); __netlink_free_handle (&nh);
__netlink_close (&nh); __netlink_close (&nh);
scratch_buffer_free (&buf);
return result; return result;
} }