From patchwork Thu Jun 1 14:55:00 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: 70455 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 925323857345 for ; Thu, 1 Jun 2023 14:55:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 925323857345 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685631336; bh=LaLM7qJubPw3DperBpWAn3eYjAb9WRBmVLURLcxNybk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=dGQQci4Ew4jTXc+xAd+JEo4RoG2/I4SOrVIv9Fo4jTRvtx9XkCeOAjynnA2t7641U N+2ivhNNgtS2iXqqNDGBI7B34AeQR+4rytDqGzcxNHo0JQ8VySJzCsCcYgpJdLSTtG 4Y+zTcQxzNHHHGdH0NdphzcxyC+UBnW26Gt3rLK4= 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 434E13857029 for ; Thu, 1 Jun 2023 14:55:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 434E13857029 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-121-r9CtwPufNvuA0Fwc6YNWlQ-1; Thu, 01 Jun 2023 10:55:04 -0400 X-MC-Unique: r9CtwPufNvuA0Fwc6YNWlQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6260e66ec46so12622636d6.3 for ; Thu, 01 Jun 2023 07:55:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685631304; x=1688223304; 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=LaLM7qJubPw3DperBpWAn3eYjAb9WRBmVLURLcxNybk=; b=VLLiRlmdMWwcFisSnjCJzmtZQP70bj5V0pL5ktAMo+tz6+uu6deu1q21i6a9RTohlJ NOzlcXbk7gOL84fQ3C99w5uGDItw3jklLRmvQncZBeB4c57JlL3pmypB6ooDUCxpEFaz GTtMu4oeDHSTlNK184RA/StQTXxXONN+za74hBusih26nDYipA7pPZJ6YJVtsJngTmLV ks3dVpqdAUdx50h92TSdYHWD/+J3UQBXoqqpjyo3GEQEO78oVEON9nZHfx7/9LlIqTHw fM0qTQ+BlveXsHP8DFKVwc1nHTj4YBBEX/vOGU/SIbO8KnoKnuDMokhGhqvijLQuhTd2 3ISw== X-Gm-Message-State: AC+VfDxdh7S8BoMO8dQQSBlXBkdhKMp1hsAGTe3WfBqrd6f0WScmwCdH f1KQuKPEboIpcmHcpbJvN/qflcNZd4lL4d3wAZmPBU7y+gtol+xaHty5opjAkjPq6sOjYycp736 2ak1pQqG6Hao6c/MqWo2FCembH0rb2Osv25BUF+AFlf6J6yNT50aDDd7igaEu7ZgSYi1IJWrN5o 32qmrA X-Received: by 2002:ad4:5aab:0:b0:5fd:7701:88c5 with SMTP id u11-20020ad45aab000000b005fd770188c5mr12218949qvg.6.1685631303944; Thu, 01 Jun 2023 07:55:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5T8r7dxQmX94lm2W3f0SmMa01T9yFshhfUE81rRWzej6MYTN4dWfXb5wrvfJuw5QuNGf45oQ== X-Received: by 2002:ad4:5aab:0:b0:5fd:7701:88c5 with SMTP id u11-20020ad45aab000000b005fd770188c5mr12218927qvg.6.1685631303623; Thu, 01 Jun 2023 07:55:03 -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 qf2-20020a0562144b8200b0062621e3b511sm4866237qvb.74.2023.06.01.07.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 07:55:02 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] check_native: Get rid of alloca Date: Thu, 1 Jun 2023 10:55:00 -0400 Message-Id: <20230601145500.2039369-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, 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 malloc rather than alloca to avoid potential stack overflow. Reviewed-by: Adhemerval Zanella --- Changes to v1: * Do not use a cleanup handler. sysdeps/unix/sysv/linux/check_native.c | 35 ++++++++------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/sysdeps/unix/sysv/linux/check_native.c b/sysdeps/unix/sysv/linux/check_native.c index 34876ca624..601f14e5b0 100644 --- a/sysdeps/unix/sysv/linux/check_native.c +++ b/sysdeps/unix/sysv/linux/check_native.c @@ -48,11 +48,20 @@ __check_native (uint32_t a1_index, int *a1_native, nladdr.nl_family = AF_NETLINK; socklen_t addr_len = sizeof (nladdr); - bool use_malloc = false; if (fd < 0) return; + /* 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; + if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0 || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0) goto out; @@ -81,26 +90,6 @@ __check_native (uint32_t a1_index, int *a1_native, memset (&nladdr, '\0', sizeof (nladdr)); nladdr.nl_family = AF_NETLINK; -#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 - 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; - } - struct iovec iov = { buf, buf_size }; if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0, @@ -170,7 +159,5 @@ __check_native (uint32_t a1_index, int *a1_native, out: __close_nocancel_nostatus (fd); - - if (use_malloc) - free (buf); + free(buf); }