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 "turnoffstub_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"IMiscdeviceService";
37 } // namespace
38 
SetUpTestCase()39 void SetUpTestCase()
40 {
41     const char **perms = new (std::nothrow) const char *[1];
42     if (perms == nullptr) {
43         return;
44     }
45     perms[0] = "ohos.permission.VIBRATE";
46     TokenInfoParams infoInstance = {
47         .dcapsNum = 0,
48         .permsNum = 1,
49         .aclsNum = 0,
50         .dcaps = nullptr,
51         .perms = perms,
52         .acls = nullptr,
53         .processName = "TurnOffStubFuzzTest",
54         .aplStr = "system_core",
55     };
56     uint64_t tokenId = GetAccessTokenId(&infoInstance);
57     SetSelfTokenID(tokenId);
58     AccessTokenKit::ReloadNativeTokenInfo();
59     delete[] perms;
60 }
61 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)62 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
63 {
64     SetUpTestCase();
65     MessageParcel datas;
66     datas.WriteInterfaceToken(VIBRATOR_INTERFACE_TOKEN);
67     datas.WriteBuffer(data, size);
68     datas.RewindRead(0);
69     MessageParcel reply;
70     MessageOption option;
71     g_service->OnStartFuzz();
72     g_service->OnRemoteRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_OFF),
73         datas, reply, option);
74     return true;
75 }
76 }  // namespace Sensors
77 }  // namespace OHOS
78 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81     /* Run your code on data */
82     if (data == nullptr) {
83         return 0;
84     }
85 
86     /* Validate the length of size */
87     if (size < OHOS::Sensors::U32_AT_SIZE) {
88         return 0;
89     }
90 
91     OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
92     return 0;
93 }
94