• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "getpropertystub_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_iam_callback_service.h"
24 #include "account_iam_client.h"
25 #include "account_iam_service.h"
26 #include "account_log_wrapper.h"
27 #include "fuzz_data.h"
28 #include "iaccount_iam.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31 
32 using namespace std;
33 using namespace OHOS::AccountSA;
34 using namespace OHOS::Security::AccessToken;
35 namespace OHOS {
36 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
37 
38 class MockGetSetPropCallback : public OHOS::AccountSA::GetSetPropCallback {
39 public:
~MockGetSetPropCallback()40     virtual ~MockGetSetPropCallback() {}
OnResult(int32_t result,const Attributes & extraInfo)41     void OnResult(int32_t result, const Attributes &extraInfo) override
42     {
43         return;
44     }
45 };
46 
GetPropertyStubFuzzTest(const uint8_t * data,size_t size)47 bool GetPropertyStubFuzzTest(const uint8_t *data, size_t size)
48 {
49     if ((data == nullptr) || (size == 0)) {
50         return false;
51     }
52     FuzzData fuzzData(data, size);
53     int32_t userId = fuzzData.GetData<int32_t>();
54     AuthType authType = static_cast<AuthType>(fuzzData.GenerateEnmu(IAMAuthType::TYPE_END));
55     std::vector<Attributes::AttributeKey> keys = {
56         fuzzData.GenerateEnmu(Attributes::AttributeKey::ATTR_AUTH_INTENTION)
57     };
58     GetPropertyRequest request = {
59         .authType = authType,
60         .keys = keys,
61     };
62     std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
63     sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
64 
65     MessageParcel dataTemp;
66     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
67         return false;
68     }
69     if (!dataTemp.WriteInt32(userId)) {
70         return false;
71     }
72     if (!dataTemp.WriteInt32(request.authType)) {
73         return false;
74     }
75     std::vector<uint32_t> attrKeys;
76     std::transform(request.keys.begin(), request.keys.end(), std::back_inserter(attrKeys),
77         [](const auto &key) { return static_cast<uint32_t>(key); });
78 
79     if (!dataTemp.WriteUInt32Vector(attrKeys)) {
80         return false;
81     }
82     if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
83         return false;
84     }
85 
86     MessageParcel reply;
87     MessageOption option;
88     uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::GET_PROPERTY);
89     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
90     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
91 
92     return true;
93 }
94 } // namespace OHOS
95 
NativeTokenGet()96 void NativeTokenGet()
97 {
98     uint64_t tokenId;
99     const char **perms = new const char *[1];
100     perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
101     NativeTokenInfoParams infoInstance = {
102         .dcapsNum = 0,
103         .permsNum = 1,
104         .aclsNum = 0,
105         .perms = perms,
106         .acls = nullptr,
107         .aplStr = "system_core",
108     };
109     infoInstance.processName = "GET_PROPERTY";
110     tokenId = GetAccessTokenId(&infoInstance);
111     SetSelfTokenID(tokenId);
112     AccessTokenKit::ReloadNativeTokenInfo();
113     delete [] perms;
114 }
115 
116 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)117 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
118 {
119     NativeTokenGet();
120     return 0;
121 }
122 
123 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)124 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
125 {
126     /* Run your code on data */
127     OHOS::GetPropertyStubFuzzTest(data, size);
128     return 0;
129 }
130