• 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 "sensorenablestub_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 "sensor.h"
28 #include "sensor_service.h"
29 
30 namespace OHOS {
31 namespace Sensors {
32 using namespace Security::AccessToken;
33 using Security::AccessToken::AccessTokenID;
34 namespace {
35 constexpr size_t U32_AT_SIZE = 4;
36 auto g_service = SensorDelayedSpSingleton<SensorService>::GetInstance();
37 const std::u16string SENSOR_INTERFACE_TOKEN = u"OHOS.Sensors.ISensorService";
38 } // namespace
39 
40 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)41 size_t GetObject(T &object, const uint8_t *data, size_t size)
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 *[2];
57     if (perms == nullptr) {
58         return;
59     }
60     perms[0] = "ohos.permission.ACCELEROMETER";
61     perms[1] = "ohos.permission.MANAGE_SENSOR";
62     TokenInfoParams infoInstance = {
63         .dcapsNum = 0,
64         .permsNum = 2,
65         .aclsNum = 0,
66         .dcaps = nullptr,
67         .perms = perms,
68         .acls = nullptr,
69         .processName = "SensorEnableStubFuzzTest",
70         .aplStr = "system_core",
71     };
72     uint64_t tokenId = GetAccessTokenId(&infoInstance);
73     SetSelfTokenID(tokenId);
74     AccessTokenKit::ReloadNativeTokenInfo();
75     delete[] perms;
76 }
77 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)78 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
79 {
80     SetUpTestCase();
81     if (g_service == nullptr) {
82         return false;
83     }
84     MessageParcel datas;
85     datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN);
86     size_t startPos = 0;
87     SensorDescriptionIPC sensorDesc {-1, 1, 0, 1};
88     datas.WriteParcelable(&sensorDesc);
89     int64_t samplingPeriod = 0;
90     startPos += GetObject<int64_t>(samplingPeriod, data + startPos, size - startPos);
91     datas.WriteInt64(samplingPeriod);
92     int64_t maxReportDelay = 0;
93     GetObject<int64_t>(maxReportDelay, data + startPos, size - startPos);
94     datas.WriteInt64(maxReportDelay);
95     datas.RewindRead(0);
96     MessageParcel reply;
97     MessageOption option;
98     g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR),
99         datas, reply, option);
100     return true;
101 }
102 }  // namespace Sensors
103 }  // namespace OHOS
104 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
106 {
107     /* Run your code on data */
108     if (data == nullptr) {
109         return 0;
110     }
111 
112     /* Validate the length of size */
113     if (size < OHOS::Sensors::U32_AT_SIZE) {
114         return 0;
115     }
116 
117     OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
118     return 0;
119 }
120