• 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_apn_plugin_fuzzer.h"
17 
18 #include <system_ability_definition.h>
19 
20 #define protected public
21 #define private public
22 #include "set_apn_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 "ienterprise_device_mgr.h"
29 #include "func_code.h"
30 #include "message_parcel.h"
31 #include "utils.h"
32 
33 namespace OHOS {
34 namespace EDM {
35 constexpr size_t MIN_SIZE = 70;
36 constexpr size_t MAX_NUM_OF_GET_STRING = 16;
37 constexpr int32_t WITHOUT_USERID = 0;
38 constexpr uint32_t SET_FLAG_FACTOR = 3;
39 constexpr uint32_t GET_FLAG_FACTOR = 2;
40 constexpr auto REQUIRED_KEYS = { "profile_name", "mcc", "mnc", "apn" };
41 constexpr uint32_t DEFAULT_INFO_SIZE = REQUIRED_KEYS.size();
42 
43 enum ApnSetFlag {
44     INSERT,
45     UPDATE,
46     SET_PREFER
47 };
48 
49 enum ApnGetFlag {
50     QUERYID,
51     QUERYINFO
52 };
53 
GenerateSetData(const uint8_t * data,int32_t & pos,int32_t stringSize,size_t size,MessageParcel & parcel)54 static void GenerateSetData(const uint8_t* data, int32_t& pos, int32_t stringSize, size_t size, MessageParcel &parcel)
55 {
56     uint32_t flag = CommonFuzzer::GetU32Data(data) % SET_FLAG_FACTOR;
57     if (flag == INSERT || flag == UPDATE) {
58         if (flag == INSERT) {
59             parcel.WriteString(EdmConstants::SetApn::ADD_FLAG);
60         } else {
61             parcel.WriteString(EdmConstants::SetApn::UPDATE_FLAG);
62             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
63         }
64         parcel.WriteInt32(DEFAULT_INFO_SIZE);
65         for (auto &ele : REQUIRED_KEYS) {
66             parcel.WriteString(ele);
67         }
68         for (uint32_t idx = 0; idx < DEFAULT_INFO_SIZE; idx++) {
69             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
70         }
71     } else {
72         if (flag == SET_PREFER) {
73             parcel.WriteString(EdmConstants::SetApn::SET_PREFER_FLAG);
74         } else {
75             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
76         }
77         parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
78     }
79 }
80 
GenerateGetData(const uint8_t * data,int32_t & pos,int32_t stringSize,size_t size,MessageParcel & parcel)81 static void GenerateGetData(const uint8_t* data, int32_t& pos, int32_t stringSize, size_t size, MessageParcel &parcel)
82 {
83     uint32_t flag = CommonFuzzer::GetU32Data(data) % GET_FLAG_FACTOR;
84     if (flag == QUERYID) {
85         parcel.WriteString(EdmConstants::SetApn::QUERY_ID_FLAG);
86         parcel.WriteInt32(DEFAULT_INFO_SIZE);
87         for (auto &ele : REQUIRED_KEYS) {
88             parcel.WriteString(ele);
89         }
90         for (uint32_t idx = 0; idx < DEFAULT_INFO_SIZE; idx++) {
91             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
92         }
93     } else {
94         if (flag == QUERYINFO) {
95             parcel.WriteString(EdmConstants::SetApn::QUERY_INFO_FLAG);
96         } else {
97             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
98         }
99         parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
100     }
101 }
102 
103 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106     if (data == nullptr) {
107         return 0;
108     }
109     if (size < MIN_SIZE) {
110         return 0;
111     }
112     int32_t pos = 0;
113     int32_t stringSize = size / MAX_NUM_OF_GET_STRING;
114     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
115         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
116         uint32_t code = EdmInterfaceCode::SET_APN_INFO;
117         code = POLICY_FUNC_CODE(operateType, code);
118 
119         AppExecFwk::ElementName admin;
120         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
121         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
122         MessageParcel parcel;
123         parcel.WriteInterfaceToken(IEnterpriseDeviceMgrIdl::GetDescriptor());
124         parcel.WriteInt32(WITHOUT_USERID);
125         if (operateType == static_cast<uint32_t>(FuncOperateType::GET)) {
126             parcel.WriteParcelable(&admin);
127             GenerateGetData(data, pos, stringSize, size, parcel);
128         } else if (operateType == static_cast<uint32_t>(FuncOperateType::SET)) {
129             parcel.WriteParcelable(&admin);
130             GenerateSetData(data, pos, stringSize, size, parcel);
131         } else if (operateType == static_cast<uint32_t>(FuncOperateType::REMOVE)) {
132             parcel.WriteParcelable(&admin);
133             parcel.WriteString(CommonFuzzer::GetString(data, pos, stringSize, size));
134         } else {
135             parcel.WriteString("");
136             parcel.WriteInt32(0);
137             parcel.WriteParcelable(&admin);
138         }
139         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
140     }
141     return 0;
142 }
143 } // namespace EDM
144 } // namespace OHOS