From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687377668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=D/T5jlw98hSWWRiiSKxzIZm5svuOY3KsUzRQWP0bcXA=; b=dwrDHa3sjCSHrIEOrelfaAgXXy3tDEB3yWP50ukHhReEGFDVb9/hotvf9E+R8hju19RTTo iJ+au/2GACmPkzVww3ZNbH3+KEp1ViANMEGq0M4l57/WtBJFIyTdEXYHlunDkeCZec8ioj Es/Qn0v7CCFpjlJpDcGM4yxrF9N4noc= 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) From: Joe Simmons-Talbott 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: Use scratch_buffer and malloc rather than alloca to avoid potential stack overflows. --- 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; } -- 2.39.2