From patchwork Wed May 31 18:39:33 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: 1788485 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=lNP4IPzZ; 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 4QWdNX5XVhz20PW for ; Thu, 1 Jun 2023 04:39:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E0983858032 for ; Wed, 31 May 2023 18:39:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E0983858032 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685558394; bh=OUfdyVvOqXwOcOh+x+OFhdK2Zs3BddCcabrH1wBmdRY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=lNP4IPzZgKMUUEa75hJG3vrbh45Imxo7p8R9gRr2famlVrWnR5N2A+cmY6S0D5UZE yOnc6OXc2sWQqbe98TS3JgHeNU2ot0RZ8KrEstLyr8dhaPInLnjyhnXxZxd1oCS9nl 0hy5+LHOdXYtoU8CCMhp2aq7qOrCA4KnndhicwRQ= 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 7F3A93858CDB for ; Wed, 31 May 2023 18:39:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F3A93858CDB Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-115-ddmyKN8iP7is9lFYj32Xcw-1; Wed, 31 May 2023 14:39:37 -0400 X-MC-Unique: ddmyKN8iP7is9lFYj32Xcw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-75cac31d806so839448085a.1 for ; Wed, 31 May 2023 11:39:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685558376; x=1688150376; 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=OUfdyVvOqXwOcOh+x+OFhdK2Zs3BddCcabrH1wBmdRY=; b=KN9/6A6vlnhXIY9vEpMe7kHhRX81kkTeUQKg0p6QXVsarnCj+hhsc+QDtckYHMr3uR VjWzYYvIr2kQ8JssXm2NIkaeLPNSs7ChpoMioGTIBs6HkNbMIFjx7qJoem6ZRgS26IPP lcBu3pjbCrk58TW7tznmCejp3STRMxI42Y/2W/w/mdbgaPT5oMwxOgvZVawSRAYGa1RX eeYQuDu3dctEAPnhymigzcWslwV4f6AYRketn8fyVxD/Lml9ScJBs1rnQdB0zRCzBEsX 4wQQDkD7Wc1VoRCypMc/VTh34JDfZcikRGtpC/piXnYtc2g01jVdsvzxBzgjo6cD2njM yYyg== X-Gm-Message-State: AC+VfDwU8BCorAXvbYMMxL6M2MSPe8Vzc9kfuKN5Y24Tc+mr4KNYaEwp OHWYR6CMmI1pykK0U5hibhseWSFyObyZO6R0YADI1hGmbtQyStpuL782jmXe8dU8tK8jO9CG2aC K3JDboU/r5CtLDNxQrwYlXI0ArOmaDsxGQ4CzncqIiMoajv8GFNmNbHwKZmkxkCTr3Demq+sTvq lw2vGN X-Received: by 2002:a05:622a:203:b0:3ef:2fbd:90cd with SMTP id b3-20020a05622a020300b003ef2fbd90cdmr7826118qtx.46.1685558376318; Wed, 31 May 2023 11:39:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GGzX2P/2+QJ/o9h8kcAKGpXkoOeou0E5nMKRGQEPxy63rDA46hHlPapEDhtoMCBW+/MAYwA== X-Received: by 2002:a05:622a:203:b0:3ef:2fbd:90cd with SMTP id b3-20020a05622a020300b003ef2fbd90cdmr7826097qtx.46.1685558375917; Wed, 31 May 2023 11:39:35 -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 j29-20020ac84c9d000000b003f38aabb88asm6263803qtv.20.2023.05.31.11.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 11:39:35 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v4] ifaddrs: Get rid of alloca Date: Wed, 31 May 2023 14:39:33 -0400 Message-Id: <20230531183933.2007586-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.4 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 scratch_buffer and malloc rather than alloca to avoid potential stack overflows. --- Changes to v3: * Get rid of the 'out_fail' label and its respective gotos since we're using a cleanup handler for the malloc'ed space. sysdeps/unix/sysv/linux/ifaddrs.c | 64 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..ef87b00423 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 @@ -122,6 +122,13 @@ __netlink_sendreq (struct netlink_handle *h, int type) } +static void +ifree (char **ptr) +{ + free (*ptr); +} + + int __netlink_request (struct netlink_handle *h, int type) { @@ -131,31 +138,19 @@ __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 __attribute__ ((__cleanup__ (ifree))) = malloc (buf_size); + if (buf == NULL) + return -1; struct iovec iov = { buf, buf_size }; if (__netlink_sendreq (h, type) < 0) - goto out_fail; + return -1; while (! done) { @@ -173,13 +168,13 @@ __netlink_request (struct netlink_handle *h, int type) read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0)); __netlink_assert_response (h->fd, read_len); if (read_len < 0) - goto out_fail; + return -1; if (nladdr.nl_pid != 0) continue; if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC)) - goto out_fail; + return -1; size_t count = 0; size_t remaining_len = read_len; @@ -205,7 +200,7 @@ __netlink_request (struct netlink_handle *h, int type) errno = EIO; else errno = -nlerr->error; - goto out_fail; + return -1; } } @@ -217,7 +212,7 @@ __netlink_request (struct netlink_handle *h, int type) nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res) + read_len); if (nlm_next == NULL) - goto out_fail; + return -1; nlm_next->next = NULL; nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len); nlm_next->size = read_len; @@ -229,14 +224,7 @@ __netlink_request (struct netlink_handle *h, int type) h->end_ptr = nlm_next; } - if (use_malloc) - free (buf); return 0; - -out_fail: - if (use_malloc) - free (buf); - return -1; } @@ -324,6 +312,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 +415,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, 1, 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 +815,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }