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 "deferredvideoprocsession_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "accesstoken_kit.h"
25 #include "camera_error_code.h"
26 #include "securec.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 int g_userId = 0;
37 std::shared_ptr<DeferredVideoProcessingSessionCallback> callback =
38 std::make_shared<DeferredVideoProcessingSessionCallback>();
39 std::shared_ptr<IDeferredVideoProcSessionCallbackFuzz> sessionCallback =
40 std::make_shared<IDeferredVideoProcSessionCallbackFuzz>();
41 std::shared_ptr<DeferredVideoProcSession> deferredVideoProcSession =
42 std::make_shared<DeferredVideoProcSession>(g_userId, sessionCallback);
43
44 /*
45 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
46 * tips: only support basic type
47 */
48 template<class T>
GetData()49 T GetData()
50 {
51 T object {};
52 size_t objectSize = sizeof(object);
53 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
54 return object;
55 }
56 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
57 if (ret != EOK) {
58 return {};
59 }
60 g_pos += objectSize;
61 return object;
62 }
63
64 template<class T>
GetArrLength(T & arr)65 uint32_t GetArrLength(T& arr)
66 {
67 if (arr == nullptr) {
68 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
69 return 0;
70 }
71 return sizeof(arr) / sizeof(arr[0]);
72 }
73
GetPermission()74 void GetPermission()
75 {
76 uint64_t tokenId;
77 const char* perms[2];
78 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
79 perms[1] = "ohos.permission.CAMERA";
80 NativeTokenInfoParams infoInstance = {
81 .dcapsNum = 0,
82 .permsNum = 2,
83 .aclsNum = 0,
84 .dcaps = NULL,
85 .perms = perms,
86 .acls = NULL,
87 .processName = "native_camera_tdd",
88 .aplStr = "system_basic",
89 };
90 tokenId = GetAccessTokenId(&infoInstance);
91 SetSelfTokenID(tokenId);
92 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
93 }
94
createSession(int userId,std::shared_ptr<IDeferredVideoProcSessionCallback> callback,sptr<DeferredProcessing::IDeferredVideoProcessingSession> & session)95 auto createSession(int userId,
96 std::shared_ptr<IDeferredVideoProcSessionCallback> callback,
97 sptr<DeferredProcessing::IDeferredVideoProcessingSession>& session)
98 {
99 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
100 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
101 "CreateDeferredVideoProcessingSession Failed to get System ability manager");
102 sptr<IRemoteObject> object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
103 CHECK_ERROR_RETURN_RET_LOG(object == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
104 "CreateDeferredVideoProcessingSession object is null");
105 sptr<ICameraService> serviceProxy = iface_cast<ICameraService>(object);
106 CHECK_ERROR_RETURN_RET_LOG(serviceProxy == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
107 "CreateDeferredVideoProcessingSession serviceProxy is null");
108
109 sptr<DeferredProcessing::IDeferredVideoProcessingSessionCallback> remoteCallback =
110 new(std::nothrow) DeferredVideoProcessingSessionCallback();
111 CHECK_ERROR_RETURN_RET_LOG(remoteCallback == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
112 "CreateDeferredVideoProcessingSession failed to new remoteCallback!");
113 serviceProxy->CreateDeferredVideoProcessingSession(userId, remoteCallback, session);
114 return CameraErrorCode::SUCCESS;
115 }
116
DeferredVideoProcSessionFuzzTest()117 void DeferredVideoProcSessionFuzzer::DeferredVideoProcSessionFuzzTest()
118 {
119 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
120 return;
121 }
122 GetPermission();
123
124 uint8_t randomNum = GetData<uint8_t>();
125 std::vector<std::string> testStrings = {"test1", "test2"};
126 std::string videoId(testStrings[randomNum % testStrings.size()]);
127 sptr<IPCFileDescriptor> ipcFileDescriptor = nullptr;
128 callback->OnProcessVideoDone(videoId, ipcFileDescriptor);
129 callback->OnError(videoId, DpsErrorCode::ERROR_SESSION_SYNC_NEEDED);
130 int32_t status = GetData<int32_t>();
131 callback->OnStateChanged(status);
132
133 deferredVideoProcSession->BeginSynchronize();
134 deferredVideoProcSession->EndSynchronize();
135 sptr<IPCFileDescriptor> srcFd = nullptr;
136 sptr<IPCFileDescriptor> dstFd = nullptr;
137 deferredVideoProcSession->AddVideo(videoId, srcFd, dstFd);
138 bool restorable = GetData<bool>();
139 deferredVideoProcSession->RemoveVideo(videoId, restorable);
140 deferredVideoProcSession->RestoreVideo(videoId);
141 sptr<DeferredProcessing::IDeferredVideoProcessingSession> session = nullptr;
142 createSession(g_userId, sessionCallback, session);
143 CHECK_ERROR_RETURN_LOG(session == nullptr, "session is null!");
144 deferredVideoProcSession->SetDeferredVideoSession(session);
145 int32_t pid = GetData<int32_t>();
146 deferredVideoProcSession->ReconnectDeferredProcessingSession();
147 deferredVideoProcSession->ConnectDeferredProcessingSession();
148 deferredVideoProcSession->GetCallback();
149 deferredVideoProcSession->remoteSession_ = nullptr;
150 deferredVideoProcSession->callback_ = nullptr;
151 deferredVideoProcSession->CameraServerDied(pid);
152 }
153
Test()154 void Test()
155 {
156 auto deferredVideoProcSessionFuzz = std::make_unique<DeferredVideoProcSessionFuzzer>();
157 if (deferredVideoProcSessionFuzz == nullptr) {
158 MEDIA_INFO_LOG("deferredVideoProcSessionFuzz is null");
159 return;
160 }
161 deferredVideoProcSessionFuzz->DeferredVideoProcSessionFuzzTest();
162 }
163
164 typedef void (*TestFuncs[1])();
165
166 TestFuncs g_testFuncs = {
167 Test,
168 };
169
FuzzTest(const uint8_t * rawData,size_t size)170 bool FuzzTest(const uint8_t* rawData, size_t size)
171 {
172 // initialize data
173 RAW_DATA = rawData;
174 g_dataSize = size;
175 g_pos = 0;
176
177 uint32_t code = GetData<uint32_t>();
178 uint32_t len = GetArrLength(g_testFuncs);
179 if (len > 0) {
180 g_testFuncs[code % len]();
181 } else {
182 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
183 }
184
185 return true;
186 }
187 } // namespace CameraStandard
188 } // namespace OHOS
189
190 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)191 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
192 {
193 if (size < OHOS::CameraStandard::THRESHOLD) {
194 return 0;
195 }
196
197 OHOS::CameraStandard::FuzzTest(data, size);
198 return 0;
199 }