• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "openuncompressedarchive_fuzzer.h"
17 #include "libpandafile/file.h"
18 #include "libziparchive/zip_archive.h"
19 
20 namespace OHOS {
CloseAndRemoveZipFile(panda::ZipArchiveHandle & handle,FILE * fp,const char * filename)21 void CloseAndRemoveZipFile(panda::ZipArchiveHandle &handle, FILE *fp, const char *filename)
22 {
23     panda::CloseArchiveFile(handle);
24     (void)fclose(fp);
25     (void)remove(filename);
26 }
27 
OpenUncompressedArchiveFuzzTest(const uint8_t * data,size_t size)28 void OpenUncompressedArchiveFuzzTest(const uint8_t *data, size_t size)
29 {
30     // Create zip file
31     const char *zip_filename = "__OpenUncompressedArchiveFuzzTest.zip";
32     const char *filename = panda::panda_file::ARCHIVE_FILENAME;
33     int ret = panda::CreateOrAddFileIntoZip(zip_filename, filename, data, size, APPEND_STATUS_CREATE, Z_NO_COMPRESSION);
34     if (ret != 0) {
35         (void)remove(zip_filename);
36         return;
37     }
38 
39     // Acquire entry
40 #ifdef PANDA_TARGET_WINDOWS
41     constexpr char const *mode = "rb";
42 #else
43     constexpr char const *mode = "rbe";
44 #endif
45     FILE *fp = fopen(zip_filename, mode);
46     if (fp == nullptr) {
47         (void)remove(zip_filename);
48         return;
49     }
50     panda::ZipArchiveHandle zipfile = nullptr;
51     if (panda::OpenArchiveFile(zipfile, fp) != panda::ZIPARCHIVE_OK) {
52         (void)fclose(fp);
53         (void)remove(zip_filename);
54         return;
55     }
56     if (panda::LocateFile(zipfile, filename) != panda::ZIPARCHIVE_OK) {
57         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
58         (void)fclose(fp);
59         return;
60     }
61     panda::EntryFileStat entry;
62     if (panda::GetCurrentFileInfo(zipfile, &entry) != panda::ZIPARCHIVE_OK) {
63         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
64         (void)fclose(fp);
65         return;
66     }
67     if (panda::OpenCurrentFile(zipfile) != panda::ZIPARCHIVE_OK) {
68         panda::CloseCurrentFile(zipfile);
69         CloseAndRemoveZipFile(zipfile, fp, zip_filename);
70         (void)fclose(fp);
71         return;
72     }
73     panda::GetCurrentFileOffset(zipfile, &entry);
74     // Call OpenUncompressedArchive
75     {
76         panda::panda_file::File::OpenUncompressedArchive(fileno(fp), zip_filename, entry.GetUncompressedSize(),
77                                                          entry.GetOffset());
78     }
79     panda::CloseCurrentFile(zipfile);
80     CloseAndRemoveZipFile(zipfile, fp, zip_filename);
81 }
82 }  // namespace OHOS
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87     /* Run your code on data */
88     OHOS::OpenUncompressedArchiveFuzzTest(data, size);
89     return 0;
90 }
91