From patchwork Tue May 16 13:45:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 1782058 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=i7NiFeV/; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QLHZY1Xfsz20KF for ; Tue, 16 May 2023 23:46:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 20A0A385734D for ; Tue, 16 May 2023 13:46:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20A0A385734D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684244771; bh=smvVQ6Xmh7pSb45N21HnCaA7EEMjim7JzK7uLvQJKvQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=i7NiFeV/JNdo62/b+zEwHoMo6n/9NIrPXFCZYghaXICEFoswabwhkJDfMD3z5yEfM WkKnBLlsMjHS70jpir1XpFzaBkCpbQaSOyniFQ5jEsdHYcW+QxXFhziJ7M+cpm2z0G d9RnU0QGINmDdUV2IFxoixadcN8DXELfGNnMFtp0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8A2263858C83 for ; Tue, 16 May 2023 13:45:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A2263858C83 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-173-Q3hPnDcvN8qV5GWyyiUNVQ-1; Tue, 16 May 2023 09:45:53 -0400 X-MC-Unique: Q3hPnDcvN8qV5GWyyiUNVQ-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3f38af0b0c2so150820181cf.2 for ; Tue, 16 May 2023 06:45:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684244753; x=1686836753; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=smvVQ6Xmh7pSb45N21HnCaA7EEMjim7JzK7uLvQJKvQ=; b=AUKuLkz7xYyJiiPuFvDX33X1N7XoEFL/cn9gnQo1K8EWg/gowr3RXRFq76nubOKflL +b2d63n99MH64lXLXQsAtRGLH2wiaFx7WaZf0hrecQStDgZPhtuVFU+BMmJ8JgQbps3W uPCm6rVvbNl+Wb13YkbSi3Z0g2pEknsodrxwIYjF02h9ePH+nmmA4aU8SD9eH4M8R2yl r3QHPgbsDdvcQueMi9Q1vWX4KVV9eeFlUS5rnfmWDAabnd6USOORF699RVH+Rw2Q69sS R68e9r14qI7+siolu1uVP1UbuPi740Vb92itv3KGt/TSX4bbH5Q3r/EeEdxN/98wmT7x tSDQ== X-Gm-Message-State: AC+VfDzbN1MabwV3BpETfHiw0ikjYLdqVGHtQX5I27OAoeeFppouTdEl a4kcqsNQZZn03NqiKv6glsLwfLfHOH3INoSoj/WxTSll8JXQSFcuGmgX0OOo4GYX8eYG9Cofc0Y 4irfOtuOhKtgjbssIk8S9fuAH1Gi+vELtgPXOc6+1WSYwaNLDf4KDLV1RcW2wtZ1iJhdENzwMcr d0fZaH X-Received: by 2002:ac8:5b84:0:b0:3f3:ad4e:8fba with SMTP id a4-20020ac85b84000000b003f3ad4e8fbamr37871712qta.55.1684244752840; Tue, 16 May 2023 06:45:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LC90MMWrHjU8rKtS4DXSI/6AvEUJ0sQOoonR/m/zneRBbeyqCslvMKY5dLon6OfZEu3AI3A== X-Received: by 2002:ac8:5b84:0:b0:3f3:ad4e:8fba with SMTP id a4-20020ac85b84000000b003f3ad4e8fbamr37871683qta.55.1684244752520; Tue, 16 May 2023 06:45:52 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id e13-20020ac84b4d000000b003f218578bb3sm6200092qts.19.2023.05.16.06.45.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 06:45:51 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Adhemerval Zanella Netto Subject: [PATCH] setsourcefilter: Replace alloca with a scratch_buffer. Date: Tue, 16 May 2023 09:45:49 -0400 Message-Id: <20230516134549.1488655-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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 --- 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; }