• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 "systemabilitystub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "input_method_system_ability.h"
22 #include "input_method_controller.h"
23 #include "accesstoken_kit.h"
24 #include "token_setproc.h"
25 #include "nativetoken_kit.h"
26 #include "global.h"
27 
28 #include "message_parcel.h"
29 
30 using namespace OHOS::Security::AccessToken;
31 using namespace OHOS::MiscServices;
32 namespace OHOS {
GrantNativePermission()33     void GrantNativePermission()
34     {
35         const char **perms = new const char *[1];
36         perms[0] = "ohos.permission.CONNECT_IME_ABILITY";
37         TokenInfoParams infoInstance = {
38             .dcapsNum = 0,
39             .permsNum = 1,
40             .aclsNum = 0,
41             .dcaps = nullptr,
42             .perms = perms,
43             .acls = nullptr,
44             .processName = "inputmethod_imf",
45             .aplStr = "system_core",
46         };
47         uint64_t tokenId = GetAccessTokenId(&infoInstance);
48         int res = SetSelfTokenID(tokenId);
49         if (res == 0) {
50             IMSA_HILOGI("SetSelfTokenID success!");
51         } else {
52             IMSA_HILOGE("SetSelfTokenID fail!");
53         }
54         AccessTokenKit::ReloadNativeTokenInfo();
55         delete[] perms;
56     }
57 
58     class TextListener : public OnTextChangedListener {
59     public:
TextListener()60         TextListener() {}
~TextListener()61         ~TextListener() {}
InsertText(const std::u16string & text)62         void InsertText(const std::u16string& text)
63         {
64         }
DeleteBackward(int32_t length)65         void DeleteBackward(int32_t length)
66         {
67         }
SetKeyboardStatus(bool status)68         void SetKeyboardStatus(bool status)
69         {
70         }
DeleteForward(int32_t length)71         void DeleteForward(int32_t length)
72         {
73         }
SendKeyEventFromInputMethod(const KeyEvent & event)74         void SendKeyEventFromInputMethod(const KeyEvent& event)
75         {
76         }
SendKeyboardInfo(const KeyboardInfo & status)77         void SendKeyboardInfo(const KeyboardInfo& status)
78         {
79         }
MoveCursor(const Direction direction)80         void MoveCursor(const Direction direction)
81         {
82         }
HandleSetSelection(int32_t start,int32_t end)83         void HandleSetSelection(int32_t start, int32_t end)
84         {
85         }
HandleExtendAction(int32_t action)86         void HandleExtendAction(int32_t action)
87         {
88         }
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)89         void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
90         {
91         }
92     };
93     constexpr size_t THRESHOLD = 10;
94     constexpr int32_t OFFSET = 4;
95     const std::u16string SYSTEMABILITY_INTERFACE_TOKEN = u"ohos.miscservices.inputmethod.IInputMethodSystemAbility";
96 
ConvertToUint32(const uint8_t * ptr)97     uint32_t ConvertToUint32(const uint8_t *ptr)
98     {
99         if (ptr == nullptr) {
100             return 0;
101         }
102         uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
103         return bigVar;
104     }
FuzzInputMethodSystemAbility(const uint8_t * rawData,size_t size)105     bool FuzzInputMethodSystemAbility(const uint8_t* rawData, size_t size)
106     {
107         GrantNativePermission();
108         uint32_t code = ConvertToUint32(rawData);
109         rawData = rawData + OFFSET;
110         size = size - OFFSET;
111 
112         sptr<InputMethodController> imc = InputMethodController::GetInstance();
113         sptr<OnTextChangedListener> textListener = new TextListener();
114         imc->Attach(textListener);
115 
116         MessageParcel data;
117         data.WriteInterfaceToken(SYSTEMABILITY_INTERFACE_TOKEN);
118         data.WriteBuffer(rawData, size);
119         data.RewindRead(0);
120         MessageParcel reply;
121         MessageOption option;
122 
123         sptr<InputMethodSystemAbility> ability = new InputMethodSystemAbility();
124         ability->OnRemoteRequest(code, data, reply, option);
125 
126         return true;
127     }
128 }
129 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
131 {
132     if (size < OHOS::THRESHOLD) {
133         return 0;
134     }
135     /* Run your code on data */
136     OHOS::FuzzInputMethodSystemAbility(data, size);
137     return 0;
138 }