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 "deferred_photo_processing_session_stub_fuzzer.h"
17 #include "buffer_info.h"
18 #include "deferred_processing_service.h"
19 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
20 #include "deferred_processing_service_ipc_interface_code.h"
21 #include "metadata_utils.h"
22 #include "ipc_skeleton.h"
23 #include "hap_token_info.h"
24 #include "accesstoken_kit.h"
25 #include "nativetoken_kit.h"
26 #include "camera_log.h"
27 #include "securec.h"
28
29 namespace OHOS {
30 namespace CameraStandard {
31 using namespace DeferredProcessing;
32 static constexpr int32_t MAX_CODE_LEN = 512;
33 static constexpr int32_t MAX_CODE_NUM = 6;
34 static constexpr int32_t MIN_SIZE_NUM = 4;
35 static const uint8_t* RAW_DATA = nullptr;
36 const size_t THRESHOLD = 10;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39
40 std::shared_ptr<DeferredPhotoProcessingSessionStubFuzz> DeferredPhotoProcessingSessionStubFuzzer::fuzz_{nullptr};
41 std::shared_ptr<DPSProwerManager> DeferredPhotoProcessingSessionStubFuzzer::manager_{nullptr};
42
43 /*
44 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
45 * tips: only support basic type
46 */
47 template<class T>
GetData()48 T GetData()
49 {
50 T object {};
51 size_t objectSize = sizeof(object);
52 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
53 return object;
54 }
55 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
56 if (ret != EOK) {
57 return {};
58 }
59 g_pos += objectSize;
60 return object;
61 }
62
63 template<class T>
GetArrLength(T & arr)64 uint32_t GetArrLength(T& arr)
65 {
66 if (arr == nullptr) {
67 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
68 return 0;
69 }
70 return sizeof(arr) / sizeof(arr[0]);
71 }
72
OnRemoteRequest(int32_t code)73 void DeferredPhotoProcessingSessionStubFuzzer::OnRemoteRequest(int32_t code)
74 {
75 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
76 return;
77 }
78 fuzz_ = std::make_shared<DeferredPhotoProcessingSessionStubFuzz>();
79 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
80 MessageParcel dataMessageParcel;
81 dataMessageParcel.WriteInterfaceToken(DeferredPhotoProcessingSessionStubFuzz::GetDescriptor());
82 dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
83 dataMessageParcel.RewindRead(0);
84 MessageParcel reply;
85 MessageOption option;
86 fuzz_->OnRemoteRequest(code, dataMessageParcel, reply, option);
87 }
88
FuzzTest()89 void DeferredPhotoProcessingSessionStubFuzzer::FuzzTest()
90 {
91 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
92 return;
93 }
94 if (manager_ == nullptr) {
95 manager_ = std::make_shared<DPSProwerManager>();
96 }
97 manager_->EnableAutoSuspend();
98 uint32_t time = GetData<uint32_t>();
99 manager_->DisableAutoSuspend(time);
100 }
101
Test()102 void Test()
103 {
104 auto deferredPhotoProcessingSessionStub = std::make_unique<DeferredPhotoProcessingSessionStubFuzzer>();
105 if (deferredPhotoProcessingSessionStub == nullptr) {
106 MEDIA_INFO_LOG("deferredPhotoProcessingSessionStub is null");
107 return;
108 }
109 for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
110 deferredPhotoProcessingSessionStub->OnRemoteRequest(i);
111 }
112 deferredPhotoProcessingSessionStub->FuzzTest();
113 }
114
115 typedef void (*TestFuncs[1])();
116
117 TestFuncs g_testFuncs = {
118 Test,
119 };
120
FuzzTest(const uint8_t * rawData,size_t size)121 bool FuzzTest(const uint8_t* rawData, size_t size)
122 {
123 // initialize data
124 RAW_DATA = rawData;
125 g_dataSize = size;
126 g_pos = 0;
127
128 uint32_t code = GetData<uint32_t>();
129 uint32_t len = GetArrLength(g_testFuncs);
130 if (len > 0) {
131 g_testFuncs[code % len]();
132 } else {
133 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
134 }
135
136 return true;
137 }
138 } // namespace CameraStandard
139 } // namespace OHOS
140
141 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)142 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
143 {
144 if (size < OHOS::CameraStandard::THRESHOLD) {
145 return 0;
146 }
147
148 OHOS::CameraStandard::FuzzTest(data, size);
149 return 0;
150 }