• 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 "unregister_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22 
23 #include "sensor_agent.h"
24 #include "sensor_agent_type.h"
25 #include "sensor_errors.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "UnregisterFuzzTest"
29 
30 namespace OHOS {
31 namespace Sensors {
32 using namespace OHOS::HiviewDFX;
33 using namespace Security::AccessToken;
34 using Security::AccessToken::AccessTokenID;
35 namespace {
36 constexpr size_t DATA_MIN_SIZE = 4;
37 } // namespace
38 
39 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)40 void GetObject(const uint8_t *data, size_t size, T &object)
41 {
42     size_t objectSize = sizeof(object);
43     if (objectSize > size) {
44         return;
45     }
46     memcpy_s(&object, objectSize, data, objectSize);
47 }
48 
SetUpTestCase()49 void SetUpTestCase()
50 {
51     const char **perms = new (std::nothrow) const char *[2];
52     CHKPV(perms);
53     perms[0] = "ohos.permission.ACCELEROMETER";
54     perms[1] = "ohos.permission.MANAGE_SENSOR";
55     TokenInfoParams infoInstance = {
56         .dcapsNum = 0,
57         .permsNum = 2,
58         .aclsNum = 0,
59         .dcaps = nullptr,
60         .perms = perms,
61         .acls = nullptr,
62         .processName = "UnregisterFuzzTest",
63         .aplStr = "system_core",
64     };
65     uint64_t tokenId = GetAccessTokenId(&infoInstance);
66     SetSelfTokenID(tokenId);
67     AccessTokenKit::ReloadNativeTokenInfo();
68     delete[] perms;
69 }
70 
UnregisterFuzzTest(const uint8_t * data,size_t size)71 void UnregisterFuzzTest(const uint8_t *data, size_t size)
72 {
73     if (data == nullptr || size < DATA_MIN_SIZE) {
74         return;
75     }
76     SetUpTestCase();
77     SensorActiveInfoCB callback = nullptr;
78     GetObject<SensorActiveInfoCB>(data, size, callback);
79     Unregister(callback);
80 }
81 } // namespace Sensors
82 } // namespace OHOS
83 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86     OHOS::Sensors::UnregisterFuzzTest(data, size);
87     return 0;
88 }
89