ipc_object_stub.cpp 21.1 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
#include "message_option.h"
#include "message_parcel.h"
Y
YOUR_NAME 已提交
35
#include "process_skeleton.h"
L
libaoping 已提交
36 37 38 39 40
#include "refbase.h"
#include "string_ex.h"
#include "sys_binder.h"
#include "unistd.h"
#include "vector"
M
mamingshuai 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#ifndef CONFIG_IPC_SINGLE
#include "dbinder_databus_invoker.h"
#include "dbinder_error_code.h"
#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
// Authentication information can be added only for processes with system permission.
static constexpr pid_t ALLOWED_UID = 10000;
#endif
L
lutao 已提交
58
static constexpr int SHELL_UID = 2000;
Y
YOUR_NAME 已提交
59
static constexpr int HIDUMPER_SERVICE_UID = 1212;
M
mamingshuai 已提交
60

61 62
IPCObjectStub::IPCObjectStub(std::u16string descriptor) : IRemoteObject(descriptor)
{
S
shufewhx 已提交
63
    ZLOGI(LABEL, "create, desc: %{public}s", Str16ToStr8(descriptor_).c_str());
64
}
M
mamingshuai 已提交
65 66 67

IPCObjectStub::~IPCObjectStub()
{
S
shufewhx 已提交
68
    ZLOGW(LABEL, "destroy, desc: %{public}s", Str16ToStr8(descriptor_).c_str());
M
mamingshuai 已提交
69 70 71 72 73 74 75 76 77 78 79 80
}

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

int32_t IPCObjectStub::GetObjectRefCount()
{
F
fanxiaoyu 已提交
81
    return GetSptrRefCount();
M
mamingshuai 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

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
Yippo 已提交
97
            ZLOGW(LABEL, "%{public}s: recv DBINDER_OBITUARY_TRANSACTION", __func__);
M
mamingshuai 已提交
98 99 100 101 102 103 104 105 106 107
            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 已提交
108 109
            ZLOGE(LABEL, "unknown OnRemoteRequest code = %{public}u, descriptor: %{public}s", code,
                Str16ToStr8(descriptor_).c_str());
M
mamingshuai 已提交
110 111 112 113 114 115 116 117 118 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
            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 已提交
154 155 156
            if (IPCSkeleton::IsLocalCalling()) {
                --refCount;
            }
M
mamingshuai 已提交
157 158 159 160 161
            reply.WriteInt32(refCount);
            break;
        }
        case DUMP_TRANSACTION: {
            pid_t uid = IPCSkeleton::GetCallingUid();
L
lutao 已提交
162
            if (!IPCSkeleton::IsLocalCalling() || (uid != 0 && uid != SHELL_UID && uid != HIDUMPER_SERVICE_UID)) {
M
mamingshuai 已提交
163 164 165 166 167 168
                ZLOGE(LABEL, "do not allow dump");
                break;
            }
            result = OnRemoteDump(code, data, reply, option);
            break;
        }
169 170 171 172
        case GET_PROTO_INFO: {
            result = ProcessProto(code, data, reply, option);
            break;
        }
M
mamingshuai 已提交
173 174 175
#ifndef CONFIG_IPC_SINGLE
        case INVOKE_LISTEN_THREAD: {
            if (!IPCSkeleton::IsLocalCalling() || IPCSkeleton::GetCallingUid() >= ALLOWED_UID) {
H
heyingjiao 已提交
176
                ZLOGE(LABEL, "%{public}s: INVOKE_LISTEN_THREAD unauthenticated user ", __func__);
M
mamingshuai 已提交
177 178 179 180 181 182 183 184
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = InvokerThread(code, data, reply, option);
            break;
        }
        case DBINDER_INCREFS_TRANSACTION: {
            if (IPCSkeleton::IsLocalCalling()) {
H
heyingjiao 已提交
185
                ZLOGE(LABEL, "dbinder incref in the same device is invalid");
M
mamingshuai 已提交
186 187 188
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
H
heyingjiao 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            pid_t callerPid = IPCSkeleton::GetCallingPid();
            pid_t callerUid = IPCSkeleton::GetCallingUid();
            uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
            std::string callerDevId = IPCSkeleton::GetCallingDeviceID();
            IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
            uint64_t stubIndex = current->QueryStubIndex(this);
            DBinderDatabusInvoker *invoker = reinterpret_cast<DBinderDatabusInvoker *>(
                IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DATABUS));
            if (invoker == nullptr) {
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            uint32_t listenFd = invoker->GetClientFd();
            // update listenFd
            ZLOGW(LABEL, "update app info listenFd: %{public}u, stubIndex: %{public}" PRIu64 ", tokenId: %{public}u",
                listenFd, stubIndex, tokenId);
            current->AttachAppInfoToStubIndex(callerPid, callerUid, tokenId, callerDevId, stubIndex, listenFd);
M
mamingshuai 已提交
206 207 208 209
            break;
        }
        case DBINDER_DECREFS_TRANSACTION: {
            if (IPCSkeleton::IsLocalCalling()) {
H
heyingjiao 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                ZLOGE(LABEL, "dbinder decref in the same device is invalid");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            // stub's refcount will be decreased either in this case or OnSessionClosed callback
            // we may race with OnSessionClosed callback, thus dec refcount only when removing appInfo sucessfully
            pid_t callerPid = IPCSkeleton::GetCallingPid();
            pid_t callerUid = IPCSkeleton::GetCallingUid();
            uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
            std::string callerDevId = IPCSkeleton::GetCallingDeviceID();
            IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
            uint64_t stubIndex = current->QueryStubIndex(this);
            DBinderDatabusInvoker *invoker = reinterpret_cast<DBinderDatabusInvoker *>(
                IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DATABUS));
            if (invoker == nullptr) {
M
mamingshuai 已提交
225 226 227
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
H
heyingjiao 已提交
228 229 230 231 232 233
            uint32_t listenFd = invoker->GetClientFd();
            // detach info whose listen fd equals the given one
            if (current->DetachAppInfoToStubIndex(callerPid, callerUid, tokenId, callerDevId, stubIndex, listenFd)) {
                current->DetachCommAuthInfo(this, callerPid, callerUid, tokenId, callerDevId);
                DecStrongRef(this);
            }
M
mamingshuai 已提交
234 235
            break;
        }
H
heyingjiao 已提交
236
        case DBINDER_ADD_COMMAUTH: {
M
mamingshuai 已提交
237
            if (IPCSkeleton::IsLocalCalling() || IPCSkeleton::GetCallingUid() >= ALLOWED_UID) {
H
heyingjiao 已提交
238
                ZLOGE(LABEL, "DBINDER_ADD_COMMAUTH unauthenticated user ");
M
mamingshuai 已提交
239 240 241
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
Z
zgit2021 已提交
242
            result = AddAuthInfo(data, reply, code);
M
mamingshuai 已提交
243 244
            break;
        }
H
heyingjiao 已提交
245
        case GET_SESSION_NAME: {
M
mamingshuai 已提交
246 247 248 249 250
            if (!IPCSkeleton::IsLocalCalling()) {
                ZLOGE(LABEL, "GET_UIDPID_INFO message is not from sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
H
heyingjiao 已提交
251
            std::string sessionName = GetSessionName();
M
mamingshuai 已提交
252 253 254 255 256 257 258 259 260 261 262 263
            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;
        }
H
heyingjiao 已提交
264
        case GET_GRANTED_SESSION_NAME: {
Y
YOUR_NAME 已提交
265
            if (!IPCSkeleton::IsLocalCalling() || !IsSamgrCall()) {
M
mamingshuai 已提交
266 267 268 269
                ZLOGE(LABEL, "GRANT_DATABUS_NAME message is excluded in sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
H
heyingjiao 已提交
270
            result = GetGrantedSessionName(code, data, reply, option);
M
mamingshuai 已提交
271 272
            break;
        }
H
heyingjiao 已提交
273
        case GET_SESSION_NAME_PID_UID: {
Y
YOUR_NAME 已提交
274
            if (!IPCSkeleton::IsLocalCalling() || !IsSamgrCall()) {
Z
zgit2021 已提交
275 276 277 278
                ZLOGE(LABEL, "TRANS_DATABUS_NAME message is excluded in sa manager");
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
H
heyingjiao 已提交
279 280 281 282 283 284 285 286 287
            result = GetSessionNameForPidUid(code, data, reply, option);
            break;
        }
        case GET_PID_UID: {
            if (!IPCSkeleton::IsLocalCalling()) {
                result = IPC_STUB_INVALID_DATA_ERR;
                break;
            }
            result = GetPidUid(data, reply);
Z
zgit2021 已提交
288 289
            break;
        }
M
mamingshuai 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
#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
H
heyingjiao 已提交
315 316
        // we only need to erase stub index here, commAuth and appInfo
        // has already been removed either in dbinder dec refcount case
317
        // or OnSessionClosed, we also remove commAuth and appInfo in case of leak
M
mamingshuai 已提交
318
        current->DetachCommAuthInfoByStub(this);
H
heyingjiao 已提交
319
        uint64_t stubIndex = current->EraseStubIndex(this);
M
mamingshuai 已提交
320 321 322 323 324 325 326
        current->DetachAppInfoToStubIndex(stubIndex);
#endif
    }
}

bool IPCObjectStub::AddDeathRecipient(const sptr<DeathRecipient> &recipient)
{
327
    (void)recipient;
M
mamingshuai 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    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 已提交
346 347 348 349 350
uint32_t IPCObjectStub::GetCallingTokenID()
{
    return IPCSkeleton::GetCallingTokenID();
}

351 352 353 354 355
uint64_t IPCObjectStub::GetCallingFullTokenID()
{
    return IPCSkeleton::GetCallingFullTokenID();
}

X
Xi_Yuhao 已提交
356 357 358 359 360
uint32_t IPCObjectStub::GetFirstTokenID()
{
    return IPCSkeleton::GetFirstTokenID();
}

361 362 363 364 365
uint64_t IPCObjectStub::GetFirstFullTokenID()
{
    return IPCSkeleton::GetFirstFullTokenID();
}

366 367 368 369 370
int IPCObjectStub::GetObjectType() const
{
    return OBJECT_TYPE_NATIVE;
}

M
mamingshuai 已提交
371 372 373
int32_t IPCObjectStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    int result = ERR_NONE;
374
    ZLOGD(LABEL, "IPCObjectStub::ProcessProto called, type = 0, normal stub object");
M
mamingshuai 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    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();
H
heyingjiao 已提交
409
    uint32_t remoteTokenId = data.ReadUint32();
M
mamingshuai 已提交
410
    if (IsDeviceIdIllegal(deviceId) || IsDeviceIdIllegal(remoteDeviceId) || sessionName.empty()) {
H
heyingjiao 已提交
411
        ZLOGE(LABEL, "%{public}s: device ID is invalid or session name nil", __func__);
M
mamingshuai 已提交
412 413 414 415 416 417 418 419 420
        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)) {
H
heyingjiao 已提交
421
        ZLOGE(LABEL, "%{public}s: fail to create databus server", __func__);
M
mamingshuai 已提交
422 423 424 425 426
        return IPC_STUB_CREATE_BUS_SERVER_ERR;
    }

    uint64_t stubIndex = current->AddStubByIndex(this);
    if (stubIndex == 0) {
H
heyingjiao 已提交
427
        ZLOGE(LABEL, "%{public}s: add stub fail", __func__);
M
mamingshuai 已提交
428 429
        return IPC_STUB_INVALID_DATA_ERR;
    }
H
heyingjiao 已提交
430 431 432 433 434 435 436 437 438 439

    uint32_t selfTokenId = static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID());
    ZLOGW(LABEL,
        "invoke databus thread, local deviceId: %{public}s, remotePid: %{public}u, remoteUid: %{public}u, "
        "stubIndex: %{public}" PRIu64 ", remote deviceId: %{public}s, tokenId: %{public}u, selfTokenId: %{public}u",
        IPCProcessSkeleton::ConvertToSecureString(deviceId).c_str(), remotePid, remoteUid, stubIndex,
        IPCProcessSkeleton::ConvertToSecureString(remoteDeviceId).c_str(), remoteTokenId, selfTokenId);
    if (!reply.WriteUint64(stubIndex) || !reply.WriteString(sessionName) || !reply.WriteString(deviceId) ||
        !reply.WriteUint32(selfTokenId)) {
        ZLOGE(LABEL, "%{public}s: write to parcel fail", __func__);
M
mamingshuai 已提交
440 441
        return IPC_STUB_INVALID_DATA_ERR;
    }
H
heyingjiao 已提交
442 443 444
    // mark listen fd as 0
    if (!current->AttachAppInfoToStubIndex(remotePid, remoteUid, remoteTokenId, remoteDeviceId, stubIndex, 0)) {
        ZLOGW(LABEL, "app info already existed, replace with 0");
M
mamingshuai 已提交
445
    }
H
heyingjiao 已提交
446 447 448 449
    if (current->AttachCommAuthInfo(this, remotePid, remoteUid, remoteTokenId, remoteDeviceId)) {
        IncStrongRef(this);
    } else {
        ZLOGW(LABEL, "comm auth info attached already");
M
mamingshuai 已提交
450 451 452 453 454 455 456
    }

    return ERR_NONE;
}

int32_t IPCObjectStub::NoticeServiceDie(MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
Y
Yippo 已提交
457
    ZLOGE(LABEL, "%{public}s enter, desc:%{public}s", __func__, Str16ToStr8(descriptor_).c_str());
M
mamingshuai 已提交
458 459
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
H
heyingjiao 已提交
460
        ZLOGE(LABEL, "%{public}s: current is null", __func__);
M
mamingshuai 已提交
461 462 463
        return IPC_STUB_CURRENT_NULL_ERR;
    }

H
heyingjiao 已提交
464
    sptr<IPCObjectProxy> ipcProxy = current->QueryCallbackProxy(this);
M
mamingshuai 已提交
465
    if (ipcProxy == nullptr) {
H
heyingjiao 已提交
466
        ZLOGE(LABEL, "%{public}s: ipc proxy is null", __func__);
M
mamingshuai 已提交
467 468 469 470 471 472 473
        return IPC_STUB_INVALID_DATA_ERR;
    }

    ipcProxy->SendObituary();
    return ERR_NONE;
}

Z
zgit2021 已提交
474
int32_t IPCObjectStub::AddAuthInfo(MessageParcel &data, MessageParcel &reply, uint32_t code)
M
mamingshuai 已提交
475 476 477 478
{
    uint32_t remotePid = data.ReadUint32();
    uint32_t remoteUid = data.ReadUint32();
    std::string remoteDeviceId = data.ReadString();
H
heyingjiao 已提交
479 480
    uint64_t stubIndex = data.ReadUint64();
    uint32_t tokenId = data.ReadUint32();
M
mamingshuai 已提交
481
    if (IsDeviceIdIllegal(remoteDeviceId)) {
H
heyingjiao 已提交
482
        ZLOGE(LABEL, "%{public}s: remote deviceId is null", __func__);
M
mamingshuai 已提交
483 484 485 486 487
        return IPC_STUB_INVALID_DATA_ERR;
    }

    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
H
heyingjiao 已提交
488
        ZLOGE(LABEL, "%{public}s: current is null", __func__);
M
mamingshuai 已提交
489 490 491
        return IPC_STUB_CURRENT_NULL_ERR;
    }

H
heyingjiao 已提交
492 493 494
    if (stubIndex == 0) {
        // keep compatible with proxy that doesn't write stubIndex when adding auth info to stub
        stubIndex = current->QueryStubIndex(this);
Z
zgit2021 已提交
495
        if (stubIndex == 0) {
H
heyingjiao 已提交
496
            ZLOGE(LABEL, "stub index is null");
Z
zgit2021 已提交
497 498
            return BINDER_CALLBACK_STUBINDEX_ERR;
        }
H
heyingjiao 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511
    }

    ZLOGW(LABEL, "add auth info pid: %{public}u, uid: %{public}u, devId: %{public}s, stubIndex: %{public}" PRIu64
        ", tokenId: %{public}u", remotePid, remoteUid,
        IPCProcessSkeleton::ConvertToSecureString(remoteDeviceId).c_str(), stubIndex, tokenId);
    // mark listen fd as 0
    if (!current->AttachAppInfoToStubIndex(remotePid, remoteUid, tokenId, remoteDeviceId, stubIndex, 0)) {
        ZLOGW(LABEL, "app info already attached, replace with 0");
    }
    if (current->AttachCommAuthInfo(this, remotePid, remoteUid, tokenId, remoteDeviceId)) {
        IncStrongRef(this);
    } else {
        ZLOGW(LABEL, "comm auth info attached already");
Z
zgit2021 已提交
512
    }
M
mamingshuai 已提交
513 514 515
    return ERR_NONE;
}

H
heyingjiao 已提交
516
std::string IPCObjectStub::GetSessionName()
M
mamingshuai 已提交
517
{
Z
zgit2021 已提交
518 519 520 521 522 523
    IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
    if (current == nullptr) {
        ZLOGE(LABEL, "get current is null");
        return std::string("");
    }
    sptr<IRemoteObject> object = current->GetSAMgrObject();
M
mamingshuai 已提交
524 525 526 527 528 529
    if (object == nullptr) {
        ZLOGE(LABEL, "get object is null");
        return std::string("");
    }

    IPCObjectProxy *samgr = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
H
heyingjiao 已提交
530
    return samgr->GetGrantedSessionName();
M
mamingshuai 已提交
531 532
}

H
heyingjiao 已提交
533 534
int32_t IPCObjectStub::GetGrantedSessionName(uint32_t code, MessageParcel &data, MessageParcel &reply,
    MessageOption &option)
M
mamingshuai 已提交
535 536 537
{
    int pid = IPCSkeleton::GetCallingPid();
    int uid = IPCSkeleton::GetCallingUid();
H
heyingjiao 已提交
538
    std::string sessionName = CreateSessionName(uid, pid);
M
mamingshuai 已提交
539
    if (sessionName.empty()) {
H
heyingjiao 已提交
540
        ZLOGE(LABEL, "pid/uid is invalid, pid = %{public}d, uid = %{public}d", pid, uid);
M
mamingshuai 已提交
541 542 543 544 545 546 547 548 549 550
        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;
}

H
heyingjiao 已提交
551 552
int32_t IPCObjectStub::GetSessionNameForPidUid(uint32_t code, MessageParcel &data, MessageParcel &reply,
    MessageOption &option)
Z
zgit2021 已提交
553 554 555 556
{
    uint32_t remotePid = data.ReadUint32();
    uint32_t remoteUid = data.ReadUint32();
    if (remotePid == static_cast<uint32_t>(IPCSkeleton::GetCallingPid())) {
H
heyingjiao 已提交
557
        ZLOGE(LABEL, "pid/uid is invalid, pid = %{public}d, uid = %{public}d", remotePid, remoteUid);
Z
zgit2021 已提交
558 559
        return IPC_STUB_INVALID_DATA_ERR;
    }
H
heyingjiao 已提交
560
    std::string sessionName = CreateSessionName(remoteUid, remotePid);
Z
zgit2021 已提交
561
    if (sessionName.empty()) {
H
heyingjiao 已提交
562
        ZLOGE(LABEL, "pid/uid is invalid, pid = %{public}d, uid = %{public}d", remotePid, remoteUid);
Z
zgit2021 已提交
563 564 565 566 567 568 569 570 571 572
        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;
}

H
heyingjiao 已提交
573 574 575 576 577 578 579 580 581 582
int IPCObjectStub::GetPidUid(MessageParcel &data, MessageParcel &reply)
{
    if (!reply.WriteUint32(getpid()) || !reply.WriteUint32(getuid())) {
        ZLOGE(LABEL, "write to parcel fail");
        return IPC_STUB_INVALID_DATA_ERR;
    }
    return ERR_NONE;
}

std::string IPCObjectStub::CreateSessionName(int uid, int pid)
M
mamingshuai 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
{
    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);
    if (softbusManager->GrantPermission(uid, pid, sessionName) != ERR_NONE) {
        ZLOGE(LABEL, "fail to Grant Permission softbus name");
        return "";
    }

    return sessionName;
}
Y
yangguangzhao 已提交
598

Y
YOUR_NAME 已提交
599
bool IPCObjectStub::IsSamgrCall()
Y
yangguangzhao 已提交
600
{
Y
YOUR_NAME 已提交
601
    return ProcessSkeleton::GetInstance()->GetSamgrFlag();
L
lutao 已提交
602
}
M
mamingshuai 已提交
603 604
#endif
} // namespace OHOS