• 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_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_depth_data_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 constexpr int32_t MAX_CODE_NUM = 4;
34 static const uint8_t* RAW_DATA = nullptr;
35 const size_t THRESHOLD = 10;
36 static size_t g_dataSize = 0;
37 static size_t g_pos;
38 
39 std::shared_ptr<HStreamDepthDataStubFuzz> HStreamDepthDataStubFuzzer::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 HStreamDepthDataStubFuzzer::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<HStreamDepthDataStubFuzz>();
77     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
78     MessageParcel reply;
79     MessageOption option;
80     MessageParcel dataMessageParcel;
81     dataMessageParcel.WriteInterfaceToken(HStreamDepthDataStubFuzz::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 
Test()87 void Test()
88 {
89     auto hstreamDepthDataStub = std::make_unique<HStreamDepthDataStubFuzzer>();
90     if (hstreamDepthDataStub == nullptr) {
91         MEDIA_INFO_LOG("hstreamDepthDataStub is null");
92         return;
93     }
94     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
95         hstreamDepthDataStub->OnRemoteRequest(i);
96     }
97     MessageParcel dataMessageParcel;
98     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
99     static const int32_t audioPolicyServiceId = 3009;
100     auto object = samgr->GetSystemAbility(audioPolicyServiceId);
101     auto proxy = std::make_shared<HStreamDepthDataCallbackProxy>(object);
102     dataMessageParcel.WriteRemoteObject(proxy->AsObject());
103     HStreamDepthDataStubFuzzer::fuzz_->HandleSetCallback(dataMessageParcel);
104     dataMessageParcel.WriteInt32(GetData<int32_t>());
105     HStreamDepthDataStubFuzzer::fuzz_->HandleSetDataAccuracy(dataMessageParcel);
106 }
107 
108 typedef void (*TestFuncs[1])();
109 
110 TestFuncs g_testFuncs = {
111     Test,
112 };
113 
FuzzTest(const uint8_t * rawData,size_t size)114 bool FuzzTest(const uint8_t* rawData, size_t size)
115 {
116     // initialize data
117     RAW_DATA = rawData;
118     g_dataSize = size;
119     g_pos = 0;
120 
121     uint32_t code = GetData<uint32_t>();
122     uint32_t len = GetArrLength(g_testFuncs);
123     if (len > 0) {
124         g_testFuncs[code % len]();
125     } else {
126         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
127     }
128 
129     return true;
130 }
131 } // namespace CameraStandard
132 } // namespace OHOS
133 
134 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)135 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
136 {
137     if (size < OHOS::CameraStandard::THRESHOLD) {
138         return 0;
139     }
140 
141     OHOS::CameraStandard::FuzzTest(data, size);
142     return 0;
143 }