1 /*
2 * Copyright (c) 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 "binddomainaccount_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 "os_account_manager.h"
24 #include "account_log_wrapper.h"
25 #include "fuzz_data.h"
26 #undef private
27 #include "os_account_constants.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 class TestCallback final : public DomainAccountCallback {
36 public:
37 TestCallback() = default;
38 ~TestCallback() = default;
OnResult(const int32_t errCode,Parcel & parcel)39 void OnResult(const int32_t errCode, Parcel &parcel) override
40 {
41 return;
42 }
43 };
44
BindDomainAccountFuzzTest(const uint8_t * data,size_t size)45 bool BindDomainAccountFuzzTest(const uint8_t* data, size_t size)
46 {
47 bool result = false;
48 if ((data != nullptr) && (size != 0)) {
49 FuzzData fuzzData(data, size);
50 std::string accountName(fuzzData.GenerateString());
51 std::string domain(fuzzData.GenerateString());
52 DomainAccountInfo domainInfo(accountName, domain);
53 auto callback = std::make_shared<TestCallback>();
54 result = OsAccountManager::BindDomainAccount(
55 fuzzData.GetData<int32_t>() % Constants::MAX_USER_ID, domainInfo, callback);
56 }
57 return result;
58 }
59 }
60
NativeTokenGet()61 void NativeTokenGet()
62 {
63 uint64_t tokenId;
64 const char **perms = new const char *[1];
65 perms[0] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
66 NativeTokenInfoParams infoInstance = {
67 .dcapsNum = 0,
68 .permsNum = 1,
69 .aclsNum = 0,
70 .perms = perms,
71 .acls = nullptr,
72 .aplStr = "system_core",
73 };
74 infoInstance.processName = "BIND_DOMAIN_ACCOUNT";
75 tokenId = GetAccessTokenId(&infoInstance);
76 SetSelfTokenID(tokenId);
77 AccessTokenKit::ReloadNativeTokenInfo();
78 delete[] perms;
79 }
80
81 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)82 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
83 {
84 NativeTokenGet();
85 return 0;
86 }
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91 /* Run your code on data */
92 OHOS::BindDomainAccountFuzzTest(data, size);
93 return 0;
94 }
95