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