• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "metadata_utils.h"
18 #include "iconsumer_surface.h"
19 #include "ipc_skeleton.h"
20 #include "access_token.h"
21 #include "hap_token_info.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 using namespace std;
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStreamCapture";
30 const size_t LIMITCOUNT = 4;
31 const int32_t LIMITSIZE = 2;
32 const int32_t PHOTO_WIDTH = 1280;
33 const int32_t PHOTO_HEIGHT = 960;
34 const int32_t PHOTO_FORMAT = 2000;
35 bool g_isStreamCapturePermission = false;
36 HStreamCapture *fuzzStreamcapture = nullptr;
StreamCaptureFuzzTestGetPermission()37 void StreamCaptureFuzzTestGetPermission()
38 {
39     if (!g_isStreamCapturePermission) {
40         uint64_t tokenId;
41         const char *perms[0];
42         perms[0] = "ohos.permission.CAMERA";
43         NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
44             .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
45         };
46         tokenId = GetAccessTokenId(&infoInstance);
47         SetSelfTokenID(tokenId);
48         OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
49         g_isStreamCapturePermission = true;
50     }
51 }
52 
StreamCaptureFuzzTest(uint8_t * rawData,size_t size)53 void StreamCaptureFuzzTest(uint8_t *rawData, size_t size)
54 {
55     if (rawData == nullptr || size < LIMITSIZE) {
56         return;
57     }
58     StreamCaptureFuzzTestGetPermission();
59 
60     int32_t itemCount = 0;
61     int32_t dataSize = 0;
62     int32_t *streams = reinterpret_cast<int32_t *>(rawData);
63     std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
64     ability = std::make_shared<OHOS::Camera::CameraMetadata>(itemCount, dataSize);
65     ability->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, streams, size / LIMITCOUNT);
66     int32_t compensationRange[2] = {rawData[0], rawData[1]};
67     ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, compensationRange,
68                       sizeof(compensationRange) / sizeof(compensationRange[0]));
69     float focalLength = rawData[0];
70     ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, 1);
71 
72     int32_t sensorOrientation = rawData[0];
73     ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, 1);
74 
75     int32_t cameraPosition = rawData[0];
76     ability->addEntry(OHOS_ABILITY_CAMERA_POSITION, &cameraPosition, 1);
77 
78     const camera_rational_t aeCompensationStep[] = {{rawData[0], rawData[1]}};
79     ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep,
80                       sizeof(aeCompensationStep) / sizeof(aeCompensationStep[0]));
81 
82     MessageParcel data;
83     data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
84     if (!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data))) {
85         return;
86     }
87     data.RewindRead(0);
88     MessageParcel reply;
89     MessageOption option;
90 
91     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
92     if (photoSurface == nullptr) {
93         return;
94     }
95     sptr<IBufferProducer> producer = photoSurface->GetProducer();
96     if (fuzzStreamcapture == nullptr) {
97         fuzzStreamcapture = new(std::nothrow) HStreamCapture(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
98     }
99     uint32_t code = 0;
100     fuzzStreamcapture->OnRemoteRequest(code, data, reply, option);
101 }
102 } // namespace CameraStandard
103 } // namespace OHOS
104 
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
107 {
108     /* Run your code on data */
109     OHOS::CameraStandard::StreamCaptureFuzzTest(data, size);
110     return 0;
111 }
112 
113