• 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_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 std::shared_ptr<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     std::string clientName;
62     std::string cameraId;
63     fuzz_ = std::make_shared<HCameraRestoreParam>(clientName, cameraId);
64     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
65 
66     MessageParcel data;
67     data.WriteRawData(rawData, size);
68 
69     data.RewindRead(0);
70     int32_t opMode = data.ReadInt32();
71     fuzz_->SetCameraOpMode(opMode);
72 
73     data.RewindRead(0);
74     int foldStaus = data.ReadInt32();
75     fuzz_->SetFoldStatus(foldStaus);
76 
77     data.RewindRead(0);
78     timeval closeCameraTime = {
79         data.ReadUint32(),
80         data.ReadUint32(),
81     };
82     fuzz_->SetCloseCameraTime(closeCameraTime);
83 
84     data.RewindRead(0);
85     int activeTime = data.ReadInt32();
86     fuzz_->SetStartActiveTime(activeTime);
87 
88     RestoreParamTypeOhos restoreParamType = RestoreParamTypeOhos::NO_NEED_RESTORE_PARAM_OHOS;
89     fuzz_->SetRestoreParamType(restoreParamType);
90 
91     std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
92     fuzz_->SetSetting(settings);
93 
94     std::vector<StreamInfo_V1_1> streamInfos;
95     fuzz_->SetStreamInfo(streamInfos);
96 
97     data.RewindRead(0);
98     auto obj = std::make_unique<int32_t>(data.ReadInt32());
99     const void *objectId = obj.get();
100     fuzz_->IncStrongRef(objectId);
101     fuzz_->IncWeakRef(objectId);
102     fuzz_->AttemptIncStrong(objectId);
103     fuzz_->AttemptIncStrongRef(objectId);
104     fuzz_->DecStrongRef(objectId);
105     fuzz_->DecWeakRef(objectId);
106     fuzz_->AttemptAcquire(objectId);
107 }
108 
109 } // namespace CameraStandard
110 } // namespace OHOS
111 
112 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
114 {
115     /* Run your code on data */
116     OHOS::CameraStandard::CameraRestoreParamFuzzer::Test(data, size);
117     return 0;
118 }