From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2206) id 213E83858C31; Mon, 29 May 2023 13:19:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 213E83858C31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685366378; bh=UOtWCp6+0m1aaDyWbRgvBLpyJNy7YZawlr/I4fP/jRE=; h=From:To:Subject:Date:From; b=UPdP9I26WuZTgCJp5WqoF2ZRKQGWir9Wf9aYyshp3g9hWrEmuEP58n8GphF6JdFzM QOlf7r41JjEuZbIn+9Hsu9eRNFJWbOTbMSnJymHBvUxJHRpVsiVa9Ka0I1w3g0Xeev D3U2VXjrH/YWpw4DfWuXMudCf6hYPpqKYEmBhn8I= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Siddhesh Poyarekar To: glibc-cvs@sourceware.org Subject: [glibc] setsourcefilter: Replace alloca with a scratch_buffer. X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: 79b2667d1eb06c6503c22f2f323c1c574ac5917b X-Git-Newrev: d9055634a34d4bcb242f84f36c9a7bb1c4019076 Message-Id: <20230529131938.213E83858C31@sourceware.org> Date: Mon, 29 May 2023 13:19:38 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d9055634a34d4bcb242f84f36c9a7bb1c4019076 commit d9055634a34d4bcb242f84f36c9a7bb1c4019076 Author: Joe Simmons-Talbott Date: Tue May 16 09:45:49 2023 -0400 setsourcefilter: Replace alloca with a scratch_buffer. Use a scratch_buffer rather than either alloca or malloc to reduce the possibility of a stack overflow. Suggested-by: Adhemerval Zanella Netto Reviewed-by: Adhemerval Zanella Diff: --- sysdeps/unix/sysv/linux/setsourcefilter.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/sysdeps/unix/sysv/linux/setsourcefilter.c b/sysdeps/unix/sysv/linux/setsourcefilter.c index 538f4de696..479744f169 100644 --- a/sysdeps/unix/sysv/linux/setsourcefilter.c +++ b/sysdeps/unix/sysv/linux/setsourcefilter.c @@ -16,13 +16,10 @@ License along with the GNU C Library; if not, see . */ -#include #include -#include #include -#include #include -#include +#include #include "getsourcefilter.h" @@ -34,17 +31,12 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, /* We have to create an struct ip_msfilter object which we can pass to the kernel. */ size_t needed = GROUP_FILTER_SIZE (numsrc); - int use_alloca = __libc_use_alloca (needed); - struct group_filter *gf; - if (use_alloca) - gf = (struct group_filter *) alloca (needed); - else - { - gf = (struct group_filter *) malloc (needed); - if (gf == NULL) - return -1; - } + struct scratch_buffer buf; + scratch_buffer_init (&buf); + if (!scratch_buffer_set_array_size (&buf, 1, needed)) + return -1; + struct group_filter *gf = buf.data; gf->gf_interface = interface; memcpy (&gf->gf_group, group, grouplen); @@ -63,12 +55,7 @@ setsourcefilter (int s, uint32_t interface, const struct sockaddr *group, else result = __setsockopt (s, sol, MCAST_MSFILTER, gf, needed); - if (! use_alloca) - { - int save_errno = errno; - free (gf); - __set_errno (save_errno); - } + scratch_buffer_free (&buf); return result; }