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