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 "registerinputer_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 "nativetoken_kit.h"
24 #include "account_iam_client.h"
25 #include "fuzz_data.h"
26 #include "token_setproc.h"
27
28 using namespace std;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::Security::AccessToken;
31
32 class MockIInputer : public OHOS::AccountSA::IInputer {
33 public:
~MockIInputer()34 virtual ~MockIInputer() {}
OnGetData(int32_t authSubType,std::vector<uint8_t> challenge,std::shared_ptr<IInputerData> inputerData)35 void OnGetData(int32_t authSubType, std::vector<uint8_t> challenge,
36 std::shared_ptr<IInputerData> inputerData) override
37 {
38 return;
39 }
40 };
41 namespace OHOS {
RegisterInputerFuzzTest(const uint8_t * data,size_t size)42 bool RegisterInputerFuzzTest(const uint8_t* data, size_t size)
43 {
44 FuzzData fuzzData(data, size);
45 int32_t authType = fuzzData.GetData<bool>() ? IAMAuthType::DOMAIN : fuzzData.GetData<int32_t>();
46 std::shared_ptr<IInputer> inputer = fuzzData.GetData<bool>() ? make_shared<MockIInputer>() : nullptr;
47 int32_t result = AccountIAMClient::GetInstance().RegisterInputer(authType, inputer);
48 result = AccountIAMClient::GetInstance().UnregisterInputer(authType);
49 return result == ERR_OK;
50 }
51 }
52
NativeTokenGet()53 void NativeTokenGet()
54 {
55 uint64_t tokenId;
56 const char **perms = new const char *[1];
57 perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
58 NativeTokenInfoParams infoInstance = {
59 .dcapsNum = 0,
60 .permsNum = 1,
61 .aclsNum = 0,
62 .perms = perms,
63 .acls = nullptr,
64 .aplStr = "system_core",
65 };
66 infoInstance.processName = "RegisterInputer";
67 tokenId = GetAccessTokenId(&infoInstance);
68 SetSelfTokenID(tokenId);
69 AccessTokenKit::ReloadNativeTokenInfo();
70 delete[] perms;
71 }
72
73 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)74 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
75 {
76 NativeTokenGet();
77 return 0;
78 }
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::RegisterInputerFuzzTest(data, size);
85 return 0;
86 }
87
88