• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_ability_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "securec.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include "token_setproc.h"
24 #include "nativetoken_kit.h"
25 #include "accesstoken_kit.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 static constexpr int32_t MAX_CODE_LEN = 512;
30 static constexpr int32_t MIN_SIZE_NUM = 4;
31 static const uint8_t* RAW_DATA = nullptr;
32 const size_t THRESHOLD = 10;
33 static size_t g_dataSize = 0;
34 static size_t g_pos;
35 
36 const int32_t NUM_TWO = 2;
37 std::shared_ptr<CameraAbility> CameraAbilityFuzzer::fuzz_{nullptr};
38 
39 /*
40 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
41 * tips: only support basic type
42 */
43 template<class T>
GetData()44 T GetData()
45 {
46     T object {};
47     size_t objectSize = sizeof(object);
48     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
49         return object;
50     }
51     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
52     if (ret != EOK) {
53         return {};
54     }
55     g_pos += objectSize;
56     return object;
57 }
58 
59 template<class T>
GetArrLength(T & arr)60 uint32_t GetArrLength(T& arr)
61 {
62     if (arr == nullptr) {
63         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
64         return 0;
65     }
66     return sizeof(arr) / sizeof(arr[0]);
67 }
68 
GetPermission()69 void GetPermission()
70 {
71     uint64_t tokenId;
72     const char* perms[2];
73     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
74     perms[1] = "ohos.permission.CAMERA";
75     NativeTokenInfoParams infoInstance = {
76         .dcapsNum = 0,
77         .permsNum = 2,
78         .aclsNum = 0,
79         .dcaps = NULL,
80         .perms = perms,
81         .acls = NULL,
82         .processName = "native_camera_tdd",
83         .aplStr = "system_basic",
84     };
85     tokenId = GetAccessTokenId(&infoInstance);
86     SetSelfTokenID(tokenId);
87     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
88 }
89 
CameraAbilityFuzzTest()90 void CameraAbilityFuzzer::CameraAbilityFuzzTest()
91 {
92     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
93         return;
94     }
95 
96     GetPermission();
97     fuzz_ = std::make_shared<CameraAbility>();
98     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
99     fuzz_->HasFlash();
100     FlashMode flashMode = static_cast<FlashMode>(
101         GetData<int32_t>() % (FlashMode::FLASH_MODE_ALWAYS_OPEN + NUM_TWO));
102     fuzz_->IsFlashModeSupported(flashMode);
103     fuzz_->GetSupportedFlashModes();
104     ExposureMode exposureMode = static_cast<ExposureMode>(
105         GetData<int32_t>() % (ExposureMode::EXPOSURE_MODE_CONTINUOUS_AUTO + NUM_TWO));
106     fuzz_->IsExposureModeSupported(exposureMode);
107     fuzz_->GetSupportedExposureModes();
108     fuzz_->GetExposureBiasRange();
109     fuzz_->GetSupportedFocusModes();
110     FocusMode focusMode = static_cast<FocusMode>(
111         GetData<int32_t>() % (FocusMode::FOCUS_MODE_LOCKED + NUM_TWO));
112     fuzz_->IsFocusModeSupported(focusMode);
113     fuzz_->GetZoomRatioRange();
114     fuzz_->GetSupportedBeautyTypes();
115     BeautyType beautyType = static_cast<BeautyType>(
116         GetData<int32_t>() % (BeautyType::SKIN_TONE + NUM_TWO));
117     fuzz_->GetSupportedBeautyRange(beautyType);
118     fuzz_->GetSupportedColorEffects();
119     fuzz_->GetSupportedColorSpaces();
120     fuzz_->IsMacroSupported();
121     fuzz_->GetSupportedPortraitEffects();
122     fuzz_->GetSupportedVirtualApertures();
123     fuzz_->GetSupportedPhysicalApertures();
124     fuzz_->GetSupportedStabilizationMode();
125     VideoStabilizationMode stabilizationMode = static_cast<VideoStabilizationMode>(
126         GetData<int32_t>() % (VideoStabilizationMode::AUTO + NUM_TWO));
127     fuzz_->IsVideoStabilizationModeSupported(stabilizationMode);
128     fuzz_->GetSupportedExposureRange();
129     SceneFeature feature = static_cast<SceneFeature>(
130         GetData<int32_t>() % (SceneFeature::FEATURE_ENUM_MAX + NUM_TWO));
131     fuzz_->IsFeatureSupported(feature);
132     fuzz_->IsLcdFlashSupported();
133 }
134 
Test()135 void Test()
136 {
137     auto cameraAbility = std::make_unique<CameraAbilityFuzzer>();
138     if (cameraAbility == nullptr) {
139         MEDIA_INFO_LOG("cameraAbility is null");
140         return;
141     }
142     cameraAbility->CameraAbilityFuzzTest();
143 }
144 
145 typedef void (*TestFuncs[1])();
146 
147 TestFuncs g_testFuncs = {
148     Test,
149 };
150 
FuzzTest(const uint8_t * rawData,size_t size)151 bool FuzzTest(const uint8_t* rawData, size_t size)
152 {
153     // initialize data
154     RAW_DATA = rawData;
155     g_dataSize = size;
156     g_pos = 0;
157 
158     uint32_t code = GetData<uint32_t>();
159     uint32_t len = GetArrLength(g_testFuncs);
160     if (len > 0) {
161         g_testFuncs[code % len]();
162     } else {
163         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
164     }
165 
166     return true;
167 }
168 } // namespace CameraStandard
169 } // namespace OHOS
170 
171 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)172 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
173 {
174     if (size < OHOS::CameraStandard::THRESHOLD) {
175         return 0;
176     }
177 
178     OHOS::CameraStandard::FuzzTest(data, size);
179     return 0;
180 }