• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "accessibilityconfig_fuzzer.h"
17 #include "accessibility_config.h"
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22 
23 namespace OHOS {
24 namespace {
25     constexpr size_t DATA_MIN_SIZE = 181;
26     constexpr char END_CHAR = '\0';
27     constexpr size_t LEN = 10;
28     bool g_flag = true;
29 } // namespace
30 using namespace OHOS::Security::AccessToken;
31 
32 class ConfigObserver : public OHOS::AccessibilityConfig::AccessibilityConfigObserver {
33 public:
OnConfigChanged(const OHOS::AccessibilityConfig::CONFIG_ID id,const OHOS::AccessibilityConfig::ConfigValue & value)34     void OnConfigChanged(const OHOS::AccessibilityConfig::CONFIG_ID id,
35         const OHOS::AccessibilityConfig::ConfigValue &value) override
36     {
37     }
38 };
39 
40 class EnableAbilityListObserver : public OHOS::AccessibilityConfig::AccessibilityEnableAbilityListsObserver {
41 public:
OnEnableAbilityListsStateChanged()42     void OnEnableAbilityListsStateChanged() override
43     {
44     }
45 };
46 
47 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)48 size_t GetObject(T &object, const uint8_t *data, size_t size)
49 {
50     size_t objectSize = sizeof(object);
51     if (objectSize > size) {
52         return 0;
53     }
54     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
55 }
56 
AddPermission()57 void AddPermission()
58 {
59     if (g_flag) {
60         const char *perms[2];
61         perms[0] = OHOS::Accessibility::OHOS_PERMISSION_READ_ACCESSIBILITY_CONFIG.c_str();
62         perms[1] = OHOS::Accessibility::OHOS_PERMISSION_WRITE_ACCESSIBILITY_CONFIG.c_str();
63         NativeTokenInfoParams infoInstance = {
64             .dcapsNum = 0,
65             .permsNum = 2,
66             .aclsNum = 0,
67             .dcaps = nullptr,
68             .perms = perms,
69             .acls = nullptr,
70             .processName = "com.accessibility.config.fuzzer.test",
71             .aplStr = "normal",
72         };
73         uint64_t tokenId = GetAccessTokenId(&infoInstance);
74         SetSelfTokenID(tokenId);
75         AccessTokenKit::ReloadNativeTokenInfo();
76         g_flag = false;
77     }
78 }
79 
GenerateCaptionProperty(OHOS::AccessibilityConfig::CaptionProperty & property,const uint8_t * data,size_t size)80 static size_t GenerateCaptionProperty(
81     OHOS::AccessibilityConfig::CaptionProperty &property, const uint8_t* data, size_t size)
82 {
83     size_t position = 0;
84     uint32_t temp = 0;
85     position += GetObject<uint32_t>(temp, &data[position], size - position);
86     property.SetFontColor(temp);
87 
88     position += GetObject<uint32_t>(temp, &data[position], size - position);
89     property.SetWindowColor(temp);
90 
91     position += GetObject<uint32_t>(temp, &data[position], size - position);
92     property.SetBackgroundColor(temp);
93 
94     position += GetObject<uint32_t>(temp, &data[position], size - position);
95     property.SetFontScale(static_cast<int32_t>(temp));
96 
97     char name[LEN + 1];
98     name[LEN] = END_CHAR;
99     for (size_t i = 0; i < LEN; i++) {
100         position += GetObject<char>(name[i], &data[position], size - position);
101     }
102     std::string family(name);
103     property.SetFontFamily(family);
104 
105     for (size_t i = 0; i < LEN; i++) {
106         position += GetObject<char>(name[i], &data[position], size - position);
107     }
108     std::string type(name);
109     property.SetFontFamily(type);
110 
111     return position;
112 }
113 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)114 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
115 {
116     if (data == nullptr || size < DATA_MIN_SIZE) {
117         return false;
118     }
119 
120     auto &abConfig = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
121     (void)abConfig.InitializeContext();
122     std::shared_ptr<ConfigObserver> cObserver = std::make_shared<ConfigObserver>();
123     std::shared_ptr<EnableAbilityListObserver> eObserver = std::make_shared<EnableAbilityListObserver>();
124 
125     size_t startPos = 0;
126     abConfig.SetScreenMagnificationState(data[startPos++] & 0x01);
127     abConfig.SetShortKeyState(data[startPos++] & 0x01);
128     abConfig.SetMouseKeyState(data[startPos++] & 0x01);
129     abConfig.SetCaptionsState(data[startPos++] & 0x01);
130     abConfig.SetHighContrastTextState(data[startPos++] & 0x01);
131     abConfig.SetInvertColorState(data[startPos++] & 0x01);
132     abConfig.SetAnimationOffState(data[startPos++] & 0x01);
133     abConfig.SetAudioMonoState(data[startPos++] & 0x01);
134 
135     uint32_t temp = 0;
136     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
137     abConfig.SetContentTimeout(temp);
138 
139     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
140     abConfig.SetDaltonizationColorFilter(static_cast<OHOS::AccessibilityConfig::DALTONIZATION_TYPE>(temp));
141 
142     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
143     abConfig.SetMouseAutoClick(static_cast<int32_t>(temp));
144 
145     float tempFloat = 0;
146     startPos += GetObject<float>(tempFloat, &data[startPos], size - startPos);
147     abConfig.SetBrightnessDiscount(tempFloat);
148 
149     startPos += GetObject<float>(tempFloat, &data[startPos], size - startPos);
150     abConfig.SetAudioBalance(tempFloat);
151 
152     char name[LEN + 1];
153     name[LEN] = END_CHAR;
154     for (size_t i = 0; i < LEN; i++) {
155         startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
156     }
157     std::string nameStr(name);
158     abConfig.SetShortkeyTarget(nameStr);
159 
160     OHOS::AccessibilityConfig::CaptionProperty property;
161     startPos += GenerateCaptionProperty(property, &data[startPos], size - startPos);
162     abConfig.SetCaptionsProperty(property);
163 
164     for (size_t i = 0; i < LEN; i++) {
165         startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
166     }
167     std::string abilityName1(name);
168     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
169     abConfig.EnableAbility(abilityName1, temp);
170 
171     for (size_t i = 0; i < LEN; i++) {
172         startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
173     }
174     std::string abilityName2(name);
175     abConfig.DisableAbility(abilityName2);
176 
177     bool flag = data[startPos++] & 0x01;
178     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
179     abConfig.SubscribeConfigObserver(static_cast<OHOS::AccessibilityConfig::CONFIG_ID>(temp), cObserver, flag);
180     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
181     abConfig.UnsubscribeConfigObserver(static_cast<OHOS::AccessibilityConfig::CONFIG_ID>(temp), cObserver);
182     abConfig.SubscribeEnableAbilityListsObserver(eObserver);
183     abConfig.UnsubscribeEnableAbilityListsObserver(eObserver);
184 
185     flag = data[startPos++] & 0x01;
186     abConfig.GetScreenMagnificationState(flag);
187     flag = data[startPos++] & 0x01;
188     abConfig.GetShortKeyState(flag);
189     flag = data[startPos++] & 0x01;
190     abConfig.GetMouseKeyState(flag);
191     flag = data[startPos++] & 0x01;
192     abConfig.GetCaptionsState(flag);
193     flag = data[startPos++] & 0x01;
194     abConfig.GetHighContrastTextState(flag);
195     flag = data[startPos++] & 0x01;
196     abConfig.GetInvertColorState(flag);
197     flag = data[startPos++] & 0x01;
198     abConfig.GetAnimationOffState(flag);
199     flag = data[startPos++] & 0x01;
200     abConfig.GetAudioMonoState(flag);
201 
202     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
203     abConfig.GetContentTimeout(temp);
204 
205     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
206     OHOS::AccessibilityConfig::DALTONIZATION_TYPE types =
207         static_cast<OHOS::AccessibilityConfig::DALTONIZATION_TYPE>(temp);
208     abConfig.GetDaltonizationColorFilter(types);
209 
210     startPos += GetObject<uint32_t>(temp, &data[startPos], size - startPos);
211     int32_t clicks = static_cast<int32_t>(temp);
212     abConfig.GetMouseAutoClick(clicks);
213 
214     startPos += GetObject<float>(tempFloat, &data[startPos], size - startPos);
215     abConfig.GetBrightnessDiscount(tempFloat);
216 
217     startPos += GetObject<float>(tempFloat, &data[startPos], size - startPos);
218     abConfig.GetAudioBalance(tempFloat);
219 
220     for (size_t i = 0; i < LEN; i++) {
221         startPos += GetObject<char>(name[i], &data[startPos], size - startPos);
222     }
223     std::string nameStrForGet(name);
224     abConfig.GetShortkeyTarget(nameStrForGet);
225 
226     OHOS::AccessibilityConfig::CaptionProperty propertyForGet;
227     GenerateCaptionProperty(propertyForGet, &data[startPos], size - startPos);
228     abConfig.GetCaptionsProperty(propertyForGet);
229 
230     return true;
231 }
232 }
233 
234 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)235 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
236 {
237     OHOS::AddPermission();
238     /* Run your code on data */
239     OHOS::DoSomethingInterestingWithMyAPI(data, size);
240     return 0;
241 }
242