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 "getavailablestatusstub_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";
35 
NativeTokenGet()36 void NativeTokenGet()
37 {
38     uint64_t tokenId;
39     const char **perms = new const char *[1];
40 
41     perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
42 
43     NativeTokenInfoParams infoInstance = {
44         .dcapsNum = 0,
45         .permsNum = 1,
46         .aclsNum = 0,
47         .perms = perms,
48         .acls = nullptr,
49         .aplStr = "system_core",
50     };
51     infoInstance.processName = "GET_AVAILABLE_STATUS";
52     tokenId = GetAccessTokenId(&infoInstance);
53     SetSelfTokenID(tokenId);
54     AccessTokenKit::ReloadNativeTokenInfo();
55     delete [] perms;
56 }
57 
GetAvailableStatusStubFuzzTest(const uint8_t * data,size_t size)58 bool GetAvailableStatusStubFuzzTest(const uint8_t *data, size_t size)
59 {
60     if ((data == nullptr) || (size == 0)) {
61         return false;
62     }
63     FuzzData fuzzData(data, size);
64     AuthType authType = static_cast<AuthType>(fuzzData.GenerateRandomEnmu(IAMAuthType::TYPE_END));
65     AuthTrustLevel authTrustLevel = fuzzData.GenerateRandomEnmu(AuthTrustLevel::ATL4);
66     MessageParcel dataTemp;
67     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
68         return false;
69     }
70     if (!dataTemp.WriteInt32(authType)) {
71         return false;
72     }
73     if (!dataTemp.WriteUint32(authTrustLevel)) {
74         return false;
75     }
76     NativeTokenGet();
77     MessageParcel reply;
78     MessageOption option;
79     uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::GET_AVAILABLE_STATUS);
80     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
81     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
82 
83     return true;
84 }
85 } // namespace OHOS
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
89 {
90     /* Run your code on data */
91     OHOS::GetAvailableStatusStubFuzzTest(data, size);
92     return 0;
93 }
94