Skip to content

Commit

Permalink
reject PTU system request when PTU not in progress (#3853)
Browse files Browse the repository at this point in the history
* reject PTU system request when PTU not in progress

* track last_ptu_app_id_ in ext prop policy mode

* fix style

* Apply suggestions from code review

Co-authored-by: Shobhit Adlakha <ShobhitAd@users.noreply.github.com>

* fix style

Co-authored-by: Shobhit Adlakha <ShobhitAd@users.noreply.github.com>
  • Loading branch information
iCollin and ShobhitAd authored Feb 11, 2022
1 parent e28694c commit 7a4a4e0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ class PolicyHandler : public PolicyHandlerInterface,
void CacheRetryInfo(const uint32_t app_id = 0,
const std::string url = std::string(),
const std::string snapshot_path = std::string()) OVERRIDE;
#else // EXTERNAL_PROPRIETARY_MODE
void UpdateLastPTUApp(const uint32_t app_id) OVERRIDE;
#endif // EXTERNAL_PROPRIETARY_MODE

uint32_t GetAppIdForSending() const OVERRIDE;
Expand Down Expand Up @@ -721,6 +723,8 @@ class PolicyHandler : public PolicyHandlerInterface,

void StopRetrySequence() OVERRIDE;

bool IsPTUSystemRequestAllowed(const uint32_t app_id) OVERRIDE;

/**
* @brief OnDeviceSwitching Notifies policy manager on device switch event so
* policy permissions should be processed accordingly
Expand Down Expand Up @@ -932,10 +936,10 @@ class PolicyHandler : public PolicyHandlerInterface,
std::shared_ptr<PolicyManager> atomic_policy_manager_;
std::shared_ptr<PolicyEventObserver> event_observer_;
uint32_t last_activated_app_id_;
uint32_t last_ptu_app_id_;

#ifndef EXTERNAL_PROPRIETARY_MODE
// PTU retry information
uint32_t last_ptu_app_id_;
std::string retry_update_url_;
std::string policy_snapshot_path_;
#endif // EXTERNAL_PROPRIETARY_MODE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ void OnSystemRequestNotification::Run() {
if (helpers::Compare<RequestType, helpers::EQ, helpers::ONE>(
request_type, RequestType::RT_PROPRIETARY, RequestType::RT_HTTP)) {
policy_handler_.OnSystemRequestReceived();
#ifdef EXTERNAL_PROPRIETARY_MODE
policy_handler_.UpdateLastPTUApp(app->app_id());
#endif
}
SendNotificationToMobile(message_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ void SystemRequest::Run() {

SDL_LOG_DEBUG("Binary data ok.");

if (mobile_apis::RequestType::PROPRIETARY == request_type ||
mobile_apis::RequestType::HTTP == request_type) {
auto app_id = application->app_id();
if (!policy_handler_.IsPTUSystemRequestAllowed(app_id)) {
SDL_LOG_DEBUG("Rejected PTU SystemRequest from app " << app_id);
SendResponse(false, mobile_apis::Result::REJECTED);
return;
}
}

if (mobile_apis::RequestType::ICON_URL == request_type) {
application_manager_.SetIconFileFromSystemRequest(file_name);
SendResponse(true, mobile_apis::Result::SUCCESS);
Expand Down
34 changes: 26 additions & 8 deletions src/components/application_manager/src/policies/policy_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,11 @@ PolicyHandler::PolicyHandler(const PolicySettings& settings,
ApplicationManager& application_manager)
: AsyncRunner("PolicyHandler async runner thread")
, last_activated_app_id_(0)
#ifndef EXTERNAL_PROPRIETARY_MODE
, last_ptu_app_id_(0)
#endif // EXTERNAL_PROPRIETARY_MODE
, statistic_manager_impl_(std::make_shared<StatisticManagerImpl>(this))
, settings_(settings)
, application_manager_(application_manager)
, last_registered_policy_app_id_(std::string()) {
}
, last_registered_policy_app_id_(std::string()) {}

PolicyHandler::~PolicyHandler() {}

Expand Down Expand Up @@ -423,13 +420,31 @@ void PolicyHandler::StopRetrySequence() {
SDL_LOG_AUTO_TRACE();
const auto policy_manager = LoadPolicyManager();
POLICY_LIB_CHECK_VOID(policy_manager);
#ifndef EXTERNAL_PROPRIETARY_MODE
// Clear cached PTU app
last_ptu_app_id_ = 0;
#endif // EXTERNAL_PROPRIETARY_MODE
policy_manager->StopRetrySequence();
}

bool PolicyHandler::IsPTUSystemRequestAllowed(const uint32_t app_id) {
SDL_LOG_AUTO_TRACE();
const auto policy_manager = LoadPolicyManager();
POLICY_LIB_CHECK_OR_RETURN(policy_manager, false);

if (policy_manager->GetPolicyTableStatus() != "UPDATING") {
SDL_LOG_DEBUG("PTU received while not UPDATING");
return false;
}

if (app_id != last_ptu_app_id_) {
SDL_LOG_DEBUG(
"PTU received from unexpected application, request was sent to "
<< last_ptu_app_id_);
return false;
}

return true;
}

bool PolicyHandler::ResetPolicyTable() {
SDL_LOG_TRACE("Reset policy table.");
const auto policy_manager = LoadPolicyManager();
Expand Down Expand Up @@ -476,6 +491,11 @@ void PolicyHandler::CacheRetryInfo(const uint32_t app_id,
retry_update_url_ = url;
policy_snapshot_path_ = snapshot_path;
}
#else // EXTERNAL_PROPRIETARY_MODE
void PolicyHandler::UpdateLastPTUApp(const uint32_t app_id) {
SDL_LOG_DEBUG("UpdateLastPTUApp to " << app_id);
last_ptu_app_id_ = app_id;
}
#endif // EXTERNAL_PROPRIETARY_MODE

uint32_t PolicyHandler::GetAppIdForSending() const {
Expand Down Expand Up @@ -1286,10 +1306,8 @@ bool PolicyHandler::ReceiveMessageFromSDK(const std::string& file,
policy_manager->CleanupUnpairedDevices();
SetDaysAfterEpoch();
policy_manager->OnPTUFinished(load_pt_result);
#ifndef EXTERNAL_PROPRIETARY_MODE
// Clean up retry information
last_ptu_app_id_ = 0;
#endif // EXTERNAL_PROPRIETARY_MODE

uint32_t correlation_id = application_manager_.GetNextHMICorrelationID();
event_observer_->subscribe_on_event(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ class PolicyHandlerInterface : public VehicleDataItemProvider {
const uint32_t app_id = 0,
const std::string url = std::string(),
const std::string snapshot_path = std::string()) = 0;
#else
virtual void UpdateLastPTUApp(const uint32_t app_id) = 0;
#endif // EXTERNAL_PROPRIETARY_MODE

/**
Expand All @@ -426,6 +428,8 @@ class PolicyHandlerInterface : public VehicleDataItemProvider {

virtual void OnPTInited() = 0;

virtual bool IsPTUSystemRequestAllowed(const uint32_t app_id) = 0;

/**
* @brief Force stops retry sequence timer and resets retry sequence
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class MockPolicyHandlerInterface : public policy::PolicyHandlerInterface {
void(const uint32_t app_id,
const std::string url,
const std::string snapshot_path));
#else
MOCK_METHOD1(UpdateLastPTUApp, void(const uint32_t app_id));
#endif
MOCK_CONST_METHOD0(GetAppIdForSending, uint32_t());
MOCK_METHOD1(
Expand All @@ -210,6 +212,7 @@ class MockPolicyHandlerInterface : public policy::PolicyHandlerInterface {
MOCK_METHOD1(OnCertificateUpdated, void(const std::string& certificate_data));
MOCK_METHOD1(OnPTUFinished, void(const bool ptu_result));
MOCK_METHOD0(OnPTInited, void());
MOCK_METHOD1(IsPTUSystemRequestAllowed, bool(const uint32_t app_id));
MOCK_METHOD0(StopRetrySequence, void());
MOCK_METHOD1(OnCertificateDecrypted, void(bool is_succeeded));
MOCK_METHOD0(CanUpdate, bool());
Expand Down

0 comments on commit 7a4a4e0

Please sign in to comment.