1 /*
2  * Copyright (c) 2022-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 #define private public
16 #define protected public
17 #include "input_method_system_ability.h"
18 #include "input_method_system_ability_proxy.h"
19 #undef private
20 
21 #include "systemabilitystub_fuzzer.h"
22 
23 #include <atomic>
24 #include <cstddef>
25 #include <cstdint>
26 #include <string_ex.h>
27 
28 #include "accesstoken_kit.h"
29 #include "global.h"
30 #include "input_method_controller.h"
31 #include "iservice_registry.h"
32 #include "message_parcel.h"
33 #include "nativetoken_kit.h"
34 #include "system_ability_definition.h"
35 #include "text_listener.h"
36 #include "token_setproc.h"
37 
38 using namespace OHOS::Security::AccessToken;
39 using namespace OHOS::MiscServices;
40 namespace OHOS {
41 std::atomic_bool g_isInitialize = false;
42 constexpr uint32_t TARGET_REMOTE_CODE_NUMS = 21;
GrantNativePermission()43 void GrantNativePermission()
44 {
45     const char **perms = new const char *[1];
46     perms[0] = "ohos.permission.CONNECT_IME_ABILITY";
47     TokenInfoParams infoInstance = {
48         .dcapsNum = 0,
49         .permsNum = 1,
50         .aclsNum = 0,
51         .dcaps = nullptr,
52         .perms = perms,
53         .acls = nullptr,
54         .processName = "inputmethod_imf",
55         .aplStr = "system_core",
56     };
57     uint64_t tokenId = GetAccessTokenId(&infoInstance);
58     int res = SetSelfTokenID(tokenId);
59     if (res == 0) {
60         IMSA_HILOGI("SetSelfTokenID success!");
61     } else {
62         IMSA_HILOGE("SetSelfTokenID fail!");
63     }
64     AccessTokenKit::ReloadNativeTokenInfo();
65     delete[] perms;
66 }
67 constexpr size_t THRESHOLD = 10;
68 constexpr int32_t OFFSET = 4;
69 const std::u16string SYSTEMABILITY_INTERFACE_TOKEN = u"ohos.miscservices.inputmethod.IInputMethodSystemAbility";
70 
ConvertToUint32(const uint8_t * ptr)71 uint32_t ConvertToUint32(const uint8_t *ptr)
72 {
73     if (ptr == nullptr) {
74         return 0;
75     }
76     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
77     return bigVar;
78 }
FuzzInputMethodSystemAbility(const uint8_t * rawData,size_t size)79 bool FuzzInputMethodSystemAbility(const uint8_t *rawData, size_t size)
80 {
81     GrantNativePermission();
82     uint32_t code = ConvertToUint32(rawData) % TARGET_REMOTE_CODE_NUMS;
83     rawData = rawData + OFFSET;
84     size = size - OFFSET;
85 
86     if (!g_isInitialize.load()) {
87         DelayedSingleton<InputMethodSystemAbility>::GetInstance()->Initialize();
88         g_isInitialize.store(true);
89     }
90 
91     sptr<InputMethodController> imc = InputMethodController::GetInstance();
92     sptr<OnTextChangedListener> textListener = new TextListener();
93     imc->Attach(textListener);
94 
95     MessageParcel datas;
96     datas.WriteInterfaceToken(SYSTEMABILITY_INTERFACE_TOKEN);
97     datas.WriteBuffer(rawData, size);
98     datas.RewindRead(0);
99     MessageParcel reply;
100     MessageOption option;
101     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
102     return true;
103 }
104 
TestDump(const uint8_t * rawData,size_t size)105 bool TestDump(const uint8_t *rawData, size_t size)
106 {
107     std::vector<std::u16string> args;
108     std::string str(reinterpret_cast<const char *>(rawData), size);
109     args.push_back(Str8ToStr16(str));
110     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->Dump(static_cast<int32_t>(size), args);
111     DelayedSingleton<InputMethodSystemAbility>::GetInstance()->DumpAllMethod(static_cast<int32_t>(size));
112     return true;
113 }
114 
115 } // namespace OHOS
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
118 {
119     if (size < OHOS::THRESHOLD) {
120         return 0;
121     }
122     /* Run your code on data */
123     OHOS::FuzzInputMethodSystemAbility(data, size);
124     OHOS::TestDump(data, size);
125     return 0;
126 }
127