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 "devauth_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "access_token.h"
22 #include "accesstoken_kit.h"
23 #include "access_token_error.h"
24 #include "hc_log.h"
25 #include "ipc_adapt.h"
26 #include "ipc_callback_stub.h"
27 #include "ipc_dev_auth_stub.h"
28 #include "ipc_sdk.h"
29 #include "ipc_service.h"
30 #include "message_parcel.h"
31 #include "nativetoken_kit.h"
32 #include "securec.h"
33 #include "token_setproc.h"
34 
35 namespace OHOS {
36 const std::u16string DEV_AUTH_SERVICE_INTERFACE_TOKEN = u"deviceauth.IMethodsIpcCall";
37 
NativeTokenSet(const char * procName)38 static void NativeTokenSet(const char *procName)
39 {
40     const char *acls[] = {"ACCESS_IDS"};
41     const char *perms[] = {
42         "ohos.permission.PLACE_CALL",
43         "ohos.permission.ACCESS_IDS"
44     };
45     uint64_t tokenId;
46     NativeTokenInfoParams infoInstance = {
47         .dcapsNum = 0,
48         .permsNum = 2,
49         .aclsNum = 1,
50         .dcaps = NULL,
51         .perms = perms,
52         .acls = acls,
53         .processName = procName,
54         .aplStr = "system_core",
55     };
56     tokenId = GetAccessTokenId(&infoInstance);
57     SetSelfTokenID(tokenId);
58     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59 }
60 
FuzzDoRegCallback(const uint8_t * data,size_t size)61 bool FuzzDoRegCallback(const uint8_t* data, size_t size)
62 {
63     (void)InitDeviceAuthService();
64     (void)MainRescInit();
65     ServiceDevAuth *serviceObj = new(std::nothrow) ServiceDevAuth();
66     if (serviceObj == nullptr) {
67         return false;
68     }
69     sptr<ServiceDevAuth> sptrObj = serviceObj;
70     uintptr_t serviceCtx = reinterpret_cast<uintptr_t>(serviceObj);
71     (void)AddMethodMap(serviceCtx);
72     for (int32_t i = IPC_CALL_ID_REG_CB; i <= IPC_CALL_ID_GET_PSEUDONYM_ID; i++) {
73         if (i == IPC_CALL_ID_AUTH_DEVICE || i == IPC_CALL_ID_GA_PROC_DATA || i == IPC_CALL_GA_CANCEL_REQUEST) {
74             NativeTokenSet("softbus_server");
75         } else if (i == IPC_CALL_ID_GET_PK_INFO_LIST) {
76             NativeTokenSet("dslm_service");
77         } else {
78             NativeTokenSet("device_manager");
79         }
80         MessageParcel datas;
81         datas.WriteInterfaceToken(DEV_AUTH_SERVICE_INTERFACE_TOKEN);
82         datas.WriteInt32(i);
83         datas.WriteInt32(size + sizeof(int32_t));
84         datas.WriteInt32(0);
85         datas.WriteInt32(size);
86         datas.WriteBuffer(data, size);
87         datas.RewindRead(0);
88         MessageParcel reply;
89         MessageOption option;
90         (void)serviceObj->OnRemoteRequest(1, datas, reply, option);
91     }
92     return true;
93 }
94 }
95 
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99     /* Run your code on data */
100     OHOS::FuzzDoRegCallback(data, size);
101     return 0;
102 }
103 
104