• 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 "hcamera_service_callback_stub_fuzzer.h"
17 #include "camera_log.h"
18 #include "hstream_capture.h"
19 #include "iservice_registry.h"
20 #include "message_parcel.h"
21 #include "accesstoken_kit.h"
22 #include "camera_metadata_info.h"
23 #include "metadata_utils.h"
24 #include "iconsumer_surface.h"
25 #include "camera_service_ipc_interface_code.h"
26 #include "securec.h"
27 #include <memory>
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 
32 static constexpr int32_t MAX_CODE_LEN = 512;
33 static constexpr int32_t MIN_SIZE_NUM = 4;
34 static constexpr int32_t MAX_CODE_NUM = 1;
35 static const uint8_t* RAW_DATA = nullptr;
36 const size_t THRESHOLD = 10;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39 
40 /*
41 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
42 * tips: only support basic type
43 */
44 template<class T>
GetData()45 T GetData()
46 {
47     T object {};
48     size_t objectSize = sizeof(object);
49     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
50         return object;
51     }
52     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
53     if (ret != EOK) {
54         return {};
55     }
56     g_pos += objectSize;
57     return object;
58 }
59 
60 template<class T>
GetArrLength(T & arr)61 uint32_t GetArrLength(T& arr)
62 {
63     if (arr == nullptr) {
64         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
65         return 0;
66     }
67     return sizeof(arr) / sizeof(arr[0]);
68 }
69 
HCameraServiceCallbackStubFuzzTest(int32_t code)70 void HCameraServiceCallbackStubFuzzer::HCameraServiceCallbackStubFuzzTest(int32_t code)
71 {
72     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
73         return;
74     }
75     std::shared_ptr<HCameraServiceCallbackStubFuzz> fuzz = std::make_shared<HCameraServiceCallbackStubFuzz>();
76     if (fuzz == nullptr) {
77         return;
78     }
79     MessageParcel reply;
80     MessageOption option;
81     MessageParcel dataMessageParcel;
82     dataMessageParcel.WriteInterfaceToken(HCameraServiceCallbackStubFuzz::GetDescriptor());
83     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
84     dataMessageParcel.RewindRead(0);
85     fuzz->OnRemoteRequest(code, dataMessageParcel, reply, option);
86     fuzz->HandleOnCameraStatusChanged(dataMessageParcel);
87     fuzz->HandleOnFlashlightStatusChanged(dataMessageParcel);
88 }
89 
HCameraMuteServiceCallbackStuFuzzTest(int32_t code)90 void HCameraServiceCallbackStubFuzzer::HCameraMuteServiceCallbackStuFuzzTest(int32_t code)
91 {
92     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
93         return;
94     }
95     std::shared_ptr<HCameraMuteServiceCallbackStubFuzz> fuzz =
96         std::make_shared<HCameraMuteServiceCallbackStubFuzz>();
97     if (fuzz == nullptr) {
98         return;
99     }
100     MessageParcel reply;
101     MessageOption option;
102     MessageParcel dataMessageParcel;
103     dataMessageParcel.WriteInterfaceToken(HCameraMuteServiceCallbackStubFuzz::GetDescriptor());
104     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
105     dataMessageParcel.RewindRead(0);
106     fuzz->OnRemoteRequest(code, dataMessageParcel, reply, option);
107     fuzz->HandleOnCameraMute(dataMessageParcel);
108 }
109 
HTorchServiceCallbackStubFuzzTest(int32_t code)110 void HCameraServiceCallbackStubFuzzer::HTorchServiceCallbackStubFuzzTest(int32_t code)
111 {
112     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
113         return;
114     }
115     std::shared_ptr<HTorchServiceCallbackStubFuzz> fuzz =
116         std::make_shared<HTorchServiceCallbackStubFuzz>();
117     if (fuzz == nullptr) {
118         return;
119     }
120     MessageParcel reply;
121     MessageOption option;
122     MessageParcel dataMessageParcel;
123     dataMessageParcel.WriteInterfaceToken(HTorchServiceCallbackStubFuzz::GetDescriptor());
124     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
125     dataMessageParcel.RewindRead(0);
126     fuzz->OnRemoteRequest(code, dataMessageParcel, reply, option);
127     fuzz->HandleOnTorchStatusChange(dataMessageParcel);
128 }
129 
HFoldServiceCallbackStubFuzzTest(int32_t code)130 void HCameraServiceCallbackStubFuzzer::HFoldServiceCallbackStubFuzzTest(int32_t code)
131 {
132     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
133         return;
134     }
135     std::shared_ptr<HFoldServiceCallbackStubFuzz> fuzz =
136         std::make_shared<HFoldServiceCallbackStubFuzz>();
137     if (fuzz == nullptr) {
138         return;
139     }
140     MessageParcel reply;
141     MessageOption option;
142     MessageParcel dataMessageParcel;
143     dataMessageParcel.WriteInterfaceToken(HFoldServiceCallbackStubFuzz::GetDescriptor());
144     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
145     dataMessageParcel.RewindRead(0);
146     fuzz->OnRemoteRequest(code, dataMessageParcel, reply, option);
147     fuzz->HandleOnFoldStatusChanged(dataMessageParcel);
148 }
149 
Test()150 void Test()
151 {
152     auto hCameraServiceCallbackStub = std::make_unique<HCameraServiceCallbackStubFuzzer>();
153     if (hCameraServiceCallbackStub == nullptr) {
154         MEDIA_INFO_LOG("hCameraServiceCallbackStub is null");
155         return;
156     }
157     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
158         hCameraServiceCallbackStub->HCameraServiceCallbackStubFuzzTest(i);
159     }
160     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
161         hCameraServiceCallbackStub->HCameraMuteServiceCallbackStuFuzzTest(i);
162     }
163     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
164         hCameraServiceCallbackStub->HTorchServiceCallbackStubFuzzTest(i);
165     }
166     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
167         hCameraServiceCallbackStub->HFoldServiceCallbackStubFuzzTest(i);
168     }
169 }
170 
171 typedef void (*TestFuncs[1])();
172 
173 TestFuncs g_testFuncs = {
174     Test,
175 };
176 
FuzzTest(const uint8_t * rawData,size_t size)177 bool FuzzTest(const uint8_t* rawData, size_t size)
178 {
179     // initialize data
180     RAW_DATA = rawData;
181     g_dataSize = size;
182     g_pos = 0;
183 
184     uint32_t code = GetData<uint32_t>();
185     uint32_t len = GetArrLength(g_testFuncs);
186     if (len > 0) {
187         g_testFuncs[code % len]();
188     } else {
189         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
190     }
191 
192     return true;
193 }
194 } // namespace CameraStandard
195 } // namespace OHOS
196 
197 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)198 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
199 {
200     if (size < OHOS::CameraStandard::THRESHOLD) {
201         return 0;
202     }
203 
204     OHOS::CameraStandard::FuzzTest(data, size);
205     return 0;
206 }