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 "enterprise_device_mgr_ability_fuzzer.h"
17 
18 #include "common_event_data.h"
19 #include "common_fuzzer.h"
20 #include "edm_ipc_interface_code.h"
21 #include "func_code.h"
22 #include "get_data_template.h"
23 #include "message_parcel.h"
24 #define protected public
25 #define private public
26 #include "enterprise_device_mgr_ability.h"
27 #undef protected
28 #undef private
29 #include "ienterprise_device_mgr.h"
30 #include "securec.h"
31 #include "utils.h"
32 
33 namespace OHOS {
34 namespace EDM {
35 constexpr size_t MIN_SIZE = 64;
36 const std::string FIRMWARE_EVENT_INFO_NAME = "version";
37 const std::string FIRMWARE_EVENT_INFO_TYPE = "packageType";
38 const std::string FIRMWARE_EVENT_INFO_CHECK_TIME = "firstReceivedTime";
39 
getCommonEventData(const uint8_t * data,size_t size)40 EventFwk::CommonEventData getCommonEventData(const uint8_t* data, size_t size)
41 {
42     int32_t pos = 0;
43     EventFwk::CommonEventData eventData;
44     EventFwk::Want want;
45     AppExecFwk::ElementName admin;
46     int32_t code = CommonFuzzer::GetU32Data(data, pos, size);
47     int uid = CommonFuzzer::GetU32Data(data, pos, size);
48     long checkTime = CommonFuzzer::GetLong(data, pos, size);
49     int32_t stringSize = (size - pos) / 5;
50     admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
51     admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
52     eventData.SetCode(code);
53     want.SetParam(FIRMWARE_EVENT_INFO_NAME, CommonFuzzer::GetString(data, pos, stringSize, size));
54     want.SetParam(FIRMWARE_EVENT_INFO_TYPE, CommonFuzzer::GetString(data, pos, stringSize, size));
55     want.SetParam(FIRMWARE_EVENT_INFO_CHECK_TIME, checkTime);
56     want.SetParam(AppExecFwk::Constants::USER_ID, uid);
57     want.SetAction(CommonFuzzer::GetString(data, pos, stringSize, size));
58     eventData.SetWant(want);
59     want.SetElement(admin);
60     return eventData;
61 }
62 
63 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     if (data == nullptr) {
67         return 0;
68     }
69     if (size < MIN_SIZE) {
70         return 0;
71     }
72     g_data = data;
73     g_size = size;
74     g_pos = 0;
75 
76     TEST::Utils::SetEdmInitialEnv();
77     sptr<EnterpriseDeviceMgrAbility> enterpriseDeviceMgrAbility = EnterpriseDeviceMgrAbility::GetInstance();
78     enterpriseDeviceMgrAbility->OnStart();
79     EventFwk::CommonEventData eventData = getCommonEventData(data, size);
80 
81     enterpriseDeviceMgrAbility->OnCommonEventSystemUpdate(eventData);
82     enterpriseDeviceMgrAbility->OnCommonEventUserRemoved(eventData);
83     enterpriseDeviceMgrAbility->OnCommonEventPackageAdded(eventData);
84     enterpriseDeviceMgrAbility->OnCommonEventPackageRemoved(eventData);
85     int32_t systemAbilityId = CommonFuzzer::GetU32Data(data);
86     const std::string deviceId(reinterpret_cast<const char*>(data), size);
87     enterpriseDeviceMgrAbility->OnAddSystemAbility(systemAbilityId, deviceId);
88     enterpriseDeviceMgrAbility->OnAppManagerServiceStart();
89     enterpriseDeviceMgrAbility->OnAbilityManagerServiceStart();
90     enterpriseDeviceMgrAbility->OnCommonEventServiceStart();
91 
92     int32_t userId = CommonFuzzer::GetU32Data(data);
93     enterpriseDeviceMgrAbility->SubscribeAppState();
94     enterpriseDeviceMgrAbility->UnsubscribeAppState();
95     AppExecFwk::ElementName admin;
96     std::string fuzzString(reinterpret_cast<const char*>(data), size);
97     admin.SetBundleName(fuzzString);
98     admin.SetAbilityName(fuzzString);
99     AdminType type = GetData<AdminType>();
100     bool isDebug = CommonFuzzer::GetU32Data(data) % 2;
101     enterpriseDeviceMgrAbility->VerifyEnableAdminCondition(admin, type, userId, isDebug);
102     std::string adminName(reinterpret_cast<const char*>(data), size);
103     const std::string policyName(reinterpret_cast<const char*>(data), size);
104     const std::string policyValue(reinterpret_cast<const char*>(data), size);
105     enterpriseDeviceMgrAbility->RemoveAdminItem(adminName, policyName, policyValue, userId);
106     std::string bundleName(reinterpret_cast<const char*>(data), size);
107     enterpriseDeviceMgrAbility->CheckCallingUid(bundleName);
108     TEST::Utils::ResetTokenTypeAndUid();
109     return 0;
110 }
111 } // namespace EDM
112 } // namespace OHOS