• 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 "hcamera_host_manager_fuzzer.h"
17 
18 #include "camera_log.h"
19 #include "message_parcel.h"
20 #include "token_setproc.h"
21 #include "nativetoken_kit.h"
22 #include "securec.h"
23 #include "portrait_session.h"
24 #include "os_account_manager.h"
25 #include "output/sketch_wrapper.h"
26 #include "hcamera_service.h"
27 #include "ipc_skeleton.h"
28 
29 using namespace OHOS::CameraStandard::DeferredProcessing;
30 namespace OHOS {
31 namespace CameraStandard {
32 using OHOS::HDI::Camera::V1_0::ICameraDeviceCallback;
33 static constexpr int32_t MAX_CODE_LEN = 512;
34 static constexpr int32_t MIN_SIZE_NUM = 4;
35 static const uint8_t* RAW_DATA = nullptr;
36 const size_t THRESHOLD = 10;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39 std::shared_ptr<HCameraHostManager> HCameraHostManagerFuzzer::fuzz_{nullptr};
40 std::shared_ptr<HCameraHostManager::CameraHostInfo> HCameraHostManagerFuzzer::hostInfo{nullptr};
41 
42 /*
43 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
44 * tips: only support basic type
45 */
46 template<class T>
GetData()47 T GetData()
48 {
49     T object {};
50     size_t objectSize = sizeof(object);
51     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
52         return object;
53     }
54     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
55     if (ret != EOK) {
56         return {};
57     }
58     g_pos += objectSize;
59     return object;
60 }
61 
62 template<class T>
GetArrLength(T & arr)63 uint32_t GetArrLength(T& arr)
64 {
65     if (arr == nullptr) {
66         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
67         return 0;
68     }
69     return sizeof(arr) / sizeof(arr[0]);
70 }
71 
HCameraHostManagerFuzzTest1()72 void HCameraHostManagerFuzzer::HCameraHostManagerFuzzTest1()
73 {
74     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
75         return;
76     }
77     wptr<HCameraHostManager> hostManager =nullptr;
78     std::shared_ptr<HCameraHostManager::StatusCallback> statusCallback = std::make_shared<StatusCallbackDemo>();
79     fuzz_ = std::make_shared<HCameraHostManager>(statusCallback);
80     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
81     uint8_t randomNum = GetData<uint8_t>();
82     std::vector<std::string> testStrings = {"test1", "test2"};
83     std::string cameraId(testStrings[randomNum % testStrings.size()]);
84     bool isEnable = GetData<bool>();
85     std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
86     fuzz_->GetCameraAbility(cameraId, ability);
87     fuzz_->SetFlashlight(cameraId, isEnable);
88 }
89 
Test()90 void Test()
91 {
92     auto hCameraHostManagerFuzzer = std::make_unique<HCameraHostManagerFuzzer>();
93     if (hCameraHostManagerFuzzer == nullptr) {
94         MEDIA_INFO_LOG("hCameraHostManagerFuzzer is null");
95         return;
96     }
97     hCameraHostManagerFuzzer->HCameraHostManagerFuzzTest1();
98 }
99 
100 typedef void (*TestFuncs[1])();
101 
102 TestFuncs g_testFuncs = {
103     Test,
104 };
105 
FuzzTest(const uint8_t * rawData,size_t size)106 bool FuzzTest(const uint8_t* rawData, size_t size)
107 {
108     // initialize data
109     RAW_DATA = rawData;
110     g_dataSize = size;
111     g_pos = 0;
112 
113     uint32_t code = GetData<uint32_t>();
114     uint32_t len = GetArrLength(g_testFuncs);
115     if (len > 0) {
116         g_testFuncs[code % len]();
117     } else {
118         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
119     }
120 
121     return true;
122 }
123 } // namespace CameraStandard
124 } // namespace OHOS
125 
126 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)127 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
128 {
129     if (size < OHOS::CameraStandard::THRESHOLD) {
130         return 0;
131     }
132 
133     OHOS::CameraStandard::FuzzTest(data, size);
134     return 0;
135 }