From patchwork Wed Jun 21 20:00:53 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: 1798106 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=8.43.85.97; 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=c+UX75Gm; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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 4QmZBt4bvMz20Zv for ; Thu, 22 Jun 2023 06:01:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 54150385841B for ; Wed, 21 Jun 2023 20:01:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 54150385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687377684; bh=D/T5jlw98hSWWRiiSKxzIZm5svuOY3KsUzRQWP0bcXA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=c+UX75GmtMGKHeaTTWrinX22IdU7Vxgvt3VDfk3a5HxbI9XWs49CRKeCmcEsR65zF oi/WV9Fn5QIPwFI2JtZ+hE4M++63qHczeqMi7ovOD6YcrcHZVk6+AUcpajHrPbA74W aKpCca3hUuHxe+WSO0NK/s+H2ZQ2d1ydhbHSVtL0= 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 1110C3858D28 for ; Wed, 21 Jun 2023 20:01:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1110C3858D28 Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-261-W9JqEN1uMB-R2afqQxP_-Q-1; Wed, 21 Jun 2023 16:01:01 -0400 X-MC-Unique: W9JqEN1uMB-R2afqQxP_-Q-1 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-56942442eb0so78247397b3.1 for ; Wed, 21 Jun 2023 13:00:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687377657; x=1689969657; 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=D/T5jlw98hSWWRiiSKxzIZm5svuOY3KsUzRQWP0bcXA=; b=BZEssNnoojS+vMQEOmF+2xldQWBFOfcsNtIawlI9G9z1kJZsFJmmGL/vmSCdf600UP ocaetEKGL5TXa1Ornqc5tL/qpSXZaF021haX9yqKlOX5xCLx3KS3R8i5BNMCbS98JGwO /sdv6yOscOmxZpUc4hSXSDuh2j8jgGrDqWdCViD41FlT0/Q/OGX7Nz/S6Tar9zs8BJnm qyC2rD/EacB88NG4OhJCD0z9bSE0PATKzB+VjwGp0lzSDSh+pIE/t5tfGPmHOXUrMW7o kiIYct3DNNkVd5549sOagx8ZfPYPxQJPlCgg7wdfuJ2J7Y9o2ywxl21D3Mr+7fCcjdg9 daxg== X-Gm-Message-State: AC+VfDz/AaRAMlbc2PtsbXlRzjxT3C98zbqbII4+8AQZtbz0O3BDDiOl MAB2KIbPmwsCa47zC2NpKsTMJTnoLFCTKKEeAWuVSHJ3UzwGtwJ8H1QR1kxHWiJ42HbQf9ro/rc gZFT6ToCyPNAks5wQtWvHBXdrB51vvXvj1JRBrxLLwf9bDb/pI7/bGbqGJwO+loLTVzCJZYaen8 FRpTfp X-Received: by 2002:a81:a0d3:0:b0:56c:ff9d:8cd9 with SMTP id x202-20020a81a0d3000000b0056cff9d8cd9mr17727572ywg.7.1687377657259; Wed, 21 Jun 2023 13:00:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4L084kDCMoiiB494xgUsWz1MPm0b5TC65KoWh7m1MehAUpu/fad8re5yDPVt0QOIlM+HxVTw== X-Received: by 2002:a81:a0d3:0:b0:56c:ff9d:8cd9 with SMTP id x202-20020a81a0d3000000b0056cff9d8cd9mr17727551ywg.7.1687377656951; Wed, 21 Jun 2023 13:00:56 -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 u67-20020a0dd246000000b00559be540b56sm279034ywd.134.2023.06.21.13.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 13:00:56 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v6] ifaddrs: Get rid of alloca Date: Wed, 21 Jun 2023 16:00:53 -0400 Message-Id: <20230621200053.3021604-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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. Reviewed-by: Adhemerval Zanella --- Changes to v5: * Don't bypass the __libc_scratch_buffer_set_array_size check by passing both the item size and count to scratch_buffer_set_array_size. sysdeps/unix/sysv/linux/ifaddrs.c | 46 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..0db9bb7847 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 @@ -131,26 +131,14 @@ __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 = malloc (buf_size); + if (buf == NULL) + goto out_fail; struct iovec iov = { buf, buf_size }; @@ -229,13 +217,11 @@ __netlink_request (struct netlink_handle *h, int type) h->end_ptr = nlm_next; } - if (use_malloc) - free (buf); + free(buf); return 0; out_fail: - if (use_malloc) - free (buf); + free(buf); return -1; } @@ -324,6 +310,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 +413,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, 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 +813,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }