117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#include "devauth_fuzzer.h"
1717fd14ceSopenharmony_ci
1817fd14ceSopenharmony_ci#include <cstddef>
1917fd14ceSopenharmony_ci#include <cstdint>
2017fd14ceSopenharmony_ci
2117fd14ceSopenharmony_ci#include "access_token.h"
2217fd14ceSopenharmony_ci#include "accesstoken_kit.h"
2317fd14ceSopenharmony_ci#include "access_token_error.h"
2417fd14ceSopenharmony_ci#include "hc_log.h"
2517fd14ceSopenharmony_ci#include "ipc_adapt.h"
2617fd14ceSopenharmony_ci#include "ipc_callback_stub.h"
2717fd14ceSopenharmony_ci#include "ipc_dev_auth_stub.h"
2817fd14ceSopenharmony_ci#include "ipc_sdk.h"
2917fd14ceSopenharmony_ci#include "ipc_service.h"
3017fd14ceSopenharmony_ci#include "message_parcel.h"
3117fd14ceSopenharmony_ci#include "nativetoken_kit.h"
3217fd14ceSopenharmony_ci#include "securec.h"
3317fd14ceSopenharmony_ci#include "token_setproc.h"
3417fd14ceSopenharmony_ci
3517fd14ceSopenharmony_cinamespace OHOS {
3617fd14ceSopenharmony_ciconst std::u16string DEV_AUTH_SERVICE_INTERFACE_TOKEN = u"deviceauth.IMethodsIpcCall";
3717fd14ceSopenharmony_ci
3817fd14ceSopenharmony_cistatic void NativeTokenSet(const char *procName)
3917fd14ceSopenharmony_ci{
4017fd14ceSopenharmony_ci    const char *acls[] = {"ACCESS_IDS"};
4117fd14ceSopenharmony_ci    const char *perms[] = {
4217fd14ceSopenharmony_ci        "ohos.permission.PLACE_CALL",
4317fd14ceSopenharmony_ci        "ohos.permission.ACCESS_IDS"
4417fd14ceSopenharmony_ci    };
4517fd14ceSopenharmony_ci    uint64_t tokenId;
4617fd14ceSopenharmony_ci    NativeTokenInfoParams infoInstance = {
4717fd14ceSopenharmony_ci        .dcapsNum = 0,
4817fd14ceSopenharmony_ci        .permsNum = 2,
4917fd14ceSopenharmony_ci        .aclsNum = 1,
5017fd14ceSopenharmony_ci        .dcaps = NULL,
5117fd14ceSopenharmony_ci        .perms = perms,
5217fd14ceSopenharmony_ci        .acls = acls,
5317fd14ceSopenharmony_ci        .processName = procName,
5417fd14ceSopenharmony_ci        .aplStr = "system_core",
5517fd14ceSopenharmony_ci    };
5617fd14ceSopenharmony_ci    tokenId = GetAccessTokenId(&infoInstance);
5717fd14ceSopenharmony_ci    SetSelfTokenID(tokenId);
5817fd14ceSopenharmony_ci    Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
5917fd14ceSopenharmony_ci}
6017fd14ceSopenharmony_ci
6117fd14ceSopenharmony_cibool FuzzDoRegCallback(const uint8_t* data, size_t size)
6217fd14ceSopenharmony_ci{
6317fd14ceSopenharmony_ci    (void)InitDeviceAuthService();
6417fd14ceSopenharmony_ci    (void)MainRescInit();
6517fd14ceSopenharmony_ci    ServiceDevAuth *serviceObj = new(std::nothrow) ServiceDevAuth();
6617fd14ceSopenharmony_ci    if (serviceObj == nullptr) {
6717fd14ceSopenharmony_ci        return false;
6817fd14ceSopenharmony_ci    }
6917fd14ceSopenharmony_ci    sptr<ServiceDevAuth> sptrObj = serviceObj;
7017fd14ceSopenharmony_ci    uintptr_t serviceCtx = reinterpret_cast<uintptr_t>(serviceObj);
7117fd14ceSopenharmony_ci    (void)AddMethodMap(serviceCtx);
7217fd14ceSopenharmony_ci    for (int32_t i = IPC_CALL_ID_REG_CB; i <= IPC_CALL_ID_GET_PSEUDONYM_ID; i++) {
7317fd14ceSopenharmony_ci        if (i == IPC_CALL_ID_AUTH_DEVICE || i == IPC_CALL_ID_GA_PROC_DATA || i == IPC_CALL_GA_CANCEL_REQUEST) {
7417fd14ceSopenharmony_ci            NativeTokenSet("softbus_server");
7517fd14ceSopenharmony_ci        } else if (i == IPC_CALL_ID_GET_PK_INFO_LIST) {
7617fd14ceSopenharmony_ci            NativeTokenSet("dslm_service");
7717fd14ceSopenharmony_ci        } else {
7817fd14ceSopenharmony_ci            NativeTokenSet("device_manager");
7917fd14ceSopenharmony_ci        }
8017fd14ceSopenharmony_ci        MessageParcel datas;
8117fd14ceSopenharmony_ci        datas.WriteInterfaceToken(DEV_AUTH_SERVICE_INTERFACE_TOKEN);
8217fd14ceSopenharmony_ci        datas.WriteInt32(i);
8317fd14ceSopenharmony_ci        datas.WriteInt32(size + sizeof(int32_t));
8417fd14ceSopenharmony_ci        datas.WriteInt32(0);
8517fd14ceSopenharmony_ci        datas.WriteInt32(size);
8617fd14ceSopenharmony_ci        datas.WriteBuffer(data, size);
8717fd14ceSopenharmony_ci        datas.RewindRead(0);
8817fd14ceSopenharmony_ci        MessageParcel reply;
8917fd14ceSopenharmony_ci        MessageOption option;
9017fd14ceSopenharmony_ci        (void)serviceObj->OnRemoteRequest(1, datas, reply, option);
9117fd14ceSopenharmony_ci    }
9217fd14ceSopenharmony_ci    return true;
9317fd14ceSopenharmony_ci}
9417fd14ceSopenharmony_ci}
9517fd14ceSopenharmony_ci
9617fd14ceSopenharmony_ci/* Fuzzer entry point */
9717fd14ceSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
9817fd14ceSopenharmony_ci{
9917fd14ceSopenharmony_ci    /* Run your code on data */
10017fd14ceSopenharmony_ci    OHOS::FuzzDoRegCallback(data, size);
10117fd14ceSopenharmony_ci    return 0;
10217fd14ceSopenharmony_ci}
10317fd14ceSopenharmony_ci
104