From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by sourceware.org (Postfix) with ESMTPS id 2CE1D3858439 for ; Tue, 16 May 2023 11:09:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CE1D3858439 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-54f92ee633bso4791480eaf.0 for ; Tue, 16 May 2023 04:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684235359; x=1686827359; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=6M1lk48uXCCwxfA9YajNUPiVJnxy1WK5nVl22A2EytQ=; b=kpdidKopEnyDxce71GIajgxY3ekKhLhRDpOds6J5fpXZuAnmoPlrHu5K+dtuRIH6LI Bhe7l0XMiYnMyqFdptCPow3rP/TgsbOWqAqopooIxJdcGN76uP5CnS6IHFEaRnmeow2m E5UfhxGjNSClq5YP5pYlFIKSswuL0SA9MksJAtXI0+YGIitDQsJ2SGuW45C2gR48Hx7T M4tU0aD+BR+jO7NDRMIF7z8dszmCYg42+O2+Fj/zswEByqn7fmcMAZu+GRDmdQY0dJ3y PozYkzZUzML+XLy/oIdJEHJIaB64iXrRbsHO5QzYNRCQgtnEoY5JapVjSmlCHC6PMADQ XyOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684235359; x=1686827359; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6M1lk48uXCCwxfA9YajNUPiVJnxy1WK5nVl22A2EytQ=; b=aZNevu3kOFKybsJzpNkBJYaL+vkibAREw+O6H1Yvn6MSKrNJtMHcsfPQm54z9CfSPz 3bhh+JtN0ClT1FMnq70tU4ixy2oRXBlSnSxvt+Xt1jqcO/63MeFiI0GeYoCLEpamAOKv iIuJmht/ZIg/t4d7oR/HkMG3YMG0hz0n5gQbDMXUqO3JeMeKsb019YwLxQZ5SqR8SIcr ZmE0GI+MH+Tx6YK4Nn10ErkuYySi7tWTOKyAZY6g7Txemv+EPJAeDbF3SC+KQ+KYbUmA QoNmGczjCOfTmKGnHNFaSnaGrIuIWSYXXYb6DE/gAQ+W7pM/VlR60R7rWSrUm7p+rVNl NDbA== X-Gm-Message-State: AC+VfDx1BRrz5DLgJh2wrohavsU/QEkSsjFOQPBqdFwyjT3B/Su1GaOg bOYGZn76uSA/rt5ircsNxJtkWA== X-Google-Smtp-Source: ACHHUZ78CYfsv+Efogn/ULL0SvHeogFK4kMGyrrM7nLP4EAcUq9oOx6UskpME0hMGdxdpd+tmSMHrQ== X-Received: by 2002:a05:6808:1528:b0:395:fa16:908a with SMTP id u40-20020a056808152800b00395fa16908amr4601230oiw.50.1684235359373; Tue, 16 May 2023 04:09:19 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c0:c914:28b3:cbf9:a103:6f88? ([2804:1b3:a7c0:c914:28b3:cbf9:a103:6f88]) by smtp.gmail.com with ESMTPSA id a25-20020a544e19000000b0039626805eb4sm1048917oiy.51.2023.05.16.04.09.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 May 2023 04:09:17 -0700 (PDT) Message-ID: <55a4e9ee-7996-bfc1-752a-b7842691d141@linaro.org> Date: Tue, 16 May 2023 08:09:13 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH v2] setsourcefilter: Use malloc() rather than alloca(). Content-Language: en-US To: Joe Simmons-Talbott , libc-alpha@sourceware.org References: <20230515190806.2770627-1-josimmon@redhat.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230515190806.2770627-1-josimmon@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 15/05/23 16:08, Joe Simmons-Talbott via Libc-alpha wrote: > To prevent possible stack overflow use malloc() rather than alloca(). > --- > Changes to v1: > - don't save and restore errno around free() > > sysdeps/unix/sysv/linux/setsourcefilter.c | 20 ++++---------------- > 1 file changed, 4 insertions(+), 16 deletions(-) > > diff --git a/sysdeps/unix/sysv/linux/setsourcefilter.c b/sysdeps/unix/sysv/linux/setsourcefilter.c > index 538f4de696..2823168d34 100644 > --- a/sysdeps/unix/sysv/linux/setsourcefilter.c > +++ b/sysdeps/unix/sysv/linux/setsourcefilter.c > @@ -16,7 +16,6 @@ > License along with the GNU C Library; if not, see > . */ > > -#include > #include > #include > #include > @@ -34,17 +33,11 @@ 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; > - } > + gf = (struct group_filter *) malloc (needed); > + if (gf == NULL) > + return -1; > > gf->gf_interface = interface; > memcpy (&gf->gf_group, group, grouplen); > @@ -63,12 +56,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); > - } > + free (gf); > > return result; > } Maybe we can use a scratch_buffer to avoid always use malloc: diff --git a/sysdeps/unix/sysv/linux/setsourcefilter.c b/sysdeps/unix/sysv/linux/setsourcefilter.c index 538f4de696..22b307ca80 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,11 @@ 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 +54,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; }