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 "photo_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
28 namespace OHOS {
29 namespace CameraStandard {
30 static constexpr int32_t MAX_CODE_LEN = 512;
31 static constexpr int32_t MIN_SIZE_NUM = 4;
32 static const uint8_t* RAW_DATA = nullptr;
33 const size_t THRESHOLD = 10;
34 static size_t g_dataSize = 0;
35 static size_t g_pos;
36
37 sptr<PhotoSession> fuzz_ = nullptr;
38 sptr<CameraManager> cameraManager_ = nullptr;
39 sptr<CaptureOutput> photoOutput_ = nullptr;
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
PhotoSessionFuzzTest()71 void PhotoSessionFuzzer::PhotoSessionFuzzTest()
72 {
73 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
74 return;
75 }
76 cameraManager_ = CameraManager::GetInstance();
77 sptr<CaptureSession> captureSession = cameraManager_->CreateCaptureSession(SceneMode::CAPTURE);
78 fuzz_ = static_cast<PhotoSession*>(captureSession.GetRefPtr());
79 if (fuzz_ == nullptr) {
80 return;
81 }
82 fuzz_->CanAddOutput(photoOutput_);
83 sptr<FluorescencePhotoSession> fluorescencePhotoSession =
84 static_cast<FluorescencePhotoSession*>(captureSession.GetRefPtr());
85 sptr<CaptureOutput> output = nullptr;
86 fluorescencePhotoSession->CanAddOutput(output);
87 uint8_t randomNum = GetData<uint8_t>();
88 std::vector<PreconfigType> preconfigTypeVec = {
89 PRECONFIG_720P,
90 PRECONFIG_1080P,
91 PRECONFIG_4K,
92 PRECONFIG_HIGH_QUALITY,
93 };
94 uint8_t underNum = randomNum % preconfigTypeVec.size();
95 PreconfigType preconfigType = preconfigTypeVec[underNum];
96 std::vector<ProfileSizeRatio> profileSizeRatioVec = {
97 UNSPECIFIED,
98 RATIO_1_1,
99 RATIO_4_3,
100 RATIO_16_9,
101 };
102 uint8_t underNumSec = randomNum % profileSizeRatioVec.size();
103 ProfileSizeRatio profileSizeRatio = profileSizeRatioVec[underNumSec];
104 fuzz_->GeneratePreconfigProfiles(preconfigType, profileSizeRatio);
105 auto configs = fuzz_->GeneratePreconfigProfiles(PRECONFIG_720P, RATIO_1_1);
106 fuzz_->IsPreconfigProfilesLegal(configs);
107 fuzz_->CanPreconfig(preconfigType, profileSizeRatio);
108 fuzz_->Preconfig(preconfigType, profileSizeRatio);
109 auto cameras = cameraManager_->GetSupportedCameras();
110 CHECK_ERROR_RETURN_LOG(cameras.empty(), "PhotoSessionFuzzer: GetSupportedCameras Error");
111 Profile photo(CameraFormat::CAMERA_FORMAT_JPEG, {640, 480});
112 fuzz_->IsPhotoProfileLegal(cameras[0], photo);
113 Profile preview(CameraFormat::CAMERA_FORMAT_YUV_420_SP, {640, 480});
114 fuzz_->IsPreviewProfileLegal(cameras[0], preview);
115 }
116
Test()117 void Test()
118 {
119 auto photoSession = std::make_unique<PhotoSessionFuzzer>();
120 if (photoSession == nullptr) {
121 MEDIA_INFO_LOG("photoSession is null");
122 return;
123 }
124 photoSession->PhotoSessionFuzzTest();
125 }
126
127 typedef void (*TestFuncs[1])();
128
129 TestFuncs g_testFuncs = {
130 Test,
131 };
132
FuzzTest(const uint8_t * rawData,size_t size)133 bool FuzzTest(const uint8_t* rawData, size_t size)
134 {
135 // initialize data
136 RAW_DATA = rawData;
137 g_dataSize = size;
138 g_pos = 0;
139
140 uint32_t code = GetData<uint32_t>();
141 uint32_t len = GetArrLength(g_testFuncs);
142 if (len > 0) {
143 g_testFuncs[code % len]();
144 } else {
145 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
146 }
147
148 return true;
149 }
150 } // namespace CameraStandard
151 } // namespace OHOS
152
153 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)154 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
155 {
156 if (size < OHOS::CameraStandard::THRESHOLD) {
157 return 0;
158 }
159
160 OHOS::CameraStandard::FuzzTest(data, size);
161 return 0;
162 }