• 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 "set_browser_policies_plugin_fuzzer.h"
17 
18 #include <system_ability_definition.h>
19 
20 #include "common_fuzzer.h"
21 #include "edm_constants.h"
22 #include "edm_ipc_interface_code.h"
23 #include "ienterprise_device_mgr.h"
24 #include "func_code.h"
25 #include "message_parcel.h"
26 #include "utils.h"
27 
28 namespace OHOS {
29 namespace EDM {
30 constexpr size_t MIN_SIZE = 16;
31 constexpr size_t WITHOUT_USERID = 0;
32 constexpr int32_t HAS_ADMIN = 0;
33 constexpr int32_t WITHOUT_ADMIN = 1;
34 
SetParcelContent(MessageParcel & parcel,uint32_t operateType,const uint8_t * data,size_t size,AppExecFwk::ElementName admin)35 void SetParcelContent(MessageParcel &parcel, uint32_t operateType,
36     const uint8_t* data, size_t size, AppExecFwk::ElementName admin)
37 {
38     parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor());
39     parcel.WriteInt32(WITHOUT_USERID);
40     if (operateType) {
41         parcel.WriteParcelable(&admin);
42         bool isSetAll = CommonFuzzer::GetU32Data(data) % 2;
43         int32_t pos = 0;
44         int32_t stringSize = size / 3;
45         std::string appId(CommonFuzzer::GetString(data, pos, stringSize, size));
46         std::string policies(CommonFuzzer::GetString(data, pos, stringSize, size));
47         if (isSetAll) {
48             parcel.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
49             std::vector<std::string> appIds;
50             std::vector<std::string> policiesList;
51             appIds.push_back(appId);
52             policiesList.push_back(policies);
53             parcel.WriteStringVector(appIds);
54             parcel.WriteStringVector(policiesList);
55         } else {
56             parcel.WriteString("");
57             parcel.WriteInt32(EdmConstants::SET_POLICY_TYPE);
58             std::string policyName(CommonFuzzer::GetString(data, pos, stringSize, size));
59             std::vector<std::string> params;
60             params.push_back(appId);
61             params.push_back(policyName);
62             params.push_back(policies);
63             parcel.WriteStringVector(params);
64         }
65     } else {
66         parcel.WriteString("");
67         bool hasAdmin = CommonFuzzer::GetU32Data(data) % 2;
68         if (hasAdmin) {
69             parcel.WriteInt32(HAS_ADMIN);
70             parcel.WriteParcelable(&admin);
71         } else {
72             parcel.WriteInt32(WITHOUT_ADMIN);
73         }
74         std::string appId(reinterpret_cast<const char*>(data), size);
75         parcel.WriteString(appId);
76     }
77 }
78 
79 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82     if (data == nullptr) {
83         return 0;
84     }
85     if (size < MIN_SIZE) {
86         return 0;
87     }
88     int32_t pos = 0;
89     int32_t stringSize = (size - pos) / 6;
90     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
91         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
92         uint32_t code = EdmInterfaceCode::SET_BROWSER_POLICIES;
93         code = POLICY_FUNC_CODE(operateType, code);
94 
95         AppExecFwk::ElementName admin;
96         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
97         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
98         MessageParcel parcel;
99         SetParcelContent(parcel, operateType, data, size, admin);
100 
101         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
102     }
103     return 0;
104 }
105 } // namespace EDM
106 } // namespace OHOS