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 
28 #define private public
29 #include "locator_ability.h"
30 #undef private
31 
32 namespace OHOS {
33 using namespace OHOS::Location;
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 
GetU32Data(const char * ptr)40 uint32_t GetU32Data(const char* ptr)
41 {
42     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
43 }
44 
GetU64Data(const char * ptr)45 uint64_t GetU64Data(const char* ptr)
46 {
47     uint64_t u64data = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
48     return (u64data << 32) | (ptr[4] << 24) | (ptr[5] << 16) | (ptr[6] << 8) | (ptr[7]);
49 }
50 
ParseData(const uint8_t * data,size_t size)51 char* ParseData(const uint8_t* data, size_t size)
52 {
53     if (data == nullptr) {
54         return nullptr;
55     }
56 
57     if (size > MAX_MEM_SIZE) {
58         return nullptr;
59     }
60 
61     char* ch = (char *)malloc(size + 1);
62     if (ch == nullptr) {
63         return nullptr;
64     }
65 
66     (void)memset_s(ch, size + 1, 0x00, size + 1);
67     if (memcpy_s(ch, size, data, size) != EOK) {
68         free(ch);
69         ch = nullptr;
70         return nullptr;
71     }
72     return ch;
73 }
74 
CheckMessageFuzzTest(const char * data,size_t size)75 bool CheckMessageFuzzTest(const char* data, size_t size)
76 {
77     MessageParcel reply;
78     AppIdentity identity;
79     int offset = 0;
80     if (size <= 32) {
81         return true;
82     }
83     identity.SetPid(static_cast<pid_t>(GetU64Data(data)));
84     identity.SetUid(static_cast<pid_t>(GetU64Data(data + (offset += U64DATA_SIZE))));
85     identity.SetTokenId(GetU32Data(data + (offset += U64DATA_SIZE)));
86     identity.SetTokenIdEx(GetU64Data(data + (offset += U32DATA_SIZE)));
87     identity.SetFirstTokenId(GetU32Data(data + (offset += U64DATA_SIZE)));
88     identity.SetBundleName(data + (offset += U32DATA_SIZE));
89 
90     auto locatorAbility = sptr<LocatorAbility>(new (std::nothrow) LocatorAbility());
91     locatorAbility->CheckLocationPermission(reply, identity);
92     locatorAbility->CheckSettingsPermission(reply, identity);
93     locatorAbility->CheckPreciseLocationPermissions(reply, identity);
94     locatorAbility->CheckLocationSwitchState(reply);
95     return true;
96 }
97 } // namespace OHOS
98 
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102     if (size < OHOS::MIN_SIZE_NUM) {
103         return 0;
104     }
105     char* ch = OHOS::ParseData(data, size);
106     if (ch != nullptr) {
107         OHOS::CheckMessageFuzzTest(ch, size);
108         free(ch);
109         ch = nullptr;
110     }
111     return 0;
112 }
113 
114