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 size_t LIMITCOUNT = 4;
30 const int32_t LIMITSIZE = 2;
31 bool g_isCameraDevicePermission = false;
32 sptr<HCameraHostManager> fuzzCameraHostManager = nullptr;
33 HCameraDevice *fuzzCameraDevice = nullptr;
34
CameraDeviceFuzzTestGetPermission()35 void CameraDeviceFuzzTestGetPermission()
36 {
37 if (!g_isCameraDevicePermission) {
38 uint64_t tokenId;
39 const char *perms[0];
40 perms[0] = "ohos.permission.CAMERA";
41 NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
42 .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
43 };
44 tokenId = GetAccessTokenId(&infoInstance);
45 SetSelfTokenID(tokenId);
46 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
47 g_isCameraDevicePermission = true;
48 }
49 }
50
CameraDeviceFuzzTest(uint8_t * rawData,size_t size)51 void CameraDeviceFuzzTest(uint8_t *rawData, size_t size)
52 {
53 if (rawData == nullptr || size < LIMITSIZE) {
54 return;
55 }
56 CameraDeviceFuzzTestGetPermission();
57
58 int32_t itemCount = 10;
59 int32_t dataSize = 100;
60 int32_t *streams = reinterpret_cast<int32_t *>(rawData);
61 std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
62 ability = std::make_shared<OHOS::Camera::CameraMetadata>(itemCount, dataSize);
63 ability->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, streams, size / LIMITCOUNT);
64 int32_t compensationRange[2] = {rawData[0], rawData[1]};
65 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, compensationRange,
66 sizeof(compensationRange) / sizeof(compensationRange[0]));
67 float focalLength = rawData[0];
68 ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, 1);
69
70 int32_t sensorOrientation = rawData[0];
71 ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, 1);
72
73 int32_t cameraPosition = rawData[0];
74 ability->addEntry(OHOS_ABILITY_CAMERA_POSITION, &cameraPosition, 1);
75
76 const camera_rational_t aeCompensationStep[] = {{rawData[0], rawData[1]}};
77 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep,
78 sizeof(aeCompensationStep) / sizeof(aeCompensationStep[0]));
79
80 MessageParcel data;
81 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
82 if (!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data))) {
83 return;
84 }
85 data.RewindRead(0);
86 MessageParcel reply;
87 MessageOption option;
88 if (fuzzCameraDevice == nullptr || fuzzCameraHostManager == nullptr) {
89 fuzzCameraHostManager = new(std::nothrow) HCameraHostManager(nullptr);
90 fuzzCameraDevice = new(std::nothrow) HCameraDevice(fuzzCameraHostManager, "", 0);
91 }
92 if (fuzzCameraDevice) {
93 uint32_t code = 4;
94 fuzzCameraDevice->OnRemoteRequest(code, data, reply, option);
95 }
96 }
97 } // namespace CameraStandard
98 } // namespace OHOS
99
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
102 {
103 /* Run your code on data */
104 OHOS::CameraStandard::CameraDeviceFuzzTest(data, size);
105 return 0;
106 }
107
108