1 /*
2 * Copyright (c) 2025 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 "slow_motion_session_fuzzer.h"
17 #include "camera_log.h"
18 #include "camera_output_capability.h"
19 #include "input/camera_manager.h"
20 #include "message_parcel.h"
21 #include "time_lapse_photo_session.h"
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "accesstoken_kit.h"
25 #include "securec.h"
26 #include "ipc_skeleton.h"
27 #include "os_account_manager.h"
28
29
30 namespace OHOS {
31 namespace CameraStandard {
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 const int32_t DEFAULT_ITEMS = 10;
37 const int32_t DEFAULT_DATA_LENGTH = 100;
38 static size_t g_dataSize = 0;
39 static size_t g_pos;
40 sptr<CameraManager> manager;
41 std::vector<Profile> previewProfile_ = {};
42 std::vector<VideoProfile> videoProfile_;
43
GetPermission()44 void GetPermission()
45 {
46 uint64_t tokenId;
47 const char *perms[2];
48 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
49 perms[1] = "ohos.permission.CAMERA";
50 NativeTokenInfoParams infoInstance = {
51 .dcapsNum = 0,
52 .permsNum = 2,
53 .aclsNum = 0,
54 .dcaps = NULL,
55 .perms = perms,
56 .acls = NULL,
57 .processName = "native_camera_tdd",
58 .aplStr = "system_basic",
59 };
60 tokenId = GetAccessTokenId(&infoInstance);
61 SetSelfTokenID(tokenId);
62 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
63 }
64
CreatePreviewOutput()65 sptr<CaptureOutput> CreatePreviewOutput()
66 {
67 previewProfile_ = {};
68 std::vector<sptr<CameraDevice>> cameras = manager->GetCameraDeviceListFromServer();
69 CHECK_ERROR_RETURN_RET(cameras.empty(), nullptr);
70 auto outputCapability = manager->GetSupportedOutputCapability(cameras[0],
71 static_cast<int32_t>(SceneMode::SLOW_MOTION));
72 previewProfile_ = outputCapability->GetPreviewProfiles();
73 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
74 if (surface == nullptr) {
75 return nullptr;
76 }
77 return manager->CreatePreviewOutput(previewProfile_[0], surface);
78 }
79
CreateVideoOutput()80 sptr<CaptureOutput> CreateVideoOutput()
81 {
82 videoProfile_ = {};
83 std::vector<sptr<CameraDevice>> cameras = manager->GetCameraDeviceListFromServer();
84 CHECK_ERROR_RETURN_RET(cameras.empty(), nullptr);
85 auto outputCapability = manager->GetSupportedOutputCapability(cameras[0],
86 static_cast<int32_t>(SceneMode::SLOW_MOTION));
87 videoProfile_ = outputCapability->GetVideoProfiles();
88 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
89 if (surface == nullptr) {
90 return nullptr;
91 }
92 return manager->CreateVideoOutput(videoProfile_[0], surface);
93 }
94
95 /*
96 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
97 * tips: only support basic type
98 */
99 template<class T>
GetData()100 T GetData()
101 {
102 T object {};
103 size_t objectSize = sizeof(object);
104 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
105 return object;
106 }
107 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
108 if (ret != EOK) {
109 return {};
110 }
111 g_pos += objectSize;
112 return object;
113 }
114
115 template<class T>
GetArrLength(T & arr)116 uint32_t GetArrLength(T& arr)
117 {
118 if (arr == nullptr) {
119 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
120 return 0;
121 }
122 return sizeof(arr) / sizeof(arr[0]);
123 }
124
SlowMotionSessionFuzzTest()125 void SlowMotionSessionFuzzer::SlowMotionSessionFuzzTest()
126 {
127 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
128 return;
129 }
130 GetPermission();
131 manager = CameraManager::GetInstance();
132 sptr<CaptureSession> captureSession = manager->CreateCaptureSession(SceneMode::SLOW_MOTION);
133 std::vector<sptr<CameraDevice>> cameras;
134 cameras = manager->GetCameraDeviceListFromServer();
135 CHECK_ERROR_RETURN_LOG(cameras.empty(), "SlowMotionSessionFuzzer: GetCameraDeviceListFromServer Error");
136 sptr<CaptureInput> input = manager->CreateCameraInput(cameras[0]);
137 CHECK_ERROR_RETURN_LOG(!input, "CreateCameraInput Error");
138 input->Open();
139 sptr<CaptureOutput> videoOutput = CreateVideoOutput();
140 sptr<CaptureOutput> previewOutput = CreatePreviewOutput();
141 captureSession->BeginConfig();
142 captureSession->AddInput(input);
143 sptr<CameraDevice> info = captureSession->innerInputDevice_->GetCameraDeviceInfo();
144 info->modePreviewProfiles_.emplace(static_cast<int32_t>(SceneMode::SLOW_MOTION), previewProfile_);
145 info->modeVideoProfiles_.emplace(static_cast<int32_t>(SceneMode::SLOW_MOTION), videoProfile_);
146 captureSession->AddOutput(previewOutput);
147 captureSession->AddOutput(videoOutput);
148 captureSession->CommitConfig();
149 input->Release();
150 sptr<SlowMotionSession> fuzz_ = static_cast<SlowMotionSession*>(captureSession.GetRefPtr());
151 if (fuzz_ == nullptr) {
152 return;
153 }
154 fuzz_->IsSlowMotionDetectionSupported();
155 Rect rect = {0, 0, 0, 0};
156 fuzz_->NormalizeRect(rect);
157 fuzz_->SetSlowMotionDetectionArea(rect);
158 std::shared_ptr<OHOS::Camera::CameraMetadata> result =
159 std::make_shared<OHOS::Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH);
160 SlowMotionSession::SlowMotionSessionMetadataResultProcessor processor(fuzz_);
161 uint64_t timestamp = 1;
162 auto metadata = make_shared<OHOS::Camera::CameraMetadata>(10, 100);
163 processor.ProcessCallbacks(timestamp, metadata);
164 std::shared_ptr<SlowMotionStateCallback> callback = std::make_shared<TestSlowMotionStateCallback>();
165 fuzz_->SetCallback(callback);
166 fuzz_->OnSlowMotionStateChange(metadata);
167 fuzz_->GetApplicationCallback();
168 bool isEnable = GetData<bool>();
169 fuzz_->LockForControl();
170 fuzz_->EnableMotionDetection(isEnable);
171 }
172
Test()173 void Test()
174 {
175 auto slowMotionSession = std::make_unique<SlowMotionSessionFuzzer>();
176 if (slowMotionSession == nullptr) {
177 MEDIA_INFO_LOG("slowMotionSession is null");
178 return;
179 }
180 slowMotionSession->SlowMotionSessionFuzzTest();
181 }
182
183 typedef void (*TestFuncs[1])();
184
185 TestFuncs g_testFuncs = {
186 Test,
187 };
188
FuzzTest(const uint8_t * rawData,size_t size)189 bool FuzzTest(const uint8_t* rawData, size_t size)
190 {
191 // initialize data
192 RAW_DATA = rawData;
193 g_dataSize = size;
194 g_pos = 0;
195
196 uint32_t code = GetData<uint32_t>();
197 uint32_t len = GetArrLength(g_testFuncs);
198 if (len > 0) {
199 g_testFuncs[code % len]();
200 } else {
201 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
202 }
203
204 return true;
205 }
206 } // namespace CameraStandard
207 } // namespace OHOS
208
209 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)210 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
211 {
212 if (size < OHOS::CameraStandard::THRESHOLD) {
213 return 0;
214 }
215
216 OHOS::CameraStandard::FuzzTest(data, size);
217 return 0;
218 }