• 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 "hstream_metadata_stub_fuzzer.h"
17 #include "camera_log.h"
18 #include "hstream_capture.h"
19 #include "iservice_registry.h"
20 #include "message_parcel.h"
21 #include "accesstoken_kit.h"
22 #include "camera_metadata_info.h"
23 #include "metadata_utils.h"
24 #include "iconsumer_surface.h"
25 #include "securec.h"
26 #include "stream_metadata_callback_proxy.h"
27 
28 namespace OHOS {
29 namespace CameraStandard {
30 static constexpr int32_t MAX_CODE_LEN  = 512;
31 static constexpr int32_t MIN_SIZE_NUM = 4;
32 static const uint8_t* RAW_DATA = nullptr;
33 const size_t THRESHOLD = 10;
34 static size_t g_dataSize = 0;
35 static size_t g_pos;
36 static const std::u16string g_interfaceToken = u"OHOS.CameraStandard.IStreamMetadata";
37 static constexpr int32_t MAX_CODE_NUM = 7;
38 
39 std::shared_ptr<HStreamMetadataStubFuzz> HStreamMetadataStubFuzzer::fuzz_{nullptr};
40 
41 /*
42 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
43 * tips: only support basic type
44 */
45 template<class T>
GetData()46 T GetData()
47 {
48     T object {};
49     size_t objectSize = sizeof(object);
50     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
51         return object;
52     }
53     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
54     if (ret != EOK) {
55         return {};
56     }
57     g_pos += objectSize;
58     return object;
59 }
60 
61 template<class T>
GetArrLength(T & arr)62 uint32_t GetArrLength(T& arr)
63 {
64     if (arr == nullptr) {
65         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
66         return 0;
67     }
68     return sizeof(arr) / sizeof(arr[0]);
69 }
70 
OnRemoteRequest(int32_t code)71 void HStreamMetadataStubFuzzer::OnRemoteRequest(int32_t code)
72 {
73     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
74         return;
75     }
76     fuzz_ = std::make_shared<HStreamMetadataStubFuzz>();
77     CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
78     MessageParcel reply;
79     MessageOption option;
80     MessageParcel dataMessageParcel;
81     dataMessageParcel.WriteInterfaceToken(HStreamMetadataStubFuzz::GetDescriptor());
82     dataMessageParcel.WriteBuffer(RAW_DATA + sizeof(uint32_t), g_dataSize - sizeof(uint32_t));
83     dataMessageParcel.RewindRead(0);
84     fuzz_->OnRemoteRequest(code, dataMessageParcel, reply, option);
85 }
86 
Test()87 void Test()
88 {
89     auto hstreamMetadataStub = std::make_unique<HStreamMetadataStubFuzzer>();
90     if (hstreamMetadataStub == nullptr) {
91         MEDIA_INFO_LOG("hstreamMetadataStub is null");
92         return;
93     }
94     for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) {
95         hstreamMetadataStub->OnRemoteRequest(i);
96     }
97     MessageParcel dataMessageParcel;
98     MessageParcel reply;
99     MessageOption option;
100     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101     static const int32_t audioPolicyServiceId = 3009;
102     auto object = samgr->GetSystemAbility(audioPolicyServiceId);
103     auto proxy = std::make_shared<StreamMetadataCallbackProxy>(object);
104     dataMessageParcel.WriteInterfaceToken(g_interfaceToken);
105     dataMessageParcel.WriteRemoteObject(proxy->AsObject());
106     HStreamMetadataStubFuzzer::fuzz_->OnRemoteRequestInner(
107         static_cast<uint32_t>(IStreamMetadataIpcCode::COMMAND_SET_CALLBACK), dataMessageParcel, reply, option);
108 }
109 
110 typedef void (*TestFuncs[1])();
111 
112 TestFuncs g_testFuncs = {
113     Test,
114 };
115 
FuzzTest(const uint8_t * rawData,size_t size)116 bool FuzzTest(const uint8_t* rawData, size_t size)
117 {
118     // initialize data
119     RAW_DATA = rawData;
120     g_dataSize = size;
121     g_pos = 0;
122 
123     uint32_t code = GetData<uint32_t>();
124     uint32_t len = GetArrLength(g_testFuncs);
125     if (len > 0) {
126         g_testFuncs[code % len]();
127     } else {
128         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
129     }
130 
131     return true;
132 }
133 } // namespace CameraStandard
134 } // namespace OHOS
135 
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
138 {
139     if (size < OHOS::CameraStandard::THRESHOLD) {
140         return 0;
141     }
142 
143     OHOS::CameraStandard::FuzzTest(data, size);
144     return 0;
145 }