• 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_builder_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 std::shared_ptr<CameraAbilityBuilder> CameraAbilityBuilderFuzzer::fuzz_{nullptr};
37 
38 /*
39 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
40 * tips: only support basic type
41 */
42 template<class T>
GetData()43 T GetData()
44 {
45     T object {};
46     size_t objectSize = sizeof(object);
47     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
48         return object;
49     }
50     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
51     if (ret != EOK) {
52         return {};
53     }
54     g_pos += objectSize;
55     return object;
56 }
57 
58 template<class T>
GetArrLength(T & arr)59 uint32_t GetArrLength(T& arr)
60 {
61     if (arr == nullptr) {
62         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
63         return 0;
64     }
65     return sizeof(arr) / sizeof(arr[0]);
66 }
67 
CameraAbilityBuilderFuzzTest()68 void CameraAbilityBuilderFuzzer::CameraAbilityBuilderFuzzTest()
69 {
70     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
71         return;
72     }
73 
74     fuzz_ = std::make_shared<CameraAbilityBuilder>();
75     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
76     sptr<CameraAbility> ability;
77     sptr<CaptureSession> session;
78     fuzz_->SetOtherTag(ability, GetData<int32_t>(), session);
79 }
80 
Test()81 void Test()
82 {
83     auto cameraAbilityBuilder = std::make_unique<CameraAbilityBuilderFuzzer>();
84     if (cameraAbilityBuilder == nullptr) {
85         MEDIA_INFO_LOG("cameraAbilityBuilder is null");
86         return;
87     }
88     cameraAbilityBuilder->CameraAbilityBuilderFuzzTest();
89 }
90 
91 typedef void (*TestFuncs[1])();
92 
93 TestFuncs g_testFuncs = {
94     Test,
95 };
96 
FuzzTest(const uint8_t * rawData,size_t size)97 bool FuzzTest(const uint8_t* rawData, size_t size)
98 {
99     // initialize data
100     RAW_DATA = rawData;
101     g_dataSize = size;
102     g_pos = 0;
103 
104     uint32_t code = GetData<uint32_t>();
105     uint32_t len = GetArrLength(g_testFuncs);
106     if (len > 0) {
107         g_testFuncs[code % len]();
108     } else {
109         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
110     }
111 
112     return true;
113 }
114 
115 } // namespace CameraStandard
116 } // namespace OHOS
117 
118 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)119 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
120 {
121     if (size < OHOS::CameraStandard::THRESHOLD) {
122         return 0;
123     }
124 
125     OHOS::CameraStandard::FuzzTest(data, size);
126     return 0;
127 }