• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 
21 #include "securec.h"
22 #include "avcontroller_callback_proxy.h"
23 #include "iavcontroller_callback.h"
24 #include "iremote_proxy.h"
25 #include "avsession_log.h"
26 #include "avsession_errors.h"
27 #include "avcontroller_callbackproxy_fuzzer.h"
28 
29 using namespace std;
30 using namespace OHOS;
31 using namespace OHOS::AVSession;
32 
33 static const int32_t MAX_CODE_TEST = 5;
34 static const int32_t MIN_SIZE_NUM = 10;
35 static const uint8_t *RAW_DATA = nullptr;
36 static size_t g_totalSize = 0;
37 static size_t g_sizePos;
38 
39 namespace {
40     /*
41     * describe: get data from FUZZ untrusted data(RAW_DATA) which size is according to sizeof(T)
42     * tips: only support basic type
43     */
44     template<class T>
GetData()45     T GetData()
46     {
47         T object {};
48         size_t objectSize = sizeof(object);
49         if (RAW_DATA == nullptr || objectSize > g_totalSize - g_sizePos) {
50             return object;
51         }
52         errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_sizePos, objectSize);
53         if (ret != EOK) {
54             return {};
55         }
56         g_sizePos += objectSize;
57         return object;
58     }
59 
60     template<class T>
GetArrLength(T & arr)61     uint32_t GetArrLength(T& arr)
62     {
63         if (arr == nullptr) {
64             SLOGE("%{public}s: The array length is equal to 0", __func__);
65             return 0;
66         }
67         return sizeof(arr) / sizeof(arr[0]);
68     }
69 
70     typedef void (*TestFuncs[1])();
71 
72     TestFuncs g_allFuncs = {
73         AVControllerCallbackProxySendRequest
74     };
75 
FuzzTest(const uint8_t * rawData,size_t size)76     bool FuzzTest(const uint8_t* rawData, size_t size)
77     {
78         if (rawData == nullptr) {
79             return false;
80         }
81 
82         // initialize data
83         RAW_DATA = rawData;
84         g_totalSize = size;
85         g_sizePos = 0;
86 
87         uint32_t code = GetData<uint32_t>();
88         uint32_t len = GetArrLength(g_allFuncs);
89         if (len > 0) {
90             g_allFuncs[code % len]();
91         } else {
92             SLOGE("%{public}s: The len length is equal to 0", __func__);
93         }
94 
95         return true;
96     }
97 }
98 
FuzzSendRequest()99 bool AvControllerCallbackProxyFuzzer::FuzzSendRequest()
100 {
101     uint32_t cmdCode = GetData<uint32_t>();
102     if (cmdCode >= MAX_CODE_TEST) {
103         return false;
104     }
105 
106     sptr<IRemoteObject> remoteObject = nullptr;
107     std::shared_ptr<AVControllerCallbackProxyFuzzerTest> avControllerCallbackProxy =
108         std::make_shared<AVControllerCallbackProxyFuzzerTest>(remoteObject);
109 
110     if (!avControllerCallbackProxy) {
111         return false;
112     }
113     MessageParcel parcel;
114     CHECK_AND_PRINT_LOG(parcel.WriteInterfaceToken(avControllerCallbackProxy->GetDescriptor()),
115         "write interface token failed");
116 
117     MessageParcel reply;
118     MessageOption option { MessageOption::TF_ASYNC };
119     auto remote = avControllerCallbackProxy->GetRemote();
120     if (remote == nullptr) {
121         SLOGD("remote is null");
122         return false;
123     }
124     parcel.WriteBuffer(RAW_DATA, g_sizePos);
125     g_sizePos += sizeof(uint32_t);
126     parcel.RewindRead(0);
127     int32_t result = AVSESSION_ERROR;
128     CHECK_AND_PRINT_LOG(remote != nullptr, "get remote service failed");
129     CHECK_AND_PRINT_LOG((result = remote->SendRequest(cmdCode, parcel, reply, option)) == 0,
130                         "send request failed");
131     return result == AVSESSION_SUCCESS;
132 }
133 
AVControllerCallbackProxySendRequest()134 void OHOS::AVSession::AVControllerCallbackProxySendRequest()
135 {
136     auto avControllerCallbackProxy = std::make_unique<AvControllerCallbackProxyFuzzer>();
137     if (avControllerCallbackProxy == nullptr) {
138         return;
139     }
140     avControllerCallbackProxy->FuzzSendRequest();
141 }
142 
143 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)144 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
145 {
146     if (size < MIN_SIZE_NUM) {
147         return 0;
148     }
149     /* Run your code on data */
150     FuzzTest(data, size);
151     return 0;
152 }
153 
154