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