• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "registeropendlpfilecallbackstub_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 RegisterOpenDlpFileCallbackStubFuzzer : public OpenDlpFileCallbackCustomize {
32 public:
RegisterOpenDlpFileCallbackStubFuzzer()33     RegisterOpenDlpFileCallbackStubFuzzer() {}
~RegisterOpenDlpFileCallbackStubFuzzer()34     ~RegisterOpenDlpFileCallbackStubFuzzer() 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 = std::make_shared<RegisterOpenDlpFileCallbackStubFuzzer>();
45     sptr<OpenDlpFileCallback> asyncStub = new (std::nothrow) OpenDlpFileCallback(callback);
46     if (!datas.WriteRemoteObject(asyncStub->AsObject())) {
47         return;
48     }
49     uint32_t code = static_cast<uint32_t>(DlpPermissionServiceInterfaceCode::REGISTER_OPEN_DLP_FILE_CALLBACK);
50     MessageParcel reply;
51     MessageOption option;
52     auto service = std::make_shared<DlpPermissionService>(SA_ID_DLP_PERMISSION_SERVICE, true);
53     service->OnRemoteRequest(code, datas, reply, option);
54 }
55 
RegisterRegisterOpenDlpFileCallbackStubFuzzer(const uint8_t * data,size_t size)56 bool RegisterRegisterOpenDlpFileCallbackStubFuzzer(const uint8_t* data, size_t size)
57 {
58     int selfTokenId = GetSelfTokenID();
59     AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); // user_id = 100
60     SetSelfTokenID(tokenId);
61     FuzzTest(data, size);
62     SetSelfTokenID(selfTokenId);
63     return true;
64 }
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::RegisterRegisterOpenDlpFileCallbackStubFuzzer(data, size);
72     return 0;
73 }
74