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 "getabilityrecordsbyprocessid_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "ability_record.h"
22 #include "app_mgr_client.h"
23 #include "configuration.h"
24 #include "parcel.h"
25 #include "securec.h"
26
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29
30 namespace OHOS {
31 namespace {
32 constexpr size_t FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 }
GetU32Data(const char * ptr)35 uint32_t GetU32Data(const char* ptr)
36 {
37 // convert fuzz input data to an integer
38 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
39 }
GetFuzzAbilityToken()40 sptr<Token> GetFuzzAbilityToken()
41 {
42 sptr<Token> token = nullptr;
43 AbilityRequest abilityRequest;
44 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
45 abilityRequest.abilityInfo.name = "MainAbility";
46 abilityRequest.abilityInfo.type = AbilityType::DATA;
47 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
48 if (abilityRecord) {
49 token = abilityRecord->GetToken();
50 }
51 return token;
52 }
53
DoSomethingInterestingWithMyAPI(const char * data,size_t size)54 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
55 {
56 AppMgrClient* appMgrClient = new AppMgrClient();
57 if (!appMgrClient) {
58 return false;
59 }
60
61 int pid = static_cast<int>(GetU32Data(data));
62 sptr<IRemoteObject> token = GetFuzzAbilityToken();
63 if (!token) {
64 std::cout << "Get ability token failed." << std::endl;
65 return false;
66 }
67 std::vector<sptr<IRemoteObject>> tokens;
68 tokens.emplace_back(token);
69
70 if (appMgrClient->GetAbilityRecordsByProcessID(pid, tokens) != 0) {
71 return false;
72 }
73
74 return true;
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81 /* Run your code on data */
82 if (data == nullptr) {
83 std::cout << "invalid data" << std::endl;
84 return 0;
85 }
86
87 /* Validate the length of size */
88 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
89 return 0;
90 }
91
92 char* ch = (char *)malloc(size + 1);
93 if (ch == nullptr) {
94 std::cout << "malloc failed." << std::endl;
95 return 0;
96 }
97
98 (void)memset_s(ch, size + 1, 0x00, size + 1);
99 if (memcpy_s(ch, size, data, size) != EOK) {
100 std::cout << "copy failed." << std::endl;
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105
106 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
107 free(ch);
108 ch = nullptr;
109 return 0;
110 }
111
112