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 "firewall_rule_plugin_fuzzer.h"
17
18 #include <system_ability_definition.h>
19
20 #include "common_fuzzer.h"
21 #include "edm_ipc_interface_code.h"
22 #include "firewall_rule.h"
23 #include "func_code.h"
24 #include "ienterprise_device_mgr.h"
25 #include "iptables_utils.h"
26 #include "message_parcel.h"
27
28 namespace OHOS {
29 namespace EDM {
30 constexpr size_t MIN_SIZE = 16;
31 constexpr int32_t WITHOUT_USERID = 0;
32 constexpr int32_t MAX_ENUM_LENGTH = 2;
33 constexpr int32_t MAX_PROTOCOL_LENGTH = 4;
34
35 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)36 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
37 {
38 if (data == nullptr) {
39 return 0;
40 }
41 if (size < MIN_SIZE) {
42 return 0;
43 }
44
45 int32_t pos = 0;
46 int32_t stringSize = size / 6;
47 for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
48 operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
49 uint32_t code = EdmInterfaceCode::FIREWALL_RULE;
50 code = POLICY_FUNC_CODE(operateType, code);
51
52 AppExecFwk::ElementName admin;
53 admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
54 admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
55 MessageParcel parcel;
56 parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor());
57 parcel.WriteInt32(WITHOUT_USERID);
58 if (operateType) {
59 parcel.WriteParcelable(&admin);
60 IPTABLES::FirewallRule firewall;
61 std::string srcAddr(reinterpret_cast<const char*>(data), size);
62 std::string destAddr(reinterpret_cast<const char*>(data), size);
63 std::string srcPort(reinterpret_cast<const char*>(data), size);
64 std::string destPort(reinterpret_cast<const char*>(data), size);
65 std::string uid(reinterpret_cast<const char*>(data), size);
66 IPTABLES::Direction directionEnum =
67 static_cast<IPTABLES::Direction>(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH);
68 IPTABLES::Action actionEnum =
69 static_cast<IPTABLES::Action>(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH);
70 IPTABLES::Protocol protocolEnum =
71 static_cast<IPTABLES::Protocol>(CommonFuzzer::GetU32Data(data) % MAX_PROTOCOL_LENGTH);
72 firewall = {directionEnum, actionEnum, protocolEnum, srcAddr, destAddr, srcPort, destPort, uid};
73 IPTABLES::FirewallRuleParcel firewallRuleParcel{firewall};
74 firewallRuleParcel.Marshalling(parcel);
75 } else {
76 parcel.WriteString("");
77 parcel.WriteInt32(0);
78 parcel.WriteParcelable(&admin);
79 }
80 CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
81 }
82 return 0;
83 }
84 } // namespace EDM
85 } // namespace OHOS