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 7F3A93858CDB for ; Wed, 31 May 2023 18:39:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F3A93858CDB 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=1685558378; 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=OUfdyVvOqXwOcOh+x+OFhdK2Zs3BddCcabrH1wBmdRY=; b=f09UTVt0CoX1RvCaMSYxbFIoY3NHq57nyAGWW8XFkoW2KjvPoOyXvieHNP7Wgx6KgXNKzj BLAlCr7uMrtRonTgbGoK7M3HktUZXPKSjUH7OeZkpT7eb2QS/WQSx3R70NC1cr4sayHJU6 L3m8tDeH3Y08Fn9SvjbZn87b3bHDDWk= 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-115-ddmyKN8iP7is9lFYj32Xcw-1; Wed, 31 May 2023 14:39:37 -0400 X-MC-Unique: ddmyKN8iP7is9lFYj32Xcw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-75cac31d806so839448085a.1 for ; Wed, 31 May 2023 11:39:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685558376; x=1688150376; 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=OUfdyVvOqXwOcOh+x+OFhdK2Zs3BddCcabrH1wBmdRY=; b=KN9/6A6vlnhXIY9vEpMe7kHhRX81kkTeUQKg0p6QXVsarnCj+hhsc+QDtckYHMr3uR VjWzYYvIr2kQ8JssXm2NIkaeLPNSs7ChpoMioGTIBs6HkNbMIFjx7qJoem6ZRgS26IPP lcBu3pjbCrk58TW7tznmCejp3STRMxI42Y/2W/w/mdbgaPT5oMwxOgvZVawSRAYGa1RX eeYQuDu3dctEAPnhymigzcWslwV4f6AYRketn8fyVxD/Lml9ScJBs1rnQdB0zRCzBEsX 4wQQDkD7Wc1VoRCypMc/VTh34JDfZcikRGtpC/piXnYtc2g01jVdsvzxBzgjo6cD2njM yYyg== X-Gm-Message-State: AC+VfDwU8BCorAXvbYMMxL6M2MSPe8Vzc9kfuKN5Y24Tc+mr4KNYaEwp OHWYR6CMmI1pykK0U5hibhseWSFyObyZO6R0YADI1hGmbtQyStpuL782jmXe8dU8tK8jO9CG2aC K3JDboU/r5CtLDNxQrwYlXI0ArOmaDsxGQ4CzncqIiMoajv8GFNmNbHwKZmkxkCTr3Demq+sTvq lw2vGN X-Received: by 2002:a05:622a:203:b0:3ef:2fbd:90cd with SMTP id b3-20020a05622a020300b003ef2fbd90cdmr7826118qtx.46.1685558376318; Wed, 31 May 2023 11:39:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GGzX2P/2+QJ/o9h8kcAKGpXkoOeou0E5nMKRGQEPxy63rDA46hHlPapEDhtoMCBW+/MAYwA== X-Received: by 2002:a05:622a:203:b0:3ef:2fbd:90cd with SMTP id b3-20020a05622a020300b003ef2fbd90cdmr7826097qtx.46.1685558375917; Wed, 31 May 2023 11:39:35 -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 j29-20020ac84c9d000000b003f38aabb88asm6263803qtv.20.2023.05.31.11.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 11:39:35 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v4] ifaddrs: Get rid of alloca Date: Wed, 31 May 2023 14:39:33 -0400 Message-Id: <20230531183933.2007586-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: Use scratch_buffer and malloc rather than alloca to avoid potential stack overflows. --- Changes to v3: * Get rid of the 'out_fail' label and its respective gotos since we're using a cleanup handler for the malloc'ed space. sysdeps/unix/sysv/linux/ifaddrs.c | 64 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/sysdeps/unix/sysv/linux/ifaddrs.c b/sysdeps/unix/sysv/linux/ifaddrs.c index 184ee224cb..ef87b00423 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,31 +138,19 @@ __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 }; if (__netlink_sendreq (h, type) < 0) - goto out_fail; + return -1; while (! done) { @@ -173,13 +168,13 @@ __netlink_request (struct netlink_handle *h, int type) read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0)); __netlink_assert_response (h->fd, read_len); if (read_len < 0) - goto out_fail; + return -1; if (nladdr.nl_pid != 0) continue; if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC)) - goto out_fail; + return -1; size_t count = 0; size_t remaining_len = read_len; @@ -205,7 +200,7 @@ __netlink_request (struct netlink_handle *h, int type) errno = EIO; else errno = -nlerr->error; - goto out_fail; + return -1; } } @@ -217,7 +212,7 @@ __netlink_request (struct netlink_handle *h, int type) nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res) + read_len); if (nlm_next == NULL) - goto out_fail; + return -1; nlm_next->next = NULL; nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len); nlm_next->size = read_len; @@ -229,14 +224,7 @@ __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 +312,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 +415,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 +815,7 @@ getifaddrs_internal (struct ifaddrs **ifap) exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); + scratch_buffer_free (&buf); return result; } -- 2.39.2