• 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 "getwantsender_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_manager_client.h"
22 #include "ability_record.h"
23 #include "parcel.h"
24 #include "sender_info.h"
25 #include "want_sender_info.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 }
GetFuzzAbilityToken()35 sptr<Token> GetFuzzAbilityToken()
36 {
37     sptr<Token> token = nullptr;
38 
39     AbilityRequest abilityRequest;
40     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
41     abilityRequest.abilityInfo.name = "MainAbility";
42     abilityRequest.abilityInfo.type = AbilityType::DATA;
43     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
44     if (abilityRecord) {
45         token = abilityRecord->GetToken();
46     }
47 
48     return token;
49 }
50 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
52 {
53     auto abilityMgr = AbilityManagerClient::GetInstance();
54     if (!abilityMgr) {
55         std::cout << "Get ability manager client failed." << std::endl;
56         return false;
57     }
58 
59     // get token
60     sptr<Token> token = GetFuzzAbilityToken();
61     if (!token) {
62         std::cout << "Get ability token failed." << std::endl;
63         return false;
64     }
65 
66     // fuzz for wantSenderInfo
67     Parcel wantSenderInfoParcel;
68     WantSenderInfo *wantSenderInfo = nullptr;
69     sptr<IWantSender> sender = nullptr;
70     if (wantSenderInfoParcel.WriteBuffer(data, size)) {
71         wantSenderInfo = WantSenderInfo::Unmarshalling(wantSenderInfoParcel);
72         if (wantSenderInfo) {
73             sender = abilityMgr->GetWantSender(*wantSenderInfo, token);
74         }
75     }
76 
77     // fuzz for senderInfo
78     Parcel senderInfoParcel;
79     SenderInfo *senderInfo = nullptr;
80     if (senderInfoParcel.WriteBuffer(data, size)) {
81         senderInfo = SenderInfo::Unmarshalling(senderInfoParcel);
82         if (sender && senderInfo) {
83             abilityMgr->SendWantSender(sender, *senderInfo);
84         }
85     }
86 
87     if (senderInfo) {
88         delete senderInfo;
89         senderInfo = nullptr;
90     }
91     if (wantSenderInfo) {
92         delete wantSenderInfo;
93         wantSenderInfo = nullptr;
94     }
95 
96     return true;
97 }
98 }
99 
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
102 {
103     /* Run your code on data */
104     if (data == nullptr) {
105         std::cout << "invalid data" << std::endl;
106         return 0;
107     }
108 
109     /* Validate the length of size */
110     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
111         return 0;
112     }
113 
114     char* ch = (char *)malloc(size + 1);
115     if (ch == nullptr) {
116         std::cout << "malloc failed." << std::endl;
117         return 0;
118     }
119 
120     (void)memset_s(ch, size + 1, 0x00, size + 1);
121     if (memcpy_s(ch, size, data, size) != EOK) {
122         std::cout << "copy failed." << std::endl;
123         free(ch);
124         ch = nullptr;
125         return 0;
126     }
127 
128     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
129     free(ch);
130     ch = nullptr;
131     return 0;
132 }
133 
134