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 "locatorcallbackstub_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 "i_locator_callback.h"
29 
30 namespace OHOS {
31 using namespace OHOS::Location;
32 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
33 const int32_t CODE_MAX = 50;
34 const int32_t CODE_MIN = 1;
35 const int32_t MIN_SIZE_NUM = 4;
36 
GetU32Data(const char * ptr)37 uint32_t GetU32Data(const char* ptr)
38 {
39     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40 }
41 
ParseData(const uint8_t * data,size_t size)42 char* ParseData(const uint8_t* data, size_t size)
43 {
44     if (data == nullptr) {
45         return nullptr;
46     }
47 
48     if (size > MAX_MEM_SIZE) {
49         return nullptr;
50     }
51 
52     char* ch = (char *)malloc(size + 1);
53     if (ch == nullptr) {
54         return nullptr;
55     }
56 
57     (void)memset_s(ch, size + 1, 0x00, size + 1);
58     if (memcpy_s(ch, size, data, size) != EOK) {
59         free(ch);
60         ch = nullptr;
61         return nullptr;
62     }
63     return ch;
64 }
65 
LocatorCallbackStubFuzzTest(const char * data,size_t size)66 bool LocatorCallbackStubFuzzTest(const char* data, size_t size)
67 {
68     uint32_t code = GetU32Data(data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
69     MessageParcel requestParcel;
70     requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
71     requestParcel.WriteBuffer(data, size);
72     requestParcel.RewindRead(0);
73 
74     MessageParcel reply;
75     MessageOption option;
76     auto callback = sptr<LocatorCallbackStub>(new (std::nothrow) LocatorCallbackStub());
77     callback->OnRemoteRequest(code, requestParcel, reply, option);
78     return true;
79 }
80 } // namespace OHOS
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     if (size < OHOS::MIN_SIZE_NUM) {
86         return 0;
87     }
88     char* ch = OHOS::ParseData(data, size);
89     if (ch != nullptr) {
90         OHOS::LocatorCallbackStubFuzzTest(ch, size);
91         free(ch);
92         ch = nullptr;
93     }
94     return 0;
95 }
96 
97