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