From 46a989a4b3f26072f4f239eaa4c8902f8b1ca18c Mon Sep 17 00:00:00 2001 From: ligongshao Date: Mon, 10 Feb 2025 09:37:25 +0800 Subject: [PATCH] 5.0.3 merge Signed-off-by: ligongshao --- .../src/sandbox_manager_proxy.cpp | 141 +++++++++---- .../cpp/src/service/sandbox_manager_stub.cpp | 188 ++++++++++++++---- .../unittest/sandbox_manager_service_test.cpp | 124 ++++++++---- 3 files changed, 334 insertions(+), 119 deletions(-) diff --git a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_proxy.cpp b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_proxy.cpp index f2d985f..1b6dfbb 100644 --- a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_proxy.cpp +++ b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_proxy.cpp @@ -16,6 +16,7 @@ #include "sandbox_manager_proxy.h" #include +#include #include #include "iremote_object.h" #include "iremote_proxy.h" @@ -44,6 +45,18 @@ SandboxManagerProxy::SandboxManagerProxy(const sptr &impl) SandboxManagerProxy::~SandboxManagerProxy() {} +static void MarshalPolicy(std::stringstream &ss, const std::vector &policy) +{ + uint32_t policyNum = policy.size(); + ss.write(reinterpret_cast(&policyNum), sizeof(policyNum)); + for (int i = 0; i < policyNum; i++) { + uint32_t pathLen = policy[i].path.length(); + ss.write(reinterpret_cast(&pathLen), sizeof(pathLen)); + ss.write(policy[i].path.c_str(), pathLen); + ss.write(reinterpret_cast(&policy[i].mode), sizeof(policy[i].mode)); + } +} + int32_t SandboxManagerProxy::SendRequest(SandboxManagerInterfaceCode code, MessageParcel &data, MessageParcel &reply) { MessageOption option(MessageOption::TF_SYNC); @@ -91,10 +104,16 @@ int32_t SandboxManagerProxy::PersistPolicy(const std::vector &policy return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -126,10 +145,16 @@ int32_t SandboxManagerProxy::UnPersistPolicy(const std::vector &poli return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -166,10 +191,16 @@ int32_t SandboxManagerProxy::PersistPolicyByTokenId( return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -205,10 +236,16 @@ int32_t SandboxManagerProxy::UnPersistPolicyByTokenId( SANDBOXMANAGER_LOG_ERROR(LABEL, "Write tokenId fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -248,11 +285,17 @@ static bool WriteSetPolicyParcel(MessageParcel &data, uint32_t tokenId, const st return false; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel failed."); - return false; + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } if (!data.WriteUint64(policyFlag)) { @@ -371,10 +414,16 @@ int32_t SandboxManagerProxy::CheckPolicy(uint32_t tokenId, const std::vector(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -424,10 +473,16 @@ int32_t SandboxManagerProxy::StartAccessingPolicy(const std::vector return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -459,10 +514,16 @@ int32_t SandboxManagerProxy::StopAccessingPolicy(const std::vector & return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -499,10 +560,16 @@ int32_t SandboxManagerProxy::CheckPersistPolicy(uint32_t tokenId, const std::vec return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - if (!data.WriteParcelable(&policyInfoVectorParcel)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policyInfoVectorParcel fail"); + std::stringstream ss; + MarshalPolicy(ss, policy); + + if (!data.WriteUint32(ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write policy len failed."); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + if (!data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length())) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Write raw data failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } diff --git a/services/sandbox_manager/main/cpp/src/service/sandbox_manager_stub.cpp b/services/sandbox_manager/main/cpp/src/service/sandbox_manager_stub.cpp index 7b4b94f..cba1ab7 100644 --- a/services/sandbox_manager/main/cpp/src/service/sandbox_manager_stub.cpp +++ b/services/sandbox_manager/main/cpp/src/service/sandbox_manager_stub.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,32 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static bool CheckPermission(const uint32_t tokenId, const std::string &permission); +static int UnMarshalPolicy(std::stringstream &ss, std::vector &policyVec) +{ + uint32_t policyNum = 0; + char buf[POLICY_PATH_LIMIT + 1] = {0}; + ss.read(reinterpret_cast(&policyNum), sizeof(policyNum)); + if (policyNum > POLICY_VECTOR_SIZE_LIMIT) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "policy num:%{public}u is invalid", policyNum); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + for (int i = 0; i < policyNum; i++) { + uint32_t pathLen = 0; + ss.read(reinterpret_cast(&pathLen), sizeof(pathLen)); + if (pathLen > POLICY_PATH_LIMIT) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "path eln:%{public}u is invalid", pathLen); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + ss.read(buf, pathLen); + PolicyInfo info; + info.path.assign(buf, pathLen); + ss.read(reinterpret_cast(&info.mode), sizeof(info.mode)); + policyVec.push_back(info); + } + return SANDBOX_MANAGER_OK; +} + int32_t SandboxManagerStub::OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { @@ -99,14 +126,24 @@ int32_t SandboxManagerStub::PersistPolicyInner(MessageParcel &data, MessageParce return PERMISSION_DENIED; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->PersistPolicy(policyInfoVectorParcel->policyVector, result); + int32_t ret = this->PersistPolicy(policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -129,15 +166,24 @@ int32_t SandboxManagerStub::UnPersistPolicyInner(MessageParcel &data, MessagePar return PERMISSION_DENIED; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "read sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->UnPersistPolicy(policyInfoVectorParcel->policyVector, result); + int32_t ret = this->UnPersistPolicy(policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -164,14 +210,23 @@ int32_t SandboxManagerStub::PersistPolicyByTokenIdInner(MessageParcel &data, Mes SANDBOXMANAGER_LOG_ERROR(LABEL, "read tokenId parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "read sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->PersistPolicyByTokenId(tokenId, policyInfoVectorParcel->policyVector, result); + int32_t ret = this->PersistPolicyByTokenId(tokenId, policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -198,15 +253,24 @@ int32_t SandboxManagerStub::UnPersistPolicyByTokenIdInner(MessageParcel &data, M SANDBOXMANAGER_LOG_ERROR(LABEL, "reply tokenId parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->UnPersistPolicyByTokenId(tokenId, policyInfoVectorParcel->policyVector, result); + int32_t ret = this->UnPersistPolicyByTokenId(tokenId, policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -223,7 +287,7 @@ int32_t SandboxManagerStub::UnPersistPolicyByTokenIdInner(MessageParcel &data, M } static int32_t ReadSetPolicyParcel(MessageParcel &data, uint32_t &tokenId, - sptr &policyInfoVectorParcel, uint64_t &policyFlag, uint64_t ×tamp) + std::vector &policyVec, uint64_t &policyFlag, uint64_t ×tamp) { uint64_t callingTokenId = IPCSkeleton::GetCallingTokenID(); if (!CheckPermission(callingTokenId, SET_POLICY_PERMISSION_NAME)) { @@ -240,9 +304,17 @@ static int32_t ReadSetPolicyParcel(MessageParcel &data, uint32_t &tokenId, return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Read policyInfoVectorParcel failed."); + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } @@ -260,13 +332,14 @@ int32_t SandboxManagerStub::SetPolicyInner(MessageParcel &data, MessageParcel &r sptr policyInfoVectorParcel = nullptr; uint64_t policyFlag; uint64_t timestamp; - int32_t readRes = ReadSetPolicyParcel(data, tokenId, policyInfoVectorParcel, policyFlag, timestamp); + std::vector policyVec; + int32_t readRes = ReadSetPolicyParcel(data, tokenId, policyVec, policyFlag, timestamp); if (readRes != SANDBOX_MANAGER_OK) { return readRes; } std::vector result; - int32_t ret = this->SetPolicy(tokenId, policyInfoVectorParcel->policyVector, policyFlag, result, timestamp); + int32_t ret = this->SetPolicy(tokenId, policyVec, policyFlag, result, timestamp); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "Write ret failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -287,7 +360,8 @@ int32_t SandboxManagerStub::SetPolicyAsyncInner(MessageParcel &data, MessageParc sptr policyInfoVectorParcel = nullptr; uint64_t policyFlag; uint64_t timestamp; - int32_t readRes = ReadSetPolicyParcel(data, tokenId, policyInfoVectorParcel, policyFlag, timestamp); + std::vector policyVec; + int32_t readRes = ReadSetPolicyParcel(data, tokenId, policyVec, policyFlag, timestamp); if (readRes != SANDBOX_MANAGER_OK) { return readRes; } @@ -354,13 +428,22 @@ int32_t SandboxManagerStub::CheckPolicyInner(MessageParcel &data, MessageParcel return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Read policyInfoVectorParcel failed."); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->CheckPolicy(tokenId, policyInfoVectorParcel->policyVector, result); + int32_t ret = this->CheckPolicy(tokenId, policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "Write ret failed."); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -400,15 +483,24 @@ int32_t SandboxManagerStub::StartAccessingPolicyInner(MessageParcel &data, Messa return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; int32_t ret = - this->StartAccessingPolicy(policyInfoVectorParcel->policyVector, result, useCallerToken, tokenId, timestamp); + this->StartAccessingPolicy(policyVec, result, useCallerToken, tokenId, timestamp); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -431,15 +523,24 @@ int32_t SandboxManagerStub::StopAccessingPolicyInner(MessageParcel &data, Messag return PERMISSION_DENIED; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->StopAccessingPolicy(policyInfoVectorParcel->policyVector, result); + int32_t ret = this->StopAccessingPolicy(policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; @@ -463,14 +564,23 @@ int32_t SandboxManagerStub::CheckPersistPolicyInner(MessageParcel &data, Message return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } - sptr policyInfoVectorParcel = data.ReadParcelable(); - if (policyInfoVectorParcel == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager data parcel fail"); + std::vector policyVec; + auto vecDataSize = data.ReadUint32(); + const void *buffer = data.ReadRawData(vecDataSize); + if (buffer == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Read raw data failed"); + return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; + } + + std::stringstream ss; + ss.write(reinterpret_cast(buffer), vecDataSize); + if (UnMarshalPolicy(ss, policyVec) != SANDBOX_MANAGER_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Unmarshal failed"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; } std::vector result; - int32_t ret = this->CheckPersistPolicy(tokenId, policyInfoVectorParcel->policyVector, result); + int32_t ret = this->CheckPersistPolicy(tokenId, policyVec, result); if (!reply.WriteInt32(ret)) { SANDBOXMANAGER_LOG_ERROR(LABEL, "reply sandbox manager ret parcel fail"); return SANDBOX_MANAGER_SERVICE_PARCEL_ERR; diff --git a/services/sandbox_manager/test/unittest/sandbox_manager_service_test.cpp b/services/sandbox_manager/test/unittest/sandbox_manager_service_test.cpp index 258a1ed..267fb53 100644 --- a/services/sandbox_manager/test/unittest/sandbox_manager_service_test.cpp +++ b/services/sandbox_manager/test/unittest/sandbox_manager_service_test.cpp @@ -74,6 +74,18 @@ Security::AccessToken::HapPolicyParams g_testPolicyPrams = { }; }; +static void MarshalPolicy(std::stringstream &ss, const std::vector &policy) +{ + uint32_t policyNum = policy.size(); + ss.write(reinterpret_cast(&policyNum), sizeof(policyNum)); + for (int i = 0; i < policyNum; i++) { + uint32_t pathLen = policy[i].path.length(); + ss.write(reinterpret_cast(&pathLen), sizeof(pathLen)); + ss.write(policy[i].path.c_str(), pathLen); + ss.write(reinterpret_cast(&policy[i].mode), sizeof(policy[i].mode)); + } +} + class SandboxManagerServiceTest : public testing::Test { public: static void SetUpTestCase(void); @@ -476,15 +488,16 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub003, TestSize.Level1) data.WriteUint32(1); data.WriteUint64(1); data.WriteUint32(1); - data.WriteParcelable(policyInfoVectorParcel); - EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->StartAccessingPolicyInner(data, reply)); + uint32_t vecDataSize = 100; // invalid vecDataSize + data.WriteUint32(vecDataSize); + EXPECT_EQ(SANDBOX_MANAGER_SERVICE_PARCEL_ERR, sandboxManagerService_->StartAccessingPolicyInner(data, reply)); EXPECT_EQ(SANDBOX_MANAGER_SERVICE_PARCEL_ERR, sandboxManagerService_->StopAccessingPolicyInner(data, reply)); data.WriteUint32(1); EXPECT_EQ(SANDBOX_MANAGER_SERVICE_PARCEL_ERR, sandboxManagerService_->StopAccessingPolicyInner(data, reply)); data.WriteUint32(1); - data.WriteParcelable(policyInfoVectorParcel); - EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->StopAccessingPolicyInner(data, reply)); + data.WriteUint32(vecDataSize); + EXPECT_EQ(SANDBOX_MANAGER_SERVICE_PARCEL_ERR, sandboxManagerService_->StopAccessingPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -506,9 +519,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub004, TestSize.Level1) .path = "", .mode = 0b01, }); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->PersistPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -533,9 +547,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub005, TestSize.Level1) .path = "", .mode = 0b01, }); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->UnPersistPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -562,9 +577,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub006, TestSize.Level1) .path = "", .mode = 0b01, }); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->PersistPolicyByTokenIdInner(data, reply)); data.WriteUint32(1); @@ -573,8 +589,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub006, TestSize.Level1) .path = "test path", .mode = 0b01, }); - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->PersistPolicyByTokenIdInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -601,9 +619,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub007, TestSize.Level1) .path = "", .mode = 0b01, }); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->UnPersistPolicyByTokenIdInner(data, reply)); data.WriteUint32(1); @@ -612,8 +631,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub007, TestSize.Level1) .path = "test path", .mode = 0b01, }); - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->PersistPolicyByTokenIdInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -641,9 +662,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub008, TestSize.Level1) .mode = 0b01, }); data.WriteUint64(1); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); data.WriteUint32(0); EXPECT_EQ(SANDBOX_MANAGER_SERVICE_PARCEL_ERR, sandboxManagerService_->SetPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); @@ -670,9 +692,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub009, TestSize.Level1) data.WriteBool(true); data.WriteUint32(1); data.WriteUint64(1); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->StartAccessingPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); } @@ -726,14 +749,18 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub011, TestSize.Level1) .path = "test", .mode = 0b01, }); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->UnPersistPolicyByTokenIdInner(data, reply3)); MessageParcel reply4; data.WriteUint32(1); - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->CheckPersistPolicyInner(data, reply4)); SetSelfTokenID(selfTokenId_); @@ -749,23 +776,32 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub012, TestSize.Level1) { SetSelfTokenID(sysGrantToken_); std::vector policy; - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policy; MessageParcel data, reply; - - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->PersistPolicyInner(data, reply)); - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->UnPersistPolicyInner(data, reply)); data.WriteBool(true); data.WriteUint32(1); data.WriteUint64(1); - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->StartAccessingPolicyInner(data, reply)); - data.WriteParcelable(&policyInfoVectorParcel); + ss.str(""); + MarshalPolicy(ss, policy); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); EXPECT_EQ(INVALID_PARAMTER, sandboxManagerService_->StopAccessingPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); @@ -790,9 +826,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub013, TestSize.Level1) MessageParcel reply; data.WriteUint32(GetSelfTokenID()); data.WriteUint64(2); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policies; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policies); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); data.WriteUint64(0); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->SetPolicyInner(data, reply)); SetSelfTokenID(selfTokenId_); @@ -841,9 +878,10 @@ HWTEST_F(SandboxManagerServiceTest, SandboxManagerStub015, TestSize.Level1) MessageParcel reply; data.WriteUint32(GetSelfTokenID()); data.WriteUint64(2); - PolicyInfoVectorParcel policyInfoVectorParcel; - policyInfoVectorParcel.policyVector = policies; - data.WriteParcelable(&policyInfoVectorParcel); + std::stringstream ss; + MarshalPolicy(ss, policies); + data.WriteUint32(ss.str().length()); + data.WriteRawData(reinterpret_cast(ss.str().data()), ss.str().length()); data.WriteUint64(0); EXPECT_EQ(SANDBOX_MANAGER_OK, sandboxManagerService_->SetPolicyAsyncInner(data, reply)); SetSelfTokenID(selfTokenId_); -- Gitee