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 patch_file = [ 51 "Backport-CVE-2025-32414-python-Read-at-most-len-4-ch-c.patch", 52 "Backport-CVE-2025-32415-schemas-Fix-heap-buffer-over-c.patch", 53 "Fix_XML_PARSE_NOBLANKS_dropping_non-whitespace_text.patch", 54 "Backport-CVE-2025-6021-tree-Fix-integer-overflow-in-xmlBuildQName-c.patch", 55 "Fix-relaxng-is-parsed-to-an-infinite-attrs-next-loop.patch", 56 "Backport-CVE-2025-6170-Fix-potential-buffer-overflow-of-interactive-shell.patch", 57 "Fix-CVE-2025-49794-CVE-2025-49796-memory-safety-issues-in-xmlSchematronReportOutput.patch", 58 "Fix-CVE-2025-49795-null-pointer-dereference-leading-to-DoS.patch" 59 ] 60 61 for patch in patch_file: 62 file_path = os.path.join(args.source_file, patch) 63 apply_patch(file_path, target_dir) 64 65 66def main(): 67 libpng_path = argparse.ArgumentParser() 68 libpng_path.add_argument('--gen-dir', help='generate path of libxml2') 69 libpng_path.add_argument('--source-file', help='libxml2 source compressed dir') 70 args = libpng_path.parse_args() 71 tar_file_path = os.path.join(args.source_file, "libxml2-2.14.0.tar.xz") 72 target_dir = os.path.join(args.gen_dir, "libxml2-2.14.0") 73 untar_file(tar_file_path, target_dir, args) 74 do_patch(args, target_dir) 75 return 0 76 77 78if __name__ == '__main__': 79 sys.exit(main()) 80