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 "imf_sa_stub_fuzz_util.h"
17 
18 #include "accesstoken_kit.h"
19 #include "global.h"
20 #include "ime_cfg_manager.h"
21 #include "iservice_registry.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "text_listener.h"
26 #include "token_setproc.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 using namespace OHOS::Security::AccessToken;
31 bool ImfSaStubFuzzUtil::isInitialize_ = false;
32 std::mutex ImfSaStubFuzzUtil::initMutex_;
33 
GrantNativePermission()34 void ImfSaStubFuzzUtil::GrantNativePermission()
35 {
36     const char **perms = new const char *[1];
37     perms[0] = "ohos.permission.CONNECT_IME_ABILITY";
38     TokenInfoParams infoInstance = {
39         .dcapsNum = 0,
40         .permsNum = 1,
41         .aclsNum = 0,
42         .dcaps = nullptr,
43         .perms = perms,
44         .acls = nullptr,
45         .processName = "broker",
46         .aplStr = "system_basic",
47     };
48     uint64_t tokenId = GetAccessTokenId(&infoInstance);
49     int res = SetSelfTokenID(tokenId);
50     if (res == 0) {
51         IMSA_HILOGI("SetSelfTokenID success!");
52     } else {
53         IMSA_HILOGE("SetSelfTokenID fail!");
54     }
55     AccessTokenKit::ReloadNativeTokenInfo();
56     delete[] perms;
57 }
58 
FuzzInputMethodSystemAbility(const uint8_t * rawData,size_t size,InputMethodInterfaceCode code)59 bool ImfSaStubFuzzUtil::FuzzInputMethodSystemAbility(const uint8_t *rawData, size_t size, InputMethodInterfaceCode code)
60 {
61     if (!isInitialize_) {
62         Initialize();
63     }
64     GrantNativePermission();
65 
66     MessageParcel datas;
67     datas.WriteInterfaceToken(SYSTEMABILITY_INTERFACE_TOKEN);
68     datas.WriteBuffer(rawData, size);
69     datas.RewindRead(0);
70     MessageParcel reply;
71     MessageOption option;
72     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->OnRemoteRequest(
73         static_cast<int32_t>(code), datas, reply, option);
74     return true;
75 }
76 
Initialize()77 void ImfSaStubFuzzUtil::Initialize()
78 {
79     std::lock_guard<std::mutex> lock(initMutex_);
80     if (isInitialize_) {
81         return;
82     }
83     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->Initialize();
84     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->InitServiceHandler();
85     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
86     ImeCfgManager::GetInstance().Init();
87     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->SubscribeCommonEvent();
88     int32_t ret = DelayedSingleton<InputMethodSystemAbility>::GetInstance()->InitKeyEventMonitor();
89     IMSA_HILOGI("init KeyEvent monitor %{public}s", ret == ErrorCode::NO_ERROR ? "success" : "failed");
90     ret = DelayedSingleton<InputMethodSystemAbility>::GetInstance()->InitWmsMonitor();
91     IMSA_HILOGI("init wms monitor %{public}s", ret ? "success" : "failed");
92     isInitialize_ = true;
93 }
94 } // namespace MiscServices
95 } // namespace OHOS
96