diff --git a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp index 9e04980f697856fd90e7f7f2558f50429a22bc2c..5b47f5303b5d105d9103a8bf0d58bc76b24125be 100644 --- a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp +++ b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp @@ -304,6 +304,9 @@ int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service) OHOS::HdfXCollie hdfXCollie("HdfRemoteAdapterAddSa_" + OHOS::ToString(saId) + "_add_sa", OHOS::HdfXCollie::DEFAULT_TIMEOUT_SECONDS, nullptr, nullptr, OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + OHOS::sptr remote = holder->remote_; + OHOS::IPCObjectStub *stub = reinterpret_cast(remote.GetRefPtr()); + stub->SetRequestSidFlag(true); int ret = saManager->AddSystemAbility(saId, holder->remote_); (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); HDF_LOGI("add sa %{public}d, ret = %{public}s", saId, (ret == 0) ? "succ" : "fail"); @@ -430,6 +433,11 @@ pid_t HdfRemoteGetCallingUid(void) return OHOS::IPCSkeleton::GetCallingUid(); } +char *HdfRemoteGetCallingSid(void) +{ + return strdup(OHOS::IPCSkeleton::GetCallingSid().c_str()); +} + int HdfRemoteAdapterDefaultDispatch(struct HdfRemoteService *service, int code, struct HdfSBuf *data, struct HdfSBuf *reply) { @@ -465,4 +473,4 @@ int HdfRemoteAdapterDefaultDispatch(struct HdfRemoteService *service, } return stub->IPCObjectStub::OnRemoteRequest(code, *dataParcel, *replyParcel, option); -} \ No newline at end of file +} diff --git a/adapter/uhdf2/manager/src/devsvc_manager_stub.c b/adapter/uhdf2/manager/src/devsvc_manager_stub.c index a85a3bbf148375be06f17d1ac9591dc2b66dd2e7..42c11b4fa9ffe0e2dd8483c5879b32e374506450 100644 --- a/adapter/uhdf2/manager/src/devsvc_manager_stub.c +++ b/adapter/uhdf2/manager/src/devsvc_manager_stub.c @@ -36,10 +36,18 @@ static int32_t AddServicePermCheck(const char *servName) { #ifdef WITH_SELINUX pid_t callingPid = HdfRemoteGetCallingPid(); - if (HdfAddServiceCheck(callingPid, servName) != 0) { - HDF_LOGE("[selinux] %{public}d haven't \"add service\" permission to %{public}s", callingPid, servName); + char *callingSid = HdfRemoteGetCallingSid(); + if (callingSid == NULL) { + HDF_LOGE("%{public}s: sid of %{public}d is null", __func__, callingPid); return HDF_ERR_NOPERM; } + if (HdfAddServiceCheck(callingSid, servName) != 0) { + HDF_LOGE("[selinux] %{public}s %{public}d haven't \"add service\" permission to %{public}s", + callingSid, callingPid, servName); + free(callingSid); + return HDF_ERR_NOPERM; + } + free(callingSid); #endif return HDF_SUCCESS; } @@ -48,10 +56,18 @@ static int32_t GetServicePermCheck(const char *servName) { #ifdef WITH_SELINUX pid_t callingPid = HdfRemoteGetCallingPid(); - if (HdfGetServiceCheck(callingPid, servName) != 0) { - HDF_LOGE("[selinux] %{public}d haven't \"get service\" permission to %{public}s", callingPid, servName); + char *callingSid = HdfRemoteGetCallingSid(); + if (callingSid == NULL) { + HDF_LOGE("%{public}s: sid of %{public}d is null", __func__, callingPid); + return HDF_ERR_NOPERM; + } + if (HdfGetServiceCheck(callingSid, servName) != 0) { + HDF_LOGE("[selinux] %{public}s %{public}d haven't \"get service\" permission to %{public}s", + callingSid, callingPid, servName); + free(callingSid); return HDF_ERR_NOPERM; } + free(callingSid); #endif return HDF_SUCCESS; @@ -61,10 +77,17 @@ static int32_t ListServicePermCheck(void) { #ifdef WITH_SELINUX pid_t callingPid = HdfRemoteGetCallingPid(); - if (HdfListServiceCheck(callingPid) != 0) { - HDF_LOGE("[selinux] %{public}d haven't \"list service\" permission", callingPid); + char *callingSid = HdfRemoteGetCallingSid(); + if (callingSid == NULL) { + HDF_LOGE("%{public}s: sid of %{public}d is null", __func__, callingPid); + return HDF_ERR_NOPERM; + } + if (HdfListServiceCheck(callingSid) != 0) { + HDF_LOGE("[selinux] %{public}s %{public}d haven't \"list service\" permission", callingSid, callingPid); + free(callingSid); return HDF_ERR_NOPERM; } + free(callingSid); #endif return HDF_SUCCESS; diff --git a/interfaces/inner_api/ipc/hdf_remote_service.h b/interfaces/inner_api/ipc/hdf_remote_service.h index 9c57b30c7e80828aecab971b0c6d38300dcbeb72..85e3f26d525ad9f5ad13fdd5979ec7b478d1bfe0 100644 --- a/interfaces/inner_api/ipc/hdf_remote_service.h +++ b/interfaces/inner_api/ipc/hdf_remote_service.h @@ -169,6 +169,13 @@ pid_t HdfRemoteGetCallingPid(void); */ pid_t HdfRemoteGetCallingUid(void); +/** + * @brief Get the caller's SID. + * + * @return The caller's SID. + */ +char* HdfRemoteGetCallingSid(void); + /** * @brief Default command distribution for invoking ipc. *