1 /*
2 * Copyright (c) 2022-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 <string>
17 #include <vector>
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include "device_manager_service.h"
20 #include "authenticate_device_service_fuzzer.h"
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27
AddPermission()28 void AddPermission()
29 {
30 const int32_t permsNum = 3;
31 const int32_t indexZero = 0;
32 const int32_t indexOne = 1;
33 const int32_t indexTwo = 2;
34 uint64_t tokenId;
35 const char *perms[permsNum];
36 perms[indexZero] = "ohos.permission.ACCESS_SERVICE_DM";
37 perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
38 perms[indexTwo] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER";
39 NativeTokenInfoParams infoInstance = {
40 .dcapsNum = 0,
41 .permsNum = permsNum,
42 .aclsNum = 0,
43 .dcaps = NULL,
44 .perms = perms,
45 .acls = NULL,
46 .processName = "device_manager",
47 .aplStr = "system_core",
48 };
49 tokenId = GetAccessTokenId(&infoInstance);
50 SetSelfTokenID(tokenId);
51 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
52 }
AuthenticateDeviceServiceFuzzTest(const uint8_t * data,size_t size)53 void AuthenticateDeviceServiceFuzzTest(const uint8_t* data, size_t size)
54 {
55 if ((data == nullptr) || (size < sizeof(int32_t))) {
56 return;
57 }
58
59 AddPermission();
60 FuzzedDataProvider fdp(data, size);
61 std::string pkgName = fdp.ConsumeRandomLengthString();
62 std::string extra = fdp.ConsumeRandomLengthString();
63 std::string deviceId = fdp.ConsumeRandomLengthString();
64 std::string udid = fdp.ConsumeRandomLengthString();
65 std::string network = fdp.ConsumeRandomLengthString();
66 int32_t authType = fdp.ConsumeIntegral<int32_t>();
67 int32_t level = fdp.ConsumeIntegral<int32_t>();
68 DeviceManagerService::GetInstance().Init();
69 DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra);
70 DeviceManagerService::GetInstance().BindDevice(pkgName, authType, deviceId, extra);
71 DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, extra);
72 DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId);
73 DeviceManagerService::GetInstance().SetUserOperation(pkgName, authType, network);
74 DeviceManagerService::GetInstance().RegisterCallerAppId(pkgName);
75 DeviceManagerService::GetInstance().UnRegisterCallerAppId(pkgName);
76 DeviceManagerService::GetInstance().GetUdidByNetworkId(pkgName, deviceId, udid);
77 DeviceManagerService::GetInstance().CheckApiPermission(level);
78 }
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 OHOS::DistributedHardware::AuthenticateDeviceServiceFuzzTest(data, size);
87
88 return 0;
89 }