From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from antelope.elm.relay.mailchannels.net (antelope.elm.relay.mailchannels.net [23.83.212.4]) by sourceware.org (Postfix) with ESMTPS id 5A28638582BC for ; Fri, 15 Sep 2023 23:48:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A28638582BC Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id ECE5B6C0C18; Fri, 15 Sep 2023 23:48:06 +0000 (UTC) Received: from pdx1-sub0-mail-a222.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 90F176C0931; Fri, 15 Sep 2023 23:48:06 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1694821686; a=rsa-sha256; cv=none; b=zLBBeAOXkvJX4BIDX3OLzQJCigtYsB+/swcihyHJgHnc3F4eHTFqIwJIfSVOhf3D52uvLz 71MzN/BQHzk18bI6Ibz4gKFLOM55VrCXy9mGcJ42/Tl9xe75nJxU8EAt4brwtO1tMXTM68 35lxFriwb+683CjLvHopB1SOIf1bpvE4G9BPLdcO7LUisjIQPqOi8Gs31DxWjH9fnws5OM yf3RTp6FGSq0ufdiQRte03zt4wwLRo7fKuVQMsFDpii8KH+5uh32wcKT0gAizxtNzgmgiZ /yfet2TX+C/f72txjkRhln//J37ah2gvnKG68fO+PP+YTmWJVMKEpLheoZskmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1694821686; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yzSTxhDiAb1IyZuc53BFgVDw6jllNRGubEmJTJqukkE=; b=WSbNHc3O4IGEjEsbZ0zjNaGGHD2vnGHi6JfQtxAf9p9BswKAvfEJHhzEXiWIXjkEUS6BLg DuSAr1ECEdJVFXc7hkqypD/ubXNAOb6MMlExKnInu6cNcw7VU3FGUnXXwQd3uKMn6DIpbf IOmnUhXZUTtlXtW8hyaWhR0tBB3MdeHU0JqCVDimNgUOPCAJrWr+U2IDWtgm9fw5CWv6jx qm/Tx0C2aaLvRbxyDJoDp69OzYxiRiodwITxGfaeem/FJpJk1MJ1U3Ig9mGUc2HSMKGvQW 3eJuGxTfW7Bfw7uvCCDcYuLoD0Wngs3VcSYGuyyKtWFVYyM7OVCC1XrQTEN0gw== ARC-Authentication-Results: i=1; rspamd-7d5dc8fd68-vm64j; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MC-Copy: stored-urls X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Harbor-Trade: 2a3b0875281d7388_1694821686810_848934695 X-MC-Loop-Signature: 1694821686810:405988897 X-MC-Ingress-Time: 1694821686810 Received: from pdx1-sub0-mail-a222.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.115.138.83 (trex/6.9.1); Fri, 15 Sep 2023 23:48:06 +0000 Received: from fedora.redhat.com (bras-vprn-toroon4834w-lp130-02-142-113-138-41.dsl.bell.ca [142.113.138.41]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a222.dreamhost.com (Postfix) with ESMTPSA id 4RnW8k0zvwz8Q; Fri, 15 Sep 2023 16:48:06 -0700 (PDT) From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: DJ Delorie Subject: [pushed 2.35 03/13] getaddrinfo: Fix leak with AI_ALL [BZ #28852] Date: Fri, 15 Sep 2023 19:47:45 -0400 Message-ID: <20230915234755.1148216-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230915234755.1148216-1-siddhesh@sourceware.org> References: <20230915234755.1148216-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1171.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_SOFTFAIL,TXREP 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 realloc in convert_hostent_to_gaih_addrtuple and fix up pointers in the result list so that a single block is maintained for hostbyname3_r/hostbyname2_r and freed in gaih_inet. This result is never merged with any other results, since the hosts database does not permit merging. Resolves BZ #28852. Signed-off-by: Siddhesh Poyarekar Reviewed-by: DJ Delorie (cherry picked from commit 300460460706ce3ffe29a7df8966e68323ec5bf1) --- sysdeps/posix/getaddrinfo.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 0629fd147b..e9deb2da6a 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -189,19 +189,16 @@ gaih_inet_serv (const char *servicename, const struct gaih_typeproto *tp, return 0; } -/* Convert struct hostent to a list of struct gaih_addrtuple objects. - h_name is not copied, and the struct hostent object must not be - deallocated prematurely. *RESULT must be NULL or a pointer to a - linked-list. The new addresses are appended at the end. */ +/* Convert struct hostent to a list of struct gaih_addrtuple objects. h_name + is not copied, and the struct hostent object must not be deallocated + prematurely. The new addresses are appended to the tuple array in + RESULT. */ static bool convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, int family, struct hostent *h, struct gaih_addrtuple **result) { - while (*result) - result = &(*result)->next; - /* Count the number of addresses in h->h_addr_list. */ size_t count = 0; for (char **p = h->h_addr_list; *p != NULL; ++p) @@ -212,10 +209,30 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, if (count == 0 || h->h_length > sizeof (((struct gaih_addrtuple) {}).addr)) return true; - struct gaih_addrtuple *array = calloc (count, sizeof (*array)); + struct gaih_addrtuple *array = *result; + size_t old = 0; + + while (array != NULL) + { + old++; + array = array->next; + } + + array = realloc (*result, (old + count) * sizeof (*array)); + if (array == NULL) return false; + *result = array; + + /* Update the next pointers on reallocation. */ + for (size_t i = 0; i < old; i++) + array[i].next = array + i + 1; + + array += old; + + memset (array, 0, count * sizeof (*array)); + for (size_t i = 0; i < count; ++i) { if (family == AF_INET && req->ai_family == AF_INET6) @@ -235,7 +252,6 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, array[0].name = h->h_name; array[count - 1].next = NULL; - *result = array; return true; } -- 2.41.0