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_metadata_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 const uint8_t* RAW_DATA = nullptr;
33 const size_t THRESHOLD = 10;
34 static size_t g_dataSize = 0;
35 static size_t g_pos;
36 static constexpr int32_t MAX_CODE_NUM = 1;
37 std::shared_ptr<HStreamMetadataCallbackStubFuzz> HStreamMetadataCallbackStubFuzzer::fuzz_{nullptr};
38
39 /*
40 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
41 * tips: only support basic type
42 */
43 template<class T>
GetData()44 T GetData()
45 {
46 T object {};
47 size_t objectSize = sizeof(object);
48 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
49 return object;
50 }
51 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
52 if (ret != EOK) {
53 return {};
54 }
55 g_pos += objectSize;
56 return object;
57 }
58
59 template<class T>
GetArrLength(T & arr)60 uint32_t GetArrLength(T& arr)
61 {
62 if (arr == nullptr) {
63 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
64 return 0;
65 }
66 return sizeof(arr) / sizeof(arr[0]);
67 }
68
OnRemoteRequest(int32_t code)69 void HStreamMetadataCallbackStubFuzzer::OnRemoteRequest(int32_t code)
70 {
71 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
72 return;
73 }
74 fuzz_ = std::make_shared<HStreamMetadataCallbackStubFuzz>();
75 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
76 MessageParcel reply;
77 MessageOption option;
78 MessageParcel dataMessageParcel;
79 dataMessageParcel.WriteInterfaceToken(HStreamMetadataCallbackStubFuzz::GetDescriptor());
80 dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
81 dataMessageParcel.RewindRead(0);
82 fuzz_->OnRemoteRequest(code, dataMessageParcel, reply, option);
83 fuzz_->HandleMetadataResult(dataMessageParcel);
84 }
85
Test()86 void Test()
87 {
88 auto hstreamMetadataCallbackStub = std::make_unique<HStreamMetadataCallbackStubFuzzer>();
89 if (hstreamMetadataCallbackStub == nullptr) {
90 MEDIA_INFO_LOG("hstreamMetadataCallbackStub is null");
91 return;
92 }
93 for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
94 hstreamMetadataCallbackStub->OnRemoteRequest(i);
95 }
96 }
97
98 typedef void (*TestFuncs[1])();
99
100 TestFuncs g_testFuncs = {
101 Test,
102 };
103
FuzzTest(const uint8_t * rawData,size_t size)104 bool FuzzTest(const uint8_t* rawData, size_t size)
105 {
106 // initialize data
107 RAW_DATA = rawData;
108 g_dataSize = size;
109 g_pos = 0;
110
111 uint32_t code = GetData<uint32_t>();
112 uint32_t len = GetArrLength(g_testFuncs);
113 if (len > 0) {
114 g_testFuncs[code % len]();
115 } else {
116 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
117 }
118
119 return true;
120 }
121 } // namespace CameraStandard
122 } // namespace OHOS
123
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
126 {
127 if (size < OHOS::CameraStandard::THRESHOLD) {
128 return 0;
129 }
130
131 OHOS::CameraStandard::FuzzTest(data, size);
132 return 0;
133 }