From patchwork Thu Sep 14 17:54: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: 722580 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp531444wro; Thu, 14 Sep 2023 10:54:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpPZTOk7fzLKI5tHDTpj4qQHVsTpwh7tV6dUoBUGMxyJvIH+TnBBKhV6USPCFZOKFs2unm X-Received: by 2002:a17:906:76cb:b0:9a5:846d:d829 with SMTP id q11-20020a17090676cb00b009a5846dd829mr5141955ejn.18.1694714062531; Thu, 14 Sep 2023 10:54:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694714062; cv=none; d=google.com; s=arc-20160816; b=i0zEcwl6yqGeWweXYksBiwdLO/nTVUDAbWlurEUDaVeCCn78UofoGAa+A9hHHbUDUA LCvYxzRhViutjKZldNrMP83ifGBL/iAMlGqW9x12N/mbxhx+KMI4jAoXXX2NaeoX9bMX ECqUq21f8MX2GK3lkIVYrOKo02dDkLPCuNFotrxr0han2Uq04YXS2mhSPUcGB6iEJ9my OhGeJn+9g7srBB1SnHGISG6H1akl6Steoes2XT38sY1I/1mLStOVIY6CUQbnkM6tStQE BVLy6FUitS96m108a08klvQ+X7arWdh08Bq/furDXcjK3TpVKTyb9QgP+RE1rJ837UrG hjMQ== 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:to:from:dkim-signature :dmarc-filter:delivered-to; bh=EGgLAW+3cLbwQ0T64mtGUeF0tcOaVvuW7NJjYET4oBI=; fh=Ji/BrrBV5vvGE0PSJoc8P6vA3gQfM/DANnlOBrK0hiY=; b=zFT0310hswODEsCh84n0cliHpOIWVGxMrNf1uM6OJzMGkK3xRKYdB0O2q/g7CyPIaR SBlJ77QTRU6U8NRELuhuGfceYHkMQpmdJBB3beKTkjjRQ3KbEZ3B/lJo+W43SIArFxcX P4HuJzKf6TbnFIztfXh5EE4OD1hj9zvj8jft6U1xwj/bEPukQSRIp8uc7qCglgjLz/b9 mT6rHcx+krolBe2XSF/Mpoh3uCjtJHg2tOhDClgaFYpk5PJt7A9UqQs5t9jQ+ABAPlvN ydXvl8o3OsY/iBDag4yAW0i7GCZl8xPY5yDVl3uUD9Z2XdvsyZmGw+xXqwoF5yeOi9JJ PJuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yy+pqLBe; 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 a5-20020a1709065f8500b0099df29b74a4si1857603eju.397.2023.09.14.10.54.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 10:54:22 -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=Yy+pqLBe; 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 527673856DE6 for ; Thu, 14 Sep 2023 17:54:21 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc2d.google.com (mail-oo1-xc2d.google.com [IPv6:2607:f8b0:4864:20::c2d]) by sourceware.org (Postfix) with ESMTPS id 2BE4E3857732 for ; Thu, 14 Sep 2023 17:54:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BE4E3857732 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-oo1-xc2d.google.com with SMTP id 006d021491bc7-57656330b80so728679eaf.3 for ; Thu, 14 Sep 2023 10:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694714053; x=1695318853; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=EGgLAW+3cLbwQ0T64mtGUeF0tcOaVvuW7NJjYET4oBI=; b=Yy+pqLBe657ZfMC0QtXLZiaKd6uy0EkZQpzJOwwhNEpX98S353DKs8ABLk8m8FA0ym CsW2MvRB6GSFp3ht1NO6o/Q3/c7eOMGYFMUZcPtKZ6OT3LKBSHO0vJyaUjRsVIhOXTOX wI/xK0JWPlVnYOgaPO3RLWnnvMcr9BZe7g2C6bWppgUUdsl8LLn5y7JT9q/SXGToRGRq uUVWWsy/Fmjp6eCn5G6bM+KJnYgdDcBUbSR/33QIfJ94o+XqckU5Gxzof8y3AJ84z4PV wZTufoT3uKk9AocEs21Bivl8UtFDlyDxjnzcYDLOQphuhWBp3tVV2v2uysn3C6b1MUTW 3lcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694714053; x=1695318853; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EGgLAW+3cLbwQ0T64mtGUeF0tcOaVvuW7NJjYET4oBI=; b=jW5BrqPwJlRSe8tjm6ehMHyYdw55k0Th7FKiX8t+n1DsvlJr7WJqLLwdCO0xM66guv oUCgrWTHnMM2IAmTRm47tl+nXu/KmUbXVV+9rqiua/E6Zz4mJSYttFcbbxqjlnWy9H8w 0xlgtYG+Zp9et5X9mBtvgWXKyIhQN6lIFC8u+O4CJ2Tey4Grj2Pbu96As/cjL52zewDE +KDfUv31+MvlY5MU5H4rRkZ/GcjRX8RD2haZG91HEeL51vgjO/AuQIWHv94a3qOjw3TP vW0Uw7pKAF2vD6MThH7tv9RV8IufnlV1YDO4lTiOnrqrGdD/Qc0fFFgGe9c6RGXXYf6z qkWA== X-Gm-Message-State: AOJu0Yxrn7/mmnvSaGt8aLCqcdBqo/Mo1AEGzSZD0+t+crbxhOVUZySn mcJNqVsEwxONQyG3Clnx63MaPwcWFqcZjnj6VxBhjw== X-Received: by 2002:a4a:9c97:0:b0:571:1fad:ebe0 with SMTP id z23-20020a4a9c97000000b005711fadebe0mr6141648ooj.3.1694714052821; Thu, 14 Sep 2023 10:54:12 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:5173:d70:d8d:4d2c]) by smtp.gmail.com with ESMTPSA id q127-20020a4a4b85000000b0057367fd1469sm889621ooa.21.2023.09.14.10.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 10:54:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Joe Simmons-Talbott Subject: [PATCH] resolve: Remove __res_context_query alloca usage Date: Thu, 14 Sep 2023 14:54:07 -0300 Message-Id: <20230914175407.2406866-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 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 | 47 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/resolv/res_query.c b/resolv/res_query.c index 049de91b95..b234db83c1 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,14 +133,14 @@ __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; @@ -146,20 +150,20 @@ __res_context_query (struct resolv_context *ctx, const char *name, /* Align the buffer. */ int npad = ((nquery1 + __alignof__ (HEADER) - 1) & ~(__alignof__ (HEADER) - 1)) - nquery1; - if (n > bufsize - npad) + if (n > buf.length - npad) { n = -1; goto unspec_nomem; } int nused = n + npad; - query2 = buf + nused; + query2 = buf.data + nused; n = __res_context_mkquery (ctx, QUERY, name, class, T_AAAA, - NULL, query2, bufsize - nused); + NULL, query2, buf.length - nused); 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 +173,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 +185,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 +226,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);