From patchwork Thu Jun 1 16:43:26 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: 1789279 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=ByrJzqAA; 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 4QXBm51TyHz20Py for ; Fri, 2 Jun 2023 02:43:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A46B3857013 for ; Thu, 1 Jun 2023 16:43:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A46B3857013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685637827; bh=edK5EXd1osNsy51sKysr5vgNzz+cc+dqZYg86BpJe1k=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ByrJzqAAZqRlDwpXUWPmE8ZOXz8aY4R15HN/9eN0WaYd2WsaaDZX4fzY2d6qusI6X 04kmVlh4AdaNR7AOVEv9+Ogz28I96f+4eXFvRp+QvCUV0nUPfeoBo46Bby7qJYqC22 JEQLPnoLACyHf9RbWtub0iKziTX7Cswi6gNnhhVs= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id B15C8385840D for ; Thu, 1 Jun 2023 16:43:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B15C8385840D Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-542-Yx6EER91M8eXdQb3XPRwpA-1; Thu, 01 Jun 2023 12:43:30 -0400 X-MC-Unique: Yx6EER91M8eXdQb3XPRwpA-1 Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-39a06b3b71aso936897b6e.2 for ; Thu, 01 Jun 2023 09:43:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685637809; x=1688229809; 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=edK5EXd1osNsy51sKysr5vgNzz+cc+dqZYg86BpJe1k=; b=KW3RWchVS2sstcP8uzeJT3I1fYbaP51f1kd7xArp+dGb+c1lWoHLajfBG1oA7l3yxe I/S8djqbnrnxMvXgGKdrSkK2LtUSAgNPmNZB1H8faSb8wr+Yph0GTSNOWdnqpbbNJiWV jPGX79tXTAeW2846vJps7bvrDg8qqts4V2xI1MLeRWOOzwr988/SS5JhlHkWOUMUNcGM uEsW7okilQ3EPzxUWHzMe2vlGuowImCo7NkXhEJJ9U5+3XNM7gXIkwp1T8/FiWn50vyb RAvqUuGbmWxp4dx50scPZ5BV2Fu1htx2QT6R20auxrMqpl7EVcLt6ffBanjyTBSw2GXR QuiA== X-Gm-Message-State: AC+VfDxl6pz1jxKUb/tnfZ2PqXnGfa08Gz/+rTB6pgMrtngOcruloOBG yd4U0eDq60J9MlPNYkyHZhZSptJJxSc/mGwf8oLqjOvh/noU83NvsDqaLyBQcc+d/BvA7sKbCq8 q01CNVm0oacNY5HRC/WVOV4XA7jYlKkcfX+wOqQ3CFjGNWY1YRme3lA8zIilrSUqSAKJF8IZMul HLD6V3 X-Received: by 2002:a05:6808:394c:b0:39a:4f06:1510 with SMTP id en12-20020a056808394c00b0039a4f061510mr5631642oib.27.1685637809439; Thu, 01 Jun 2023 09:43:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4bEO1XqckjSpngc2US80cGZAzy/ACjYSIzNpDBnnrMAXx9T9vi1zxbhUUx1p7i3Sp4iQlpfw== X-Received: by 2002:a05:6808:394c:b0:39a:4f06:1510 with SMTP id en12-20020a056808394c00b0039a4f061510mr5631629oib.27.1685637809055; Thu, 01 Jun 2023 09:43:29 -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 p6-20020a0cf686000000b0062838c6a6d5sm1944432qvn.54.2023.06.01.09.43.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 09:43:28 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v5] ifaddrs: Get rid of alloca Date: Thu, 1 Jun 2023 12:43:26 -0400 Message-Id: <20230601164326.3697401-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. Reviewed-by: Adhemerval Zanella --- Changes to v4: * Don't use a cleanup handler. 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..a10e8d9292 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, 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 +813,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }