• 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 "locatorcallbackhost_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locator_ability.h"
27 
28 #include "locator_callback_napi.h"
29 #include "permission_manager.h"
30 
31 namespace OHOS {
32 using namespace OHOS::Location;
33 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
34 const int32_t LOCATION_PERM_NUM = 4;
MockNativePermission()35 void MockNativePermission()
36 {
37     const char *perms[] = {
38         ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
39         ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
40     };
41     NativeTokenInfoParams infoInstance = {
42         .dcapsNum = 0,
43         .permsNum = LOCATION_PERM_NUM,
44         .aclsNum = 0,
45         .dcaps = nullptr,
46         .perms = perms,
47         .acls = nullptr,
48         .processName = "GnssAbility_FuzzTest",
49         .aplStr = "system_basic",
50     };
51     auto tokenId = GetAccessTokenId(&infoInstance);
52     SetSelfTokenID(tokenId);
53     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
54     LocatorAbility::GetInstance()->EnableAbility(true);
55 }
56 
ParseData(const uint8_t * data,size_t size)57 char* ParseData(const uint8_t* data, size_t size)
58 {
59     if (data == nullptr) {
60         return nullptr;
61     }
62 
63     if (size > MAX_MEM_SIZE) {
64         return nullptr;
65     }
66 
67     char* ch = (char *)malloc(size + 1);
68     if (ch == nullptr) {
69         return nullptr;
70     }
71 
72     (void)memset_s(ch, size + 1, 0x00, size + 1);
73     if (memcpy_s(ch, size, data, size) != EOK) {
74         free(ch);
75         ch = nullptr;
76         return nullptr;
77     }
78     return ch;
79 }
80 
LocatorCallbackHost001FuzzTest(const char * data,size_t size)81 bool LocatorCallbackHost001FuzzTest(const char* data, size_t size)
82 {
83     MessageParcel requestParcel;
84     requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
85     requestParcel.WriteBuffer(data, size);
86     requestParcel.RewindRead(0);
87 
88     MessageParcel reply;
89     MessageOption option;
90 
91     auto callback = sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
92     callback->OnRemoteRequest(ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT, requestParcel, reply, option);
93 
94     return true;
95 }
96 
LocatorCallbackHost002FuzzTest(const char * data,size_t size)97 bool LocatorCallbackHost002FuzzTest(const char* data, size_t size)
98 {
99     MessageParcel requestParcel;
100     requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
101     requestParcel.WriteBuffer(data, size);
102     requestParcel.RewindRead(0);
103 
104     MessageParcel reply;
105     MessageOption option;
106 
107     auto callback = sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
108     callback->OnRemoteRequest(ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT, requestParcel, reply, option);
109 
110     return true;
111 }
112 
LocatorCallbackHost003FuzzTest(const char * data,size_t size)113 bool LocatorCallbackHost003FuzzTest(const char* data, size_t size)
114 {
115     MessageParcel requestParcel;
116     requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
117     requestParcel.WriteBuffer(data, size);
118     requestParcel.RewindRead(0);
119 
120     MessageParcel reply;
121     MessageOption option;
122 
123     auto callback = sptr<LocatorCallbackNapi>(new (std::nothrow) LocatorCallbackNapi());
124     callback->OnRemoteRequest(ILocatorCallback::RECEIVE_ERROR_INFO_EVENT, requestParcel, reply, option);
125 
126     return true;
127 }
128 } // namespace OHOS
129 
130 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)131 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
132 {
133     OHOS::MockNativePermission();
134     char* ch = OHOS::ParseData(data, size);
135     if (ch != nullptr) {
136         OHOS::LocatorCallbackHost001FuzzTest(ch, size);
137         OHOS::LocatorCallbackHost002FuzzTest(ch, size);
138         OHOS::LocatorCallbackHost003FuzzTest(ch, size);
139         free(ch);
140         ch = nullptr;
141     }
142     return 0;
143 }
144 
145