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 "allowed_usb_devices_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 "func_code.h"
23 #include "get_data_template.h"
24 #include "ienterprise_device_mgr.h"
25 #include "message_parcel.h"
26 #include "usb_device_id.h"
27 #include "utils.h"
28 
29 namespace OHOS {
30 namespace EDM {
31 constexpr size_t MIN_SIZE = 16;
32 constexpr int32_t WITHOUT_USERID = 0;
33 constexpr int32_t USB_DEVICE_ID_SIZE = 1;
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     int32_t pos = 0;
45     int32_t stringSize = size / 6;
46     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
47         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
48         uint32_t code = EdmInterfaceCode::ALLOWED_USB_DEVICES;
49         code = POLICY_FUNC_CODE(operateType, code);
50 
51         AppExecFwk::ElementName admin;
52         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
53         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
54         MessageParcel parcel;
55         parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor());
56         parcel.WriteInt32(WITHOUT_USERID);
57         if (operateType) {
58             parcel.WriteParcelable(&admin);
59             parcel.WriteInt32(USB_DEVICE_ID_SIZE);
60             UsbDeviceId usbDeviceId = GetData<UsbDeviceId>();
61             std::vector<UsbDeviceId> usbDeviceIds = { usbDeviceId };
62             std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(),
63                 [&](const auto usbDeviceId) { usbDeviceId.Marshalling(parcel); });
64         } else {
65             parcel.WriteString("");
66             parcel.WriteInt32(0);
67             parcel.WriteParcelable(&admin);
68         }
69         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
70     }
71     return 0;
72 }
73 } // namespace EDM
74 } // namespace OHOS