1 /*
2 * Copyright (c) 2022-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 "receivefds_fuzzer.h"
17 #include <string>
18 #include "securec.h"
19 #include "fd_holder_internal.h"
20
21 namespace OHOS {
22 const uint8_t* BASE_DATA = nullptr;
23 size_t g_baseSize = 0;
24 size_t g_basePos = 0;
25
GetData()26 template <class T> T GetData()
27 {
28 T object{};
29 size_t objectSize = sizeof(object);
30 if ((BASE_DATA == nullptr) || (objectSize > g_baseSize - g_basePos)) {
31 return object;
32 }
33 errno_t ret = memcpy_s(&object, objectSize, BASE_DATA + g_basePos, objectSize);
34 if (ret != EOK) {
35 return {};
36 }
37 g_basePos += objectSize;
38 return object;
39 }
40
FuzzReceiveFds(const uint8_t * data,size_t size)41 bool FuzzReceiveFds(const uint8_t* data, size_t size)
42 {
43 bool result = false;
44 BASE_DATA = data;
45 g_baseSize = size;
46 g_basePos = 0;
47 if (size > sizeof(struct iovec)) {
48 struct iovec iovec = GetData<struct iovec>();
49 if (!ReceiveFds(1, iovec, nullptr, false, nullptr)) {
50 result = true;
51 }
52 }
53 return result;
54 }
55 }
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60 /* Run your code on data */
61 OHOS::FuzzReceiveFds(data, size);
62 return 0;
63 }
64