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