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 "updateaccountinfo_fuzzer.h"
17
18 #include "access_token.h"
19 #include "access_token_error.h"
20 #include "accesstoken_kit.h"
21 #include "domain_account_client.h"
22 #include "fuzz_data.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 using namespace std;
27 using namespace OHOS::AccountSA;
28 using namespace OHOS::Security::AccessToken;
29
30 namespace {
31 constexpr int32_t PERMISSION_COUNT_NUM = 2;
32 constexpr int32_t FIRST_PARAM_INDEX = 0;
33 constexpr int32_t SECOND_PARAM_INDEX = 1;
34 }
35
36 namespace OHOS {
37 namespace {
38 const int ENUM_MAX = 4;
39 }
UpdateAccountInfoFuzzTest(const uint8_t * data,size_t size)40 bool UpdateAccountInfoFuzzTest(const uint8_t* data, size_t size)
41 {
42 bool result = false;
43 if ((data == nullptr) || (size == 0)) {
44 return false;
45 }
46 FuzzData fuzzData(data, size);
47 DomainAccountInfo oldAccountInfo;
48 oldAccountInfo.domain_ = fuzzData.GenerateString();
49 oldAccountInfo.accountName_ = fuzzData.GenerateString();
50 oldAccountInfo.accountId_ = fuzzData.GenerateString();
51 oldAccountInfo.isAuthenticated = fuzzData.GenerateBool();
52 oldAccountInfo.serverConfigId_ = fuzzData.GenerateString();
53 int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
54 oldAccountInfo.status_ = static_cast<DomainAccountStatus>(typeNumber);
55
56 DomainAccountInfo newAccountInfo;
57 newAccountInfo.domain_ = fuzzData.GenerateString();
58 newAccountInfo.accountName_ = fuzzData.GenerateString();
59 newAccountInfo.accountId_ = fuzzData.GenerateString();
60 newAccountInfo.isAuthenticated = fuzzData.GenerateBool();
61 newAccountInfo.serverConfigId_ = fuzzData.GenerateString();
62 typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
63 newAccountInfo.status_ = static_cast<DomainAccountStatus>(typeNumber);
64
65 result = DomainAccountClient::GetInstance().UpdateAccountInfo(oldAccountInfo, newAccountInfo);
66 return result == ERR_OK;
67 }
68 }
69
NativeTokenGet()70 void NativeTokenGet()
71 {
72 uint64_t tokenId;
73 const char **perms = new const char *[PERMISSION_COUNT_NUM];
74 perms[FIRST_PARAM_INDEX] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
75 perms[SECOND_PARAM_INDEX] = "ohos.permission.MANAGE_DOMAIN_ACCOUNTS";
76 NativeTokenInfoParams infoInstance = {
77 .dcapsNum = 0,
78 .permsNum = PERMISSION_COUNT_NUM,
79 .aclsNum = 0,
80 .perms = perms,
81 .acls = nullptr,
82 .aplStr = "system_core",
83 };
84 infoInstance.processName = "RegisterInputer";
85 tokenId = GetAccessTokenId(&infoInstance);
86 SetSelfTokenID(tokenId);
87 AccessTokenKit::ReloadNativeTokenInfo();
88 delete[] perms;
89 }
90
91 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)92 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
93 {
94 NativeTokenGet();
95 return 0;
96 }
97
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101 /* Run your code on data */
102 OHOS::UpdateAccountInfoFuzzTest(data, size);
103 return 0;
104 }
105
106