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 #include <fuzzer/FuzzedDataProvider.h>
27
28 using namespace std;
29
30 namespace OHOS {
31 namespace CameraStandard {
32 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStreamCapture";
33 const size_t LIMITCOUNT = 4;
34 const int32_t LIMITSIZE = 2;
35 const int32_t PHOTO_WIDTH = 1280;
36 const int32_t PHOTO_HEIGHT = 960;
37 const int32_t PHOTO_FORMAT = 2000;
38 bool g_isStreamCapturePermission = false;
StreamCaptureFuzzTestGetPermission()39 void StreamCaptureFuzzTestGetPermission()
40 {
41 if (!g_isStreamCapturePermission) {
42 uint64_t tokenId;
43 const char *perms[0];
44 perms[0] = "ohos.permission.CAMERA";
45 NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
46 .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
47 };
48 tokenId = GetAccessTokenId(&infoInstance);
49 SetSelfTokenID(tokenId);
50 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
51 g_isStreamCapturePermission = true;
52 }
53 }
54
StreamCaptureFuzzTest(uint8_t * rawData,size_t size)55 void StreamCaptureFuzzTest(uint8_t *rawData, size_t size)
56 {
57 CHECK_ERROR_RETURN(rawData == nullptr || size < LIMITSIZE);
58 StreamCaptureFuzzTestGetPermission();
59 FuzzedDataProvider fdp(rawData, size);
60
61 int32_t itemCount = 10;
62 int32_t dataSize = 100;
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 = fdp.ConsumeFloatingPoint<float>();
71 ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, 1);
72
73 int32_t sensorOrientation = fdp.ConsumeIntegral<int32_t>();
74 ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, 1);
75
76 int32_t cameraPosition = fdp.ConsumeIntegral<int32_t>();
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_ERROR_RETURN_LOG(!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data)),
86 "StreamCaptureFuzzer: EncodeCameraMetadata Error");
87 data.RewindRead(0);
88
89 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
90 CHECK_ERROR_RETURN_LOG(!photoSurface, "StreamCaptureFuzzer: Create photoSurface Error");
91 sptr<IBufferProducer> producer = photoSurface->GetProducer();
92 sptr<HStreamCapture> streamcapture = new HStreamCapture(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
93 CHECK_ERROR_RETURN(streamcapture == nullptr);
94 streamcapture->Release();
95 }
96 } // namespace CameraStandard
97 } // namespace OHOS
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
101 {
102 /* Run your code on data */
103 OHOS::CameraStandard::StreamCaptureFuzzTest(data, size);
104 return 0;
105 }
106
107