1 /*
2 * Copyright (c) 2023 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 "parsedlpheader_fuzzer.h"
17 #include <dlfcn.h>
18 #include <iostream>
19 #include <fcntl.h>
20 #include <fstream>
21 #include <thread>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <string>
25 #include <unistd.h>
26 #include "accesstoken_kit.h"
27 #include "dlp_file.h"
28 #include "dlp_permission_log.h"
29 #include "dlp_permission.h"
30 #include "securec.h"
31 #include "token_setproc.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 typedef ssize_t (*WriteFuncT)(int fd, const void* buf, size_t count);
write(int fd,const void * buf,size_t count)37 ssize_t write(int fd, const void* buf, size_t count)
38 {
39 WriteFuncT func = reinterpret_cast<WriteFuncT>(dlsym(RTLD_NEXT, "write"));
40 if (func == nullptr) {
41 return -1;
42 }
43 return (*func)(fd, buf, count);
44 }
45 #ifdef __cplusplus
46 }
47 #endif
48
49 using namespace OHOS::Security::DlpPermission;
50 using namespace OHOS::Security::AccessToken;
51 using namespace std;
52 namespace OHOS {
53 static const uint32_t BUFFERSIZE = 40;
FuzzTest(const uint8_t * data,size_t size)54 static void FuzzTest(const uint8_t* data, size_t size)
55 {
56 int fd = open("/data/fuse_test.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
57
58 DlpFile testFile(fd, "abc", 0, false);
59 uint32_t txtSize = static_cast<uint32_t>(size) % 100;
60
61 struct DlpHeader header = {
62 .magic = DLP_FILE_MAGIC,
63 .offlineAccess = 0,
64 .txtOffset = sizeof(struct DlpHeader) + 20 + 20,
65 .txtSize = txtSize,
66 .certOffset = sizeof(struct DlpHeader),
67 .certSize = 20,
68 .contactAccountOffset = sizeof(struct DlpHeader) + 20,
69 .contactAccountSize = 20,
70 .offlineCertOffset = 0,
71 .offlineCertSize = 0,
72 };
73 write(fd, &header, sizeof(header));
74 uint8_t buffer[BUFFERSIZE] = {0};
75 write(fd, buffer, BUFFERSIZE);
76 testFile.ParseDlpHeader();
77 }
78
ParseCertFuzzTest(const uint8_t * data,size_t size)79 bool ParseCertFuzzTest(const uint8_t* data, size_t size)
80 {
81 int selfTokenId = GetSelfTokenID();
82 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); // user_id = 100
83 SetSelfTokenID(tokenId);
84 FuzzTest(data, size);
85 SetSelfTokenID(selfTokenId);
86 return true;
87 }
88 } // namespace OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::ParseCertFuzzTest(data, size);
95 return 0;
96 }
97