ipc_object_stub.cpp 21.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ipc_object_stub.h"
L
libaoping 已提交
17 18 19

#include <cstdint>
#include <ctime>
M
mamingshuai 已提交
20
#include <string>
L
libaoping 已提交
21 22 23 24

#include "hilog/log_c.h"
#include "hilog/log_cpp.h"
#include "iosfwd"
M
mamingshuai 已提交
25 26
#include "ipc_debug.h"
#include "ipc_process_skeleton.h"
L
libaoping 已提交
27
#include "ipc_skeleton.h"
M
mamingshuai 已提交
28
#include "ipc_thread_skeleton.h"
L
libaoping 已提交
29 30 31
#include "ipc_types.h"
#include "iremote_invoker.h"
#include "iremote_object.h"
M
mamingshuai 已提交
32
#include "log_tags.h"
L
libaoping 已提交
33 34 35 36 37 38 39
#include "message_option.h"
#include "message_parcel.h"
#include "refbase.h"
#include "string_ex.h"
#include "sys_binder.h"
#include "unistd.h"
#include "vector"
M
mamingshuai 已提交
40 41

#ifndef CONFIG_IPC_SINGLE
Y
yangguangzhao 已提交
42 43
#include "accesstoken_kit.h"
#include "access_token_adapter.h"
M
mamingshuai 已提交
44 45
#include "dbinder_databus_invoker.h"
#include "dbinder_error_code.h"
46
#include "rpc_feature_set.h"
M
mamingshuai 已提交
47 48 49 50 51 52 53 54 55 56 57
#include "ISessionService.h"
#endif

namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE;
#endif

using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectStub" };
#ifndef CONFIG_IPC_SINGLE
Y
yangguangzhao 已提交
58
using namespace OHOS::Security;
M
mamingshuai 已提交
59 60
// Authentication information can be added only for processes with system permission.
static constexpr pid_t ALLOWED_UID = 10000;
L
lutao 已提交
61 62
static constexpr pid_t SHELL_UID = 2000;
static constexpr int APL_BASIC = 2;
M
mamingshuai 已提交
63
// Only the samgr can obtain the UID and PID.
Y
yangguangzhao 已提交
64
static const std::string SAMGR_PROCESS_NAME = "samgr";
M
mamingshuai 已提交
65 66
#endif

67 68 69
IPCObjectStub::IPCObjectStub(std::u16string descriptor) : IRemoteObject(descriptor)
{
}
M
mamingshuai 已提交
70 71 72

IPCObjectStub::~IPCObjectStub()
{
F
fanxiaoyu 已提交
73
    ZLOGD(LABEL, "destroy, desc: %{public}s", Str16ToStr8(descriptor_).c_str());
M
mamingshuai 已提交
74 75 76 77 78 79 80 81 82 83 84 85
}

bool IPCObjectStub::IsDeviceIdIllegal(const std::string &deviceID)
{
    if (deviceID.empty() || deviceID.length() > DEVICEID_LENGTH) {
        return true;
    }
    return false;
}

int32_t IPCObjectStub::GetObjectRefCount()
{
F
fanxiaoyu 已提交
86
    return GetSptrRefCount();
M
mamingshuai 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

int IPCObjectStub::Dump(int fd, const std::vector<std::u16string> &args)
{
    const size_t numArgs = args.size();
    ZLOGE(LABEL, "Invalid call on Stub:fd:%d, args:%zu", fd, numArgs);
    return ERR_NONE;
}

int IPCObjectStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int result = ERR_NONE;
    switch (code) {
#ifndef CONFIG_IPC_SINGLE
        case DBINDER_OBITUARY_TRANSACTION: {
Y
yangguangzhao 已提交
102
            if (!IsSamgrCall(IPCSkeleton::GetCallingTokenID())) {
M
mamingshuai 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
                ZLOGE(LABEL, "%s: DBINDER_OBITUARY_TRANSACTION unauthenticated user ", __func__);
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            if (data.ReadInt32() == IRemoteObject::DeathRecipient::NOTICE_DEATH_RECIPIENT) {
                result = NoticeServiceDie(data, reply, option);
            } else {
                result = IPC_STUB_INVALID_DATA_ERR;
            }
            break;
        }
#endif
        default:
            result = IPC_STUB_UNKNOW_TRANS_ERR;
F
fanxiaoyu 已提交
117 118
            ZLOGE(LABEL, "unknown OnRemoteRequest code = %{public}u, descriptor: %{public}s", code,
                Str16ToStr8(descriptor_).c_str());
M
mamingshuai 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            break;
    }
    return result;
}

int IPCObjectStub::OnRemoteDump(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int result = ERR_NONE;
    int fd = data.ReadFileDescriptor();
    std::vector<std::u16string> args;
    if (fd != INVALID_FD) {
        if (data.ReadString16Vector(&args)) {
            result = Dump(fd, args);
        }
        ::close(fd);
    } else {
        result = IPC_STUB_INVALID_DATA_ERR;
    }
    return result;
}

int IPCObjectStub::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int result = ERR_NONE;
    switch (code) {
        case PING_TRANSACTION: {
            if (!reply.WriteInt32(ERR_NONE)) {
                result = IPC_STUB_WRITE_PARCEL_ERR;
            }
            break;
        }
        case INTERFACE_TRANSACTION: {
            std::u16string descriptor = GetObjectDescriptor();
            if (!reply.WriteString16(descriptor)) {
                ZLOGE(LABEL, "write to parcel fail");
                result = IPC_STUB_WRITE_PARCEL_ERR;
            }
            break;
        }
        case SYNCHRONIZE_REFERENCE: {
            int refCount = GetObjectRefCount();
            // when handle transaction the invoker would try to acquire
            // the object's reference to defense the object being released
            // so the actual we should decrement the temporary reference.
F
fanxiaoyu 已提交
163 164 165
            if (IPCSkeleton::IsLocalCalling()) {
                --refCount;
            }
M
mamingshuai 已提交
166 167 168
            reply.WriteInt32(refCount);
            break;
        }
L
lutao 已提交
169
#ifndef CONFIG_IPC_SINGLE
M
mamingshuai 已提交
170 171
        case DUMP_TRANSACTION: {
            pid_t uid = IPCSkeleton::GetCallingUid();
L
lutao 已提交
172 173
            uint32_t calllingTokenID = IPCSkeleton::GetFirstTokenID();
            calllingTokenID = calllingTokenID == 0 ? IPCSkeleton::GetCallingTokenID() : calllingTokenID;
L
lutao 已提交
174 175
            if (!IPCSkeleton::IsLocalCalling() ||
                (uid != 0 && uid != SHELL_UID && !HasDumpPermission(calllingTokenID))) {
M
mamingshuai 已提交
176 177 178 179 180 181
                ZLOGE(LABEL, "do not allow dump");
                break;
            }
            result = OnRemoteDump(code, data, reply, option);
            break;
        }
L
lutao 已提交
182
#endif
183 184 185 186
        case GET_PROTO_INFO: {
            result = ProcessProto(code, data, reply, option);
            break;
        }
M
mamingshuai 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#ifndef CONFIG_IPC_SINGLE
        case INVOKE_LISTEN_THREAD: {
            if (!IPCSkeleton::IsLocalCalling() || IPCSkeleton::GetCallingUid() >= ALLOWED_UID) {
                ZLOGE(LABEL, "%s: INVOKE_LISTEN_THREAD unauthenticated user ", __func__);
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = InvokerThread(code, data, reply, option);
            break;
        }
        case DBINDER_INCREFS_TRANSACTION: {
            if (IPCSkeleton::IsLocalCalling()) {
                ZLOGE(LABEL, "%s: cannot be called in same device", __func__);
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = IncStubRefs(data, reply);
            break;
        }
        case DBINDER_DECREFS_TRANSACTION: {
            if (IPCSkeleton::IsLocalCalling()) {
                ZLOGE(LABEL, "%s: cannot be called in same device", __func__);
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = DecStubRefs(data, reply);
            break;
        }
Z
zgit2021 已提交
215 216
        case DBINDER_ADD_COMMAUTH:
        case DBINDER_TRANS_COMMAUTH: {
M
mamingshuai 已提交
217 218 219 220 221
            if (IPCSkeleton::IsLocalCalling() || IPCSkeleton::GetCallingUid() >= ALLOWED_UID) {
                ZLOGE(LABEL, "%s: DBINDER_ADD_COMMAUTH unauthenticated user ", __func__);
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
Z
zgit2021 已提交
222
            result = AddAuthInfo(data, reply, code);
M
mamingshuai 已提交
223 224 225 226 227 228 229 230
            break;
        }
        case GET_UIDPID_INFO: {
            if (!IPCSkeleton::IsLocalCalling()) {
                ZLOGE(LABEL, "GET_UIDPID_INFO message is not from sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
231 232
            int32_t systemAbilityId = data.ReadInt32();
            std::string sessionName = GetDataBusName(systemAbilityId);
M
mamingshuai 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245
            if (sessionName.empty()) {
                ZLOGE(LABEL, "sessionName is empty");
                result = IPC_STUB_CREATE_BUS_SERVER_ERR;
                break;
            }
            if (!reply.WriteString(sessionName)) {
                ZLOGE(LABEL, "write to parcel fail");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            break;
        }
        case GRANT_DATABUS_NAME: {
Y
yangguangzhao 已提交
246
            if (!IPCSkeleton::IsLocalCalling() || !IsSamgrCall((uint32_t)RpcGetSelfTokenID())) {
M
mamingshuai 已提交
247 248 249 250 251 252 253
                ZLOGE(LABEL, "GRANT_DATABUS_NAME message is excluded in sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = GrantDataBusName(code, data, reply, option);
            break;
        }
Z
zgit2021 已提交
254
        case TRANS_DATABUS_NAME: {
Y
yangguangzhao 已提交
255
            if (!IPCSkeleton::IsLocalCalling() || !IsSamgrCall((uint32_t)RpcGetSelfTokenID())) {
Z
zgit2021 已提交
256 257 258 259 260 261 262
                ZLOGE(LABEL, "TRANS_DATABUS_NAME message is excluded in sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = TransDataBusName(code, data, reply, option);
            break;
        }
M
mamingshuai 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
#endif
        default:
            result = OnRemoteRequest(code, data, reply, option);
            break;
    }

    return result;
}

void IPCObjectStub::OnFirstStrongRef(const void *objectId)
{
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();

    if (current != nullptr) {
        current->AttachObject(this);
    }
}

void IPCObjectStub::OnLastStrongRef(const void *objectId)
{
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();

    if (current != nullptr) {
        current->DetachObject(this);
#ifndef CONFIG_IPC_SINGLE
        current->DetachStubRecvRefInfo(this);
        current->DetachStubSendRefInfo(this);
        (void)current->DetachStubRefTimes(this);
        current->DetachCommAuthInfoByStub(this);
        uint64_t stubIndex = current->EraseStubIndex(reinterpret_cast<IRemoteObject *>(this));
        current->DetachAppInfoToStubIndex(stubIndex);
#endif
    }
}

bool IPCObjectStub::AddDeathRecipient(const sptr<DeathRecipient> &recipient)
{
300
    (void)recipient;
M
mamingshuai 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    return false;
}

bool IPCObjectStub::RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
{
    return false;
}

pid_t IPCObjectStub::GetCallingPid()
{
    return IPCSkeleton::GetCallingPid();
}

pid_t IPCObjectStub::GetCallingUid()
{
    return IPCSkeleton::GetCallingUid();
}

X
Xi_Yuhao 已提交
319 320 321 322 323
uint32_t IPCObjectStub::GetCallingTokenID()
{
    return IPCSkeleton::GetCallingTokenID();
}

324 325 326 327 328
uint64_t IPCObjectStub::GetCallingFullTokenID()
{
    return IPCSkeleton::GetCallingFullTokenID();
}

X
Xi_Yuhao 已提交
329 330 331 332 333
uint32_t IPCObjectStub::GetFirstTokenID()
{
    return IPCSkeleton::GetFirstTokenID();
}

334 335 336 337 338
uint64_t IPCObjectStub::GetFirstFullTokenID()
{
    return IPCSkeleton::GetFirstFullTokenID();
}

339 340 341 342 343
int IPCObjectStub::GetObjectType() const
{
    return OBJECT_TYPE_NATIVE;
}

M
mamingshuai 已提交
344 345 346
int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int result = ERR_NONE;
347
    ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object");
M
mamingshuai 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    if (!reply.WriteUint32(IRemoteObject::IF_PROT_BINDER)) {
        ZLOGE(LABEL, "write to parcel fail");
        result = IPC_STUB_WRITE_PARCEL_ERR;
    }
    return result;
}

#ifndef CONFIG_IPC_SINGLE
int32_t IPCObjectStub::InvokerThread(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    switch (data.ReadUint32()) {
        case IRemoteObject::DATABUS_TYPE: {
            if (InvokerDataBusThread(data, reply) != ERR_NONE) {
                ZLOGE(LABEL, "Invoker databus thread fail");
                return IPC_STUB_INVOKE_THREAD_ERR;
            }
            break;
        }
        default: {
            ZLOGE(LABEL, "InvokerThread Invalid Type");
            return IPC_STUB_INVALID_DATA_ERR;
        }
    }

    return ERR_NONE;
}

int32_t IPCObjectStub::InvokerDataBusThread(MessageParcel &data, MessageParcel &reply)
{
    std::string deviceId = data.ReadString();
    uint32_t remotePid = data.ReadUint32();
    uint32_t remoteUid = data.ReadUint32();
    std::string remoteDeviceId = data.ReadString();
    std::string sessionName = data.ReadString();
382 383 384 385 386 387 388 389 390 391
    uint32_t featureSet = data.ReadUint32();
    uint32_t tokenId = 0;

    std::shared_ptr<FeatureSetData> feature = std::make_shared<FeatureSetData>();
    if (feature == nullptr) {
        ZLOGE(LABEL, "%s: feature null", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    feature->featureSet = featureSet;
    feature->tokenId = tokenId;
M
mamingshuai 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    if (IsDeviceIdIllegal(deviceId) || IsDeviceIdIllegal(remoteDeviceId) || sessionName.empty()) {
        ZLOGE(LABEL, "%s: device ID is invalid or session name nil", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }

    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "IPCProcessSkeleton is nullptr");
        return IPC_STUB_CURRENT_NULL_ERR;
    }
    if (!current->CreateSoftbusServer(sessionName)) {
        ZLOGE(LABEL, "%s: fail to create databus server", __func__);
        return IPC_STUB_CREATE_BUS_SERVER_ERR;
    }

    uint64_t stubIndex = current->AddStubByIndex(this);
    if (stubIndex == 0) {
        ZLOGE(LABEL, "%s: add stub fail", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    if (!reply.WriteUint64(stubIndex) || !reply.WriteString(sessionName) || !reply.WriteString(deviceId)) {
        ZLOGE(LABEL, "%s: write to parcel fail", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    if (!current->AttachAppInfoToStubIndex(remotePid, remoteUid, remoteDeviceId, stubIndex)) {
        ZLOGE(LABEL, "fail to attach appinfo to stubIndex, maybe attach already");
    }
419
    if (!current->AttachCommAuthInfo(this, (int32_t)remotePid, (int32_t)remoteUid, remoteDeviceId, feature)) {
M
mamingshuai 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
        ZLOGE(LABEL, "fail to attach comm auth info");
    }

    return ERR_NONE;
}

int32_t IPCObjectStub::NoticeServiceDie(MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "%s: current is null", __func__);
        return IPC_STUB_CURRENT_NULL_ERR;
    }

    IPCObjectProxy *ipcProxy = current->QueryCallbackProxy(this);
    if (ipcProxy == nullptr) {
        ZLOGE(LABEL, "%s: ipc proxy is null", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }

    ipcProxy->SendObituary();

    if (!current->DetachCallbackStub(this)) {
        ZLOGE(LABEL, "%s: fail to detach callback stub", __func__);
        // do nothing, RemoveDeathRecipient can delete this too
    }

    return ERR_NONE;
}

int32_t IPCObjectStub::IncStubRefs(MessageParcel &data, MessageParcel &reply)
{
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "%s: current is null", __func__);
        return IPC_STUB_CURRENT_NULL_ERR;
    }
    std::string deviceId = IPCSkeleton::GetCallingDeviceID();
    if (deviceId.empty()) {
        ZLOGE(LABEL, "%s: calling error", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    if (!current->AttachStubRecvRefInfo(this, IPCSkeleton::GetCallingPid(), deviceId)) {
        ZLOGE(LABEL, "%s: attach stub ref info err, already in", __func__);
        return ERR_NONE;
    }
    if (!current->DecStubRefTimes(this)) {
        this->IncStrongRef(this);
    }
    return ERR_NONE;
}

int32_t IPCObjectStub::DecStubRefs(MessageParcel &data, MessageParcel &reply)
{
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "%s: current is null", __func__);
        return IPC_STUB_CURRENT_NULL_ERR;
    }

    std::string deviceId = IPCSkeleton::GetCallingDeviceID();
    current->DetachStubRefInfo(this, IPCSkeleton::GetCallingPid(), deviceId);
    return ERR_NONE;
}

Z
zgit2021 已提交
485
int32_t IPCObjectStub::AddAuthInfo(MessageParcel &data, MessageParcel &reply, uint32_t code)
M
mamingshuai 已提交
486 487 488 489
{
    uint32_t remotePid = data.ReadUint32();
    uint32_t remoteUid = data.ReadUint32();
    std::string remoteDeviceId = data.ReadString();
490 491 492 493 494 495 496 497 498 499 500
    uint32_t remoteFeature = data.ReadUint32();
    uint32_t tokenId = 0;

    std::shared_ptr<FeatureSetData> feature = nullptr;
    feature.reset(reinterpret_cast<FeatureSetData *>(::operator new(sizeof(FeatureSetData))));
    if (feature == nullptr) {
        ZLOGE(LABEL, "%s: feature null", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    feature->featureSet = remoteFeature;
    feature->tokenId = tokenId;
M
mamingshuai 已提交
501 502 503 504 505 506 507 508 509 510 511
    if (IsDeviceIdIllegal(remoteDeviceId)) {
        ZLOGE(LABEL, "%s: remote deviceId is null", __func__);
        return IPC_STUB_INVALID_DATA_ERR;
    }

    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "%s: current is null", __func__);
        return IPC_STUB_CURRENT_NULL_ERR;
    }

512
    if (!current->AttachCommAuthInfo(this, (int32_t)remotePid, (int32_t)remoteUid, remoteDeviceId, feature)) {
M
mamingshuai 已提交
513 514 515
        ZLOGE(LABEL, "fail to attach comm auth info fail");
        return IPC_STUB_INVALID_DATA_ERR;
    }
Z
zgit2021 已提交
516 517 518 519 520 521 522 523 524 525
    if (code == DBINDER_TRANS_COMMAUTH) {
        uint64_t stubIndex = data.ReadUint64();
        if (stubIndex == 0) {
            ZLOGE(LABEL, "fail to attach comm auth info fail");
            return BINDER_CALLBACK_STUBINDEX_ERR;
        }
        if (!current->AttachAppInfoToStubIndex(remotePid, remoteUid, remoteDeviceId, stubIndex)) {
            ZLOGE(LABEL, "fail to add appinfo and stubIndex, maybe attach already");
        }
    }
M
mamingshuai 已提交
526 527 528
    return ERR_NONE;
}

529
std::string IPCObjectStub::GetDataBusName(int32_t systemAbilityId)
M
mamingshuai 已提交
530
{
Z
zgit2021 已提交
531 532 533 534 535 536
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "get current is null");
        return std::string("");
    }
    sptr<IRemoteObject> object = current->GetSAMgrObject();
M
mamingshuai 已提交
537 538 539 540 541 542
    if (object == nullptr) {
        ZLOGE(LABEL, "get object is null");
        return std::string("");
    }

    IPCObjectProxy *samgr = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
543
    return samgr->GetDataBusName(systemAbilityId);
M
mamingshuai 已提交
544 545 546 547 548 549
}

int32_t IPCObjectStub::GrantDataBusName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int pid = IPCSkeleton::GetCallingPid();
    int uid = IPCSkeleton::GetCallingUid();
550 551
    int systemAbilityId = data.ReadInt32();
    std::string sessionName = CreateDatabusName(uid, pid, systemAbilityId);
M
mamingshuai 已提交
552 553 554 555 556 557 558 559 560 561 562 563
    if (sessionName.empty()) {
        ZLOGE(LABEL, "pid/uid is invalid, pid = {public}%d, uid = {public}%d", pid, uid);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    if (!reply.WriteUint32(IRemoteObject::IF_PROT_DATABUS) || !reply.WriteString(sessionName)) {
        ZLOGE(LABEL, "write to parcel fail");
        return IPC_STUB_INVALID_DATA_ERR;
    }

    return ERR_NONE;
}

Z
zgit2021 已提交
564 565 566 567 568 569 570 571
int32_t IPCObjectStub::TransDataBusName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    uint32_t remotePid = data.ReadUint32();
    uint32_t remoteUid = data.ReadUint32();
    if (remotePid == static_cast<uint32_t>(IPCSkeleton::GetCallingPid())) {
        ZLOGE(LABEL, "pid/uid is invalid, pid = {public}%d, uid = {public}%d", remotePid, remoteUid);
        return IPC_STUB_INVALID_DATA_ERR;
    }
572
    std::string sessionName = CreateDatabusName(remoteUid, remotePid, 0);
Z
zgit2021 已提交
573 574 575 576 577 578 579 580 581 582 583 584
    if (sessionName.empty()) {
        ZLOGE(LABEL, "pid/uid is invalid, pid = {public}%d, uid = {public}%d", remotePid, remoteUid);
        return IPC_STUB_INVALID_DATA_ERR;
    }
    if (!reply.WriteUint32(IRemoteObject::IF_PROT_DATABUS) || !reply.WriteString(sessionName)) {
        ZLOGE(LABEL, "write to parcel fail");
        return IPC_STUB_INVALID_DATA_ERR;
    }

    return ERR_NONE;
}

585
std::string IPCObjectStub::CreateDatabusName(int uid, int pid, int systemAbilityId)
M
mamingshuai 已提交
586 587 588 589 590 591 592 593
{
    std::shared_ptr<ISessionService> softbusManager = ISessionService::GetInstance();
    if (softbusManager == nullptr) {
        ZLOGE(LABEL, "fail to get softbus service");
        return "";
    }

    std::string sessionName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
594 595 596
    if (systemAbilityId > 0) {
        sessionName += std::string("_") + std::to_string(systemAbilityId);
    }
M
mamingshuai 已提交
597 598 599 600 601 602 603
    if (softbusManager->GrantPermission(uid, pid, sessionName) != ERR_NONE) {
        ZLOGE(LABEL, "fail to Grant Permission softbus name");
        return "";
    }

    return sessionName;
}
Y
yangguangzhao 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

bool IPCObjectStub::IsSamgrCall(uint32_t accessToken)
{
    auto tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessToken);
    if (tokenType != AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
        ZLOGE(LABEL, "not native call");
        return false;
    }
    AccessToken::NativeTokenInfo nativeTokenInfo;
    int32_t result = AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
    if (result == ERR_NONE && nativeTokenInfo.processName == SAMGR_PROCESS_NAME) {
        return true;
    }
    ZLOGE(LABEL, "not samgr called, processName:%{private}s", nativeTokenInfo.processName.c_str());
    return false;
}
L
lutao 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

bool IPCObjectStub::HasDumpPermission(uint32_t accessToken) const
{
    int res = AccessToken::AccessTokenKit::VerifyAccessToken(accessToken, "ohos.permission.DUMP");
    if (res == AccessToken::PermissionState::PERMISSION_GRANTED) {
        return true;
    }
    bool ret = false;
    auto tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessToken);
    if (tokenType == AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
        AccessToken::NativeTokenInfo nativeTokenInfo;
        int32_t result = AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
        ret =  (result == ERR_NONE && nativeTokenInfo.apl >= APL_BASIC);
    } else if (tokenType == AccessToken::ATokenTypeEnum::TOKEN_HAP) {
        AccessToken::HapTokenInfo hapTokenInfo;
        int32_t result = AccessToken::AccessTokenKit::GetHapTokenInfo(accessToken, hapTokenInfo);
        ret =  (result == ERR_NONE && hapTokenInfo.apl >= APL_BASIC);
    }
    if (!ret) {
Z
zhangboyuan 已提交
639
        ZLOGD(LABEL, "No dump permission, please check!");
L
lutao 已提交
640 641 642
    }
    return ret;
}
M
mamingshuai 已提交
643 644
#endif
} // namespace OHOS