• 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 "subscribeconstraintsstub_fuzzer.h"
17 #include <string>
18 #include <thread>
19 #include <vector>
20 #include "accesstoken_kit.h"
21 #include "fuzz_data.h"
22 #include "ios_account.h"
23 #include "os_account_constraint_subscriber_manager.h"
24 #include "os_account_manager_service.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27 
28 using namespace std;
29 using namespace OHOS::AccountSA;
30 using namespace OHOS::Security::AccessToken;
31 namespace OHOS {
32 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
33 
SubscribeOsAccountConstraintStubFuzzTest(const uint8_t * data,size_t size)34 bool SubscribeOsAccountConstraintStubFuzzTest(const uint8_t *data, size_t size)
35 {
36     if ((data == nullptr) || (size == 0)) {
37         return false;
38     }
39 
40     MessageParcel datas;
41     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
42     FuzzData fuzzData(data, size);
43     auto useOsAccountConstraintSubscribeInfo = fuzzData.GenerateBool();
44     if (useOsAccountConstraintSubscribeInfo) {
45         std::string testStr = fuzzData.GenerateString();
46         std::set<string> constraints = {testStr};
47         OsAccountConstraintSubscribeInfo subscriber(constraints);
48         if (!datas.WriteParcelable(&subscriber)) {
49             return false;
50         }
51     }
52 
53     auto useOsAccountConstraintSubscriberManager = fuzzData.GenerateBool();
54     if (useOsAccountConstraintSubscriberManager) {
55         if (!datas.WriteRemoteObject(OsAccountConstraintSubscriberManager::GetInstance()->AsObject())) {
56             return false;
57         }
58     }
59 
60     MessageParcel reply;
61     MessageOption option;
62     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
63 
64     osAccountManagerService_ ->OnRemoteRequest(
65         static_cast<int32_t>(IOsAccountIpcCode::COMMAND_SUBSCRIBE_OS_ACCOUNT_CONSTRAINTS), datas, reply, option);
66 
67     return true;
68 }
69 } // namespace OHOS
70 
NativeTokenGet()71 void NativeTokenGet()
72 {
73     uint64_t tokenId;
74     const char **perms = new const char *[1];
75 
76     perms[0] = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
77 
78     NativeTokenInfoParams infoInstance = {
79         .dcapsNum = 0,
80         .permsNum = 1,
81         .aclsNum = 0,
82         .perms = perms,
83         .acls = nullptr,
84         .aplStr = "system_core",
85     };
86     infoInstance.processName = "SUBSCRIBE_OSACCOUNT_CONSTRAINT";
87     tokenId = GetAccessTokenId(&infoInstance);
88     SetSelfTokenID(tokenId);
89     AccessTokenKit::ReloadNativeTokenInfo();
90     delete [] perms;
91 }
92 
93 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)94 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
95 {
96     NativeTokenGet();
97     return 0;
98 }
99 
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
102 {
103     /* Run your code on data */
104     OHOS::SubscribeOsAccountConstraintStubFuzzTest(data, size);
105     return 0;
106 }
107