• 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    patch_file = [
51        "Fix-memleaks-in-xmlXIncludeProcessFlags.patch",
52        "backport-parser-Fix-potential-memory-leak-in-xmlParseAttValue.patch",
53        "Fix-memory-leaks-for-xmlACatalogAdd.patch",
54        "Fix-memory-leaks-in-xmlACatalogAdd-when-xmlHashAddEntry-failed.patch",
55        "backport-CVE-2022-40303-Fix-integer-overflows-with-XML_PARSE_.patch",
56        "backport-CVE-2022-40304-Fix-dict-corruption-caused-by-entity-.patch",
57        "backport-schemas-Fix-null-pointer-deref-in-xmlSchemaCheckCOSS.patch",
58        "libxml2-multilib.patch",
59        "Fix-CVE-2023-45322-pre-patch.patch",
60        "Fix-CVE-2023-45322.patch",
61        "Fix-CVE-2023-25062.patch"
62    ]
63
64    for patch in patch_file:
65        file_path = os.path.join(args.source_file, patch)
66        apply_patch(file_path, target_dir)
67
68
69def main():
70    libpng_path = argparse.ArgumentParser()
71    libpng_path.add_argument('--gen-dir', help='generate path of libxml2')
72    libpng_path.add_argument('--source-file', help='libxml2 source compressed dir')
73    args = libpng_path.parse_args()
74    tar_file_path = os.path.join(args.source_file, "libxml2-2.9.14.tar.xz")
75    target_dir = os.path.join(args.gen_dir, "libxml2-2.9.14")
76    untar_file(tar_file_path, target_dir, args)
77    do_patch(args, target_dir)
78    return 0
79
80
81if __name__ == '__main__':
82    sys.exit(main())
83