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 "getvibratorcapacitystub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "accesstoken_kit.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "securec.h"
25 #include "token_setproc.h"
26
27 #include "miscdevice_service.h"
28
29 namespace OHOS {
30 namespace Sensors {
31 using namespace Security::AccessToken;
32 using Security::AccessToken::AccessTokenID;
33 namespace {
34 constexpr size_t U32_AT_SIZE = 12;
35 auto g_service = MiscdeviceDelayedSpSingleton<MiscdeviceService>::GetInstance();
36 const std::u16string VIBRATOR_INTERFACE_TOKEN = u"OHOS.Sensors.IMiscdeviceService";
37 static sptr<IRemoteObject> g_remote = new (std::nothrow) IPCObjectStub();
38 } // namespace
39
40 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)41 size_t GetObject(const uint8_t *data, size_t size, T &object)
42 {
43 size_t objectSize = sizeof(object);
44 if (objectSize > size) {
45 return 0;
46 }
47 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
48 if (ret != EOK) {
49 return 0;
50 }
51 return objectSize;
52 }
53
SetUpTestCase()54 void SetUpTestCase()
55 {
56 const char **perms = new (std::nothrow) const char *[1];
57 if (perms == nullptr) {
58 return;
59 }
60 perms[0] = "ohos.permission.VIBRATE";
61 TokenInfoParams infoInstance = {
62 .dcapsNum = 0,
63 .permsNum = 1,
64 .aclsNum = 0,
65 .dcaps = nullptr,
66 .perms = perms,
67 .acls = nullptr,
68 .processName = "GetVibratorCapacityStubFuzzTest",
69 .aplStr = "system_core",
70 };
71 uint64_t tokenId = GetAccessTokenId(&infoInstance);
72 SetSelfTokenID(tokenId);
73 AccessTokenKit::ReloadNativeTokenInfo();
74 delete[] perms;
75 }
76
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)77 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
78 {
79 SetUpTestCase();
80 if (g_service == nullptr) {
81 return false;
82 }
83 MessageParcel datas;
84 datas.WriteInterfaceToken(VIBRATOR_INTERFACE_TOKEN);
85 VibratorIdentifierIPC identifier;
86 size_t startPos = 0;
87 startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
88 GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
89 datas.WriteParcelable(&identifier);
90 datas.WriteBuffer(data, size);
91 datas.RewindRead(0);
92 MessageParcel reply;
93 MessageOption option;
94 g_service->OnStartFuzz();
95 g_service->OnRemoteRequest(static_cast<uint32_t>(IMiscdeviceServiceIpcCode::COMMAND_GET_VIBRATOR_CAPACITY),
96 datas, reply, option);
97 return true;
98 }
99 } // namespace Sensors
100 } // namespace OHOS
101
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
103 {
104 /* Run your code on data */
105 if (data == nullptr) {
106 return 0;
107 }
108
109 /* Validate the length of size */
110 if (size < OHOS::Sensors::U32_AT_SIZE) {
111 return 0;
112 }
113
114 OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
115 return 0;
116 }
117