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