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 "installdlpsandboxstub_fuzzer.h"
17 #include <iostream>
18 #include <string>
19 #include <vector>
20 #include <thread>
21 #include "accesstoken_kit.h"
22 #include "dlp_permission_log.h"
23 #include "dlp_permission.h"
24 #include "securec.h"
25 #include "token_setproc.h"
26
27 using namespace OHOS::Security::DlpPermission;
28 using namespace OHOS::Security::AccessToken;
29 namespace OHOS {
30 const std::string TEST_URI = "datashare:///media/file/8";
FuzzTest(const uint8_t * data,size_t size)31 static void FuzzTest(const uint8_t* data, size_t size)
32 {
33 if ((data == nullptr) || (size == 0)) {
34 return;
35 }
36 std::string bundleName(reinterpret_cast<const char*>(data), size);
37 DLPFileAccess dlpFileAccess = static_cast<DLPFileAccess>(size);
38 int32_t userId = static_cast<int32_t>(size);
39 std::string uri(reinterpret_cast<const char*>(data), size);
40
41 MessageParcel datas;
42 datas.WriteInterfaceToken(IDlpPermissionService::GetDescriptor());
43 bundleName = "com.ohos.dlpmanager";
44 if (!datas.WriteString(bundleName)) {
45 return;
46 }
47 uint32_t type = static_cast<uint32_t>(dlpFileAccess);
48 if (!datas.WriteUint32(type)) {
49 return;
50 }
51 if (!datas.WriteInt32(userId)) {
52 return;
53 }
54 if (!datas.WriteString(uri)) {
55 return;
56 }
57 uint32_t code = static_cast<uint32_t>(DlpPermissionServiceInterfaceCode::INSTALL_DLP_SANDBOX);
58 MessageParcel reply;
59 MessageOption option;
60 auto service = std::make_shared<DlpPermissionService>(SA_ID_DLP_PERMISSION_SERVICE, true);
61 service->OnRemoteRequest(code, datas, reply, option);
62 }
63
InstallDlpSandboxFuzzTest(const uint8_t * data,size_t size)64 bool InstallDlpSandboxFuzzTest(const uint8_t* data, size_t size)
65 {
66 int selfTokenId = GetSelfTokenID();
67 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); // user_id = 100
68 SetSelfTokenID(tokenId);
69 FuzzTest(data, size);
70 SetSelfTokenID(selfTokenId);
71 return true;
72 }
73 } // namespace OHOS
74
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 OHOS::InstallDlpSandboxFuzzTest(data, size);
80 return 0;
81 }
82