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 "abilitymanagerserviceseventh_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "ability_manager_service.h"
24 #undef protected
25 #undef private
26 
27 #include "ability_record.h"
28 
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 namespace {
34 constexpr int INPUT_ZERO = 0;
35 constexpr int INPUT_ONE = 1;
36 constexpr int INPUT_TWO = 2;
37 constexpr int INPUT_THREE = 3;
38 constexpr size_t FOO_MAX_LEN = 1024;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr size_t OFFSET_ZERO = 24;
41 constexpr size_t OFFSET_ONE = 16;
42 constexpr size_t OFFSET_TWO = 8;
43 class MyAbilityConnection : public IAbilityConnection {
44 public:
45     MyAbilityConnection() = default;
46     virtual ~MyAbilityConnection() = default;
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)47     void OnAbilityConnectDone(
48         const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode) override
49     {}
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)50     void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override
51     {}
AsObject()52     sptr<IRemoteObject> AsObject() override
53     {
54         return {};
55     }
56 };
57 }
58 
GetU32Data(const char * ptr)59 uint32_t GetU32Data(const char* ptr)
60 {
61     // convert fuzz input data to an integer
62     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
63         ptr[INPUT_THREE];
64 }
65 
GetFuzzAbilityToken()66 sptr<Token> GetFuzzAbilityToken()
67 {
68     sptr<Token> token = nullptr;
69     AbilityRequest abilityRequest;
70     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
71     abilityRequest.abilityInfo.name = "MainAbility";
72     abilityRequest.abilityInfo.type = AbilityType::DATA;
73     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
74     if (abilityRecord) {
75         token = abilityRecord->GetToken();
76     }
77     return token;
78 }
79 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)80 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
81 {
82     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
83     Parcel wantParcel;
84     Want* want = nullptr;
85     if (wantParcel.WriteBuffer(data, size)) {
86         want = Want::Unmarshalling(wantParcel);
87         if (!want) {
88             return false;
89         }
90     }
91     sptr<IRemoteObject> token = GetFuzzAbilityToken();
92     sptr<IAbilityConnection> connect = new MyAbilityConnection();
93 
94     // fuzz for AbilityManagerService
95     auto abilityms = std::make_shared<AbilityManagerService>();
96     abilityms->GetDataAbilityManagerByToken(token);
97     abilityms->ConnectBmsService();
98     sptr<IWantSender> target;
99     std::shared_ptr<WantSenderInfo> wantSenderInfo;
100     abilityms->GetWantSenderInfo(target, wantSenderInfo);
101     abilityms->GetAppMemorySize();
102     abilityms->IsRamConstrainedDevice();
103     AmsConfigurationParameter::GetInstance().GetMissionSaveTime();
104     abilityms->GetMissionIdByAbilityToken(token);
105     abilityms->GetAbilityTokenByMissionId(int32Param);
106     abilityms->StartRemoteAbilityByCall(*want, token, token);
107     AppExecFwk::ElementName element;
108     abilityms->ReleaseRemoteAbility(token, element);
109     abilityms->StartAbilityByCall(*want, connect, token);
110     abilityms->ReleaseCall(connect, element);
111 
112     return true;
113 }
114 }
115 
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
118 {
119     /* Run your code on data */
120     if (data == nullptr) {
121         return 0;
122     }
123 
124     /* Validate the length of size */
125     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
126         return 0;
127     }
128 
129     char* ch = static_cast<char*>(malloc(size + 1));
130     if (ch == nullptr) {
131         std::cout << "malloc failed." << std::endl;
132         return 0;
133     }
134 
135     (void)memset_s(ch, size + 1, 0x00, size + 1);
136     if (memcpy_s(ch, size, data, size) != EOK) {
137         std::cout << "copy failed." << std::endl;
138         free(ch);
139         ch = nullptr;
140         return 0;
141     }
142 
143     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
144     free(ch);
145     ch = nullptr;
146     return 0;
147 }
148 
149