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_restore_param_fuzzer.h"
17 #include "hcamera_restore_param.h"
18 #include "hstream_capture.h"
19 #include "message_parcel.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 #include "accesstoken_kit.h"
23 #include "camera_metadata_info.h"
24 #include "metadata_utils.h"
25 
26 namespace {
27 
28 const int32_t LIMITSIZE = 20;
29 
30 }
31 
32 namespace OHOS {
33 namespace CameraStandard {
34 
35 bool CameraRestoreParamFuzzer::hasPermission = false;
36 HCameraRestoreParam *CameraRestoreParamFuzzer::fuzz = nullptr;
37 
CheckPermission()38 void CameraRestoreParamFuzzer::CheckPermission()
39 {
40     if (!hasPermission) {
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         hasPermission = true;
51     }
52 }
53 
Test(uint8_t * rawData,size_t size)54 void CameraRestoreParamFuzzer::Test(uint8_t *rawData, size_t size)
55 {
56     if (rawData == nullptr || size < LIMITSIZE) {
57         return;
58     }
59     CheckPermission();
60 
61     if (fuzz == nullptr) {
62         std::string clientName;
63         std::string cameraId;
64         fuzz = new HCameraRestoreParam(clientName, cameraId);
65     }
66 
67     MessageParcel data;
68     data.WriteRawData(rawData, size);
69 
70     data.RewindRead(0);
71     int32_t opMode = data.ReadInt32();
72     fuzz->SetCameraOpMode(opMode);
73 
74     data.RewindRead(0);
75     int foldStaus = data.ReadInt32();
76     fuzz->SetFoldStatus(foldStaus);
77 
78     data.RewindRead(0);
79     timeval closeCameraTime = {
80         data.ReadUint32(),
81         data.ReadUint32(),
82     };
83     fuzz->SetCloseCameraTime(closeCameraTime);
84 
85     data.RewindRead(0);
86     int activeTime = data.ReadInt32();
87     fuzz->SetStartActiveTime(activeTime);
88 
89     RestoreParamTypeOhos restoreParamType = RestoreParamTypeOhos::NO_NEED_RESTORE_PARAM_OHOS;
90     fuzz->SetRestoreParamType(restoreParamType);
91 
92     std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
93     fuzz->SetSetting(settings);
94 
95     std::vector<StreamInfo_V1_1> streamInfos;
96     fuzz->SetStreamInfo(streamInfos);
97 
98     data.RewindRead(0);
99     auto obj = std::make_unique<int32_t>(data.ReadInt32());
100     const void *objectId = obj.get();
101     fuzz->IncStrongRef(objectId);
102     fuzz->IncWeakRef(objectId);
103     fuzz->AttemptIncStrong(objectId);
104     fuzz->AttemptIncStrongRef(objectId);
105     fuzz->DecStrongRef(objectId);
106     fuzz->DecWeakRef(objectId);
107     fuzz->AttemptAcquire(objectId);
108 }
109 
110 } // namespace CameraStandard
111 } // namespace OHOS
112 
113 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)114 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
115 {
116     /* Run your code on data */
117     OHOS::CameraStandard::CameraRestoreParamFuzzer::Test(data, size);
118     return 0;
119 }