1 /*
2 * Copyright (c) 2022-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 "opensession_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_client.h"
24 #include "fuzz_data.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27
28
29 using namespace std;
30 using namespace OHOS::AccountSA;
31 using namespace OHOS::Security::AccessToken;
32
33 namespace OHOS {
OpenSessionFuzzTest(const uint8_t * data,size_t size)34 bool OpenSessionFuzzTest(const uint8_t* data, size_t size)
35 {
36 FuzzData fuzzData(data, size);
37 int32_t userId = fuzzData.GetData<int32_t>();
38 std::vector<uint8_t> challenge;
39 int32_t result = AccountIAMClient::GetInstance().OpenSession(userId, challenge);
40 return result == ERR_OK;
41 }
42 }
43
NativeTokenGet()44 void NativeTokenGet()
45 {
46 uint64_t tokenId;
47 const char **perms = new const char *[1];
48 perms[0] = "ohos.permission.MANAGE_USER_IDM";
49 NativeTokenInfoParams infoInstance = {
50 .dcapsNum = 0,
51 .permsNum = 1,
52 .aclsNum = 0,
53 .perms = perms,
54 .acls = nullptr,
55 .aplStr = "system_core",
56 };
57 infoInstance.processName = "RegisterInputer";
58 tokenId = GetAccessTokenId(&infoInstance);
59 SetSelfTokenID(tokenId);
60 AccessTokenKit::ReloadNativeTokenInfo();
61 delete[] perms;
62 }
63
LLVMFuzzerInitialize(int * argc,char *** argv)64 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
65 {
66 NativeTokenGet();
67 return 0;
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::OpenSessionFuzzTest(data, size);
75 return 0;
76 }
77
78