• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "vibratestub_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 = 4;
35 auto g_service = MiscdeviceDelayedSpSingleton<MiscdeviceService>::GetInstance();
36 const std::u16string VIBRATOR_INTERFACE_TOKEN = u"OHOS.Sensors.IMiscdeviceService";
37 } // namespace
38 
39 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)40 size_t GetObject(const uint8_t *data, size_t size, T &object)
41 {
42     size_t objectSize = sizeof(object);
43     if (objectSize > size) {
44         return 0;
45     }
46     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
47     if (ret != EOK) {
48         return 0;
49     }
50     return objectSize;
51 }
52 
SetUpTestCase()53 void SetUpTestCase()
54 {
55     const char **perms = new (std::nothrow) const char *[1];
56     if (perms == nullptr) {
57         return;
58     }
59     perms[0] = "ohos.permission.VIBRATE";
60     TokenInfoParams infoInstance = {
61         .dcapsNum = 0,
62         .permsNum = 1,
63         .aclsNum = 0,
64         .dcaps = nullptr,
65         .perms = perms,
66         .acls = nullptr,
67         .processName = "VibrateStubFuzzTest",
68         .aplStr = "system_core",
69     };
70     uint64_t tokenId = GetAccessTokenId(&infoInstance);
71     SetSelfTokenID(tokenId);
72     AccessTokenKit::ReloadNativeTokenInfo();
73     delete[] perms;
74 }
75 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)76 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
77 {
78     SetUpTestCase();
79     if (g_service == nullptr) {
80         return false;
81     }
82     MessageParcel datas;
83     datas.WriteInterfaceToken(VIBRATOR_INTERFACE_TOKEN);
84     VibratorIdentifierIPC identifier;
85     size_t startPos = 0;
86     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
87     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
88     datas.WriteParcelable(&identifier);
89     int32_t timeOut = 0;
90     startPos += GetObject<int32_t>(data + startPos, size - startPos, timeOut);
91     datas.WriteInt32(timeOut);
92     int32_t usage = 0;
93     startPos += GetObject<int32_t>(data + startPos, size - startPos, usage);
94     datas.WriteInt32(usage);
95     int32_t systemUsage = 0;
96     GetObject<int32_t>(data + startPos, size - startPos, systemUsage);
97     datas.WriteInt32(systemUsage);
98     datas.RewindRead(0);
99     MessageParcel reply;
100     MessageOption option;
101     g_service->OnStartFuzz();
102     g_service->OnRemoteRequest(static_cast<uint32_t>(IMiscdeviceServiceIpcCode::COMMAND_VIBRATE),
103         datas, reply, option);
104     return true;
105 }
106 } // namespace Sensors
107 } // namespace OHOS
108 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
110 {
111     /* Run your code on data */
112     if (data == nullptr) {
113         return 0;
114     }
115 
116     /* Validate the length of size */
117     if (size < OHOS::Sensors::U32_AT_SIZE) {
118         return 0;
119     }
120 
121     OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
122     return 0;
123 }
124