• 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 "abilitycontext_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_record.h"
22 #define private public
23 #include "fa_ability_context.h"
24 #undef private
25 #include "want.h"
26 #include "parcel.h"
27 #include "securec.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 OFFSET_ZERO = 24;
39 constexpr size_t OFFSET_ONE = 16;
40 constexpr size_t OFFSET_TWO = 8;
41 constexpr size_t U32_AT_SIZE = 4;
42 }
GetU32Data(const char * ptr)43 uint32_t GetU32Data(const char* ptr)
44 {
45     // convert fuzz input data to an integer
46     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
47         ptr[INPUT_THREE];
48 }
GetFuzzAbilityToken()49 sptr<Token> GetFuzzAbilityToken()
50 {
51     sptr<Token> token = nullptr;
52 
53     AbilityRequest abilityRequest;
54     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
55     abilityRequest.abilityInfo.name = "MainAbility";
56     abilityRequest.abilityInfo.type = AbilityType::PAGE;
57     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
58     if (abilityRecord) {
59         token = abilityRecord->GetToken();
60     }
61 
62     return token;
63 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)64 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
65 {
66     AbilityContext abilityContext;
67     // fuzz for want
68     Parcel wantParcel;
69     Want* want = nullptr;
70     if (wantParcel.WriteBuffer(data, size)) {
71         want = Want::Unmarshalling(wantParcel);
72         if (!want) {
73             return false;
74         }
75     }
76     int requestCode = static_cast<int>(GetU32Data(data));
77     abilityContext.StartAbility(*want, requestCode);
78     sptr<AAFwk::IAbilityConnection> conn = nullptr;
79     abilityContext.ConnectAbility(*want, conn);
80     abilityContext.StopAbility(*want);
81     std::string name(data, size);
82     int mode = static_cast<int>(GetU32Data(data));
83     abilityContext.GetDir(name, mode);
84     std::string bundleName(data, size);
85     int flag = static_cast<int>(GetU32Data(data));
86     int accountId = static_cast<int>(GetU32Data(data));
87     abilityContext.CreateBundleContext(bundleName, flag, accountId);
88     std::string permission(data, size);
89     int pid = static_cast<int>(GetU32Data(data));
90     int uid = static_cast<int>(GetU32Data(data));
91     abilityContext.VerifyPermission(permission, pid, uid);
92     std::string permissionName(data, size);
93     std::string des(data, size);
94     abilityContext.GetPermissionDes(permissionName, des);
95     std::vector<std::string> permissions;
96     std::string fileName(data, size);
97     std::string deviceId(data, size);
98     std::string abilityName(data, size);
99     std::string moduleName(data, size);
100     abilityContext.SetCallingContext(deviceId, bundleName, abilityName, moduleName);
101     std::shared_ptr<ContextDeal> base = nullptr;
102     abilityContext.AttachBaseContext(base);
103     std::string type(data, size);
104     abilityContext.GetExternalFilesDir(type);
105     std::string url(data, size);
106     Uri uri = Uri(url);
107     abilityContext.UnauthUriPermission(permission, uri, uid);
108     int patternId = static_cast<int>(GetU32Data(data));
109     abilityContext.SetPattern(patternId);
110     BundleInfo bundleInfo;
111     std::shared_ptr<ContextDeal> deal = nullptr;
112     abilityContext.InitResourceManager(bundleInfo, deal);
113     int resId = static_cast<int>(GetU32Data(data));
114     abilityContext.GetString(resId);
115     abilityContext.GetStringArray(resId);
116     abilityContext.GetIntArray(resId);
117     int themeId = static_cast<int>(GetU32Data(data));
118     abilityContext.SetTheme(themeId);
119     abilityContext.GetColor(resId);
120     abilityContext.SetColorMode(mode);
121     std::vector<AAFwk::Want> wants;
122     abilityContext.StartAbilities(wants);
123     if (want) {
124         delete want;
125         want = nullptr;
126     }
127     return (abilityContext.DisconnectAbility(conn) == 0);
128 }
129 }
130 
131 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)132 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
133 {
134     /* Run your code on data */
135     if (data == nullptr) {
136         std::cout << "invalid data" << std::endl;
137         return 0;
138     }
139 
140     /* Validate the length of size */
141     if (size < OHOS::U32_AT_SIZE) {
142         return 0;
143     }
144 
145     char* ch = static_cast<char*>(malloc(size + 1));
146     if (ch == nullptr) {
147         std::cout << "malloc failed." << std::endl;
148         return 0;
149     }
150 
151     (void)memset_s(ch, size + 1, 0x00, size + 1);
152     if (memcpy_s(ch, size, data, size) != EOK) {
153         std::cout << "copy failed." << std::endl;
154         free(ch);
155         ch = nullptr;
156         return 0;
157     }
158 
159     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
160     free(ch);
161     ch = nullptr;
162     return 0;
163 }
164 
165