1 /*
2 * Copyright (c) 2023 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 #include <string>
18 #include <vector>
19
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 "iaccount_iam.h"
26 #include "nativetoken_kit.h"
27 #include "token_setproc.h"
28 using namespace std;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::Security::AccessToken;
31 namespace OHOS {
32 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
NativeTokenGet()33 void NativeTokenGet()
34 {
35 uint64_t tokenId;
36 const char **perms = new const char *[1];
37
38 perms[0] = "ohos.permission.MANAGE_USER_IDM";
39
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
61 MessageParcel dataTemp;
62 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
63 return false;
64 }
65
66 int32_t userId = size;
67 if (!dataTemp.WriteInt32(userId)) {
68 return false;
69 }
70
71 NativeTokenGet();
72
73 MessageParcel reply;
74 MessageOption option;
75 uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::OPEN_SESSION);
76 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
77 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
78
79 return true;
80 }
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86 /* Run your code on data */
87 OHOS::OpenSessionStubFuzzTest(data, size);
88 return 0;
89 }
90