• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "sensoragent_fuzzer.h"
17 
18 #include <unistd.h>
19 #include <thread>
20 
21 #include "accesstoken_kit.h"
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "securec.h"
25 
26 #include "sensor_agent.h"
27 #include "sensor_agent_type.h"
28 #include "sensor_errors.h"
29 
30 namespace OHOS {
31 namespace Sensors {
32 using namespace OHOS::HiviewDFX;
33 using namespace OHOS::Security::AccessToken;
34 using OHOS::Security::AccessToken::AccessTokenID;
35 namespace {
36 constexpr int64_t g_samplingInterval = 200000000;
37 constexpr int64_t g_reportInterval = 200000000;
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     CHKPV(perms);
58     perms[0] = "ohos.permission.ACCELEROMETER";
59     perms[1] = "ohos.permission.MANAGE_SENSOR";
60     TokenInfoParams infoInstance = {
61         .dcapsNum = 0,
62         .permsNum = 2,
63         .aclsNum = 0,
64         .dcaps = nullptr,
65         .perms = perms,
66         .acls = nullptr,
67         .processName = "SensorAgentFuzzTest",
68         .aplStr = "system_core",
69     };
70     uint64_t tokenId = GetAccessTokenId(&infoInstance);
71     SetSelfTokenID(tokenId);
72     AccessTokenKit::ReloadNativeTokenInfo();
73     delete[] perms;
74 }
75 
SensorDataCallbackImpl(SensorEvent * event)76 void SensorDataCallbackImpl(SensorEvent *event)
77 {
78     if (event == nullptr) {
79         return;
80     }
81 }
82 
CheckSensorTypeId(int32_t sensorTypeId)83 bool CheckSensorTypeId(int32_t sensorTypeId)
84 {
85     int32_t count = -1;
86     SensorInfo *sensorInfo = nullptr;
87     int32_t ret = GetAllSensors(&sensorInfo, &count);
88     if (ret != 0) {
89         return false;
90     }
91     for (int32_t i = 0; i < count; i++) {
92         if ((sensorInfo + i)->sensorTypeId == sensorTypeId) {
93             return true;
94         }
95     }
96     return false;
97 }
98 
SensorAgentFuzzTest(const uint8_t * data,size_t size)99 void SensorAgentFuzzTest(const uint8_t *data, size_t size)
100 {
101     SetUpTestCase();
102     size_t startPos = 0;
103     int32_t sensorTypeId = 0;
104     int32_t mode = 0;
105     GetObject<int32_t>(sensorTypeId, data + startPos, size - startPos);
106     GetObject<int32_t>(mode, data, size);
107     bool validSensorId = CheckSensorTypeId(sensorTypeId);
108     if (!validSensorId) {
109         sensorTypeId = SENSOR_TYPE_ID_ACCELEROMETER;
110     }
111     SensorUser user;
112     user.callback = SensorDataCallbackImpl;
113     SubscribeSensor(sensorTypeId, &user);
114     SetBatch(sensorTypeId, &user, g_samplingInterval, g_reportInterval);
115     ActivateSensor(sensorTypeId, &user);
116     SetMode(sensorTypeId, &user, mode);
117     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
118     DeactivateSensor(sensorTypeId, &user);
119     UnsubscribeSensor(sensorTypeId, &user);
120 }
121 } // namespace Sensors
122 } // namespace OHOS
123 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)124 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
125 {
126     OHOS::Sensors::SensorAgentFuzzTest(data, size);
127     return 0;
128 }
129 
130