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