1 /*
2 * Copyright (c) 2025 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 "disallow_mobile_data_plugin_fuzzer.h"
17
18 #include <system_ability_definition.h>
19
20 #define protected public
21 #define private public
22 #include "disallow_mobile_data_plugin.h"
23 #undef protected
24 #undef private
25 #include "common_fuzzer.h"
26 #include "edm_constants.h"
27 #include "edm_ipc_interface_code.h"
28 #include "handle_policy_data.h"
29 #include "ienterprise_device_mgr.h"
30 #include "func_code.h"
31 #include "message_parcel.h"
32 #include "utils.h"
33
34 namespace OHOS {
35 namespace EDM {
36 constexpr size_t MIN_SIZE = 16;
37 constexpr size_t WITHOUT_USERID = 0;
38
DoSomethingInterestingWithAPI(const uint8_t * data,size_t size,int32_t pos,int32_t stringSize)39 void DoSomethingInterestingWithAPI(const uint8_t* data, size_t size, int32_t pos, int32_t stringSize)
40 {
41 DisallowMobileDataPlugin plugin;
42 std::string adminName = CommonFuzzer::GetString(data, pos, stringSize, size);
43 std::string policyData = CommonFuzzer::GetString(data, pos, stringSize, size);
44 std::string mergeData = CommonFuzzer::GetString(data, pos, stringSize, size);
45 int32_t userId = CommonFuzzer::GetU32Data(data);
46 plugin.OnAdminRemove(adminName, policyData, mergeData, userId);
47 MessageParcel parcel;
48 int32_t forceOpen = CommonFuzzer::GetU32Data(data);
49 parcel.WriteInt32(forceOpen);
50 plugin.OnHandleForceOpen(parcel);
51
52 MessageParcel setParcel;
53 MessageParcel replyParcel;
54 uint32_t code = EdmInterfaceCode::DISALLOWED_MOBILE_DATA;
55 code = POLICY_FUNC_CODE(static_cast<uint32_t>(FuncOperateType::SET), code);
56 setParcel.WriteString(EdmConstants::MobileData::DISALLOW_FLAG);
57 bool isDisallow = CommonFuzzer::GetU32Data(data) % 2;
58 setParcel.WriteBool(isDisallow);
59 HandlePolicyData handlePolicyData;
60 plugin.OnHandlePolicy(code, setParcel, replyParcel, handlePolicyData, userId);
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 int32_t pos = 0;
73 int32_t stringSize = size / 15;
74 for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
75 operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
76 uint32_t code = EdmInterfaceCode::DISALLOWED_MOBILE_DATA;
77 code = POLICY_FUNC_CODE(operateType, code);
78 AppExecFwk::ElementName admin;
79 admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
80 admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
81 MessageParcel parcel;
82 parcel.WriteInterfaceToken(IEnterpriseDeviceMgrIdl::GetDescriptor());
83 parcel.WriteInt32(WITHOUT_USERID);
84 if (operateType) {
85 parcel.WriteParcelable(&admin);
86 std::vector<std::string> key {CommonFuzzer::GetString(data, pos, stringSize, size)};
87 std::vector<std::string> value {CommonFuzzer::GetString(data, pos, stringSize, size)};
88 parcel.WriteStringVector(key);
89 parcel.WriteStringVector(value);
90 } else {
91 parcel.WriteString("");
92 bool hasAdmin = CommonFuzzer::GetU32Data(data) % 2;
93 if (hasAdmin) {
94 parcel.WriteInt32(0);
95 parcel.WriteParcelable(&admin);
96 } else {
97 parcel.WriteInt32(0);
98 }
99 parcel.WriteInt32(CommonFuzzer::GetU32Data(data));
100 }
101 CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
102 }
103 DoSomethingInterestingWithAPI(data, size, pos, stringSize);
104 return 0;
105 }
106 } // namespace EDM
107 } // namespace OHOS