1 /*
2 * Copyright (c) 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 "preprocess_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22
23 #include "vibrator_agent.h"
24
25 namespace OHOS {
26 namespace {
27 constexpr size_t DATA_MIN_SIZE = 20;
28 } // namespace
29
30 using namespace Security::AccessToken;
31 using Security::AccessToken::AccessTokenID;
32
33 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)34 size_t GetObject(const uint8_t *data, size_t size, T &object)
35 {
36 size_t objectSize = sizeof(object);
37 if (objectSize > size) {
38 return 0;
39 }
40 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
41 if (ret != EOK) {
42 return 0;
43 }
44 return objectSize;
45 }
46
SetUpTestCase()47 void SetUpTestCase()
48 {
49 const char **perms = new (std::nothrow) const char *[1];
50 if (perms == nullptr) {
51 return;
52 }
53 perms[0] = "ohos.permission.VIBRATE";
54 TokenInfoParams infoInstance = {
55 .dcapsNum = 0,
56 .permsNum = 1,
57 .aclsNum = 0,
58 .dcaps = nullptr,
59 .perms = perms,
60 .acls = nullptr,
61 .processName = "PreProcessFuzzTest",
62 .aplStr = "system_core",
63 };
64 uint64_t tokenId = GetAccessTokenId(&infoInstance);
65 SetSelfTokenID(tokenId);
66 AccessTokenKit::ReloadNativeTokenInfo();
67 delete[] perms;
68 }
69
PreProcessFuzzTest(const uint8_t * data,size_t size)70 void PreProcessFuzzTest(const uint8_t *data, size_t size)
71 {
72 if (data == nullptr || size < DATA_MIN_SIZE) {
73 return;
74 }
75 SetUpTestCase();
76 VibratorFileDescription fileDescription { 0 };
77 size_t startPos = 0;
78 startPos += GetObject<int32_t>(data + startPos, size - startPos, fileDescription.fd);
79 startPos += GetObject<int64_t>(data + startPos, size - startPos, fileDescription.offset);
80 startPos += GetObject<int64_t>(data + startPos, size - startPos, fileDescription.length);
81
82 VibratorPackage package { 0 };
83 startPos += GetObject<int32_t>(data + startPos, size - startPos, package.patternNum);
84 GetObject<int32_t>(data + startPos, size - startPos, package.packageDuration);
85 OHOS::Sensors::PreProcess(fileDescription, package);
86 }
87 } // namespace OHOS
88
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
90 {
91 OHOS::PreProcessFuzzTest(data, size);
92 return 0;
93 }