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 "camera_server_photo_proxy_fuzzer.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include "camera_log.h"
20
21 namespace OHOS {
22 namespace CameraStandard {
23 static constexpr int32_t MAX_CODE_LEN = 512;
24 static constexpr int32_t MIN_SIZE_NUM = 4;
25 static constexpr int32_t BUFFER_HANDLE_RESERVE_TEST_SIZE = 16;
26 static const uint8_t* RAW_DATA = nullptr;
27 const size_t THRESHOLD = 10;
28 static size_t g_dataSize = 0;
29 static size_t g_pos;
30 std::shared_ptr<CameraServerPhotoProxy> CameraServerPhotoProxyFuzzer::fuzz_{nullptr};
31
32 /*
33 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
34 * tips: only support basic type
35 */
36 template<class T>
GetData()37 T GetData()
38 {
39 T object {};
40 size_t objectSize = sizeof(object);
41 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
42 return object;
43 }
44 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
45 if (ret != EOK) {
46 return {};
47 }
48 g_pos += objectSize;
49 return object;
50 }
51
52 template<class T>
GetArrLength(T & arr)53 uint32_t GetArrLength(T& arr)
54 {
55 if (arr == nullptr) {
56 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
57 return 0;
58 }
59 return sizeof(arr) / sizeof(arr[0]);
60 }
61
CameraServerPhotoProxyFuzzTest()62 void CameraServerPhotoProxyFuzzer::CameraServerPhotoProxyFuzzTest()
63 {
64 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
65 return;
66 }
67 fuzz_ = std::make_shared<CameraServerPhotoProxy>();
68 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
69 size_t handleSize = sizeof(BufferHandle) + (sizeof(int32_t) * (BUFFER_HANDLE_RESERVE_TEST_SIZE * 2));
70 fuzz_->bufferHandle_ = static_cast<BufferHandle *>(malloc(handleSize));
71 fuzz_->imageFormat_ = GetData<int32_t>();
72 fuzz_->isMmaped_ = GetData<bool>();
73 fuzz_->GetFileDataAddr();
74 fuzz_->GetFormat();
75 fuzz_->deferredProcType_ = 0;
76 fuzz_->GetDeferredProcType();
77 fuzz_->deferredProcType_ = 1;
78 fuzz_->GetDeferredProcType();
79 fuzz_->mode_ = GetData<int32_t>();
80 fuzz_->isMmaped_ = GetData<bool>();
81 fuzz_->GetShootingMode();
82 free(fuzz_->bufferHandle_);
83 fuzz_->bufferHandle_ = nullptr;
84 }
85
Test()86 void Test()
87 {
88 auto cameraServerPhotoProxy = std::make_unique<CameraServerPhotoProxyFuzzer>();
89 if (cameraServerPhotoProxy == nullptr) {
90 MEDIA_INFO_LOG("cameraServerPhotoProxy is null");
91 return;
92 }
93 cameraServerPhotoProxy->CameraServerPhotoProxyFuzzTest();
94 }
95
96 typedef void (*TestFuncs[1])();
97
98 TestFuncs g_testFuncs = {
99 Test,
100 };
101
FuzzTest(const uint8_t * rawData,size_t size)102 bool FuzzTest(const uint8_t* rawData, size_t size)
103 {
104 // initialize data
105 RAW_DATA = rawData;
106 g_dataSize = size;
107 g_pos = 0;
108
109 uint32_t code = GetData<uint32_t>();
110 uint32_t len = GetArrLength(g_testFuncs);
111 if (len > 0) {
112 g_testFuncs[code % len]();
113 } else {
114 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
115 }
116
117 return true;
118 }
119 } // namespace CameraStandard
120 } // namespace OHOS
121
122 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)123 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
124 {
125 if (size < OHOS::CameraStandard::THRESHOLD) {
126 return 0;
127 }
128
129 OHOS::CameraStandard::FuzzTest(data, size);
130 return 0;
131 }