• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "createosaccount_fuzzer.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21 
22 #include "access_token.h"
23 #include "access_token_error.h"
24 #include "accesstoken_kit.h"
25 #include "account_log_wrapper.h"
26 #include "fuzz_data.h"
27 #include "nativetoken_kit.h"
28 #include "os_account_constants.h"
29 #include "os_account_manager.h"
30 #include "securec.h"
31 #include "token_setproc.h"
32 
33 using namespace std;
34 using namespace OHOS::AccountSA;
35 using namespace OHOS::Security::AccessToken;
36 
37 namespace OHOS {
38 constexpr uint32_t MAX_ACCOUNT_TYPE_COUNT = 5;
39 
GetAccountType(FuzzData & fuzzData,bool useValid)40 OsAccountType GetAccountType(FuzzData& fuzzData, bool useValid)
41 {
42     if (useValid) {
43         return OsAccountType::NORMAL;
44     }
45     return static_cast<OsAccountType>(fuzzData.GetData<uint32_t>() % MAX_ACCOUNT_TYPE_COUNT);
46 }
47 
CreateOsAccountFuzzTest(const uint8_t * data,size_t size)48 bool CreateOsAccountFuzzTest(const uint8_t* data, size_t size)
49 {
50     if ((data == nullptr) || (size == 0)) {
51         return false;
52     }
53 
54     FuzzData fuzzData(data, size);
55     bool useValidParams = fuzzData.GetData<bool>();
56 
57     OsAccountInfo osAccountInfoOne;
58     OsAccountType testType = GetAccountType(fuzzData, useValidParams);
59     std::string accountName = fuzzData.GenerateString();
60 
61     int32_t result = OsAccountManager::CreateOsAccount(accountName, testType, osAccountInfoOne);
62     if (result == ERR_OK) {
63         ACCOUNT_LOGI("CreateOsAccountFuzzTest RemoveOsAccount");
64         OsAccountManager::RemoveOsAccount(osAccountInfoOne.GetLocalId());
65     }
66 
67     return result == ERR_OK;
68 }
69 
70 } // namespace OHOS
71 
NativeTokenGet()72 void NativeTokenGet()
73 {
74     uint64_t tokenId;
75     const char **perms = new const char *[1];
76     perms[0] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
77     NativeTokenInfoParams infoInstance = {
78         .dcapsNum = 0,
79         .permsNum = 1,
80         .aclsNum = 0,
81         .perms = perms,
82         .acls = nullptr,
83         .aplStr = "system_core",
84     };
85     infoInstance.processName = "RegisterInputer";
86     tokenId = GetAccessTokenId(&infoInstance);
87     SetSelfTokenID(tokenId);
88     AccessTokenKit::ReloadNativeTokenInfo();
89     delete[] perms;
90 }
91 
92 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)93 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
94 {
95     NativeTokenGet();
96     return 0;
97 }
98 
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102     OHOS::CreateOsAccountFuzzTest(data, size);
103     return 0;
104 }