From patchwork Fri May 26 16:54:19 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: 70168 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DA1F385843E for ; Fri, 26 May 2023 16:54:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DA1F385843E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685120090; bh=WQVBIpTN9YY1xJFzCa0n4fZgUEdQraDtIbzan7AknWU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=h+o3gYjqiUiaMCCWrxVRWde1ncBYQFKq1AhnJgbD4DjnLuM+RhlUtaGTJcX5S+pFe sSfPqdtpsbmr1FEcUw2bkYR6/g6HI/yUb0oZ654sq8XVv5lJfaihBv3jrHnaehkg/v AcBK8KLcI/quKXiHkINoVM/yvrq+PdAAdb/gGU+w= 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 23E273858C2C for ; Fri, 26 May 2023 16:54:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23E273858C2C 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-398-ry0IeRsCM-uH8OtQ992Rkg-1; Fri, 26 May 2023 12:54:23 -0400 X-MC-Unique: ry0IeRsCM-uH8OtQ992Rkg-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6238dcbc30bso7550846d6.1 for ; Fri, 26 May 2023 09:54:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685120062; x=1687712062; 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=WQVBIpTN9YY1xJFzCa0n4fZgUEdQraDtIbzan7AknWU=; b=OHhdPQihgZPzDdcNR9Bq7DSrL6iQLq37DMwQcAnnkebO1hoch9pK4bDnrhLUs1s3MB OwAuAt4ZG2irhcblILt4OEdNBtX7qLE9e9TZS67Rg7L6porXmDAM88a5wbvJJ6CUMtCA LOavWABM0kq+pW7umf/M6duvyNr+3rXsyV2o+XyU6QBP+obd29fPtfh/Jd2mNwFzdcoG Tb5bXQGEaAgj+G0PhAmAe1gX+tvbe3gkQnx5f7EUPgekibHwzetZBvg+1/tUst++n+IM Fsd232Ap3jYI9B6Y4bzyHysyX+BJKXifd0fDf+W00oqPsmC/2jb2h0MikaoLPiUWRc/N ft9Q== X-Gm-Message-State: AC+VfDxFiMKyD9XbYco8bSQFbkS3Biyymck3fZRwwJsxTxMATTRxzxv3 +gugqsf84YvWubTJLA2/zO/k2TuVNdvo2MU3BzZknzjPknkfRY0yBPgU/EnmQD5NItGM+rE1Xt9 ncxTNRjX6eYNlezEiUB1FSBGpaWcJHpsiZOMTt6MneBpw3bZhlhUMH5TS1qoEJGmnl2EXwRRk3V HoZO9h X-Received: by 2002:a05:6214:2684:b0:5c7:d03c:f2b2 with SMTP id gm4-20020a056214268400b005c7d03cf2b2mr1988256qvb.28.1685120062416; Fri, 26 May 2023 09:54:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mnvcFjrkC8SqqsrEJNeQPFvVU03wTDlHgLzLrhZNM2CGXz1bLuO2KLOsGsYkFK9XoRjl/1w== X-Received: by 2002:a05:6214:2684:b0:5c7:d03c:f2b2 with SMTP id gm4-20020a056214268400b005c7d03cf2b2mr1988242qvb.28.1685120062070; Fri, 26 May 2023 09:54:22 -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 pz14-20020ad4550e000000b0061b2a2f949bsm3474qvb.61.2023.05.26.09.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 09:54:21 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] ifaddrs: Get rid of alloca Date: Fri, 26 May 2023 12:54:19 -0400 Message-Id: <20230526165419.1543475-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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Use scratch_buffer and malloc rather than alloca to avoid potential stack overflows. --- 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..e42da62e9b 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,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 +417,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 +818,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; }