• 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_device_manager_fuzzer.h"
17 
18 #include "camera_log.h"
19 #include "message_parcel.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include "token_setproc.h"
24 #include "nativetoken_kit.h"
25 #include "accesstoken_kit.h"
26 #include "securec.h"
27 #include "ipc_skeleton.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 using namespace OHOS::HDI::Camera::V1_0;
32 static constexpr int32_t MAX_CODE_LEN = 512;
33 static constexpr int32_t MIN_SIZE_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 sptr<HCameraDevice> g_HCameraDevice = nullptr;
39 std::string g_cameraID;
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 
InitCameraDevice()71 void InitCameraDevice()
72 {
73     if (g_HCameraDevice != nullptr) {
74         return;
75     }
76     sptr<HCameraHostManager> cameraHostManager = new HCameraHostManager(nullptr);
77     std::vector<std::string> cameraIds;
78     cameraHostManager->GetCameras(cameraIds);
79     CHECK_ERROR_RETURN_LOG(cameraIds.empty(), "Fuzz:PrepareHCameraDevice: GetCameras returns empty");
80     g_cameraID = cameraIds[0];
81     auto callingTokenId = IPCSkeleton::GetCallingTokenID();
82     std::string permissionName = OHOS_PERMISSION_CAMERA;
83     int32_t ret = CheckPermission(permissionName, callingTokenId);
84     CHECK_ERROR_RETURN_LOG(ret != CAMERA_OK, "Fuzz:PrepareHCameraDevice: CheckPermission Failed");
85     g_HCameraDevice = new HCameraDevice(cameraHostManager, g_cameraID, callingTokenId);
86 }
87 
HCameraDeviceManagerFuzzTest1()88 void HCameraDeviceManagerFuzzer::HCameraDeviceManagerFuzzTest1()
89 {
90     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
91         return;
92     }
93     auto hCameraDeviceManager = HCameraDeviceManager::GetInstance();
94     if (hCameraDeviceManager == nullptr) {
95         MEDIA_INFO_LOG("hCameraDeviceManager is null");
96         return;
97     };
98     InitCameraDevice();
99     pid_t pid = GetData<int32_t>();
100     hCameraDeviceManager->GetCameraHolderByPid(pid);
101     hCameraDeviceManager->GetCamerasByPid(pid);
102     hCameraDeviceManager->GetActiveClient();
103     hCameraDeviceManager->GetActiveCameraHolders();
104     int32_t state = GetData<int32_t>();
105     hCameraDeviceManager->SetStateOfACamera(g_cameraID, state);
106     hCameraDeviceManager->GetCameraStateOfASide();
107     sptr<ICameraBroker> callback = nullptr;
108     hCameraDeviceManager->SetPeerCallback(callback);
109     hCameraDeviceManager->UnsetPeerCallback();
110     std::vector<sptr<HCameraDevice>> cameraNeedEvict = {};
111     int32_t type = 0;
112     hCameraDeviceManager->GetConflictDevices(cameraNeedEvict, g_HCameraDevice, type);
113     hCameraDeviceManager->RemoveDevice(g_cameraID);
114     int32_t processState = GetData<int32_t>();
115     hCameraDeviceManager->GenerateEachProcessCameraState(processState, GetData<uint32_t>());
116     hCameraDeviceManager->IsMultiCameraActive(pid);
117 }
118 
Test()119 void Test()
120 {
121     auto hCameraDeviceManagerFuzzer = std::make_unique<HCameraDeviceManagerFuzzer>();
122     if (hCameraDeviceManagerFuzzer == nullptr) {
123         MEDIA_INFO_LOG("hCameraDeviceManagerFuzzer is null");
124         return;
125     }
126     hCameraDeviceManagerFuzzer->HCameraDeviceManagerFuzzTest1();
127 }
128 
129 typedef void (*TestFuncs[1])();
130 
131 TestFuncs g_testFuncs = {
132     Test,
133 };
134 
FuzzTest(const uint8_t * rawData,size_t size)135 bool FuzzTest(const uint8_t* rawData, size_t size)
136 {
137     // initialize data
138     RAW_DATA = rawData;
139     g_dataSize = size;
140     g_pos = 0;
141 
142     uint32_t code = GetData<uint32_t>();
143     uint32_t len = GetArrLength(g_testFuncs);
144     if (len > 0) {
145         g_testFuncs[code % len]();
146     } else {
147         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
148     }
149 
150     return true;
151 }
152 } // namespace CameraStandard
153 } // namespace OHOS
154 
155 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)156 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
157 {
158     if (size < OHOS::CameraStandard::THRESHOLD) {
159         return 0;
160     }
161 
162     OHOS::CameraStandard::FuzzTest(data, size);
163     return 0;
164 }