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 "stream_capture_fuzzer.h"
17 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
18 #include "metadata_utils.h"
19 #include "iconsumer_surface.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 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStreamCapture";
31 const size_t LIMITCOUNT = 4;
32 const int32_t LIMITSIZE = 2;
33 const int32_t PHOTO_WIDTH = 1280;
34 const int32_t PHOTO_HEIGHT = 960;
35 const int32_t PHOTO_FORMAT = 2000;
36 bool g_isStreamCapturePermission = false;
37 HStreamCapture *fuzzStreamcapture = nullptr;
StreamCaptureFuzzTestGetPermission()38 void StreamCaptureFuzzTestGetPermission()
39 {
40 if (!g_isStreamCapturePermission) {
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_isStreamCapturePermission = true;
51 }
52 }
53
StreamCaptureFuzzTest(uint8_t * rawData,size_t size)54 void StreamCaptureFuzzTest(uint8_t *rawData, size_t size)
55 {
56 if (rawData == nullptr || size < LIMITSIZE) {
57 return;
58 }
59 StreamCaptureFuzzTestGetPermission();
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 "StreamCaptureFuzzer: EncodeCameraMetadata Error");
87 data.RewindRead(0);
88 MessageParcel reply;
89 MessageOption option;
90
91 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
92 CHECK_AND_RETURN_LOG(photoSurface, "StreamCaptureFuzzer: Create photoSurface Error");
93 sptr<IBufferProducer> producer = photoSurface->GetProducer();
94 if (fuzzStreamcapture == nullptr) {
95 fuzzStreamcapture = new(std::nothrow) HStreamCapture(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
96 if (fuzzStreamcapture == nullptr) {
97 return;
98 }
99 }
100 uint32_t code = 0;
101 fuzzStreamcapture->OnRemoteRequest(code, data, reply, option);
102 }
103 } // namespace CameraStandard
104 } // namespace OHOS
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::CameraStandard::StreamCaptureFuzzTest(data, size);
111 return 0;
112 }
113
114