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 "authuserstub_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 MockIDMCallback : public OHOS::AccountSA::IDMCallback {
39 public:
~MockIDMCallback()40 virtual ~MockIDMCallback()
41 {
42 }
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)43 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
44 {
45 return;
46 }
OnResult(int32_t result,const Attributes & extraInfo)47 void OnResult(int32_t result, const Attributes &extraInfo) override
48 {
49 return;
50 }
51 };
52
AuthUserStubFuzzTest(const uint8_t * data,size_t size)53 bool AuthUserStubFuzzTest(const uint8_t *data, size_t size)
54 {
55 if ((data == nullptr) || (size == 0)) {
56 return false;
57 }
58 FuzzData fuzzData(data, size);
59 int32_t userId = fuzzData.GetData<int32_t>();
60 std::vector<uint8_t> challenge = {fuzzData.GetData<uint8_t>()};
61 AuthType authType = static_cast<AuthType>(fuzzData.GenerateEnmu(IAMAuthType::TYPE_END));
62 AuthTrustLevel authTrustLevel = fuzzData.GenerateEnmu(AuthTrustLevel::ATL4);
63 std::shared_ptr<IDMCallback> ptr = make_shared<MockIDMCallback>();
64 sptr<IIDMCallback> callback = new (std::nothrow) IDMCallbackService(userId, ptr);
65
66 MessageParcel dataTemp;
67 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
68 return false;
69 }
70 if (!dataTemp.WriteInt32(userId)) {
71 return false;
72 }
73 if (!dataTemp.WriteUInt8Vector(challenge)) {
74 return false;
75 }
76 if (!dataTemp.WriteInt32(authType)) {
77 return false;
78 }
79 if (!dataTemp.WriteUint32(authTrustLevel)) {
80 return false;
81 }
82 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
83 return false;
84 }
85 MessageParcel reply;
86 MessageOption option;
87 uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::AUTH_USER);
88 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
89 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
90
91 return true;
92 }
93 } // namespace OHOS
94
NativeTokenGet()95 void NativeTokenGet()
96 {
97 uint64_t tokenId;
98 const char **perms = new const char *[1];
99 perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
100 NativeTokenInfoParams infoInstance = {
101 .dcapsNum = 0,
102 .permsNum = 1,
103 .aclsNum = 0,
104 .perms = perms,
105 .acls = nullptr,
106 .aplStr = "system_core",
107 };
108 infoInstance.processName = "AUTH_USER";
109 tokenId = GetAccessTokenId(&infoInstance);
110 SetSelfTokenID(tokenId);
111 AccessTokenKit::ReloadNativeTokenInfo();
112 delete [] perms;
113 }
114
115 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)116 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
117 {
118 NativeTokenGet();
119 return 0;
120 }
121
122 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)123 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
124 {
125 /* Run your code on data */
126 OHOS::AuthUserStubFuzzTest(data, size);
127 return 0;
128 }
129