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