From patchwork Fri May 26 00:22:47 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: 1786322 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=qaI8XVe1; 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 4QS5HQ09q0z20PT for ; Fri, 26 May 2023 10:23:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6CA843857702 for ; Fri, 26 May 2023 00:23:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CA843857702 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685060591; bh=UpoMz8WaCbPyNv2/O/v61/dSVEVwXDomGNovvvKpYBc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=qaI8XVe1Povteb4wCKxsWMjhWnj15pLynzaLFFveWiagY6Jan5pDXwH5ji6qK/Jq3 w9zxLXV2XbHFW+lAKCIhPe4o51PMVYJBvCY1w0bSDIbo3PLX69rNNmvUe37B/lsKP3 7Q1JBBBR1NFl38truEIFaYg2ErPy8i6V8SkiVwpI= 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 AB3DC3858D39 for ; Fri, 26 May 2023 00:22:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB3DC3858D39 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-HwMQ_Rd1NRKgs2QTk9Jf6Q-1; Thu, 25 May 2023 20:22:52 -0400 X-MC-Unique: HwMQ_Rd1NRKgs2QTk9Jf6Q-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-625125594bbso2768656d6.3 for ; Thu, 25 May 2023 17:22:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685060572; x=1687652572; 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=UpoMz8WaCbPyNv2/O/v61/dSVEVwXDomGNovvvKpYBc=; b=KAzUhhe6HeNRg69M+FbWJPOO2Zj+iE1Ic5G/jas6VsFcTnW9S1aih76BIRzI5VQPVz +G+h+JkLsg7u7LE8UvhvnG+JBqS1dteZtz34o90qQyfsJCLdEo+ZGyTkh0s4M5HsQqxR DpdbBbkCg6kjJChgGcalTDDy0WZAvB/5eCo+Tfi5u5mPkFeb41bFz830k7VRcYNVbVNi kNuS+aojb4Y+eA/sszfqkG7dpJfWweaUhvEFguIUby9yOzQe8wKlro/iuWAhBl32akqw c3emf69MX9Lat2qhK7QR8WqwP3t9eoermyBjYcNF76iMI1YfgIj/ndgO0NC5V38fsX+d 4wgQ== X-Gm-Message-State: AC+VfDz9QFJKoCN+dyXhjZpyuL5qzYXOlWA1RQjAczecVmKpMLJzbUL6 lTBCHhEV9XjwTSmwG/FMy3iC2CJntbeqvYF622uvdad8gtQtzSaZzefXSk1YuCFpMiwhp5vab4v SvwpgsGw6h59z5Bqbj6rkZxtL7finAzQ11sAjzWmYKmVZariFKLJFxmhFmJr7rDfUtlsZ9/q3tm khm722 X-Received: by 2002:ad4:5ccb:0:b0:623:855a:9239 with SMTP id iu11-20020ad45ccb000000b00623855a9239mr419314qvb.26.1685060571879; Thu, 25 May 2023 17:22:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ72m6kxy00iyRU0SrnEvsDpCXAoFtlecWQWfjlYy2VJmQojBG/dfT7MCSE9GsYV7KPdr79VVA== X-Received: by 2002:ad4:5ccb:0:b0:623:855a:9239 with SMTP id iu11-20020ad45ccb000000b00623855a9239mr419295qvb.26.1685060571540; Thu, 25 May 2023 17:22:51 -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 lf19-20020a0562142cd300b006211c23abc5sm796406qvb.112.2023.05.25.17.22.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 May 2023 17:22:50 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] ifaddrs: Get rid of alloca Date: Thu, 25 May 2023 20:22:47 -0400 Message-Id: <20230526002247.1898289-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.2 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_buffers rather than alloca to avoid potential stack overflows. --- sysdeps/unix/sysv/linux/ifaddrs.c | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..ca37b4f6db 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 @@ -138,19 +138,11 @@ __netlink_request (struct netlink_handle *h, int type) #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; - } + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); + if (!scratch_buffer_set_array_size (&sbuf, 1, buf_size)) + goto out_fail; + char *buf = sbuf.data; struct iovec iov = { buf, buf_size }; @@ -229,13 +221,11 @@ __netlink_request (struct netlink_handle *h, int type) h->end_ptr = nlm_next; } - if (use_malloc) - free (buf); + scratch_buffer_free (&sbuf); return 0; out_fail: - if (use_malloc) - free (buf); + scratch_buffer_free (&sbuf); return -1; } @@ -324,6 +314,7 @@ 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; *ifap = NULL; @@ -425,7 +416,13 @@ getifaddrs_internal (struct ifaddrs **ifap) } /* Table for mapping kernel index to entry in our list. */ - map_newlink_data = alloca (newlink * sizeof (int)); + scratch_buffer_init (&buf); + 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 +817,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }