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 "hstream_capture_fuzzer.h"
17 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
18 #include "message_parcel.h"
19 #include "iservice_registry.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include "token_setproc.h"
24 #include "iconsumer_surface.h"
25 #include "nativetoken_kit.h"
26 #include "accesstoken_kit.h"
27 #include "camera_metadata_info.h"
28 #include "metadata_utils.h"
29 #include "camera_service_ipc_interface_code.h"
30 #include "camera_device.h"
31 #include "camera_manager.h"
32 #include "ipc_skeleton.h"
33 #include "securec.h"
34
35 namespace OHOS {
36 namespace CameraStandard {
37 using namespace OHOS::HDI::Camera::V1_0;
38 static constexpr int32_t MAX_CODE_LEN = 512;
39 static constexpr int32_t MIN_SIZE_NUM = 4;
40 static const uint8_t* RAW_DATA = nullptr;
41 const size_t THRESHOLD = 10;
42 static size_t g_dataSize = 0;
43 static size_t g_pos;
44
45 const int32_t PHOTO_WIDTH = 1280;
46 const int32_t PHOTO_HEIGHT = 960;
47 const int32_t PHOTO_FORMAT = 2000;
48 const int32_t ITEMCOUNT = 10;
49 const int32_t DATASIZE = 100;
50 const int32_t NUM_1 = 1;
51 const int32_t NUM_1024 = 1024;
52
53 std::shared_ptr<HStreamCapture> HStreamCaptureFuzzer::fuzz_{nullptr};
54 sptr<IBufferProducer> producer;
55
56 /*
57 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
58 * tips: only support basic type
59 */
60 template<class T>
GetData()61 T GetData()
62 {
63 T object {};
64 size_t objectSize = sizeof(object);
65 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
66 return object;
67 }
68 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
69 if (ret != EOK) {
70 return {};
71 }
72 g_pos += objectSize;
73 return object;
74 }
75
76 template<class T>
GetArrLength(T & arr)77 uint32_t GetArrLength(T& arr)
78 {
79 if (arr == nullptr) {
80 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
81 return 0;
82 }
83 return sizeof(arr) / sizeof(arr[0]);
84 }
85
HStreamCaptureFuzzTest1()86 void HStreamCaptureFuzzer::HStreamCaptureFuzzTest1()
87 {
88 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
89 return;
90 }
91 int32_t captureId = GetData<int32_t>();
92 uint8_t randomNum = GetData<uint8_t>();
93 std::vector<bool> testBools = {true, false};
94 bool isEnabled = (testBools[randomNum % testBools.size()]);
95 bool enabled = (testBools[randomNum % testBools.size()]);
96 fuzz_->CreateMediaLibraryPhotoAssetProxy(captureId);
97 fuzz_->GetPhotoAssetInstance(captureId);
98 fuzz_->GetAddPhotoProxyEnabled();
99 fuzz_->AcquireBufferToPrepareProxy(captureId);
100 StreamInfo_V1_1 streamInfo;
101 fuzz_->SetStreamInfo(streamInfo);
102 fuzz_->FillingPictureExtendStreamInfos(streamInfo, GetData<int32_t>());
103 fuzz_->SetThumbnail(isEnabled, producer);
104 fuzz_->EnableRawDelivery(enabled);
105 std::vector<std::string> bufferNames = {"rawImage",
106 "gainmapImage", "deepImage", "exifImage", "debugImage"};
107 for (const auto& bufName : bufferNames) {
108 fuzz_->SetBufferProducerInfo(bufName, producer);
109 }
110 int32_t type = GetData<int32_t>();
111 fuzz_->DeferImageDeliveryFor(type);
112 fuzz_->PrepareBurst(captureId);
113 fuzz_->ResetBurst();
114 fuzz_->ResetBurstKey(captureId);
115 fuzz_->GetBurstKey(captureId);
116 fuzz_->IsBurstCapture(captureId);
117 fuzz_->IsBurstCover(captureId);
118 fuzz_->GetCurBurstSeq(captureId);
119 fuzz_->IsDeferredPhotoEnabled();
120 fuzz_->IsDeferredVideoEnabled();
121 auto videoCodecType = GetData<int32_t>();
122 fuzz_->SetMovingPhotoVideoCodecType(videoCodecType);
123 fuzz_->GetMovingPhotoVideoCodecType();
124 fuzz_->SetCameraPhotoRotation(GetData<bool>());
125 sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
126 fuzz_->SetCameraPhotoProxyInfo(cameraPhotoProxy);
127 }
128
HStreamCaptureFuzzTest2()129 void HStreamCaptureFuzzer::HStreamCaptureFuzzTest2()
130 {
131 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
132 return;
133 }
134 auto captureId = GetData<int32_t>();
135 auto interfaceCode = GetData<int32_t>();
136 auto timestamp = GetData<uint64_t>();
137 auto isDelay = GetData<bool>();
138 uint8_t randomNum = GetData<uint8_t>();
139 std::vector<std::string> testStrings = {"test1", "test2"};
140 std::string imageId(testStrings[randomNum % testStrings.size()]);
141 fuzz_->SetBurstImages(captureId, imageId);
142 fuzz_->CheckResetBurstKey(captureId);
143 std::shared_ptr<OHOS::Camera::CameraMetadata> captureSettings;
144 captureSettings = std::make_shared<OHOS::Camera::CameraMetadata>(ITEMCOUNT, DATASIZE);
145 fuzz_->CheckBurstCapture(captureSettings, GetData<int32_t>());
146 sptr<HCameraHostManager> cameraHostManager = new HCameraHostManager(nullptr);
147 std::string cameraId;
148 uint32_t callingTokenId = GetData<uint32_t>();
149 sptr<HCameraDevice> camDevice = new (std::nothrow)
150 HCameraDevice(cameraHostManager, cameraId, callingTokenId);
151 camDevice->OpenDevice(true);
152 fuzz_->OnCaptureReady(captureId, timestamp);
153 fuzz_->Capture(captureSettings);
154 fuzz_->CancelCapture();
155 fuzz_->SetMode(GetData<int32_t>());
156 fuzz_->GetMode();
157 fuzz_->ConfirmCapture();
158 fuzz_->EndBurstCapture(captureSettings);
159 fuzz_->Release();
160 fuzz_->ReleaseStream(isDelay);
161 fuzz_->OnCaptureStarted(captureId);
162 fuzz_->OnCaptureStarted(captureId, GetData<int32_t>());
163 fuzz_->OnCaptureEnded(captureId, GetData<int32_t>());
164 auto errorCode = GetData<int32_t>();
165 fuzz_->OnCaptureError(captureId, errorCode);
166 fuzz_->OnFrameShutter(captureId, timestamp);
167 fuzz_->OnFrameShutterEnd(captureId, timestamp);
168 CameraInfoDumper infoDumper(0);
169 fuzz_->DumpStreamInfo(infoDumper);
170 fuzz_->OperatePermissionCheck(interfaceCode);
171 CaptureInfo captureInfoPhoto;
172 fuzz_->ProcessCaptureInfoPhoto(captureInfoPhoto, captureSettings, captureId);
173 fuzz_->SetRotation(captureSettings, captureId);
174 }
175
Test()176 void Test()
177 {
178 auto hstreamCapture = std::make_unique<HStreamCaptureFuzzer>();
179 if (hstreamCapture == nullptr) {
180 MEDIA_INFO_LOG("hstreamCapture is null");
181 return;
182 }
183 sptr<Surface> photoSurface;
184 photoSurface = Surface::CreateSurfaceAsConsumer("hstreamcapture");
185 producer = photoSurface->GetProducer();
186 HStreamCaptureFuzzer::fuzz_ = std::make_shared
187 <HStreamCapture>(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
188 CHECK_ERROR_RETURN_LOG(!HStreamCaptureFuzzer::fuzz_, "Create fuzz_ Error");
189 sptr<HDI::Camera::V1_0::IStreamOperator> streamOperator;
190 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility =
191 std::make_shared<OHOS::Camera::CameraMetadata>(NUM_1, NUM_1024);
192 HStreamCaptureFuzzer::fuzz_->LinkInput(streamOperator, cameraAbility);
193 HStreamCaptureFuzzer::fuzz_->cameraAbility_ = cameraAbility;
194 hstreamCapture->HStreamCaptureFuzzTest1();
195 hstreamCapture->HStreamCaptureFuzzTest2();
196 }
197
198 typedef void (*TestFuncs[1])();
199
200 TestFuncs g_testFuncs = {
201 Test,
202 };
203
FuzzTest(const uint8_t * rawData,size_t size)204 bool FuzzTest(const uint8_t* rawData, size_t size)
205 {
206 // initialize data
207 RAW_DATA = rawData;
208 g_dataSize = size;
209 g_pos = 0;
210
211 uint32_t code = GetData<uint32_t>();
212 uint32_t len = GetArrLength(g_testFuncs);
213 if (len > 0) {
214 g_testFuncs[code % len]();
215 } else {
216 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
217 }
218
219 return true;
220 }
221 } // namespace CameraStandard
222 } // namespace OHOS
223
224 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)225 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
226 {
227 if (size < OHOS::CameraStandard::THRESHOLD) {
228 return 0;
229 }
230
231 OHOS::CameraStandard::FuzzTest(data, size);
232 return 0;
233 }