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 "devauthcb_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_CB_INTERFACE_TOKEN = u"deviceauth.ICommIpcCallback";
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 
FuzzDoCallback(const uint8_t * data,size_t size)61 bool FuzzDoCallback(const uint8_t* data, size_t size)
62 {
63     NativeTokenSet("device_manager");
64     StubDevAuthCb *remoteObj = new(std::nothrow) StubDevAuthCb();
65     if (remoteObj == nullptr) {
66         return false;
67     }
68     sptr<StubDevAuthCb> remoteSptr = remoteObj;
69     MessageParcel datas;
70     datas.WriteInterfaceToken(DEV_AUTH_CB_INTERFACE_TOKEN);
71     datas.WriteInt32(0);
72     datas.WritePointer(0x0);
73     datas.RewindRead(0);
74     MessageParcel reply;
75     MessageOption option;
76     (void)remoteObj->OnRemoteRequest(1, datas, reply, option);
77     return true;
78 }
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::FuzzDoCallback(data, size);
86     return 0;
87 }
88 
89