• 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 "binddomainaccountstub_fuzzer.h"
17 #include <string>
18 #include <thread>
19 #include <vector>
20 
21 #include "access_token.h"
22 #include "access_token_error.h"
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "domain_account_callback_service.h"
26 #include "fuzz_data.h"
27 #include "ios_account.h"
28 #include "os_account_manager_service.h"
29 #include "token_setproc.h"
30 
31 using namespace std;
32 using namespace OHOS::AccountSA;
33 using namespace OHOS::Security::AccessToken;
34 
35 namespace OHOS {
36 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
37 
38 class TestCallback final : public DomainAccountCallback {
39 public:
40     TestCallback() = default;
41     ~TestCallback() = default;
OnResult(const int32_t errCode,Parcel & parcel)42     void OnResult(const int32_t errCode, Parcel &parcel) override
43     {
44         return;
45     }
46 };
47 
BindDomainAccountStubFuzzTest(const uint8_t * data,size_t size)48 bool BindDomainAccountStubFuzzTest(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     MessageParcel datas;
56     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
57     int32_t localId = static_cast<int32_t>(fuzzData.GetData<size_t>() % Constants::MAX_USER_ID);
58     if (!datas.WriteInt32(localId)) {
59         return false;
60     }
61     auto useDomainAccountInfo = fuzzData.GenerateBool();
62     if (useDomainAccountInfo) {
63         DomainAccountInfo domainInfo(fuzzData.GenerateString(), fuzzData.GenerateString());
64         if (!datas.WriteParcelable(&domainInfo)) {
65             return false;
66         }
67     }
68     auto useDomainAccountCallbackService = fuzzData.GenerateBool();
69     if (useDomainAccountCallbackService) {
70         std::shared_ptr<DomainAccountCallback> callbackPtr = std::make_shared<TestCallback>();
71         sptr<DomainAccountCallbackService> callbackService =
72             new (std::nothrow) DomainAccountCallbackService(callbackPtr);
73         if ((callbackService == nullptr) || (!datas.WriteRemoteObject(callbackService->AsObject()))) {
74             return false;
75         }
76     }
77 
78     MessageParcel reply;
79     MessageOption option;
80 
81     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
82 
83     osAccountManagerService_->OnRemoteRequest(
84         static_cast<int32_t>(IOsAccountIpcCode::COMMAND_BIND_DOMAIN_ACCOUNT), datas, reply, option);
85 
86     return true;
87 }
88 } // namespace OHOS
89 
NativeTokenGet()90 void NativeTokenGet()
91 {
92     uint64_t tokenId;
93     const char **perms = new const char *[1];
94     perms[0] = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
95     NativeTokenInfoParams infoInstance = {
96         .dcapsNum = 0,
97         .permsNum = 1,
98         .aclsNum = 0,
99         .perms = perms,
100         .acls = nullptr,
101         .aplStr = "system_core",
102     };
103     infoInstance.processName = "BIND_DOMAIN_ACCOUNT";
104     tokenId = GetAccessTokenId(&infoInstance);
105     SetSelfTokenID(tokenId);
106     AccessTokenKit::ReloadNativeTokenInfo();
107     delete[] perms;
108 }
109 
110 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)111 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
112 {
113     NativeTokenGet();
114     return 0;
115 }
116 
117 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)118 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
119 {
120     /* Run your code on data */
121     OHOS::BindDomainAccountStubFuzzTest(data, size);
122     return 0;
123 }
124