1 /*
2 * Copyright (c) 2023 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_processing_stub_fuzzer.h"
17 #include "buffer_info.h"
18 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
19 #include "metadata_utils.h"
20 #include "ipc_skeleton.h"
21 #include "access_token.h"
22 #include "hap_token_info.h"
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 using namespace std;
27
28 namespace OHOS {
29 namespace CameraStandard {
30 using namespace OHOS::CameraStandard::DeferredProcessing;
31 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IDeferredPhotoProcessingSession";
32 const size_t LIMITCOUNT = 4;
33 const int32_t LIMITSIZE = 2;
34 const int USERID = 1;
35 bool g_isDeferredProcessingPermission = false;
36 DeferredPhotoProcessingSession *fuzz = nullptr;
37
DeferredProcessingFuzzTestGetPermission()38 void DeferredProcessingFuzzTestGetPermission()
39 {
40 if (!g_isDeferredProcessingPermission) {
41 uint64_t tokenId;
42 const char *perms[0];
43 perms[0] = "ohos.permission.CAMERA";
44 NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
45 .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
46 };
47 tokenId = GetAccessTokenId(&infoInstance);
48 SetSelfTokenID(tokenId);
49 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
50 g_isDeferredProcessingPermission = true;
51 }
52 }
53
DeferredProcessingFuzzTest(uint8_t * rawData,size_t size)54 void DeferredProcessingFuzzTest(uint8_t *rawData, size_t size)
55 {
56 if (rawData == nullptr || size < LIMITSIZE) {
57 return;
58 }
59 DeferredProcessingFuzzTestGetPermission();
60
61 int32_t itemCount = 0;
62 int32_t dataSize = 0;
63 int32_t *streams = reinterpret_cast<int32_t *>(rawData);
64 std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
65 ability = std::make_shared<OHOS::Camera::CameraMetadata>(itemCount, dataSize);
66 ability->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, streams, size / LIMITCOUNT);
67 int32_t compensationRange[2] = {rawData[0], rawData[1]};
68 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, compensationRange,
69 sizeof(compensationRange) / sizeof(compensationRange[0]));
70 float focalLength = rawData[0];
71 ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, 1);
72
73 int32_t sensorOrientation = rawData[0];
74 ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, 1);
75
76 int32_t cameraPosition = rawData[0];
77 ability->addEntry(OHOS_ABILITY_CAMERA_POSITION, &cameraPosition, 1);
78
79 const camera_rational_t aeCompensationStep[] = {{rawData[0], rawData[1]}};
80 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep,
81 sizeof(aeCompensationStep) / sizeof(aeCompensationStep[0]));
82
83 MessageParcel data;
84 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
85 CHECK_AND_RETURN_LOG(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data),
86 "DeferredProcessingFuzzer: EncodeCameraMetadata Error");
87 data.RewindRead(0);
88 MessageParcel reply;
89 MessageOption option;
90
91 sptr<IDeferredPhotoProcessingSessionCallbackFuzz> IDPSessionCallbackFuzz = nullptr;
92
93 if (fuzz == nullptr) {
94 fuzz = new DeferredPhotoProcessingSession(USERID, nullptr, nullptr, IDPSessionCallbackFuzz);
95 }
96
97 if (fuzz != nullptr) {
98 const uint32_t maxNum = 1;
99 for (uint32_t code = 0; code < maxNum; ++code) {
100 data.RewindRead(0);
101 reply.RewindWrite(0);
102 fuzz->OnRemoteRequest(code, data, reply, option);
103 }
104 }
105 }
106
TestBufferInfo(uint8_t * rawData,size_t size)107 void TestBufferInfo(uint8_t *rawData, size_t size)
108 {
109 CHECK_ERROR_RETURN(rawData == nullptr || size < LIMITSIZE);
110 MessageParcel data;
111 data.WriteRawData(rawData, size);
112 const int32_t MAX_BUFF_SIZE = 1024 * 1024;
113 int32_t dataSize = (data.ReadInt32() % MAX_BUFF_SIZE) + 1;
114 auto sharedBuffer = make_shared<SharedBuffer>(dataSize);
115 sharedBuffer->GetSize();
116 sharedBuffer->GetFd();
117 sharedBuffer->Initialize();
118 sharedBuffer->GetSize();
119 sharedBuffer->CopyFrom(rawData, size);
120 sharedBuffer->GetFd();
121 sharedBuffer->Reset();
122 bool isHighQuality = data.ReadBool();
123 bool isCloudImageEnhanceSupported = false;
124 BufferInfo info(sharedBuffer, dataSize, isHighQuality, isCloudImageEnhanceSupported);
125 info.GetDataSize();
126 info.IsHighQuality();
127 info.GetIPCFileDescriptor();
128 }
129 } // namespace CameraStandard
130 } // namespace OHOS
131
132 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)133 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
134 {
135 /* Run your code on data */
136 OHOS::CameraStandard::DeferredProcessingFuzzTest(data, size);
137 OHOS::CameraStandard::TestBufferInfo(data, size);
138 return 0;
139 }
140
141