• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "closeability_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_manager_client.h"
22 #include "ability_record.h"
23 
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AppExecFwk;
26 
27 namespace OHOS {
28 namespace {
29 constexpr size_t U32_AT_SIZE = 4;
30 }
GetFuzzAbilityToken()31 sptr<Token> GetFuzzAbilityToken()
32 {
33     sptr<Token> token = nullptr;
34 
35     AbilityRequest abilityRequest;
36     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
37     abilityRequest.abilityInfo.name = "MainAbility";
38     abilityRequest.abilityInfo.type = AbilityType::DATA;
39     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
40     if (abilityRecord) {
41         token = abilityRecord->GetToken();
42     }
43 
44     return token;
45 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)46 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
47 {
48     auto abilitymgr = AbilityManagerClient::GetInstance();
49     int resultCode = 0;
50     if (!abilitymgr) {
51         return false;
52     }
53 
54     // get token
55     sptr<IRemoteObject> token = GetFuzzAbilityToken();
56     if (!token) {
57         std::cout << "Get ability token failed." << std::endl;
58         return false;
59     }
60 
61     // fuzz for want
62     Parcel wantParcel;
63     Want* resultWant = nullptr;
64     if (wantParcel.WriteBuffer(data, size)) {
65         resultWant = Want::Unmarshalling(wantParcel);
66         if (resultWant) {
67             ErrCode result = abilitymgr->CloseAbility(token, resultCode, resultWant);
68             if (result != 0) {
69                 return false;
70             }
71         }
72     }
73     if (resultWant) {
74         delete resultWant;
75         resultWant = nullptr;
76     }
77     return true;
78 }
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     if (data == nullptr) {
86         std::cout << "invalid data" << std::endl;
87         return 0;
88     }
89 
90     /* Validate the length of size */
91     if (size < OHOS::U32_AT_SIZE) {
92         return 0;
93     }
94 
95     char* ch = static_cast<char*>(malloc(size + 1));
96     if (ch == nullptr) {
97         std::cout << "malloc failed." << std::endl;
98         return 0;
99     }
100 
101     (void)memset_s(ch, size + 1, 0x00, size + 1);
102     if (memcpy_s(ch, size, data, size) != EOK) {
103         std::cout << "copy failed." << std::endl;
104         free(ch);
105         ch = nullptr;
106         return 0;
107     }
108 
109     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
110     free(ch);
111     ch = nullptr;
112     return 0;
113 }
114 
115