check samgr by access token

Signed-off-by: yangguangzhao <yangguangzhao1@huawei.com>
This commit is contained in:
yangguangzhao 2022-05-26 14:40:22 +08:00
parent 2fbaa6e871
commit c4ffc39674
5 changed files with 26 additions and 29 deletions

View File

@ -365,7 +365,7 @@ static bool CheckDBinder(const char *sessionName)
return false;
}
if (strcmp(DBINDER_SERVICE_NAME, sessionName) == 0) {
return false;
return true;
}
if (StrStartWith(sessionName, DBINDER_BUS_NAME_PREFIX)) {
return true;

View File

@ -1,16 +1,4 @@
[
{
"SESSION_NAME": "DBinderService",
"DEVID": "NETWORKID",
"APP_INFO": [
{
"TYPE": "native_app",
"UID": "1000",
"PKG_NAME": "DBinderService",
"ACTIONS": "create,open"
}
]
},
{
"SESSION_NAME": "DistributedFileService.*",
"REGEXP": "true",

View File

@ -18,6 +18,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "accesstoken_kit.h"
#include "ipc_skeleton.h"
#include "permission_entry.h"
#include "softbus_adapter_mem.h"
@ -32,11 +33,13 @@
#endif
namespace {
using namespace OHOS::Security;
const std::string PERMISSION_JSON_FILE = PERMISSION_JSON_FILE_PATH;
const std::string DANGER_APP_PERMISSION = "ohos.permission.DISTRIBUTED_DATASYNC";
const int32_t SYSTEM_UID = 1000;
const int32_t MULTE_USER_RADIX = 100000;
const char *dbinderSessionName = "DBinderService";
const std::string SAMGR_PROCESS_NAME = "samgr";
}
int32_t TransPermissionInit(void)
@ -128,20 +131,19 @@ int32_t RemoveTransPermission(const char *sessionName)
int32_t CheckDynamicPermission(void)
{
int32_t callingUid = (int32_t)OHOS::IPCSkeleton::GetCallingUid();
int32_t callingPid = (int32_t)OHOS::IPCSkeleton::GetCallingPid();
int32_t dbinderUid = 0;
int32_t dbinderPid = 0;
uint32_t callingToken = OHOS::IPCSkeleton::GetCallingTokenID();
int32_t ret = TransGetUidAndPid(dbinderSessionName, &dbinderUid, &dbinderPid);
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "RemovePermissionInner get dbinder uid and pid failed");
return ret;
auto tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(callingToken);
if (tokenType != AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not native call");
return SOFTBUS_ERR;
}
if (callingUid != dbinderUid || callingPid != dbinderPid) {
SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "RemovePermission denied: invalid uid %d or pid %d",
callingUid, callingPid);
return SOFTBUS_PERMISSION_DENIED;
AccessToken::NativeTokenInfo nativeTokenInfo;
int32_t result = AccessToken::AccessTokenKit::GetNativeTokenInfo(callingToken, nativeTokenInfo);
if (result == SOFTBUS_OK && nativeTokenInfo.processName == SAMGR_PROCESS_NAME) {
return SOFTBUS_OK;
}
return SOFTBUS_OK;
SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR,
"check dynamic permission failed, processName:%{private}s", nativeTokenInfo.processName.c_str());
return SOFTBUS_ERR;
}

View File

@ -175,6 +175,7 @@ if (defined(ohos_lite)) {
if (is_standard_system) {
external_deps = bus_center_server_external_deps
external_deps += [
"access_token:libaccesstoken_sdk",
"deviceauth_standard:deviceauth_sdk",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",

View File

@ -57,6 +57,10 @@ static sptr<IRemoteObject> GetSystemAbility()
int32_t TransServerProxyInit(void)
{
std::lock_guard<std::mutex> lock(g_mutex);
if (g_serverProxy != nullptr) {
return SOFTBUS_OK;
}
sptr<IRemoteObject> object = GetSystemAbility();
if (object == nullptr) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Get remote softbus object failed!\n");
@ -179,8 +183,10 @@ int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType,
int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
{
if (g_serverProxy == nullptr) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!");
return SOFTBUS_ERR;
if (TransServerProxyInit() != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "grant permission g_serverProxy is nullptr!");
return SOFTBUS_ERR;
}
}
if (sessionName == nullptr) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "sessionName is nullptr");