• 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 
17 #include "checkmessage_fuzzer.h"
18 
19 #include "accesstoken_kit.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "nativetoken_kit.h"
25 #include "system_ability_definition.h"
26 #include "token_setproc.h"
27 #include "hook_utils.h"
28 
29 #define private public
30 #include "locator_ability.h"
31 #undef private
32 
33 namespace OHOS {
34 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
35 const int32_t U32DATA_SIZE = 4;
36 const int32_t U64DATA_SIZE = 8;
37 const int32_t MIN_SIZE_NUM = 8;
38 
39 namespace Location {
40 
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
44 }
45 
GetU64Data(const char * ptr)46 uint64_t GetU64Data(const char* ptr)
47 {
48     uint64_t u64data = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
49     return (u64data << 32) | (ptr[4] << 24) | (ptr[5] << 16) | (ptr[6] << 8) | (ptr[7]);
50 }
51 
ParseData(const uint8_t * data,size_t size)52 char* ParseData(const uint8_t* data, size_t size)
53 {
54     if (data == nullptr) {
55         return nullptr;
56     }
57 
58     if (size > MAX_MEM_SIZE) {
59         return nullptr;
60     }
61 
62     char* ch = (char *)malloc(size + 1);
63     if (ch == nullptr) {
64         return nullptr;
65     }
66 
67     (void)memset_s(ch, size + 1, 0x00, size + 1);
68     if (memcpy_s(ch, size, data, size) != EOK) {
69         free(ch);
70         ch = nullptr;
71         return nullptr;
72     }
73     return ch;
74 }
75 
CheckMessageFuzzTest(const char * data,size_t size)76 bool CheckMessageFuzzTest(const char* data, size_t size)
77 {
78     MessageParcel reply;
79     AppIdentity identity;
80     int offset = 0;
81     if (size <= 32) {
82         return true;
83     }
84     identity.SetPid(static_cast<pid_t>(GetU64Data(data)));
85     identity.SetUid(static_cast<pid_t>(GetU64Data(data + (offset += U64DATA_SIZE))));
86     identity.SetTokenId(GetU32Data(data + (offset += U64DATA_SIZE)));
87     identity.SetTokenIdEx(GetU64Data(data + (offset += U32DATA_SIZE)));
88     identity.SetFirstTokenId(GetU32Data(data + (offset += U64DATA_SIZE)));
89     identity.SetBundleName(data + (offset += U32DATA_SIZE));
90 
91     auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
92     locatorAbility->CheckLocationPermission(identity.GetTokenId(), identity.GetFirstTokenId());
93     locatorAbility->CheckPreciseLocationPermissions(identity.GetTokenId(), identity.GetFirstTokenId());
94     locatorAbility->CheckLocationSwitchState();
95     return true;
96 }
97 
HookUtilFuzzTest(const char * data,size_t size)98 bool HookUtilFuzzTest(const char* data, size_t size)
99 {
100     MessageParcel reply;
101     AppIdentity identity;
102     int offset = 0;
103     if (size <= 32) {
104         return true;
105     }
106     std::string testString = data + (offset += U32DATA_SIZE);
107     std::shared_ptr<Request> request = std::make_shared<Request>();
108     std::unique_ptr<Location> location = std::make_unique<Location>();
109     std::vector<std::string> names;
110     std::vector<std::string> values;
111     HookUtils::ExecuteHookWhenStartLocation(request);
112     HookUtils::ExecuteHookWhenStopLocation(request);
113     HookUtils::ExecuteHookWhenGetAddressFromLocation(testString);
114     HookUtils::ExecuteHookWhenGetAddressFromLocationName(testString);
115     HookUtils::ExecuteHookWhenReportInnerInfo(0, names, values);
116     HookUtils::ExecuteHookWhenAddWorkRecord(false, false, testString, testString);
117 
118     HookUtils::CheckGnssLocationValidity(location);
119     HookUtils::ExecuteHookWhenCheckAppForUser(testString);
120     HookUtils::ExecuteHookReportManagerGetCacheLocation(testString, 0);
121     HookUtils::ExecuteHookEnableAbility(testString, false, 0);
122     HookUtils::ExecuteHookWhenPreStartLocating(testString);
123     HookUtils::ExecuteHookWhenAddNetworkRequest(testString);
124     HookUtils::ExecuteHookWhenRemoveNetworkRequest(testString);
125     int test = 0;
126     HookUtils::ExecuteHookWhenSetAgnssServer(testString, test);
127     HookUtils::ExecuteHookWhenSimStateChange(testString);
128     HookUtils::ExecuteHookWhenReportBluetoothScanResult(testString, testString);
129     HookUtils::ExecuteHookWhenStartScanBluetoothDevice(testString, testString);
130     std::vector<std::shared_ptr<LocatingRequiredData>> result;
131     HookUtils::ExecuteHookWhenWifiScanStateChanged(result);
132     HookUtils::ExecuteHookWhenCustConfigPolicyChange();
133     return true;
134 }
135 }
136 } // namespace OHOS
137 
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
140 {
141     if (size < OHOS::MIN_SIZE_NUM) {
142         return 0;
143     }
144     char* ch = OHOS::Location::ParseData(data, size);
145     if (ch != nullptr) {
146         OHOS::Location::CheckMessageFuzzTest(ch, size);
147         OHOS::Location::HookUtilFuzzTest(ch, size);
148         free(ch);
149         ch = nullptr;
150     }
151     return 0;
152 }
153 
154