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 "prepareremoteauthstub_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_service.h"
25 #include "account_log_wrapper.h"
26 #include "fuzz_data.h"
27 #include "account_i_a_m_stub.h"
28 #include "securec.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 PreRemoteAuthCallbackImpl : public PreRemoteAuthCallback {
39 public:
40 virtual ~PreRemoteAuthCallbackImpl() = default;
OnResult(int32_t result)41 void OnResult(int32_t result) override
42 {
43 return;
44 }
45 };
46
PrepareRemoteAuthStubFuzzTest(const uint8_t * data,size_t size)47 bool PrepareRemoteAuthStubFuzzTest(const uint8_t *data, size_t size)
48 {
49 if ((data == nullptr) || (size == 0)) {
50 return false;
51 }
52 MessageParcel dataTemp;
53 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
54 return false;
55 }
56 FuzzData fuzzData(data, size);
57 std::string remoteNetworkId = fuzzData.GenerateString();
58 if (!dataTemp.WriteString(remoteNetworkId)) {
59 return false;
60 }
61 auto callback = std::make_shared<PreRemoteAuthCallbackImpl>();
62 sptr<IPreRemoteAuthCallback> wrapper = new (std::nothrow) PreRemoteAuthCallbackService(callback);
63 if (!dataTemp.WriteRemoteObject(wrapper->AsObject())) {
64 return false;
65 }
66 MessageParcel reply;
67 MessageOption option;
68 uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_PREPARE_REMOTE_AUTH);
69 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
70 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
71
72 return true;
73 }
74 } // namespace OHOS
75
NativeTokenGet()76 void NativeTokenGet()
77 {
78 uint64_t tokenId;
79 const char **perms = new const char *[1];
80 perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
81 NativeTokenInfoParams infoInstance = {
82 .dcapsNum = 0,
83 .permsNum = 1,
84 .aclsNum = 0,
85 .perms = perms,
86 .acls = nullptr,
87 .aplStr = "system_core",
88 };
89 infoInstance.processName = "PrepareRemoteAuth";
90 tokenId = GetAccessTokenId(&infoInstance);
91 SetSelfTokenID(tokenId);
92 AccessTokenKit::ReloadNativeTokenInfo();
93 delete [] perms;
94 }
95
96 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)97 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
98 {
99 NativeTokenGet();
100 return 0;
101 }
102
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
105 {
106 /* Run your code on data */
107 OHOS::PrepareRemoteAuthStubFuzzTest(data, size);
108 return 0;
109 }
110