• 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 "createosaccountwithfullinfo_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_log_wrapper.h"
24 #include "fuzz_data.h"
25 #include "nativetoken_kit.h"
26 #include "os_account_constants.h"
27 #include "os_account_manager.h"
28 #include "os_account_manager_service.h"
29 #include "os_account_proxy.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 int32_t START_USER_ID_100 = 100;
39 constexpr int64_t TEST_TIMESTAMP_2022 = 1640995200;
40 
GetUserId(FuzzData & fuzzData,bool useValid,int32_t defaultId)41 int32_t GetUserId(FuzzData& fuzzData, bool useValid, int32_t defaultId)
42 {
43     return useValid ? defaultId : fuzzData.GetData<int32_t>();
44 }
45 
GetTimestamp(FuzzData & fuzzData,bool useValid,int64_t defaultTime=TEST_TIMESTAMP_2022)46 int64_t GetTimestamp(FuzzData& fuzzData, bool useValid, int64_t defaultTime = TEST_TIMESTAMP_2022)
47 {
48     return useValid ? defaultTime : fuzzData.GetData<int64_t>();
49 }
50 
CreateOsAccountWithFullInfoFuzzTest(const uint8_t * data,size_t size)51 bool CreateOsAccountWithFullInfoFuzzTest(const uint8_t* data, size_t size)
52 {
53     if ((data == nullptr) || (size == 0)) {
54         return false;
55     }
56 
57     FuzzData fuzzData(data, size);
58     bool useValidParams = fuzzData.GetData<bool>();
59 
60     OsAccountInfo osAccountInfo;
61     osAccountInfo.SetLocalName(fuzzData.GenerateString());
62     osAccountInfo.SetLocalId(START_USER_ID_100);
63     osAccountInfo.SetSerialNumber(GetUserId(fuzzData, useValidParams, START_USER_ID_100));
64     osAccountInfo.SetCreateTime(GetTimestamp(fuzzData, useValidParams));
65     osAccountInfo.SetLastLoginTime(GetTimestamp(fuzzData, useValidParams));
66 
67     int32_t result = OsAccountManager::CreateOsAccountWithFullInfo(osAccountInfo);
68     if (result == ERR_OK) {
69         ACCOUNT_LOGI("CreateOsAccountWithFullInfoFuzzTest RemoveOsAccount");
70         OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
71     }
72     auto servicePtr = new (std::nothrow) OsAccountManagerService();
73     std::shared_ptr<OsAccountProxy> osAccountProxy = std::make_shared<OsAccountProxy>(servicePtr->AsObject());
74     result = osAccountProxy->CreateOsAccountWithFullInfo(osAccountInfo);
75     if (result == ERR_OK) {
76         ACCOUNT_LOGI("CreateOsAccountWithFullInfoFuzzTest RemoveOsAccount");
77         OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId());
78     }
79 
80     return result == ERR_OK;
81 }
82 } // namespace OHOS
83 
NativeTokenGet()84 void NativeTokenGet()
85 {
86     uint64_t tokenId;
87     const char **perms = new const char *[1];
88     perms[0] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
89     NativeTokenInfoParams infoInstance = {
90         .dcapsNum = 0,
91         .permsNum = 1,
92         .aclsNum = 0,
93         .perms = perms,
94         .acls = nullptr,
95         .aplStr = "system_core",
96     };
97     infoInstance.processName = "RegisterInputer";
98     tokenId = GetAccessTokenId(&infoInstance);
99     SetSelfTokenID(tokenId);
100     AccessTokenKit::ReloadNativeTokenInfo();
101     delete[] perms;
102 }
103 
104 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)105 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
106 {
107     NativeTokenGet();
108     return 0;
109 }
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114     OHOS::CreateOsAccountWithFullInfoFuzzTest(data, size);
115     return 0;
116 }
117