Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2861EC3DA4A for ; Fri, 9 Aug 2024 11:16:55 +0000 (UTC) Received: from MA0PR01CU012.outbound.protection.outlook.com (MA0PR01CU012.outbound.protection.outlook.com [40.107.57.97]) by mx.groups.io with SMTP id smtpd.web11.81729.1723202205608717177 for ; Fri, 09 Aug 2024 04:16:46 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kpit.com header.s=selector1 header.b=LcE5m1iK; spf=pass (domain: kpit.com, ip: 40.107.57.97, mailfrom: nikhil.r@kpit.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hYagq8lGtHDjxxeD90eMcd2Lq4BI0OK57VFOyDvitw+vy109xVqKz+OrG5FH/GdtykYb28uN69z1O1e92xruqf91aBuboe/lo1Q38oaelg0LLODSo054Tpo+aGWc/jYUR1pDpozTapOUOnHgh8vS8TpFR5k/qFgUFmrP+Oqr1Y9kitbtbq9vHZZ5eYc/AqlCIJ1rE4HUmMaLMtlb9rNHD33EI3fLUHllqC3k9OcQ4RcCVz75Y3OXzQMQ8Hhv0KdCwgk1DT4XrCcWu5vzhD4j2SE1hHhHJSJAtVG+iLohPJz6/g1ivp3qxAakuu5H+igIB7Jso/oLtI3Q7rk7hpHvng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bMrv+AGslIDLRaPOudcw4Ojcwd9WC+rx5LS+QbwaAWk=; b=Y64KrS3+8kgQBE3qqoX+P8Bo68/bhof2HeWrdhmx2X0VcCZinncKsIXOo6NT9S8acT1vaiKBDCd83a6S+qtrrioLLr7HySoYbE84+PIEAgtRMdJlMF4FbX3kTprhdaTTJKW1dy66Qf8/AHFaLmipVfEM8omv7KtRSTYXmNoMA+sDJPBmWgkIB5uEAPlVupwjQWkRkq7rmXw3eXPcgjzSlvOzFIGXVqbDAwFwEfnTEwSeQatZR2AC70nB5TK++z7lvCSY7w/tCnfuJT/Na8ZxiqbMMTncXheJDs3POxdAoeqK1ru50BoAzdWOem/Ja3XTWzj1iWZjI1j5S67CidDV/A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=fail (sender ip is 103.243.227.16) smtp.rcpttodomain=lists.openembedded.org smtp.mailfrom=kpit.com; dmarc=fail (p=reject sp=none pct=100) action=oreject header.from=kpit.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kpit.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bMrv+AGslIDLRaPOudcw4Ojcwd9WC+rx5LS+QbwaAWk=; b=LcE5m1iK8J6lo8nuVB7X8/uOL2PArR7LQWa8kFOxhzD2ChMKsPFJDmzEfSgCVBG60Elku4ZestBHJUv8DvdLgHCNYlWSoIzeDblYoKC7ZxOjQK3gCTbwpkDzmOS1dZV6fMvzJhhv4vmZ6Vu05cuWMUlhGifIHTGfEYv5zBGkGEI= Received: from SI2P153CA0013.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::12) by PN3PR01MB9952.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:16e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.8; Fri, 9 Aug 2024 11:16:37 +0000 Received: from SG2PEPF000B66CE.apcprd03.prod.outlook.com (2603:1096:4:140:cafe::3f) by SI2P153CA0013.outlook.office365.com (2603:1096:4:140::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.9 via Frontend Transport; Fri, 9 Aug 2024 11:16:36 +0000 X-MS-Exchange-Authentication-Results: spf=fail (sender IP is 103.243.227.16) smtp.mailfrom=kpit.com; dkim=none (message not signed) header.d=none;dmarc=fail action=oreject header.from=kpit.com; Received-SPF: Fail (protection.outlook.com: domain of kpit.com does not designate 103.243.227.16 as permitted sender) receiver=protection.outlook.com; client-ip=103.243.227.16; helo=mail.kpit.com; Received: from mail.kpit.com (103.243.227.16) by SG2PEPF000B66CE.mail.protection.outlook.com (10.167.240.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.8 via Frontend Transport; Fri, 9 Aug 2024 11:16:35 +0000 Received: from L-17494.kpit.com (10.30.140.23) by PH3EXCH01.kpit.com (10.52.16.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 9 Aug 2024 16:46:33 +0530 From: Nikhil R To: , CC: Bindu Bhabu Subject: [OE-core][kirkstone][PATCH] expat: fix CVE-2023-52425 Date: Fri, 9 Aug 2024 16:46:17 +0530 Message-ID: <20240809111617.27054-1-nikhilr5@kpit.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Type: text/plain; charset="no" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.30.140.23] X-ClientProxiedBy: PH3ExchFE01.kpit.com (10.52.16.111) To PH3EXCH01.kpit.com (10.52.16.25) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SG2PEPF000B66CE:EE_|PN3PR01MB9952:EE_ X-MS-Office365-Filtering-Correlation-Id: 5bbebc4e-3498-44da-0421-08dcb864bb5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?OW72AzWcXjGyx/LmvLxRMpoWYgJM752DACosT17LMPIfHOrJrtiHalwvAbEJ?= =?us-ascii?Q?2OUM8g4u9ByXYqyZnifFhv1CkWldd3s2G2+yuMDYec7Eg/UDbB+sa1RgBRk0?= =?us-ascii?Q?xx44iND2APefG9d8sxM9P4kwoZqH8s1LnxFccjX31tUpKe3qlF/TCeOh4eRG?= =?us-ascii?Q?S/8Wg3Ko+LNYLv676iT+va9vkZEBk48Js2p17dbaD4Tcp7rrTH+PyFoZl9dj?= =?us-ascii?Q?iJoTn03UkQB+scMlvTSDLDfwHatDjylpqPOCxEEMiZjakoVVmCp7/6Ru9OSY?= =?us-ascii?Q?yJa66Of2Lq8l4vdpPoKsYdq6SAPj3eQG+M4FNZ+P8PJNhWnNuk/WNFAmd04F?= =?us-ascii?Q?MJVZPkQJkQ3ZtXZfgbgbmo4hG+MF4D43pNPDgCiW+WESM1MDQp3tAQ76nFb8?= =?us-ascii?Q?jYfF1ezEbZUNR7vBis8txQoVFZ14U7BQ1Oleg2cqYtRcP125dGnicFFCNcSk?= =?us-ascii?Q?BuEcxojauNTtYSyMNiDM/3hXqyiLOudsySh9/XITrGLDrH84XEb6j+g8cW3s?= =?us-ascii?Q?71CQroSAImReJq1wxjCQDAPqfVQEDLk00S0GHoWtDy1u/IblF5LQEeKH0wEl?= =?us-ascii?Q?xe/xJmuwFwszxo6QuBaFMXgTNlYMETdMtCRObxgo7DJt8CjELXkw0xJumfsN?= =?us-ascii?Q?uWu0cNEzh7EqZlYbCdjb2DAfzvP9D2sidWab96rNUyiOm0V3Qcr2CzggD3GX?= =?us-ascii?Q?lCcp0kbisYHXAo0OLTUmHKQcUkx5SYryNbVuSR3JhreofhWMNTZnRxib4uHR?= =?us-ascii?Q?qDG6vFc3sfY1MsvcjdJ7m9C6PTvcfzQQ8QPxz25dYnWzDEBgYU1wzmsvX0rT?= =?us-ascii?Q?0RzXRN6VEh4PikyOrpMfAjixkPJwB3BIfGwjFbo6c/vD8o+YtzJOlYwi8fUm?= =?us-ascii?Q?Hsj0KG+HqAyhfVW+oqIGqxv90whVgZTjKGPqO/UrHbfs7HmiyvkifiM0186k?= =?us-ascii?Q?dTtMEdvbWd6jiEKqn+sb68NrHLIbzUOIj6rqyyJBMALnZ4VUtwz4SIB5oS1Z?= =?us-ascii?Q?/bKe2sNeTUMSGkPxkjUoWKnT274KYkr/ULZ/RXjwt3Z9RNHEbTc9YugK5MUJ?= =?us-ascii?Q?3RzEg4PpfyNvaJ2BEzWJlHFsLKkAjyYhkbwrFyuu9Vw+YFFPtUI4W1PDIJKx?= =?us-ascii?Q?tBDT2JmIGokZUaIMQB/PDB+GNe32OIyD1tbCha7HTzFWGd29iGuIgMZehcBF?= =?us-ascii?Q?UE0zj/j81TYdWOfVMhZ/agHd2heB1lARL9T89cUktgIyIf/czSZV6S5KDm9c?= =?us-ascii?Q?PdUe8hcmSaPEv98cR2bKLYOp02ssq8ujgxrHi0lz3zVfd7NCnLFSrUnUBnvi?= =?us-ascii?Q?O0yg/XJnjgLkxAUcCWzxbCe/QDdVtGdd373wJmmeZOF5l4ISqrPVdBEPPJtb?= =?us-ascii?Q?kJK5OLIN/An+7c9KVBcUjVjW6SI+f+rXg07bhlgz4/z+DChGFg=3D=3D?= X-Forefront-Antispam-Report: CIP:103.243.227.16;CTRY:IN;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.kpit.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(376014);DIR:OUT;SFP:1102; X-OriginatorOrg: kpit.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Aug 2024 11:16:35.8415 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5bbebc4e-3498-44da-0421-08dcb864bb5e X-MS-Exchange-CrossTenant-Id: 3539451e-b46e-4a26-a242-ff61502855c7 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3539451e-b46e-4a26-a242-ff61502855c7;Ip=[103.243.227.16];Helo=[mail.kpit.com] X-MS-Exchange-CrossTenant-AuthSource: SG2PEPF000B66CE.apcprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PN3PR01MB9952 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Fri, 09 Aug 2024 11:16:55 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/203166 libexpat through 2.5.0 allows a denial of service (resource consumption) because many full reparsings are required in the case of a large token for which multiple buffer fills are needed. References: https://security-tracker.debian.org/tracker/CVE-2023-52425 https://ubuntu.com/security/CVE-2023-52425 https://packages.ubuntu.com/mantic/expat https://github.com/libexpat/libexpat/pull/789 Signed-off-by: Bindu Bhabu --- .../expat/expat/CVE-2023-52425.patch | 581 ++++++++++++++++++ meta/recipes-core/expat/expat_2.5.0.bb | 1 + 2 files changed, 582 insertions(+) create mode 100644 meta/recipes-core/expat/expat/CVE-2023-52425.patch diff --git a/meta/recipes-core/expat/expat/CVE-2023-52425.patch b/meta/reci= pes-core/expat/expat/CVE-2023-52425.patch new file mode 100644 index 0000000000..77e24fd163 --- /dev/null +++ b/meta/recipes-core/expat/expat/CVE-2023-52425.patch @@ -0,0 +1,581 @@ +Backport of https://github.com/libexpat/libexpat/pull/789 + +From 9cdf9b8d77d5c2c2a27d15fb68dd3f83cafb45a1 Mon Sep 17 00:00:00 2001 +From: Snild Dolkow +Date: Thu, 17 Aug 2023 16:25:26 +0200 +Subject: [PATCH] Skip parsing after repeated partials on the same token +MIME-Version: 1.0 +Content-Type: text/plain; charset=3DUTF-8 +Content-Transfer-Encoding: 8bit + +When the parse buffer contains the starting bytes of a token but not +all of them, we cannot parse the token to completion. We call this a +partial token. When this happens, the parse position is reset to the +start of the token, and the parse() call returns. The client is then +expected to provide more data and call parse() again. + +In extreme cases, this means that the bytes of a token may be parsed +many times: once for every buffer refill required before the full token +is present in the buffer. + +Math: + Assume there's a token of T bytes + Assume the client fills the buffer in chunks of X bytes + We'll try to parse X, 2X, 3X, 4X ... until mX =3D=3D T (technically >=3D= ) + That's (m=C2=B2+m)X/2 =3D (T=C2=B2/X+T)/2 bytes parsed (arithmetic progr= ession) + While it is alleviated by larger refills, this amounts to O(T=C2=B2) + +Expat grows its internal buffer by doubling it when necessary, but has +no way to inform the client about how much space is available. Instead, +we add a heuristic that skips parsing when we've repeatedly stopped on +an incomplete token. Specifically: + + * Only try to parse if we have a certain amount of data buffered + * Every time we stop on an incomplete token, double the threshold + * As soon as any token completes, the threshold is reset + +This means that when we get stuck on an incomplete token, the threshold +grows exponentially, effectively making the client perform larger buffer +fills, limiting how many times we can end up re-parsing the same bytes. + +Math: + Assume there's a token of T bytes + Assume the client fills the buffer in chunks of X bytes + We'll try to parse X, 2X, 4X, 8X ... until (2^k)X =3D=3D T (or larger) + That's (2^(k+1)-1)X bytes parsed -- e.g. 15X if T =3D 8X + This is equal to 2T-X, which amounts to O(T) + +We could've chosen a faster growth rate, e.g. 4 or 8. Those seem to +increase performance further, at the cost of further increasing the +risk of growing the buffer more than necessary. This can easily be +adjusted in the future, if desired. + +This is all completely transparent to the client, except for: +1. possible delay of some callbacks (when our heuristic overshoots) +2. apps that never do isFinal=3DXML_TRUE could miss data at the end + +For the affected testdata, this change shows a 100-400x speedup. +The recset.xml benchmark shows no clear change either way. + +Before: +benchmark -n ../testdata/largefiles/recset.xml 65535 3 + 3 loops, with buffer size 65535. Average time per loop: 0.270223 +benchmark -n ../testdata/largefiles/aaaaaa_attr.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 15.033048 +benchmark -n ../testdata/largefiles/aaaaaa_cdata.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.018027 +benchmark -n ../testdata/largefiles/aaaaaa_comment.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 11.775362 +benchmark -n ../testdata/largefiles/aaaaaa_tag.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 11.711414 +benchmark -n ../testdata/largefiles/aaaaaa_text.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.019362 + +After: +./run.sh benchmark -n ../testdata/largefiles/recset.xml 65535 3 + 3 loops, with buffer size 65535. Average time per loop: 0.269030 +./run.sh benchmark -n ../testdata/largefiles/aaaaaa_attr.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.044794 +./run.sh benchmark -n ../testdata/largefiles/aaaaaa_cdata.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.016377 +./run.sh benchmark -n ../testdata/largefiles/aaaaaa_comment.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.027022 +./run.sh benchmark -n ../testdata/largefiles/aaaaaa_tag.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.099360 +./run.sh benchmark -n ../testdata/largefiles/aaaaaa_text.xml 4096 3 + 3 loops, with buffer size 4096. Average time per loop: 0.017956 + +CVE: CVE-2023-52425 +Upstream-Status: Backport [http://archive.ubuntu.com/ubuntu/pool/main/e/ex= pat/expat_2.5.0-2ubuntu0.1.debian.tar.xz] +Comments: Hunks refreshed. + Patch difference/summary has also been modified. +Signed-off-by: Bhabu Bindu + +--- + lib/expat.h | 5 +++++ + lib/libexpat.def.cmake | 2 ++ + lib/xmlparse.c | 228 ++++++++++++++++++++++++++++++++++++++++++--= -------------------------------- + xmlwf/xmlwf.c | 20 ++++++++++++++++++++ + xmlwf/xmlwf_helpgen.py | 4 ++++ + 5 files changed, 156 insertions(+), 103 deletions(-) + +--- a/lib/expat.h ++++ b/lib/expat.h +@@ -16,6 +16,7 @@ + Copyright (c) 2016 Thomas Beutlich + Copyright (c) 2017 Rhodri James + Copyright (c) 2022 Thijs Schreijer ++ Copyright (c) 2023 Sony Corporation / Snild Dolkow + Licensed under the MIT license: + + Permission is hereby granted, free of charge, to any person obtain= ing +@@ -1050,6 +1051,10 @@ XML_SetBillionLaughsAttackProtectionActi + XML_Parser parser, unsigned long long activationThresholdBytes); + #endif + ++/* Added in Expat 2.6.0. */ ++XMLPARSEAPI(XML_Bool) ++XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled); ++ + /* Expat follows the semantic versioning convention. + See http://semver.org. + */ +--- a/lib/libexpat.def.cmake ++++ b/lib/libexpat.def.cmake +@@ -77,3 +77,5 @@ EXPORTS + ; added with version 2.4.0 + @_EXPAT_COMMENT_DTD_OR_GE@ XML_SetBillionLaughsAttackProtectionActivation= Threshold @69 + @_EXPAT_COMMENT_DTD_OR_GE@ XML_SetBillionLaughsAttackProtectionMaximumAmp= lification @70 ++; added with version 2.6.0 ++ XML_SetReparseDeferralEnabled @71 +--- a/lib/xmlparse.c ++++ b/lib/xmlparse.c +@@ -36,6 +36,7 @@ + Copyright (c) 2022 Samanta Navarro + Copyright (c) 2022 Jeffrey Walton + Copyright (c) 2022 Jann Horn ++ Copyright (c) 2023 Sony Corporation / Snild Dolkow + Licensed under the MIT license: + + Permission is hereby granted, free of charge, to any person obtain= ing +@@ -73,6 +74,7 @@ + # endif + #endif + ++#include + #include + #include /* memset(), memcpy() */ + #include +@@ -196,6 +198,8 @@ typedef char ICHAR; + /* Do safe (NULL-aware) pointer arithmetic */ + #define EXPAT_SAFE_PTR_DIFF(p, q) (((p) && (q)) ? ((p) - (q)) : 0) + ++#define EXPAT_MIN(a, b) (((a) < (b)) ? (a) : (b)) ++ + #include "internal.h" + #include "xmltok.h" + #include "xmlrole.h" +@@ -617,6 +621,9 @@ struct XML_ParserStruct { + const char *m_bufferLim; + XML_Index m_parseEndByteIndex; + const char *m_parseEndPtr; ++ size_t m_partialTokenBytesBefore; /* used in heuristic to avoid O(n^2) = */ ++ XML_Bool m_reparseDeferralEnabled; ++ int m_lastBufferRequestSize; + XML_Char *m_dataBuf; + XML_Char *m_dataBufEnd; + XML_StartElementHandler m_startElementHandler; +@@ -948,6 +955,46 @@ get_hash_secret_salt(XML_Parser parser) + return parser->m_hash_secret_salt; + } + ++static enum XML_Error ++callProcessor(XML_Parser parser, const char *start, const char *end, ++ const char **endPtr) { ++ const size_t have_now =3D EXPAT_SAFE_PTR_DIFF(end, start); ++ ++ if (parser->m_reparseDeferralEnabled ++ && ! parser->m_parsingStatus.finalBuffer) { ++ // Heuristic: don't try to parse a partial token again until the amou= nt of ++ // available data has increased significantly. ++ const size_t had_before =3D parser->m_partialTokenBytesBefore; ++ // ...but *do* try anyway if we're close to causing a reallocation. ++ size_t available_buffer ++ =3D EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer); ++#if XML_CONTEXT_BYTES > 0 ++ available_buffer -=3D EXPAT_MIN(available_buffer, XML_CONTEXT_BYTES); ++#endif ++ available_buffer ++ +=3D EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd= ); ++ // m_lastBufferRequestSize is never assigned a value < 0, so the cast= is ok ++ const bool enough ++ =3D (have_now >=3D 2 * had_before) ++ || ((size_t)parser->m_lastBufferRequestSize > available_buffer)= ; ++ ++ if (! enough) { ++ *endPtr =3D start; // callers may expect this to be set ++ return XML_ERROR_NONE; ++ } ++ } ++ const enum XML_Error ret =3D parser->m_processor(parser, start, end, en= dPtr); ++ if (ret =3D=3D XML_ERROR_NONE) { ++ // if we consumed nothing, remember what we had on this parse attempt= . ++ if (*endPtr =3D=3D start) { ++ parser->m_partialTokenBytesBefore =3D have_now; ++ } else { ++ parser->m_partialTokenBytesBefore =3D 0; ++ } ++ } ++ return ret; ++} ++ + static XML_Bool /* only valid for root parser */ + startParsing(XML_Parser parser) { + /* hash functions must be initialized before setContext() is called */ +@@ -1129,6 +1176,9 @@ parserInit(XML_Parser parser, const XML_ + parser->m_bufferEnd =3D parser->m_buffer; + parser->m_parseEndByteIndex =3D 0; + parser->m_parseEndPtr =3D NULL; ++ parser->m_partialTokenBytesBefore =3D 0; ++ parser->m_reparseDeferralEnabled =3D XML_TRUE; ++ parser->m_lastBufferRequestSize =3D 0; + parser->m_declElementType =3D NULL; + parser->m_declAttributeId =3D NULL; + parser->m_declEntity =3D NULL; +@@ -1298,6 +1348,7 @@ XML_ExternalEntityParserCreate(XML_Parse + to worry which hash secrets each table has. + */ + unsigned long oldhash_secret_salt; ++ XML_Bool oldReparseDeferralEnabled; + + /* Validate the oldParser parameter before we pull everything out of it= */ + if (oldParser =3D=3D NULL) +@@ -1342,6 +1393,7 @@ XML_ExternalEntityParserCreate(XML_Parse + to worry which hash secrets each table has. + */ + oldhash_secret_salt =3D parser->m_hash_secret_salt; ++ oldReparseDeferralEnabled =3D parser->m_reparseDeferralEnabled; + + #ifdef XML_DTD + if (! context) +@@ -1394,6 +1446,7 @@ XML_ExternalEntityParserCreate(XML_Parse + parser->m_defaultExpandInternalEntities =3D oldDefaultExpandInternalEnt= ities; + parser->m_ns_triplets =3D oldns_triplets; + parser->m_hash_secret_salt =3D oldhash_secret_salt; ++ parser->m_reparseDeferralEnabled =3D oldReparseDeferralEnabled; + parser->m_parentParser =3D oldParser; + #ifdef XML_DTD + parser->m_paramEntityParsing =3D oldParamEntityParsing; +@@ -1848,55 +1901,8 @@ XML_Parse(XML_Parser parser, const char + parser->m_parsingStatus.parsing =3D XML_PARSING; + } + +- if (len =3D=3D 0) { +- parser->m_parsingStatus.finalBuffer =3D (XML_Bool)isFinal; +- if (! isFinal) +- return XML_STATUS_OK; +- parser->m_positionPtr =3D parser->m_bufferPtr; +- parser->m_parseEndPtr =3D parser->m_bufferEnd; +- +- /* If data are left over from last buffer, and we now know that these +- data are the final chunk of input, then we have to check them agai= n +- to detect errors based on that fact. +- */ +- parser->m_errorCode +- =3D parser->m_processor(parser, parser->m_bufferPtr, +- parser->m_parseEndPtr, &parser->m_bufferPtr= ); +- +- if (parser->m_errorCode =3D=3D XML_ERROR_NONE) { +- switch (parser->m_parsingStatus.parsing) { +- case XML_SUSPENDED: +- /* It is hard to be certain, but it seems that this case +- * cannot occur. This code is cleaning up a previous parse +- * with no new data (since len =3D=3D 0). Changing the parsing +- * state requires getting to execute a handler function, and +- * there doesn't seem to be an opportunity for that while in +- * this circumstance. +- * +- * Given the uncertainty, we retain the code but exclude it +- * from coverage tests. +- * +- * LCOV_EXCL_START +- */ +- XmlUpdatePosition(parser->m_encoding, parser->m_positionPtr, +- parser->m_bufferPtr, &parser->m_position); +- parser->m_positionPtr =3D parser->m_bufferPtr; +- return XML_STATUS_SUSPENDED; +- /* LCOV_EXCL_STOP */ +- case XML_INITIALIZED: +- case XML_PARSING: +- parser->m_parsingStatus.parsing =3D XML_FINISHED; +- /* fall through */ +- default: +- return XML_STATUS_OK; +- } +- } +- parser->m_eventEndPtr =3D parser->m_eventPtr; +- parser->m_processor =3D errorProcessor; +- return XML_STATUS_ERROR; +- } +-#ifndef XML_CONTEXT_BYTES +- else if (parser->m_bufferPtr =3D=3D parser->m_bufferEnd) { ++#if XML_CONTEXT_BYTES =3D=3D 0 ++ if (parser->m_bufferPtr =3D=3D parser->m_bufferEnd) { + const char *end; + int nLeftOver; + enum XML_Status result; +@@ -1907,12 +1913,15 @@ XML_Parse(XML_Parser parser, const char + parser->m_processor =3D errorProcessor; + return XML_STATUS_ERROR; + } ++ // though this isn't a buffer request, we assume that `len` is the ap= p's ++ // preferred buffer fill size, and therefore save it here. ++ parser->m_lastBufferRequestSize =3D len; + parser->m_parseEndByteIndex +=3D len; + parser->m_positionPtr =3D s; + parser->m_parsingStatus.finalBuffer =3D (XML_Bool)isFinal; + + parser->m_errorCode +- =3D parser->m_processor(parser, s, parser->m_parseEndPtr =3D s + = len, &end); ++ =3D callProcessor(parser, s, parser->m_parseEndPtr =3D s + len, &= end); + + if (parser->m_errorCode !=3D XML_ERROR_NONE) { + parser->m_eventEndPtr =3D parser->m_eventPtr; +@@ -1939,23 +1948,25 @@ XML_Parse(XML_Parser parser, const char + &parser->m_position); + nLeftOver =3D s + len - end; + if (nLeftOver) { +- if (parser->m_buffer =3D=3D NULL +- || nLeftOver > parser->m_bufferLim - parser->m_buffer) { +- /* avoid _signed_ integer overflow */ +- char *temp =3D NULL; +- const int bytesToAllocate =3D (int)((unsigned)len * 2U); +- if (bytesToAllocate > 0) { +- temp =3D (char *)REALLOC(parser, parser->m_buffer, bytesToAlloc= ate); +- } +- if (temp =3D=3D NULL) { +- parser->m_errorCode =3D XML_ERROR_NO_MEMORY; +- parser->m_eventPtr =3D parser->m_eventEndPtr =3D NULL; +- parser->m_processor =3D errorProcessor; +- return XML_STATUS_ERROR; +- } +- parser->m_buffer =3D temp; +- parser->m_bufferLim =3D parser->m_buffer + bytesToAllocate; ++ // Back up and restore the parsing status to avoid XML_ERROR_SUSPEN= DED ++ // (and XML_ERROR_FINISHED) from XML_GetBuffer. ++ const enum XML_Parsing originalStatus =3D parser->m_parsingStatus.p= arsing; ++ parser->m_parsingStatus.parsing =3D XML_PARSING; ++ void *const temp =3D XML_GetBuffer(parser, nLeftOver); ++ parser->m_parsingStatus.parsing =3D originalStatus; ++ // GetBuffer may have overwritten this, but we want to remember wha= t the ++ // app requested, not how many bytes were left over after parsing. ++ parser->m_lastBufferRequestSize =3D len; ++ if (temp =3D=3D NULL) { ++ // NOTE: parser->m_errorCode has already been set by XML_GetBuffe= r(). ++ parser->m_eventPtr =3D parser->m_eventEndPtr =3D NULL; ++ parser->m_processor =3D errorProcessor; ++ return XML_STATUS_ERROR; + } ++ // Since we know that the buffer was empty and XML_CONTEXT_BYTES is= 0, we ++ // don't have any data to preserve, and can copy straight into the = start ++ // of the buffer rather than the GetBuffer return pointer (which ma= y be ++ // pointing further into the allocated buffer). + memcpy(parser->m_buffer, end, nLeftOver); + } + parser->m_bufferPtr =3D parser->m_buffer; +@@ -1966,16 +1977,15 @@ XML_Parse(XML_Parser parser, const char + parser->m_eventEndPtr =3D parser->m_bufferPtr; + return result; + } +-#endif /* not defined XML_CONTEXT_BYTES */ +- else { +- void *buff =3D XML_GetBuffer(parser, len); +- if (buff =3D=3D NULL) +- return XML_STATUS_ERROR; +- else { +- memcpy(buff, s, len); +- return XML_ParseBuffer(parser, len, isFinal); +- } ++#endif /* XML_CONTEXT_BYTES =3D=3D 0 */ ++ void *buff =3D XML_GetBuffer(parser, len); ++ if (buff =3D=3D NULL) ++ return XML_STATUS_ERROR; ++ if (len > 0) { ++ assert(s !=3D NULL); // make sure s=3D=3DNULL && len!=3D0 was rejecte= d above ++ memcpy(buff, s, len); + } ++ return XML_ParseBuffer(parser, len, isFinal); + } + + enum XML_Status XMLCALL +@@ -2015,8 +2025,8 @@ XML_ParseBuffer(XML_Parser parser, int l + parser->m_parseEndByteIndex +=3D len; + parser->m_parsingStatus.finalBuffer =3D (XML_Bool)isFinal; + +- parser->m_errorCode =3D parser->m_processor( +- parser, start, parser->m_parseEndPtr, &parser->m_bufferPtr); ++ parser->m_errorCode =3D callProcessor(parser, start, parser->m_parseEnd= Ptr, ++ &parser->m_bufferPtr); + + if (parser->m_errorCode !=3D XML_ERROR_NONE) { + parser->m_eventEndPtr =3D parser->m_eventPtr; +@@ -2061,8 +2071,12 @@ XML_GetBuffer(XML_Parser parser, int len + default:; + } + +- if (len > EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd)= ) { +-#ifdef XML_CONTEXT_BYTES ++ // whether or not the request succeeds, `len` seems to be the app's pre= ferred ++ // buffer fill size; remember it. ++ parser->m_lastBufferRequestSize =3D len; ++ if (len > EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd) ++ || parser->m_buffer =3D=3D NULL) { ++#if XML_CONTEXT_BYTES > 0 + int keep; + #endif /* defined XML_CONTEXT_BYTES */ + /* Do not invoke signed arithmetic overflow: */ +@@ -2083,10 +2097,11 @@ XML_GetBuffer(XML_Parser parser, int len + return NULL; + } + neededSize +=3D keep; +-#endif /* defined XML_CONTEXT_BYTES */ +- if (neededSize +- <=3D EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buffer)) = { +-#ifdef XML_CONTEXT_BYTES ++#endif /* XML_CONTEXT_BYTES > 0 */ ++ if (parser->m_buffer && parser->m_bufferPtr ++ && neededSize ++ <=3D EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bu= ffer)) { ++#if XML_CONTEXT_BYTES > 0 + if (keep < EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffe= r)) { + int offset + =3D (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_b= uffer) +@@ -2099,19 +2114,17 @@ XML_GetBuffer(XML_Parser parser, int len + parser->m_bufferPtr -=3D offset; + } + #else +- if (parser->m_buffer && parser->m_bufferPtr) { +- memmove(parser->m_buffer, parser->m_bufferPtr, +- EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_buffer= Ptr)); +- parser->m_bufferEnd +- =3D parser->m_buffer +- + EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_buffer= Ptr); +- parser->m_bufferPtr =3D parser->m_buffer; +- } +-#endif /* not defined XML_CONTEXT_BYTES */ ++ memmove(parser->m_buffer, parser->m_bufferPtr, ++ EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPt= r)); ++ parser->m_bufferEnd ++ =3D parser->m_buffer ++ + EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPt= r); ++ parser->m_bufferPtr =3D parser->m_buffer; ++#endif /* XML_CONTEXT_BYTES > 0 */ + } else { + char *newBuf; + int bufferSize +- =3D (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buf= ferPtr); ++ =3D (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buf= fer); + if (bufferSize =3D=3D 0) + bufferSize =3D INIT_BUFFER_SIZE; + do { +@@ -2208,7 +2221,7 @@ XML_ResumeParser(XML_Parser parser) { + } + parser->m_parsingStatus.parsing =3D XML_PARSING; + +- parser->m_errorCode =3D parser->m_processor( ++ parser->m_errorCode =3D callProcessor( + parser, parser->m_bufferPtr, parser->m_parseEndPtr, &parser->m_buff= erPtr); + + if (parser->m_errorCode !=3D XML_ERROR_NONE) { +@@ -2576,6 +2576,15 @@ XML_SetBillionLaughsAttackProtectionActi + } + #endif /* XML_GE =3D=3D 1 */ + ++XML_Bool XMLCALL ++XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled) { ++ if (parser !=3D NULL && (enabled =3D=3D XML_TRUE || enabled =3D=3D XML_= FALSE)) { ++ parser->m_reparseDeferralEnabled =3D enabled; ++ return XML_TRUE; ++ } ++ return XML_FALSE; ++} ++ + /* Initially tag->rawName always points into the parse buffer; + for those TAG instances opened while the current parse buffer was + processed, and not yet closed, we need to store tag->rawName in a more +@@ -4497,15 +4506,15 @@ entityValueInitProcessor(XML_Parser pars + parser->m_processor =3D entityValueProcessor; + return entityValueProcessor(parser, next, end, nextPtr); + } +- /* If we are at the end of the buffer, this would cause XmlPrologTok = to +- return XML_TOK_NONE on the next call, which would then cause the +- function to exit with *nextPtr set to s - that is what we want for= other +- tokens, but not for the BOM - we would rather like to skip it; +- then, when this routine is entered the next time, XmlPrologTok wil= l +- return XML_TOK_INVALID, since the BOM is still in the buffer ++ /* XmlPrologTok has now set the encoding based on the BOM it found, a= nd we ++ must move s and nextPtr forward to consume the BOM. ++ ++ If we didn't, and got XML_TOK_NONE from the next XmlPrologTok call= , we ++ would leave the BOM in the buffer and return. On the next call to = this ++ function, our XmlPrologTok call would return XML_TOK_INVALID, sinc= e it ++ is not valid to have multiple BOMs. + */ +- else if (tok =3D=3D XML_TOK_BOM && next =3D=3D end +- && ! parser->m_parsingStatus.finalBuffer) { ++ else if (tok =3D=3D XML_TOK_BOM) { + # if XML_GE =3D=3D 1 + if (! accountingDiffTolerated(parser, tok, s, next, __LINE__, + XML_ACCOUNT_DIRECT)) { +@@ -4500,7 +4522,7 @@ entityValueInitProcessor(XML_Parser pars + # endif + + *nextPtr =3D next; +- return XML_ERROR_NONE; ++ s =3D next; + } + /* If we get this token, we have the start of what might be a + normal tag, but not a declaration (i.e. it doesn't begin with +--- a/xmlwf/xmlwf.c ++++ b/xmlwf/xmlwf.c +@@ -914,6 +914,9 @@ usage(const XML_Char *prog, int rc) { + T(" -a FACTOR set maximum tolerated [a]mplification factor (de= fault: 100.0)\n") + T(" -b BYTES set number of output [b]ytes needed to activate = (default: 8 MiB)\n") + T("\n") ++ T("reparse deferral:\n") ++ T(" -q disable reparse deferral, and allow [q]uadratic= parse runtime with large tokens\n") ++ T("\n") + T("info arguments:\n") + T(" -h show this [h]elp message and exit\n") + T(" -v show program's [v]ersion number and exit\n") +@@ -967,6 +970,8 @@ tmain(int argc, XML_Char **argv) { + unsigned long long attackThresholdBytes; + XML_Bool attackThresholdGiven =3D XML_FALSE; + ++ XML_Bool disableDeferral =3D XML_FALSE; ++ + int exitCode =3D XMLWF_EXIT_SUCCESS; + enum XML_ParamEntityParsing paramEntityParsing + =3D XML_PARAM_ENTITY_PARSING_NEVER; +@@ -1089,6 +1094,11 @@ tmain(int argc, XML_Char **argv) { + #endif + break; + } ++ case T('q'): { ++ disableDeferral =3D XML_TRUE; ++ j++; ++ break; ++ } + case T('\0'): + if (j > 1) { + i++; +@@ -1134,6 +1144,16 @@ tmain(int argc, XML_Char **argv) { + #endif + } + ++ if (disableDeferral) { ++ const XML_Bool success =3D XML_SetReparseDeferralEnabled(parser, XM= L_FALSE); ++ if (! success) { ++ // This prevents tperror(..) from reporting misleading "[..]: Suc= cess" ++ errno =3D EINVAL; ++ tperror(T("Failed to disable reparse deferral")); ++ exit(XMLWF_EXIT_INTERNAL_ERROR); ++ } ++ } ++ + if (requireStandalone) + XML_SetNotStandaloneHandler(parser, notStandalone); + XML_SetParamEntityParsing(parser, paramEntityParsing); +--- a/xmlwf/xmlwf_helpgen.py ++++ b/xmlwf/xmlwf_helpgen.py +@@ -81,6 +81,10 @@ billion_laughs.add_argument('-a', metava + help=3D'set maximum tolerated [a]mplification= factor (default: 100.0)') + billion_laughs.add_argument('-b', metavar=3D'BYTES', help=3D'set number o= f output [b]ytes needed to activate (default: 8 MiB)') + ++reparse_deferral =3D parser.add_argument_group('reparse deferral') ++reparse_deferral.add_argument('-q', metavar=3D'FACTOR', ++ help=3D'disable reparse deferral, and allow [= q]uadratic parse runtime with large tokens') ++ + parser.add_argument('files', metavar=3D'FILE', nargs=3D'*', help=3D'file = to process (default: STDIN)') + + info =3D parser.add_argument_group('info arguments') diff --git a/meta/recipes-core/expat/expat_2.5.0.bb b/meta/recipes-core/exp= at/expat_2.5.0.bb index 31e989cfe2..09bc7a0a0b 100644 --- a/meta/recipes-core/expat/expat_2.5.0.bb +++ b/meta/recipes-core/expat/expat_2.5.0.bb @@ -22,6 +22,7 @@ SRC_URI =3D "https://github.com/libexpat/libexpat/release= s/download/R_${VERSION_TA file://CVE-2023-52426-009.patch \ file://CVE-2023-52426-010.patch \ file://CVE-2023-52426-011.patch \ + file://CVE-2023-52425.patch \ " UPSTREAM_CHECK_URI =3D "https://github.com/libexpat/libexpat/releases/" -- 2.25.1 This message contains information that may be privileged or confidential an= d is the property of the KPIT Technologies Ltd. It is intended only for the= person to whom it is addressed. If you are not the intended recipient, you= are not authorized to read, print, retain copy, disseminate, distribute, o= r use this message or any part thereof. If you receive this message in erro= r, please notify the sender immediately and delete all copies of this messa= ge. KPIT Technologies Ltd. does not accept any liability for virus infected= mails.