• 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 "camera_device_fuzzer.h"
17 #include "metadata_utils.h"
18 #include "ipc_skeleton.h"
19 #include "access_token.h"
20 #include "hap_token_info.h"
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 using namespace std;
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ICameraDeviceService";
29 const int32_t LIMITSIZE = 2;
30 
CameraDeviceFuzzTest(uint8_t * rawData,size_t size)31 void CameraDeviceFuzzTest(uint8_t *rawData, size_t size)
32 {
33     if (rawData == nullptr || size < LIMITSIZE) {
34         return;
35     }
36 
37     uint64_t tokenId;
38     const char *perms[0];
39     perms[0] = "ohos.permission.CAMERA";
40     NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
41         .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
42     };
43     tokenId = GetAccessTokenId(&infoInstance);
44     SetSelfTokenID(tokenId);
45     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
46 
47     uint32_t code = 4;
48     int32_t itemCount = 10;
49     int32_t dataSize = 100;
50     uint8_t *streams = rawData;
51     std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
52     ability = std::make_shared<OHOS::Camera::CameraMetadata>(itemCount, dataSize);
53     ability->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, streams, size);
54     int32_t compensationRange[2] = {rawData[0], rawData[1]};
55     ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, compensationRange,
56                       sizeof(compensationRange) / sizeof(compensationRange[0]));
57     float focalLength = rawData[0];
58     ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, sizeof(float));
59 
60     int32_t sensorOrientation = rawData[0];
61     ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, sizeof(int32_t));
62 
63     int32_t cameraPosition = rawData[0];
64     ability->addEntry(OHOS_ABILITY_CAMERA_POSITION, &cameraPosition, sizeof(int32_t));
65 
66     const camera_rational_t aeCompensationStep[] = {{rawData[0], rawData[1]}};
67     ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep,
68                       sizeof(aeCompensationStep) / sizeof(aeCompensationStep[0]));
69 
70     MessageParcel data;
71     data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
72     if (!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data))) {
73         return;
74     }
75     data.RewindRead(0);
76     MessageParcel reply;
77     MessageOption option;
78     sptr<HCameraHostManager> cameraHostManager = new(std::nothrow) HCameraHostManager(nullptr);
79     std::shared_ptr<HCameraDevice> cameraDevice =
80         std::make_shared<HCameraDevice>(cameraHostManager, "", 0);
81     cameraDevice->OnRemoteRequest(code, data, reply, option);
82 }
83 } // namespace CameraStandard
84 } // namespace OHOS
85 
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
88 {
89     /* Run your code on data */
90     OHOS::CameraStandard::CameraDeviceFuzzTest(data, size);
91     return 0;
92 }
93 
94