• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "registerpermcallback_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 } // namespace
38 
39 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)40 size_t GetObject(T &object, const uint8_t *data, size_t size)
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 *[2];
56     if (perms == nullptr) {
57         return;
58     }
59     perms[0] = "ohos.permission.ACCELEROMETER";
60     perms[1] = "ohos.permission.MANAGE_SENSOR";
61     TokenInfoParams infoInstance = {
62         .dcapsNum = 0,
63         .permsNum = 2,
64         .aclsNum = 0,
65         .dcaps = nullptr,
66         .perms = perms,
67         .acls = nullptr,
68         .processName = "CreateDataChannelStubFuzzTest",
69         .aplStr = "system_core",
70     };
71     uint64_t tokenId = GetAccessTokenId(&infoInstance);
72     SetSelfTokenID(tokenId);
73     AccessTokenKit::ReloadNativeTokenInfo();
74     delete[] perms;
75 }
76 
RegisterPermCallbackFuzzTest(const uint8_t * data,size_t size)77 bool RegisterPermCallbackFuzzTest(const uint8_t *data, size_t size)
78 {
79     SetUpTestCase();
80     int32_t sensorId = 0;
81     GetObject<int32_t>(sensorId, data, size);
82     g_service->RegisterPermCallback(sensorId);
83     g_service->UnregisterPermCallback();
84     return true;
85 }
86 }  // namespace Sensors
87 }  // namespace OHOS
88 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91     /* Run your code on data */
92     if (data == nullptr) {
93         return 0;
94     }
95 
96     /* Validate the length of size */
97     if (size < OHOS::Sensors::U32_AT_SIZE) {
98         return 0;
99     }
100 
101     OHOS::Sensors::RegisterPermCallbackFuzzTest(data, size);
102     return 0;
103 }
104