1 /*
2  * Copyright (c) 2024 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 "unregistertokensynccallbackstub_fuzzer.h"
17 
18 #undef private
19 #include "accesstoken_callback_stubs.h"
20 #include "accesstoken_kit.h"
21 #include "accesstoken_manager_service.h"
22 #include "i_accesstoken_manager.h"
23 #include "token_setproc.h"
24 #include "token_sync_kit_interface.h"
25 
26 using namespace std;
27 using namespace OHOS::Security::AccessToken;
28 namespace {
29 class TokenSyncCallbackImpl : public TokenSyncCallbackStub {
30 public:
31     TokenSyncCallbackImpl() = default;
32     virtual ~TokenSyncCallbackImpl() = default;
33 
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID)34     int32_t GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) override
35     {
36         return TokenSyncError::TOKEN_SYNC_OPENSOURCE_DEVICE;
37     };
38 
DeleteRemoteHapTokenInfo(AccessTokenID tokenID)39     int32_t DeleteRemoteHapTokenInfo(AccessTokenID tokenID) override
40     {
41         return TokenSyncError::TOKEN_SYNC_SUCCESS;
42     };
43 
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo)44     int32_t UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) override
45     {
46         return TokenSyncError::TOKEN_SYNC_SUCCESS;
47     };
48 };
49 
NativeTokenGet()50 bool NativeTokenGet()
51 {
52     AccessTokenID token = AccessTokenKit::GetNativeTokenId("token_sync_service");
53     if (token == 0) {
54         return false;
55     }
56     SetSelfTokenID(token);
57     return true;
58 }
59 };
60 
61 namespace OHOS {
RegisterTokenSyncCallbackStubFuzzTest(const uint8_t * data,size_t size)62     bool RegisterTokenSyncCallbackStubFuzzTest(const uint8_t* data, size_t size)
63     {
64         if ((data == nullptr) || (size == 0)) {
65             return false;
66         }
67     #ifdef TOKEN_SYNC_ENABLE
68         sptr<TokenSyncCallbackImpl> callback = new (std::nothrow) TokenSyncCallbackImpl();
69         if (callback == nullptr) {
70             return false;
71         }
72 
73         MessageParcel datas;
74         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
75         uint32_t code = static_cast<uint32_t>(
76             AccessTokenInterfaceCode::UNREGISTER_TOKEN_SYNC_CALLBACK);
77 
78         MessageParcel reply;
79         MessageOption option;
80         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
81     #endif // TOKEN_SYNC_ENABLE
82         return true;
83     }
84 } // namespace OHOS
85 
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
88 {
89     /* Run your code on data */
90 #ifdef TOKEN_SYNC_ENABLE
91     if (!NativeTokenGet()) {
92         return 0;
93     }
94 #endif
95     OHOS::RegisterTokenSyncCallbackStubFuzzTest(data, size);
96     return 0;
97 }