From patchwork Wed Sep 20 10:16:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 724682 Delivered-To: patch@linaro.org Received: by 2002:a5d:49cb:0:b0:31d:da82:a3b4 with SMTP id t11csp107881wrs; Wed, 20 Sep 2023 03:16:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF9hC+4z2n3/TxV0kD6DkgNrEjp+CVq7QQanjdLcBTtt1hy7Gg3WtqJA1auDxZmNuRYvzJH X-Received: by 2002:a05:6402:619:b0:530:bad5:248e with SMTP id n25-20020a056402061900b00530bad5248emr1725312edv.3.1695204987246; Wed, 20 Sep 2023 03:16:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695204987; cv=none; d=google.com; s=arc-20160816; b=ILiIPld9C+Ldl+aD/W6Rkhb6J5Y0zEColRttDO+feIR9UXDez1/GpCzuj2pTBHcR/i WIMFJrkO8lK3sgGebINiJq2HwfuDY4WOIa74eBkYZo8Yu+lCrS0xe2/vNZ5Oli1KSXrK fhkoJsD92Kh1hTfZL3OVIFD9uCQjHqAVLhg2qfaYa0D08nDEqp0LAFy627mYlASAiv9W maJuyaE7N9MrU6kE0fKsPK//LXzmMrrfYOVeeRUkE7YWBBfwI6N8OoyLpiIlG/bioxMy fq5m45vO69kuEPSpcdV4Nlb+Eyvoim/H+Muy69vkJ4yZbM5PnKyRZswQzgeTUqYbcNCL /0sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=UFgkirgUV8RBvxUXRnaMR8Qaau42GS1/W9x/qhMkdi4=; fh=bQQSoO3IaiOGW6Yvzkiz7udWQj1rsJvMzjz2/PAtgm8=; b=e3SpGhfK3rvUDGRL1tDpzXAODngBBwVpeeIreM0sr1O3mknnOp18S0mTipfg30Nq2s H0EZvqG8D8HPMEa5+FOvSk39bavLUrBDdCRHoLU2MOqyEoBIMYeI+4zwN2c7BLsRv/uz nmqlm4MxmBsCYgsK3mIdoxx2/+Z4pcD/Stb0jH813L8v0wgAg1ITP4WJjuCwnszd+3WE x9JqavqtswXRyuDuqiC1jLwajkJx5AoCjLdjbINjOsY3gvJY/H6v44i1qzH1808OflM6 916rW1eqL/j/fbO+KymtZaGboO4A246ueYCWtBX0Xzh4I2GzhAZQMksHb0vzoTy+x55v YfZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dzcVUy4c; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u18-20020a056402111200b0052ed507d45esi11765329edv.111.2023.09.20.03.16.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 03:16:27 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dzcVUy4c; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DBCE13858C27 for ; Wed, 20 Sep 2023 10:16:25 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id 1BBD1385735A for ; Wed, 20 Sep 2023 10:16:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1BBD1385735A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-530ab2d9e89so5759165a12.2 for ; Wed, 20 Sep 2023 03:16:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695204975; x=1695809775; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=UFgkirgUV8RBvxUXRnaMR8Qaau42GS1/W9x/qhMkdi4=; b=dzcVUy4cIeTxKi6I2e671UOUFMRja/jww/wNMG/a/NTsxxzX89GN45DupxKyghllVH A8tTiRTnKRpMnSgJF/HtA4jXLZl8UkuFeo3ysVmcixQ8hbDHh3OwYsQ/c304BtmKLuht tDKCBErssTLneoiI6lFxs5OlH5DeIy8lPXEcETaCiWM4VQX2h9Sc/4EEtTcb3vcmyAuq lfWA4QzORYa9QQx37kB6LGTh2mjv2ME3Krq7nWYBTUWQi8TuvYpxexaY25FRkO3KmhO2 e4ljlJtCb1JZzBTGowfNNP2HRqlClIsXIecVptVcnsNzMmJGpfIDr1LdaPY9/+8E90hM gK4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695204975; x=1695809775; 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=UFgkirgUV8RBvxUXRnaMR8Qaau42GS1/W9x/qhMkdi4=; b=A5nRQU7GvzvTAVVqnjYKAoiD4/32JGwxKteYki34zus0V6LseOmGIFySHeDvUhLNg8 8oqCEDg7kJ+z2pQYvG2sBNu0fFRps+iMQygmpKELpIuJTY0J/qNDtKTb7xK+rKjt77S2 lslXwsbEStsiRzOW6oiz0DsL2yOfze+6WgsWUdfL0dp9QakYqqXUdYlcZqVxCilJZNSF QvmEobLPVRC7nah50nlfGmrW3kxi3AQEzqkiKKfFmwZSneZYBH4aYbjxw6RKE9yryskn nL25T8JzB/uHHymalYpdKryMaiHm0hVhyGFIuHOY7EpBKXiG18iFSxmCxb2s6svocPSS CFLQ== X-Gm-Message-State: AOJu0Yyd9UMO+vKtvcpGNX2ta1afBcuK5fEVo46xfNfdVs3pZJ0yufJp rotLmgPiFWWVqPLcWn82PNDvNRbw01vLk939xEvZ4tio X-Received: by 2002:aa7:d610:0:b0:530:c3ac:b684 with SMTP id c16-20020aa7d610000000b00530c3acb684mr1965228edr.23.1695204975073; Wed, 20 Sep 2023 03:16:15 -0700 (PDT) Received: from ubuntu-vm.. (static-212-193-78-212.thenetworkfactory.nl. [212.78.193.212]) by smtp.gmail.com with ESMTPSA id d25-20020aa7d5d9000000b005288f0e547esm8663095eds.55.2023.09.20.03.16.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 03:16:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Florian Weimer Cc: Adhemerval Zanella , Joe Simmons-Talbott Subject: [PATCH v2] resolve: Remove __res_context_query alloca usage Date: Wed, 20 Sep 2023 10:16:07 +0000 Message-Id: <20230920101607.48421-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org The bufsize on current Linux build is: size_t bufsize = (type == 439963904 ? 2 : 1) * (12 + 4 + 255 + 1); So with upper bound as 544 (2 * (12 + 4 + 255 + 1)). However, it might increase to 2 * PACKETSIZE later with malloc. The default scratch_buffer should fullfill the most usual allocation requirement. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Joe Simmons-Talbott --- resolv/res_query.c | 54 ++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/resolv/res_query.c b/resolv/res_query.c index 049de91b95..1b148a2a05 100644 --- a/resolv/res_query.c +++ b/resolv/res_query.c @@ -80,6 +80,7 @@ #include #include #include +#include #if PACKETSZ > 65536 #define MAXPACKET PACKETSZ @@ -114,11 +115,14 @@ __res_context_query (struct resolv_context *ctx, const char *name, struct __res_state *statp = ctx->resp; UHEADER *hp = (UHEADER *) answer; UHEADER *hp2; - int n, use_malloc = 0; - - size_t bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE; - u_char *buf = alloca (bufsize); - u_char *query1 = buf; + int n; + + /* It requires 2 times QUERYSIZE for type == T_QUERY_A_AND_AAAA. */ + struct scratch_buffer buf; + scratch_buffer_init (&buf); + _Static_assert (2 * QUERYSIZE <= sizeof (buf.__space.__c), + "scratch_buffer too small"); + u_char *query1 = buf.data; int nquery1 = -1; u_char *query2 = NULL; int nquery2 = 0; @@ -129,37 +133,28 @@ __res_context_query (struct resolv_context *ctx, const char *name, if (type == T_QUERY_A_AND_AAAA) { n = __res_context_mkquery (ctx, QUERY, name, class, T_A, NULL, - query1, bufsize); + query1, buf.length); if (n > 0) { if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0) { /* Use RESOLV_EDNS_BUFFER_SIZE because the receive buffer can be reallocated. */ - n = __res_nopt (ctx, n, query1, bufsize, + n = __res_nopt (ctx, n, query1, buf.length, RESOLV_EDNS_BUFFER_SIZE); if (n < 0) goto unspec_nomem; } nquery1 = n; - /* Align the buffer. */ - int npad = ((nquery1 + __alignof__ (HEADER) - 1) - & ~(__alignof__ (HEADER) - 1)) - nquery1; - if (n > bufsize - npad) - { - n = -1; - goto unspec_nomem; - } - int nused = n + npad; - query2 = buf + nused; + query2 = buf.data + n; n = __res_context_mkquery (ctx, QUERY, name, class, T_AAAA, - NULL, query2, bufsize - nused); + NULL, query2, buf.length - n); if (n > 0 && (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0) /* Use RESOLV_EDNS_BUFFER_SIZE because the receive buffer can be reallocated. */ - n = __res_nopt (ctx, n, query2, bufsize, + n = __res_nopt (ctx, n, query2, buf.length, RESOLV_EDNS_BUFFER_SIZE); nquery2 = n; } @@ -169,7 +164,7 @@ __res_context_query (struct resolv_context *ctx, const char *name, else { n = __res_context_mkquery (ctx, QUERY, name, class, type, NULL, - query1, bufsize); + query1, buf.length); if (n > 0 && (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0) @@ -181,27 +176,25 @@ __res_context_query (struct resolv_context *ctx, const char *name, advertise = anslen; else advertise = RESOLV_EDNS_BUFFER_SIZE; - n = __res_nopt (ctx, n, query1, bufsize, advertise); + n = __res_nopt (ctx, n, query1, buf.length, advertise); } nquery1 = n; } - if (__glibc_unlikely (n <= 0) && !use_malloc) { + if (__glibc_unlikely (n <= 0)) { /* Retry just in case res_nmkquery failed because of too short buffer. Shouldn't happen. */ - bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * MAXPACKET; - buf = malloc (bufsize); - if (buf != NULL) { - query1 = buf; - use_malloc = 1; + if (scratch_buffer_set_array_size (&buf, + T_QUERY_A_AND_AAAA ? 2 : 1, + MAXPACKET)) { + query1 = buf.data; goto again; } } if (__glibc_unlikely (n <= 0)) { RES_SET_H_ERRNO(statp, NO_RECOVERY); - if (use_malloc) - free (buf); + scratch_buffer_free (&buf); return (n); } @@ -224,8 +217,7 @@ __res_context_query (struct resolv_context *ctx, const char *name, answerp2_malloced); } - if (use_malloc) - free (buf); + scratch_buffer_free (&buf); if (n < 0) { RES_SET_H_ERRNO(statp, TRY_AGAIN); return (n);