• 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 "capture_output_fuzzer.h"
17 #include "camera_input.h"
18 #include "camera_log.h"
19 #include "camera_photo_proxy.h"
20 #include "capture_input.h"
21 #include "capture_output.h"
22 #include "capture_scene_const.h"
23 #include "input/camera_manager.h"
24 #include "message_parcel.h"
25 #include "refbase.h"
26 #include <cstddef>
27 #include <cstdint>
28 #include <memory>
29 #include "token_setproc.h"
30 #include "nativetoken_kit.h"
31 #include "accesstoken_kit.h"
32 #include "securec.h"
33 
34 namespace OHOS {
35 namespace CameraStandard {
36 static constexpr int32_t MAX_CODE_LEN = 512;
37 static constexpr int32_t MIN_SIZE_NUM = 4;
38 static const uint8_t* RAW_DATA = nullptr;
39 const size_t THRESHOLD = 10;
40 static size_t g_dataSize = 0;
41 static size_t g_pos;
42 
43 std::shared_ptr<CaptureOutput> CaptureOutputFuzzer::fuzz_{nullptr};
44 
45 /*
46 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
47 * tips: only support basic type
48 */
49 template<class T>
GetData()50 T GetData()
51 {
52     T object {};
53     size_t objectSize = sizeof(object);
54     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
55         return object;
56     }
57     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
58     if (ret != EOK) {
59         return {};
60     }
61     g_pos += objectSize;
62     return object;
63 }
64 
65 template<class T>
GetArrLength(T & arr)66 uint32_t GetArrLength(T& arr)
67 {
68     if (arr == nullptr) {
69         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
70         return 0;
71     }
72     return sizeof(arr) / sizeof(arr[0]);
73 }
74 
CaptureOutputFuzzTest()75 void CaptureOutputFuzzer::CaptureOutputFuzzTest()
76 {
77     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
78         return;
79     }
80     int32_t randomInt = GetData<int32_t>();
81     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
82     CHECK_ERROR_RETURN_LOG(photoSurface, "PhotoOutputFuzzer: create photoSurface Error");
83     sptr<IBufferProducer> bufferProducer = photoSurface->GetProducer();
84     CHECK_ERROR_RETURN_LOG(bufferProducer, "GetProducer Error");
85     sptr<IStreamCommon> stream;
86     CaptureOutputType randomOutputType = static_cast<CaptureOutputType>(
87     (randomInt % static_cast<int32_t>(CAPTURE_OUTPUT_TYPE_MAX)));
88     fuzz_ = std::make_shared<CaptureOutputTest>(randomOutputType, StreamType::CAPTURE, bufferProducer, stream);
89     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
90     fuzz_->GetBufferProducer();
91     int32_t pid = GetData<int32_t>();
92     fuzz_->OnCameraServerDied(pid);
93     fuzz_->IsStreamCreated();
94     DepthProfile depthProfile;
95     fuzz_->SetDepthProfile(depthProfile);
96     fuzz_->GetDepthProfile();
97     fuzz_->ClearProfiles();
98     fuzz_->AddTag(CaptureOutput::Tag::DYNAMIC_PROFILE);
99     fuzz_->RemoveTag(CaptureOutput::Tag::DYNAMIC_PROFILE);
100 }
101 
Test()102 void Test()
103 {
104     auto captureOutput = std::make_unique<CaptureOutputFuzzer>();
105     if (captureOutput == nullptr) {
106         MEDIA_INFO_LOG("captureOutput is null");
107         return;
108     }
109     captureOutput->CaptureOutputFuzzTest();
110 }
111 
112 typedef void (*TestFuncs[1])();
113 
114 TestFuncs g_testFuncs = {
115     Test,
116 };
117 
FuzzTest(const uint8_t * rawData,size_t size)118 bool FuzzTest(const uint8_t* rawData, size_t size)
119 {
120     // initialize data
121     RAW_DATA = rawData;
122     g_dataSize = size;
123     g_pos = 0;
124 
125     uint32_t code = GetData<uint32_t>();
126     uint32_t len = GetArrLength(g_testFuncs);
127     if (len > 0) {
128         g_testFuncs[code % len]();
129     } else {
130         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
131     }
132 
133     return true;
134 }
135 } // namespace CameraStandard
136 } // namespace OHOS
137 
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
140 {
141     if (size < OHOS::CameraStandard::THRESHOLD) {
142         return 0;
143     }
144 
145     OHOS::CameraStandard::FuzzTest(data, size);
146     return 0;
147 }