• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "cancelstub_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 #include "access_token.h"
21 #include "access_token_error.h"
22 #include "accesstoken_kit.h"
23 #include "account_iam_service.h"
24 #include "account_log_wrapper.h"
25 #include "fuzz_data.h"
26 #include "iaccount_iam.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 
30 using namespace std;
31 using namespace OHOS::AccountSA;
32 using namespace OHOS::Security::AccessToken;
33 namespace OHOS {
34 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
CancelStubFuzzTest(const uint8_t * data,size_t size)35 bool CancelStubFuzzTest(const uint8_t *data, size_t size)
36 {
37     if ((data == nullptr) || (size == 0)) {
38         return false;
39     }
40     MessageParcel dataTemp;
41     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
42         return false;
43     }
44     FuzzData fuzzData(data, size);
45     int32_t userId = fuzzData.GetData<int32_t>();
46     if (!dataTemp.WriteInt32(userId)) {
47         return false;
48     }
49     MessageParcel reply;
50     MessageOption option;
51     uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::CANCEL);
52     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
53     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
54 
55     return true;
56 }
57 } // namespace OHOS
58 
NativeTokenGet()59 void NativeTokenGet()
60 {
61     uint64_t tokenId;
62     const char **perms = new const char *[1];
63     perms[0] = "ohos.permission.MANAGE_USER_IDM";
64     NativeTokenInfoParams infoInstance = {
65         .dcapsNum = 0,
66         .permsNum = 1,
67         .aclsNum = 0,
68         .perms = perms,
69         .acls = nullptr,
70         .aplStr = "system_core",
71     };
72     infoInstance.processName = "CANCEL";
73     tokenId = GetAccessTokenId(&infoInstance);
74     SetSelfTokenID(tokenId);
75     AccessTokenKit::ReloadNativeTokenInfo();
76     delete [] perms;
77 }
78 
79 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)80 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
81 {
82     NativeTokenGet();
83     return 0;
84 }
85 
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
88 {
89     /* Run your code on data */
90     OHOS::CancelStubFuzzTest(data, size);
91     return 0;
92 }
93