• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import subprocess
19import sys
20
21
22def untar_file(tar_file_path, extract_path, args):
23    try:
24        if os.path.exists(extract_path):
25            rm_cmd = ['rm', '-rf', extract_path]
26            subprocess.run(rm_cmd, check=True)
27
28        tar_cmd = ['tar', '-xvf', tar_file_path, '-C', args.gen_dir]
29        subprocess.run(tar_cmd, check=True)
30
31    except Exception as e:
32        print("tar error!")
33        return
34
35
36def apply_patch(patch_file, target_dir):
37    try:
38        if not os.path.exists(target_dir):
39            return
40
41        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
42        subprocess.run(patch_cmd, check=True)
43
44    except Exception as e:
45        print("apply_patch error!")
46        return
47
48
49def do_patch(args, target_dir):
50
51    patch_file = [
52        "Fix-memleaks-in-xmlXIncludeProcessFlags.patch",
53        "backport-parser-Fix-potential-memory-leak-in-xmlParseAttValue.patch",
54        "Fix-memory-leaks-for-xmlACatalogAdd.patch",
55        "Fix-memory-leaks-in-xmlACatalogAdd-when-xmlHashAddEntry-failed.patch",
56        "backport-CVE-2022-40303-Fix-integer-overflows-with-XML_PARSE_.patch",
57        "backport-CVE-2022-40304-Fix-dict-corruption-caused-by-entity-.patch",
58        "backport-schemas-Fix-null-pointer-deref-in-xmlSchemaCheckCOSS.patch",
59        "libxml2-multilib.patch",
60        "Fix-CVE-2023-25062.patch",
61        "Fix-CVE-2023-45322-pre.patch",
62        "Fix-CVE-2023-45322-first.patch",
63        "Fix-CVE-2023-45322-second.patch",
64        "backport-Add-whitespace-folding-for-some-atomic-data-types-th.patch",
65        "backport-Properly-fold-whitespace-around-the-QName-value-when.patch",
66        "backport-Avoid-arithmetic-on-freed-pointers.patch",
67        "backport-fix-xmlXPathParserContext-could-be-double-delete-in-.patch",
68        "backport-Use-UPDATE_COMPAT-consistently-in-buf.c.patch",
69        "backport-Restore-behavior-of-htmlDocContentDumpFormatOutput.patch",
70        "backport-Fix-use-after-free-bugs-when-calling-xmlTextReaderCl.patch",
71        "backport-Use-xmlNewDocText-in-xmlXIncludeCopyRange.patch",
72        "backport-xmlBufAvail-should-return-length-without-including-a.patch",
73        "backport-Fix-integer-overflow-in-xmlBufferDump.patch",
74        "backport-Fix-missing-NUL-terminators-in-xmlBuf-and-xmlBuffer-.patch",
75        "backport-Reserve-byte-for-NUL-terminator-and-report-errors-co.patch",
76        "backport-Fix-unintended-fall-through-in-xmlNodeAddContentLen.patch",
77        "backport-Don-t-reset-nsDef-when-changing-node-content.patch",
78        "backport-Avoid-double-free-if-malloc-fails-in-inputPush.patch",
79        "backport-Fix-memory-leak-in-xmlLoadEntityContent-error-path.patch",
80        "backport-Reset-nsNr-in-xmlCtxtReset.patch",
81        "backport-Fix-htmlReadMemory-mixing-up-XML-and-HTML-functions.patch",
82        "backport-Don-t-initialize-SAX-handler-in-htmlReadMemory.patch",
83        "backport-Fix-HTML-parser-with-threads-and-without-legacy.patch",
84        "backport-Fix-xmlCtxtReadDoc-with-encoding.patch",
85        "backport-Use-xmlStrlen-in-CtxtReadDoc.patch",
86        "backport-Create-stream-with-buffer-in-xmlNewStringInputStream.patch",
87        "backport-Use-xmlStrlen-in-xmlNewStringInputStream.patch",
88        "backport-Fix-memory-leak-with-invalid-XSD.patch",
89        "backport-Make-XPath-depth-check-work-with-recursive-invocatio.patch",
90        "backport-Fix-overflow-check-in-SAX2.c.patch",
91        "backport-xinclude-Fix-memory-leak-when-fuzzing.patch",
92        "backport-xinclude-Fix-more-memory-leaks-in-xmlXIncludeLoadDoc.patch",
93        "backport-schemas-Fix-infinite-loop-in-xmlSchemaCheckElemSubst.patch",
94        "backport-malloc-fail-Fix-memory-leak-in-xmlCreatePushParserCt.patch",
95        "backport-malloc-fail-Fix-memory-leak-in-xmlStaticCopyNodeList.patch",
96        "backport-malloc-fail-Fix-memory-leak-in-xmlNewPropInternal.patch",
97        "backport-malloc-fail-Fix-memory-leak-in-xmlNewDocNodeEatName.patch",
98        "backport-malloc-fail-Fix-infinite-loop-in-xmlSkipBlankChars.patch",
99        "backport-malloc-fail-Fix-memory-leak-in-xmlSAX2ExternalSubset.patch",
100        "backport-malloc-fail-Fix-memory-leak-in-xmlParseReference.patch",
101        "backport-malloc-fail-Fix-use-after-free-in-xmlXIncludeAddNode.patch",
102        "backport-malloc-fail-Fix-memory-leak-in-xmlStringGetNodeList.patch",
103        "backport-parser-Fix-error-message-in-xmlParseCommentComplex.patch",
104        "backport-io-Fix-buffer-full-error-with-certain-buffer-sizes.patch",
105        "backport-reader-Switch-to-xmlParserInputBufferCreateMem.patch",
106        "backport-uri-Allow-port-without-host.patch",
107        "backport-parser-Fix-consumed-accounting-when-switching-encodi.patch",
108        "backport-html-Fix-check-for-end-of-comment-in-push-parser.patch",
109        "backport-parser-Fix-push-parser-with-1-3-byte-initial-chunk.patch",
110        "backport-parser-Restore-parser-state-in-xmlParseCDSect.patch",
111        "backport-parser-Remove-dangerous-check-in-xmlParseCharData.patch",
112        "backport-parser-Don-t-call-DefaultSAXHandlerInit-from-xmlInit.patch",
113        "backport-Correctly-relocate-internal-pointers-after-realloc.patch",
114        "backport-Avoid-creating-an-out-of-bounds-pointer-by-rewriting.patch",
115        "backport-error-Make-sure-that-error-messages-are-valid-UTF-8.patch",
116        "backport-io-Check-for-memory-buffer-early-in-xmlParserInputGrow.patch",
117        "backport-io-Remove-xmlInputReadCallbackNop.patch",
118        "backport-Revert-uri-Allow-port-without-host.patch",
119        "backport-xmlParseStartTag2-contains-typo-when-checking-for-default.patch",
120        "backport-parser-Fix-integer-overflow-of-input-ID.patch",
121        "backport-parser-Don-t-increase-depth-twice-when-parsing-internal.patch",
122        "backport-xpath-number-should-return-NaN.patch",
123        "backport-error-Don-t-move-past-current-position.patch",
124        "backport-malloc-fail-Handle-memory-errors-in-xmlTextReaderEntPush.patch",
125        "backport-malloc-fail-Fix-infinite-loop-in-xmlParseTextDecl.patch",
126        "backport-malloc-fail-Fix-null-deref-in-xmlAddDefAttrs.patch",
127        "backport-malloc-fail-Fix-null-deref-if-growing-input-buffer-fails.patch",
128        "backport-malloc-fail-Fix-null-deref-in-xmlSAX2AttributeInternal.patch",
129        "backport-malloc-fail-Fix-null-deref-in-xmlBufResize.patch",
130        "backport-buf-Fix-return-value-of-xmlBufGetInputBase.patch",
131        "backport-malloc-fail-Don-t-call-xmlErrMemory-in-xmlstring.c.patch",
132        "backport-malloc-fail-Fix-reallocation-in-inputPush.patch",
133        "backport-malloc-fail-Fix-use-after-free-in-xmlParseStartTag2.patch",
134        "backport-malloc-fail-Add-error-checks-in-xmlXPathEqualValuesCommon.patch",
135        "backport-malloc-fail-Add-error-check-in-xmlXPathEqualNodeSetFloat.patch",
136        "backport-malloc-fail-Fix-error-check-in-xmlXPathCompareValues.patch",
137        "backport-malloc-fail-Record-malloc-failure-in-xmlXPathCompLiteral.patch",
138        "backport-malloc-fail-Check-return-value-of-xmlXPathNodeSetDupNs.patch",
139        "backport-malloc-fail-Fix-null-deref-in-xmlXIncludeLoadTxt.patch",
140        "backport-malloc-fail-Fix-reallocation-in-xmlXIncludeNewRef.patch",
141        "backport-xinclude-Fix-quadratic-behavior-in-xmlXIncludeLoadTx.patch",
142        "backport-malloc-fail-Fix-memory-leak-in-xmlParserInputBufferCreateMem.patch",
143        "backport-malloc-fail-Check-for-malloc-failure-in-xmlFindCharEncodingHandler.patch",
144        "backport-malloc-fail-Fix-leak-of-xmlCharEncodingHandler.patch",
145        "backport-malloc-fail-Fix-memory-leak-in-xmlParseEntityDecl.patch",
146        "backport-encoding-Cast-toupper-argument-to-unsigned-char.patch",
147        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathCompareValues.patch",
148        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathTryStreamCompile.patch",
149        "backport-malloc-fail-Fix-memory-leak-after-calling-valuePush.patch",
150        "backport-malloc-fail-Fix-memory-leak-after-calling-xmlXPathWrapNodeSet.patch",
151        "backport-malloc-fail-Fix-memory-leak-in-xmlXIncludeAddNode.patch",
152        "backport-malloc-fail-Fix-memory-leak-after-xmlRegNewState.patch",
153        "backport-malloc-fail-Fix-memory-leak-in-xmlSAX2StartElementNs.patch",
154        "backport-malloc-fail-Fix-memory-leak-in-xmlGetDtdElementDesc2.patch",
155        "backport-malloc-fail-Fix-memory-leak-in-xmlDocDumpFormatMemoryEnc.patch",
156        "backport-malloc-fail-Fix-infinite-loop-in-htmlParseStartTag1.patch",
157        "backport-malloc-fail-Fix-memory-leak-in-xmlXIncludeLoadTxt.patch",
158        "backport-malloc-fail-Fix-memory-leak-in-xmlCopyPropList.patch",
159        "backport-malloc-fail-Fix-memory-leak-after-calling-xmlXPathNodeSetMerge.patch",
160        "backport-malloc-fail-Fix-memory-leak-after-calling-xmlXPathWrapString.patch",
161        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathEqualValuesCommon.patch",
162        "backport-malloc-fail-Fix-memory-leak-in-htmlCreateMemoryParserCtxt.patch",
163        "backport-malloc-fail-Fix-memory-leak-in-htmlCreatePushParserCtxt.patch",
164        "backport-malloc-fail-Fix-infinite-loop-in-htmlParseContentInternal.patch",
165        "backport-malloc-fail-Fix-infinite-loop-in-htmlParseStartTag2.patch",
166        "backport-malloc-fail-Fix-null-deref-in-htmlnamePush.patch",
167        "backport-malloc-fail-Fix-infinite-loop-in-htmlParseDocTypeDecl.patch",
168        "backport-malloc-fail-Fix-error-code-in-htmlParseChunk.patch",
169        "backport-malloc-fail-Fix-memory-leak-in-xmlFAParseCharProp.patch",
170        "backport-malloc-fail-Fix-leak-of-xmlRegAtom.patch",
171        "backport-malloc-fail-Fix-memory-leak-in-xmlRegexpCompile.patch",
172        "backport-malloc-fail-Fix-OOB-read-after-xmlRegGetCounter.patch",
173        "backport-parser-Fix-OOB-read-when-formatting-error-message.patch",
174        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathEqualNodeSetF.patch",
175        "backport-malloc-fail-Fix-use-after-free-related-to-xmlXPathNo.patch",
176        "backport-regexp-Add-sanity-check-in-xmlRegCalloc2.patch",
177        "backport-malloc-fail-Fix-null-deref-in-xmlXPathCompiledEvalIn.patch",
178        "backport-malloc-fail-Fix-null-deref-after-xmlPointerListAddSi.patch",
179        "backport-malloc-fail-Fix-memory-leak-in-xmlGetNsList.patch",
180        "backport-malloc-fail-Check-for-malloc-failure-in-xmlHashAddEn.patch",
181        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathCacheNewNodeS.patch",
182        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathDistinctSorte.patch",
183        "backport-xpath-Fix-harmless-integer-overflow-in-xmlXPathTrans.patch",
184        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathNameFunction.patch",
185        "backport-malloc-fail-Fix-memory-leak-in-xmlSchemaItemListAddS.patch",
186        "backport-malloc-fail-Fix-null-deref-in-xmlGet-Min-Max-Occurs.patch",
187        "backport-malloc-fail-Fix-null-deref-in-xmlSchemaValAtomicType.patch",
188        "backport-malloc-fail-Fix-null-deref-in-xmlSchemaInitTypes.patch",
189        "backport-malloc-fail-Fix-memory-leak-in-xmlSchemaParse.patch",
190        "backport-malloc-fail-Fix-memory-leak-in-xmlCopyNamespaceList.patch",
191        "backport-malloc-fail-Fix-another-memory-leak-in-xmlSchemaBuck.patch",
192        "backport-malloc-fail-Fix-null-deref-in-xmlSchemaParseUnion.patch",
193        "backport-malloc-fail-Fix-memory-leak-in-WXS_ADD_-LOCAL-GLOBAL.patch",
194        "backport-malloc-fail-Fix-memory-leak-in-xmlSchemaBucketCreate.patch",
195        "backport-malloc-fail-Fix-null-deref-in-xmlSchemaParseWildcard.patch",
196        "backport-malloc-fail-Fix-type-confusion-after-xmlSchemaFixupT.patch",
197        "backport-malloc-fail-Fix-null-deref-after-xmlSchemaItemList-A.patch",
198        "backport-malloc-fail-Fix-null-deref-after-xmlSchemaCompareDat.patch",
199        "backport-malloc-fail-Fix-memory-leak-in-xmlSchemaParseUnion.patch",
200        "backport-malloc-fail-Fix-memory-leak-in-xmlXPathRegisterNs.patch",
201        "backport-catalog-Fix-memory-leaks.patch",
202        "backport-CVE-2023-29469.patch",
203        "backport-CVE-2023-28484.patch",
204        "backport-valid-Allow-xmlFreeValidCtxt-NULL.patch",
205        "backport-parser-Use-size_t-when-subtracting-input-buffer-poin.patch",
206        "backport-malloc-fail-Fix-null-deref-in-xmlParserInputShrink.patch",
207        "backport-xmllint-Fix-memory-leak-with-pattern-stream.patch",
208        "backport-xzlib-Fix-implicit-sign-change-in-xz_open.patch",
209        "backport-html-Fix-quadratic-behavior-in-htmlParseTryOrFinish.patch",
210        "backport-valid-Make-xmlValidateElement-non-recursive.patch",
211        "backport-malloc-fail-Fix-buffer-overread-in-htmlParseScript.patch",
212        "backport-malloc-fail-Add-more-error-checks-when-parsing-names.patch",
213        "backport-malloc-fail-Add-error-check-in-htmlParseHTMLAttribut.patch",
214        "backport-parser-Limit-name-length-in-xmlParseEncName.patch",
215        "backport-encoding-Fix-error-code-in-asciiToUTF8.patch",
216        "backport-malloc-fail-Fix-buffer-overread-with-HTML-doctype-de.patch",
217        "backport-parser-Fix-regression-in-xmlParserNodeInfo-accountin.patch",
218        "backport-regexp-Fix-cycle-check-in-xmlFAReduceEpsilonTransiti.patch",
219        "backport-regexp-Fix-checks-for-eliminated-transitions.patch",
220        "backport-regexp-Fix-determinism-checks.patch",
221        "backport-regexp-Fix-mistake-in-previous-commit.patch",
222        "backport-regexp-Fix-null-deref-in-xmlFAFinishReduceEpsilonTra.patch",
223        "backport-hash-Fix-possible-startup-crash-with-old-libxslt-ver.patch",
224        "backport-parser-Fix-old-SAX1-parser-with-custom-callbacks.patch",
225        "backport-xmllint-Fix-use-after-free-with-maxmem.patch",
226        "backport-malloc-fail-Check-for-malloc-failures-when-creating.patch",
227        "backport-malloc-fail-Fix-buffer-overread-after-htmlParseScrip.patch",
228        "backport-xmlValidatePopElement-can-return-invalid-value-1.patch",
229        "backport-Fix-use-after-free-in-xmlParseContentInternal.patch",
230        "backport-malloc-fail-Fix-null-deref-after-xmlXIncludeNewRef.patch",
231        "backport-xpath-Ignore-entity-ref-nodes-when-computing-node-ha.patch",
232        "backport-SAX-Always-initialize-SAX1-element-handlers.patch",
233        "Fix-malloc-fail.patch"
234    ]
235
236    for patch in patch_file:
237        file_path = os.path.join(args.source_file, patch)
238        apply_patch(file_path, target_dir)
239
240
241def main():
242    libpng_path = argparse.ArgumentParser()
243    libpng_path.add_argument('--gen-dir', help='generate path of libxml2')
244    libpng_path.add_argument('--source-file', help='libxml2 source compressed dir')
245    args = libpng_path.parse_args()
246    tar_file_path = os.path.join(args.source_file, "libxml2-2.9.14.tar.xz")
247    target_dir = os.path.join(args.gen_dir, "libxml2-2.9.14")
248    untar_file(tar_file_path, target_dir, args)
249    do_patch(args, target_dir)
250    return 0
251
252
253if __name__ == '__main__':
254    sys.exit(main())
255