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_video_processing_session_proxy_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 "metadata_utils.h"
21 #include "ipc_skeleton.h"
22 #include "access_token.h"
23 #include "hap_token_info.h"
24 #include "accesstoken_kit.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.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 MIN_SIZE_NUM = 4;
34 static const uint8_t* RAW_DATA = nullptr;
35 const size_t THRESHOLD = 10;
36 static size_t g_dataSize = 0;
37 static size_t g_pos;
38
39 std::shared_ptr<DeferredVideoProcessingSessionProxy>
40 DeferredVideoProcessingSessionProxyFuzzer::fuzz_{nullptr};
41
42 /*
43 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
44 * tips: only support basic type
45 */
46 template<class T>
GetData()47 T GetData()
48 {
49 T object {};
50 size_t objectSize = sizeof(object);
51 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
52 return object;
53 }
54 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
55 if (ret != EOK) {
56 return {};
57 }
58 g_pos += objectSize;
59 return object;
60 }
61
62 template<class T>
GetArrLength(T & arr)63 uint32_t GetArrLength(T& arr)
64 {
65 if (arr == nullptr) {
66 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
67 return 0;
68 }
69 return sizeof(arr) / sizeof(arr[0]);
70 }
71
DeferredVideoProcessingSessionProxyFuzzTest()72 void DeferredVideoProcessingSessionProxyFuzzer::DeferredVideoProcessingSessionProxyFuzzTest()
73 {
74 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
75 return;
76 }
77
78 sptr<IRemoteObject> object;
79 fuzz_ = std::make_shared<DeferredVideoProcessingSessionProxy>(object);
80 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
81 fuzz_->BeginSynchronize();
82 fuzz_->EndSynchronize();
83 uint8_t randomNum = GetData<uint8_t>();
84 std::vector<std::string> testStrings = {"test1", "test2"};
85 std::string videoId(testStrings[randomNum % testStrings.size()]);
86 sptr<IPCFileDescriptor> srcFd;
87 sptr<IPCFileDescriptor> dstFd;
88 fuzz_->AddVideo(videoId, srcFd, dstFd);
89 fuzz_->RemoveVideo(videoId, GetData<bool>());
90 fuzz_->RestoreVideo(videoId);
91 }
92
Test()93 void Test()
94 {
95 auto deferredVideoProcessingSessionProxy = std::make_unique<DeferredVideoProcessingSessionProxyFuzzer>();
96 if (deferredVideoProcessingSessionProxy == nullptr) {
97 MEDIA_INFO_LOG("deferredVideoProcessingSessionProxy is null");
98 return;
99 }
100 deferredVideoProcessingSessionProxy->DeferredVideoProcessingSessionProxyFuzzTest();
101 }
102
103 typedef void (*TestFuncs[1])();
104
105 TestFuncs g_testFuncs = {
106 Test,
107 };
108
FuzzTest(const uint8_t * rawData,size_t size)109 bool FuzzTest(const uint8_t* rawData, size_t size)
110 {
111 // initialize data
112 RAW_DATA = rawData;
113 g_dataSize = size;
114 g_pos = 0;
115
116 uint32_t code = GetData<uint32_t>();
117 uint32_t len = GetArrLength(g_testFuncs);
118 if (len > 0) {
119 g_testFuncs[code % len]();
120 } else {
121 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
122 }
123
124 return true;
125 }
126 } // namespace CameraStandard
127 } // namespace OHOS
128
129 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
131 {
132 if (size < OHOS::CameraStandard::THRESHOLD) {
133 return 0;
134 }
135
136 OHOS::CameraStandard::FuzzTest(data, size);
137 return 0;
138 }