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 "portrait_session_fuzzer.h"
17 #include "access_token.h"
18 #include "camera_log.h"
19 #include "camera_output_capability.h"
20 #include "input/camera_manager.h"
21 #include "message_parcel.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 <cstdint>
28 #include <vector>
29 #include "camera_util.h"
30 #include "hap_token_info.h"
31 #include "metadata_utils.h"
32 #include "surface.h"
33 #include "os_account_manager.h"
34
35 namespace OHOS {
36 namespace CameraStandard {
37 static constexpr int32_t MAX_CODE_LEN = 512;
38 static constexpr int32_t MIN_SIZE_NUM = 4;
39 static const uint8_t* RAW_DATA = nullptr;
40 const size_t THRESHOLD = 10;
41 static size_t g_dataSize = 0;
42 static size_t g_pos;
43 sptr<CameraManager> cameraManager_ = nullptr;
44 std::vector<Profile> previewProfile_ = {};
45 std::vector<Profile> photoProfile_ = {};
46 bool g_preIsSupportedPortraitmode = false;
47 bool g_phoIsSupportedPortraitmode = false;
48
49 /*
50 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
51 * tips: only support basic type
52 */
53 template<class T>
GetData()54 T GetData()
55 {
56 T object {};
57 size_t objectSize = sizeof(object);
58 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
59 return object;
60 }
61 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
62 if (ret != EOK) {
63 return {};
64 }
65 g_pos += objectSize;
66 return object;
67 }
68
69 template<class T>
GetArrLength(T & arr)70 uint32_t GetArrLength(T& arr)
71 {
72 if (arr == nullptr) {
73 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
74 return 0;
75 }
76 return sizeof(arr) / sizeof(arr[0]);
77 }
78
CreatePreviewOutput()79 sptr<CaptureOutput> CreatePreviewOutput()
80 {
81 previewProfile_ = {};
82 std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
83 if (!cameraManager_ || cameras.empty()) {
84 return nullptr;
85 }
86 g_preIsSupportedPortraitmode = false;
87 for (sptr<CameraDevice> camDevice : cameras) {
88 std::vector<SceneMode> modes = cameraManager_->GetSupportedModes(camDevice);
89 if (find(modes.begin(), modes.end(), SceneMode::PORTRAIT) != modes.end()) {
90 g_preIsSupportedPortraitmode = true;
91 }
92
93 if (!g_preIsSupportedPortraitmode) {
94 continue;
95 }
96
97 auto outputCapability = cameraManager_->GetSupportedOutputCapability(camDevice, static_cast<int32_t>(PORTRAIT));
98 if (!outputCapability) {
99 return nullptr;
100 }
101
102 previewProfile_ = outputCapability->GetPreviewProfiles();
103 if (previewProfile_.empty()) {
104 return nullptr;
105 }
106
107 sptr<Surface> surface = Surface::CreateSurfaceAsConsumer();
108 if (surface == nullptr) {
109 return nullptr;
110 }
111 return cameraManager_->CreatePreviewOutput(previewProfile_[0], surface);
112 }
113 return nullptr;
114 }
115
CreatePhotoOutput()116 sptr<CaptureOutput> CreatePhotoOutput()
117 {
118 photoProfile_ = {};
119 std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
120 if (!cameraManager_ || cameras.empty()) {
121 return nullptr;
122 }
123 g_phoIsSupportedPortraitmode = false;
124 for (sptr<CameraDevice> camDevice : cameras) {
125 std::vector<SceneMode> modes = cameraManager_->GetSupportedModes(camDevice);
126 if (find(modes.begin(), modes.end(), SceneMode::PORTRAIT) != modes.end()) {
127 g_phoIsSupportedPortraitmode = true;
128 }
129
130 if (!g_phoIsSupportedPortraitmode) {
131 continue;
132 }
133
134 auto outputCapability = cameraManager_->GetSupportedOutputCapability(camDevice, static_cast<int32_t>(PORTRAIT));
135 if (!outputCapability) {
136 return nullptr;
137 }
138
139 photoProfile_ = outputCapability->GetPhotoProfiles();
140 if (photoProfile_.empty()) {
141 return nullptr;
142 }
143
144 sptr<IConsumerSurface> surface = IConsumerSurface::Create();
145 if (surface == nullptr) {
146 return nullptr;
147 }
148 sptr<IBufferProducer> surfaceProducer = surface->GetProducer();
149 return cameraManager_->CreatePhotoOutput(photoProfile_[0], surfaceProducer);
150 }
151 return nullptr;
152 }
153
NativeAuthorization()154 void NativeAuthorization()
155 {
156 const char *perms[2];
157 uint64_t tokenId = 0;
158 int32_t uid = 0;
159 int32_t userId = 0;
160 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
161 perms[1] = "ohos.permission.CAMERA";
162 NativeTokenInfoParams infoInstance = {
163 .dcapsNum = 0,
164 .permsNum = 2,
165 .aclsNum = 0,
166 .dcaps = NULL,
167 .perms = perms,
168 .acls = NULL,
169 .processName = "native_camera_tdd",
170 .aplStr = "system_basic",
171 };
172 tokenId = GetAccessTokenId(&infoInstance);
173 uid = IPCSkeleton::GetCallingUid();
174 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
175 MEDIA_DEBUG_LOG("CameraPortraitSessionUnitTest::NativeAuthorization TearDown");
176 SetSelfTokenID(tokenId);
177 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
178 }
179
PortraitSessionFuzzTest()180 void PortraitSessionFuzzer::PortraitSessionFuzzTest()
181 {
182 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
183 return;
184 }
185 NativeAuthorization();
186 cameraManager_ = CameraManager::GetInstance();
187 std::vector<sptr<CameraDevice>> cameras = cameraManager_->GetCameraDeviceListFromServer();
188 CHECK_ERROR_RETURN_LOG(cameras.empty(), "PortraitSessionFuzzer: GetCameraDeviceListFromServer Error");
189 sptr<CaptureInput> input = cameraManager_->CreateCameraInput(cameras[0]);
190 CHECK_ERROR_RETURN_LOG(!input, "CreateCameraInput Error");
191 sptr<CameraInput> camInput = (sptr<CameraInput> &)input;
192 std::string cameraSettings = camInput->GetCameraSettings();
193 camInput->SetCameraSettings(cameraSettings);
194 camInput->GetCameraDevice()->Open();
195 sptr<CaptureOutput> preview = CreatePreviewOutput();
196 sptr<CaptureOutput> photo = CreatePhotoOutput();
197 sptr<CaptureSession> captureSession = cameraManager_->CreateCaptureSession(SceneMode::PORTRAIT);
198 sptr<PortraitSession> portraitSession = nullptr;
199 portraitSession = static_cast<PortraitSession *> (captureSession.GetRefPtr());
200 portraitSession->BeginConfig();
201 portraitSession->AddInput(input);
202 sptr<CameraDevice> info = captureSession->innerInputDevice_->GetCameraDeviceInfo();
203 info->modePreviewProfiles_.emplace(static_cast<int32_t>(PORTRAIT), previewProfile_);
204 info->modePhotoProfiles_.emplace(static_cast<int32_t>(PORTRAIT), photoProfile_);
205 portraitSession->AddOutput(preview);
206 portraitSession->AddOutput(photo);
207 portraitSession->CommitConfig();
208 portraitSession->LockForControl();
209 auto portraitEffect = portraitSession->GetSupportedPortraitEffects();
210 portraitSession->GetPortraitEffect();
211 if (!portraitEffect.empty()) {
212 portraitSession->SetPortraitEffect(portraitEffect[0]);
213 }
214 portraitSession->CanAddOutput(photo);
215 portraitSession->UnlockForControl();
216 }
217
Test()218 void Test()
219 {
220 auto portraitSession = std::make_unique<PortraitSessionFuzzer>();
221 if (portraitSession == nullptr) {
222 MEDIA_INFO_LOG("portraitSession is null");
223 return;
224 }
225 // std::cout << "aning PortraitSessionFuzzTest" << std::endl;
226 portraitSession->PortraitSessionFuzzTest();
227 }
228
229 typedef void (*TestFuncs[1])();
230
231 TestFuncs g_testFuncs = {
232 Test,
233 };
234
FuzzTest(const uint8_t * rawData,size_t size)235 bool FuzzTest(const uint8_t* rawData, size_t size)
236 {
237 // initialize data
238 RAW_DATA = rawData;
239 g_dataSize = size;
240 g_pos = 0;
241
242 uint32_t code = GetData<uint32_t>();
243 uint32_t len = GetArrLength(g_testFuncs);
244 if (len > 0) {
245 g_testFuncs[code % len]();
246 } else {
247 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
248 }
249
250 return true;
251 }
252 } // namespace CameraStandard
253 } // namespace OHOS
254
255 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)256 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
257 {
258 if (size < OHOS::CameraStandard::THRESHOLD) {
259 return 0;
260 }
261
262 OHOS::CameraStandard::FuzzTest(data, size);
263 return 0;
264 }