From patchwork Tue May 30 15:25:39 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: 1787718 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=S2hGL0zG; 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 4QVx7J1Y5yz20PW for ; Wed, 31 May 2023 01:26:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 556243858417 for ; Tue, 30 May 2023 15:26:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 556243858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685460361; bh=7zAEW0sqm7ZwJ6jjlkjWsuyD9qvJ9mtfVEripV2dpxA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=S2hGL0zGUh8dHK3U135Ru4V4fwdRU3gs0v0T1HF8HyD5OAHERmp53aiYvo4tqWBLz XJlF1wAYGJXoZusiUDXh6rANZVTq2UorecMhrpZ+f09TNKza1Aw2Pk01z3XpvrPi/K cOIVktZWaxLypL0hcQzr3wzM1kr8U3zujC7WL/Bc= 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 91E5C3858D20 for ; Tue, 30 May 2023 15:25:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 91E5C3858D20 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-427-hCkF7A85Ns22PuowcOV4ug-1; Tue, 30 May 2023 11:25:43 -0400 X-MC-Unique: hCkF7A85Ns22PuowcOV4ug-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-75b02834141so325378785a.1 for ; Tue, 30 May 2023 08:25:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685460342; x=1688052342; 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=7zAEW0sqm7ZwJ6jjlkjWsuyD9qvJ9mtfVEripV2dpxA=; b=OuUgH5ofYvKTiz/jBvFa0W6i4Nky2Pl4CHtRShkO4vnDKWzpG07TahVT3nN6gCfCIt AXn2K3+39haaIiCz4tgHotXcCqJ6iUE48aDDgwFOT98pGlPd7H2ORilgd0p3oxGmfZD6 e1K18LjUTf0TdxD44q7oc7l5U0rrgRjrQ4N8NOogQrkCJMlETVT5yK/V8whLtQsvLSGp WCCPdFTQ5zBb7ptwskO0dnHIy2g9X4D71gzalzHj2HFsXemQ/YXXFnhTx3c7CVh60Y4x drFrb+73+INTkHhjVv9Mqo31UeCYRzx2DH0NlnTCLJmd6BeZ7npbaaiToVvOKcl58+oJ KDfg== X-Gm-Message-State: AC+VfDze7QTHA0QDAjS9d1Q4VNSfv8c79ldQ1u7Q1HDrfAPvI7wAXYTJ 3ojtZyMYRE/WuyC1GcsXEUzIk/HzDlDoEF5aNZI+2kzdalsg6YG5ErtUvuiTxXR95vX2j8R4X7z ShZIOT0ZqgxD4VZ0iuYymZGxhit3dhYFBJwm9ODfnvvipLHed+Mdbp9ZYllrvtaj5LwETeKFGeU vl663u X-Received: by 2002:a05:620a:c4d:b0:75b:23a0:d9d5 with SMTP id u13-20020a05620a0c4d00b0075b23a0d9d5mr2302692qki.43.1685460342033; Tue, 30 May 2023 08:25:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7tvHOL1QDGAlJUGutgM6TeHW19Mc2q+rgq21xMFv5ZRcG8NfZIeZ4qzE+QXW6savh8MuVk5Q== X-Received: by 2002:a05:620a:c4d:b0:75b:23a0:d9d5 with SMTP id u13-20020a05620a0c4d00b0075b23a0d9d5mr2302665qki.43.1685460341660; Tue, 30 May 2023 08:25:41 -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 b16-20020a0cfb50000000b0062629fa5c4esm1329836qvq.30.2023.05.30.08.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 08:25:41 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3] ifaddrs: Get rid of alloca Date: Tue, 30 May 2023 11:25:39 -0400 Message-Id: <20230530152539.2063770-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.3 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 v2: * Initialize the scratch_buffer earlier to avoid memory access errors when calling scratch_buffer_free for failure cases. Changes to v1: * in __netlink_request use an 8kb buffer size and malloc rather than a scratch_buffer. Suggested-by: Adhemerval Zanella Netto sysdeps/unix/sysv/linux/ifaddrs.c | 51 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..f62510811a 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,26 +138,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 __attribute__ ((__cleanup__ (ifree))) = malloc (buf_size); + if (buf == NULL) + return -1; struct iovec iov = { buf, buf_size }; @@ -229,13 +224,9 @@ __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 +315,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 +418,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 +818,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }