• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "systemtraversalparameter_fuzzer.h"
17 
18 #include <string>
19 #include "fuzz_utils.h"
20 #include "init.h"
21 #include "securec.h"
22 #include "init_param.h"
23 
FakeShowParam(ParamHandle handle,void * cookie)24 static void FakeShowParam(ParamHandle handle, void *cookie)
25 {
26     Cookie *nameAndValue = reinterpret_cast<Cookie*>(cookie);
27     char *name = nameAndValue->data;
28     unsigned int nameLen = nameAndValue->size;
29     char value[PARAM_CONST_VALUE_LEN_MAX] = {0};
30     SystemGetParameterName(handle, name, nameLen);
31     uint32_t size = PARAM_CONST_VALUE_LEN_MAX;
32     SystemGetParameterValue(handle, value, &size);
33     printf("\t%s = %s \n", name, value);
34 }
35 
36 namespace OHOS {
FuzzSystemTraversalParameter(const uint8_t * data,size_t size)37     bool FuzzSystemTraversalParameter(const uint8_t* data, size_t size)
38     {
39         bool result = false;
40         CloseStdout();
41         Cookie instance = {0, nullptr};
42         Cookie *cookie = &instance;
43         if (size == 0) {
44             return false;
45         }
46         if (size > PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX) {
47             return true;
48         }
49         cookie->data = static_cast<char *>(malloc(size));
50         if (cookie->data == nullptr) {
51             return true;
52         }
53         cookie->size = size;
54         std::string str(reinterpret_cast<const char*>(data), size - 1);
55         int ret = memcpy_s(cookie->data, size, str.c_str(), size);
56         if (ret != EOK) {
57             return false;
58         }
59         if (!SystemTraversalParameter(nullptr, FakeShowParam, reinterpret_cast<void*>(cookie))) {
60             result = true;
61         }
62         free(cookie->data);
63         cookie->data = nullptr;
64         cookie->size = 0;
65         return result;
66     }
67 }
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::FuzzSystemTraversalParameter(data, size);
74     return 0;
75 }
76