• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "allowed_usb_devices_plugin.h"
21 #include "array_usb_device_id_serializer.h"
22 #include "common_fuzzer.h"
23 #include "edm_ipc_interface_code.h"
24 #include "func_code.h"
25 #include "get_data_template.h"
26 #include "ienterprise_device_mgr.h"
27 #include "message_parcel.h"
28 #include "plugin_manager.h"
29 #include "usb_device_id.h"
30 #include "utils.h"
31 
32 namespace OHOS {
33 namespace EDM {
34 constexpr size_t MIN_SIZE = 20;
35 constexpr int32_t WITHOUT_USERID = 0;
36 constexpr int32_t USB_DEVICE_ID_SIZE = 1;
37 
DoSomethingInterestingWithAPI(const uint8_t * data,size_t size,int32_t pos,int32_t stringSize)38 void DoSomethingInterestingWithAPI(const uint8_t* data, size_t size, int32_t pos, int32_t stringSize)
39 {
40     AllowUsbDevicesPlugin plugin;
41     std::string adminName = CommonFuzzer::GetString(data, pos, stringSize, size);
42     std::vector<UsbDeviceId> emptyData;
43     std::vector<UsbDeviceId> policyData = { GetData<UsbDeviceId>() };
44     std::vector<UsbDeviceId> mergeData = { GetData<UsbDeviceId>() };
45     int32_t userId = CommonFuzzer::GetU32Data(data);
46     plugin.OnAdminRemove(adminName, emptyData, mergeData, userId);
47     plugin.OnAdminRemove(adminName, policyData, emptyData, userId);
48     plugin.OnAdminRemove(adminName, policyData, mergeData, userId);
49 
50     int32_t systemAbilityId = CommonFuzzer::GetU32Data(data);
51     plugin.OnOtherServiceStart(systemAbilityId);
52 }
53 
54 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
56 {
57     if (data == nullptr) {
58         return 0;
59     }
60     if (size < MIN_SIZE) {
61         return 0;
62     }
63     int32_t pos = 0;
64     int32_t stringSize = size / 10;
65     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
66         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
67         uint32_t code = EdmInterfaceCode::ALLOWED_USB_DEVICES;
68         code = POLICY_FUNC_CODE(operateType, code);
69 
70         AppExecFwk::ElementName admin;
71         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
72         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
73         MessageParcel parcel;
74         parcel.WriteInterfaceToken(IEnterpriseDeviceMgrIdl::GetDescriptor());
75         parcel.WriteInt32(WITHOUT_USERID);
76         if (operateType) {
77             parcel.WriteParcelable(&admin);
78             parcel.WriteInt32(USB_DEVICE_ID_SIZE);
79             UsbDeviceId usbDeviceId = GetData<UsbDeviceId>();
80             std::vector<UsbDeviceId> usbDeviceIds = { usbDeviceId };
81             std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(),
82                 [&](const auto usbDeviceId) { usbDeviceId.Marshalling(parcel); });
83         } else {
84             parcel.WriteString("");
85             parcel.WriteInt32(0);
86             parcel.WriteParcelable(&admin);
87         }
88         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
89     }
90 
91     DoSomethingInterestingWithAPI(data, size, pos, stringSize);
92     return 0;
93 }
94 } // namespace EDM
95 } // namespace OHOS