• 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 "hstream_depth_data_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 
28 namespace OHOS {
29 namespace CameraStandard {
30 static constexpr int32_t MAX_CODE_LEN = 512;
31 static constexpr int32_t MIN_SIZE_NUM = 4;
32 static constexpr int32_t MAX_CODE_NUM = 1;
33 static const uint8_t* RAW_DATA = nullptr;
34 const size_t THRESHOLD = 10;
35 static size_t g_dataSize = 0;
36 static size_t g_pos;
37 
38 std::shared_ptr<HStreamDepthDataCallbackFuzz> HStreamDepthDataCallbackStubFuzzer::fuzz_{nullptr};
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 
OnRemoteRequest(int32_t code)70 void HStreamDepthDataCallbackStubFuzzer::OnRemoteRequest(int32_t code)
71 {
72     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
73         return;
74     }
75     fuzz_ = std::make_shared<HStreamDepthDataCallbackFuzz>();
76     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
77     MessageParcel reply;
78     MessageOption option;
79     MessageParcel dataMessageParcel;
80     dataMessageParcel.WriteInterfaceToken(HStreamDepthDataCallbackFuzz::GetDescriptor());
81     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
82     dataMessageParcel.RewindRead(0);
83     fuzz_->OnRemoteRequest(code, dataMessageParcel, reply, option);
84     fuzz_->HandleOnDepthDataError(dataMessageParcel);
85 }
86 
Test()87 void Test()
88 {
89     auto hstreamDepthDataCallbackStub = std::make_unique<HStreamDepthDataCallbackStubFuzzer>();
90     if (hstreamDepthDataCallbackStub == nullptr) {
91         MEDIA_INFO_LOG("hstreamDepthDataCallbackStub is null");
92         return;
93     }
94     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
95         hstreamDepthDataCallbackStub->OnRemoteRequest(i);
96     }
97 }
98 
99 typedef void (*TestFuncs[1])();
100 
101 TestFuncs g_testFuncs = {
102     Test,
103 };
104 
FuzzTest(const uint8_t * rawData,size_t size)105 bool FuzzTest(const uint8_t* rawData, size_t size)
106 {
107     // initialize data
108     RAW_DATA = rawData;
109     g_dataSize = size;
110     g_pos = 0;
111 
112     uint32_t code = GetData<uint32_t>();
113     uint32_t len = GetArrLength(g_testFuncs);
114     if (len > 0) {
115         g_testFuncs[code % len]();
116     } else {
117         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
118     }
119 
120     return true;
121 }
122 } // namespace CameraStandard
123 } // namespace OHOS
124 
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
127 {
128     if (size < OHOS::CameraStandard::THRESHOLD) {
129         return 0;
130     }
131 
132     OHOS::CameraStandard::FuzzTest(data, size);
133     return 0;
134 }