• 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_proxy_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "accesstoken_kit.h"
25 #include "securec.h"
26 #include "system_ability_definition.h"
27 #include "iservice_registry.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 
38 std::shared_ptr<HStreamDepthDataCallbackProxy> HStreamDepthDataCallbackProxyFuzzer::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 
HStreamDepthDataCallbackProxyFuzzTest()70 void HStreamDepthDataCallbackProxyFuzzer::HStreamDepthDataCallbackProxyFuzzTest()
71 {
72     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
73         return;
74     }
75     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (samgr == nullptr) {
77         return;
78     }
79     sptr<IRemoteObject> impl = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
80     if (impl == nullptr) {
81         return;
82     }
83     fuzz_ = std::make_shared<HStreamDepthDataCallbackProxy>(impl);
84     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
85 }
86 
Test()87 void Test()
88 {
89     auto hstreamDepthDataCallbackProxy = std::make_unique<HStreamDepthDataCallbackProxyFuzzer>();
90     if (hstreamDepthDataCallbackProxy == nullptr) {
91         MEDIA_INFO_LOG("hstreamDepthDataCallbackProxy is null");
92         return;
93     }
94     hstreamDepthDataCallbackProxy->HStreamDepthDataCallbackProxyFuzzTest();
95 }
96 
97 typedef void (*TestFuncs[1])();
98 
99 TestFuncs g_testFuncs = {
100     Test,
101 };
102 
FuzzTest(const uint8_t * rawData,size_t size)103 bool FuzzTest(const uint8_t* rawData, size_t size)
104 {
105     // initialize data
106     RAW_DATA = rawData;
107     g_dataSize = size;
108     g_pos = 0;
109 
110     uint32_t code = GetData<uint32_t>();
111     uint32_t len = GetArrLength(g_testFuncs);
112     if (len > 0) {
113         g_testFuncs[code % len]();
114     } else {
115         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
116     }
117 
118     return true;
119 }
120 } // namespace CameraStandard
121 } // namespace OHOS
122 
123 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)124 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
125 {
126     if (size < OHOS::CameraStandard::THRESHOLD) {
127         return 0;
128     }
129 
130     OHOS::CameraStandard::FuzzTest(data, size);
131     return 0;
132 }