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 "unregisteropendlpfilecallbackstub_fuzzer.h"
17 #include <iostream>
18 #include <string>
19 #include <thread>
20 #include <vector>
21 #include "accesstoken_kit.h"
22 #include "dlp_permission.h"
23 #include "dlp_permission_log.h"
24 #include "open_dlp_file_callback.h"
25 #include "securec.h"
26 #include "token_setproc.h"
27
28 using namespace OHOS::Security::DlpPermission;
29 using namespace OHOS::Security::AccessToken;
30
31 class UnregisterOpenDlpFileCallbackStubFuzzer : public OpenDlpFileCallbackCustomize {
32 public:
UnregisterOpenDlpFileCallbackStubFuzzer()33 UnregisterOpenDlpFileCallbackStubFuzzer() {}
~UnregisterOpenDlpFileCallbackStubFuzzer()34 ~UnregisterOpenDlpFileCallbackStubFuzzer() override {}
35
OnOpenDlpFile(OpenDlpFileCallbackInfo & result)36 void OnOpenDlpFile(OpenDlpFileCallbackInfo& result) override {}
37 };
38
39 namespace OHOS {
FuzzTest(const uint8_t * data,size_t size)40 static void FuzzTest(const uint8_t* data, size_t size)
41 {
42 MessageParcel datas;
43 datas.WriteInterfaceToken(IDlpPermissionService::GetDescriptor());
44 std::shared_ptr<OpenDlpFileCallbackCustomize> callback =
45 std::make_shared<UnregisterOpenDlpFileCallbackStubFuzzer>();
46 sptr<OpenDlpFileCallback> asyncStub = new (std::nothrow) OpenDlpFileCallback(callback);
47 if (!datas.WriteRemoteObject(asyncStub->AsObject())) {
48 return;
49 }
50 uint32_t code = static_cast<uint32_t>(DlpPermissionServiceInterfaceCode::UN_REGISTER_OPEN_DLP_FILE_CALLBACK);
51 MessageParcel reply;
52 MessageOption option;
53 auto service = std::make_shared<DlpPermissionService>(SA_ID_DLP_PERMISSION_SERVICE, true);
54 service->appStateObserver_ = new (std::nothrow) AppStateObserver();
55 service->OnRemoteRequest(code, datas, reply, option);
56 }
57
UnRegisterUnregisterOpenDlpFileCallbackStubFuzzer(const uint8_t * data,size_t size)58 bool UnRegisterUnregisterOpenDlpFileCallbackStubFuzzer(const uint8_t* data, size_t size)
59 {
60 int selfTokenId = GetSelfTokenID();
61 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); // user_id = 100
62 SetSelfTokenID(tokenId);
63 FuzzTest(data, size);
64 SetSelfTokenID(selfTokenId);
65 return true;
66 }
67 } // namespace OHOS
68
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72 /* Run your code on data */
73 OHOS::UnRegisterUnregisterOpenDlpFileCallbackStubFuzzer(data, size);
74 return 0;
75 }
76