| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/win/src/sandbox_policy_base.h" | 5 #include "sandbox/win/src/sandbox_policy_base.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| 11 #include "sandbox/win/src/app_container.h" | 11 #include "sandbox/win/src/app_container.h" |
| 12 #include "sandbox/win/src/filesystem_dispatcher.h" | 12 #include "sandbox/win/src/filesystem_dispatcher.h" |
| 13 #include "sandbox/win/src/filesystem_policy.h" | 13 #include "sandbox/win/src/filesystem_policy.h" |
| 14 #include "sandbox/win/src/handle_dispatcher.h" | 14 #include "sandbox/win/src/handle_dispatcher.h" |
| 15 #include "sandbox/win/src/handle_policy.h" | 15 #include "sandbox/win/src/handle_policy.h" |
| 16 #include "sandbox/win/src/job.h" | 16 #include "sandbox/win/src/job.h" |
| 17 #include "sandbox/win/src/interception.h" | 17 #include "sandbox/win/src/interception.h" |
| 18 #include "sandbox/win/src/process_mitigations.h" |
| 18 #include "sandbox/win/src/named_pipe_dispatcher.h" | 19 #include "sandbox/win/src/named_pipe_dispatcher.h" |
| 19 #include "sandbox/win/src/named_pipe_policy.h" | 20 #include "sandbox/win/src/named_pipe_policy.h" |
| 20 #include "sandbox/win/src/policy_broker.h" | 21 #include "sandbox/win/src/policy_broker.h" |
| 21 #include "sandbox/win/src/policy_engine_processor.h" | 22 #include "sandbox/win/src/policy_engine_processor.h" |
| 22 #include "sandbox/win/src/policy_low_level.h" | 23 #include "sandbox/win/src/policy_low_level.h" |
| 23 #include "sandbox/win/src/process_thread_dispatcher.h" | 24 #include "sandbox/win/src/process_thread_dispatcher.h" |
| 24 #include "sandbox/win/src/process_thread_policy.h" | 25 #include "sandbox/win/src/process_thread_policy.h" |
| 25 #include "sandbox/win/src/registry_dispatcher.h" | 26 #include "sandbox/win/src/registry_dispatcher.h" |
| 26 #include "sandbox/win/src/registry_policy.h" | 27 #include "sandbox/win/src/registry_policy.h" |
| 27 #include "sandbox/win/src/restricted_token_utils.h" | 28 #include "sandbox/win/src/restricted_token_utils.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 memset(mem, 0, kTotalPolicySz); | 47 memset(mem, 0, kTotalPolicySz); |
| 47 sandbox::PolicyGlobal* policy = reinterpret_cast<sandbox::PolicyGlobal*>(mem); | 48 sandbox::PolicyGlobal* policy = reinterpret_cast<sandbox::PolicyGlobal*>(mem); |
| 48 policy->data_size = kTotalPolicySz - sizeof(sandbox::PolicyGlobal); | 49 policy->data_size = kTotalPolicySz - sizeof(sandbox::PolicyGlobal); |
| 49 return policy; | 50 return policy; |
| 50 } | 51 } |
| 51 } | 52 } |
| 52 | 53 |
| 53 namespace sandbox { | 54 namespace sandbox { |
| 54 | 55 |
| 55 SANDBOX_INTERCEPT IntegrityLevel g_shared_delayed_integrity_level; | 56 SANDBOX_INTERCEPT IntegrityLevel g_shared_delayed_integrity_level; |
| 57 SANDBOX_INTERCEPT MitigationFlags g_shared_delayed_mitigations; |
| 56 | 58 |
| 57 // Initializes static members. | 59 // Initializes static members. |
| 58 HWINSTA PolicyBase::alternate_winstation_handle_ = NULL; | 60 HWINSTA PolicyBase::alternate_winstation_handle_ = NULL; |
| 59 HDESK PolicyBase::alternate_desktop_handle_ = NULL; | 61 HDESK PolicyBase::alternate_desktop_handle_ = NULL; |
| 60 | 62 |
| 61 PolicyBase::PolicyBase() | 63 PolicyBase::PolicyBase() |
| 62 : ref_count(1), | 64 : ref_count(1), |
| 63 lockdown_level_(USER_LOCKDOWN), | 65 lockdown_level_(USER_LOCKDOWN), |
| 64 initial_level_(USER_LOCKDOWN), | 66 initial_level_(USER_LOCKDOWN), |
| 65 job_level_(JOB_LOCKDOWN), | 67 job_level_(JOB_LOCKDOWN), |
| 66 ui_exceptions_(0), | 68 ui_exceptions_(0), |
| 67 use_alternate_desktop_(false), | 69 use_alternate_desktop_(false), |
| 68 use_alternate_winstation_(false), | 70 use_alternate_winstation_(false), |
| 69 file_system_init_(false), | 71 file_system_init_(false), |
| 70 relaxed_interceptions_(true), | 72 relaxed_interceptions_(true), |
| 71 integrity_level_(INTEGRITY_LEVEL_LAST), | 73 integrity_level_(INTEGRITY_LEVEL_LAST), |
| 72 delayed_integrity_level_(INTEGRITY_LEVEL_LAST), | 74 delayed_integrity_level_(INTEGRITY_LEVEL_LAST), |
| 75 mitigations_(0), |
| 76 delayed_mitigations_(0), |
| 73 policy_maker_(NULL), | 77 policy_maker_(NULL), |
| 74 policy_(NULL) { | 78 policy_(NULL) { |
| 75 ::InitializeCriticalSection(&lock_); | 79 ::InitializeCriticalSection(&lock_); |
| 76 // Initialize the IPC dispatcher array. | 80 // Initialize the IPC dispatcher array. |
| 77 memset(&ipc_targets_, NULL, sizeof(ipc_targets_)); | 81 memset(&ipc_targets_, NULL, sizeof(ipc_targets_)); |
| 78 Dispatcher* dispatcher = NULL; | 82 Dispatcher* dispatcher = NULL; |
| 79 | 83 |
| 80 dispatcher = new FilesystemDispatcher(this); | 84 dispatcher = new FilesystemDispatcher(this); |
| 81 ipc_targets_[IPC_NTCREATEFILE_TAG] = dispatcher; | 85 ipc_targets_[IPC_NTCREATEFILE_TAG] = dispatcher; |
| 82 ipc_targets_[IPC_NTOPENFILE_TAG] = dispatcher; | 86 ipc_targets_[IPC_NTOPENFILE_TAG] = dispatcher; |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 return rv; | 273 return rv; |
| 270 | 274 |
| 271 return SBOX_ALL_OK; | 275 return SBOX_ALL_OK; |
| 272 } | 276 } |
| 273 | 277 |
| 274 ResultCode PolicyBase::SetCapability(const wchar_t* sid) { | 278 ResultCode PolicyBase::SetCapability(const wchar_t* sid) { |
| 275 capabilities_.push_back(sid); | 279 capabilities_.push_back(sid); |
| 276 return SBOX_ALL_OK; | 280 return SBOX_ALL_OK; |
| 277 } | 281 } |
| 278 | 282 |
| 283 ResultCode PolicyBase::SetProcessMitigations( |
| 284 MitigationFlags flags) { |
| 285 if (!CanSetProcessMitigationsPreStartup(flags)) |
| 286 return SBOX_ERROR_BAD_PARAMS; |
| 287 mitigations_ = flags; |
| 288 return SBOX_ALL_OK; |
| 289 } |
| 290 |
| 291 MitigationFlags PolicyBase::GetProcessMitigations() { |
| 292 return mitigations_; |
| 293 } |
| 294 |
| 295 ResultCode PolicyBase::SetDelayedProcessMitigations( |
| 296 MitigationFlags flags) { |
| 297 if (!CanSetProcessMitigationsPostStartup(flags)) |
| 298 return SBOX_ERROR_BAD_PARAMS; |
| 299 delayed_mitigations_ = flags; |
| 300 return SBOX_ALL_OK; |
| 301 } |
| 302 |
| 303 MitigationFlags PolicyBase::GetDelayedProcessMitigations() { |
| 304 return delayed_mitigations_; |
| 305 } |
| 306 |
| 279 void PolicyBase::SetStrictInterceptions() { | 307 void PolicyBase::SetStrictInterceptions() { |
| 280 relaxed_interceptions_ = false; | 308 relaxed_interceptions_ = false; |
| 281 } | 309 } |
| 282 | 310 |
| 283 ResultCode PolicyBase::AddRule(SubSystem subsystem, Semantics semantics, | 311 ResultCode PolicyBase::AddRule(SubSystem subsystem, Semantics semantics, |
| 284 const wchar_t* pattern) { | 312 const wchar_t* pattern) { |
| 285 if (NULL == policy_) { | 313 if (NULL == policy_) { |
| 286 policy_ = MakeBrokerPolicyMemory(); | 314 policy_ = MakeBrokerPolicyMemory(); |
| 287 DCHECK(policy_); | 315 DCHECK(policy_); |
| 288 policy_maker_ = new LowLevelPolicy(policy_); | 316 policy_maker_ = new LowLevelPolicy(policy_); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 if (!appcontainer_list_.get() || !appcontainer_list_->HasAppContainer()) | 471 if (!appcontainer_list_.get() || !appcontainer_list_->HasAppContainer()) |
| 444 return NULL; | 472 return NULL; |
| 445 | 473 |
| 446 return appcontainer_list_.get(); | 474 return appcontainer_list_.get(); |
| 447 } | 475 } |
| 448 | 476 |
| 449 bool PolicyBase::AddTarget(TargetProcess* target) { | 477 bool PolicyBase::AddTarget(TargetProcess* target) { |
| 450 if (NULL != policy_) | 478 if (NULL != policy_) |
| 451 policy_maker_->Done(); | 479 policy_maker_->Done(); |
| 452 | 480 |
| 481 if (!ApplyProcessMitigationsToSuspendedProcess(target->Process(), |
| 482 mitigations_)) { |
| 483 return false; |
| 484 } |
| 485 |
| 453 if (!SetupAllInterceptions(target)) | 486 if (!SetupAllInterceptions(target)) |
| 454 return false; | 487 return false; |
| 455 | 488 |
| 456 if (!SetupHandleCloser(target)) | 489 if (!SetupHandleCloser(target)) |
| 457 return false; | 490 return false; |
| 458 | 491 |
| 459 // Initialize the sandbox infrastructure for the target. | 492 // Initialize the sandbox infrastructure for the target. |
| 460 if (ERROR_SUCCESS != target->Init(this, policy_, kIPCMemSize, kPolMemSize)) | 493 if (ERROR_SUCCESS != target->Init(this, policy_, kIPCMemSize, kPolMemSize)) |
| 461 return false; | 494 return false; |
| 462 | 495 |
| 463 g_shared_delayed_integrity_level = delayed_integrity_level_; | 496 g_shared_delayed_integrity_level = delayed_integrity_level_; |
| 464 ResultCode ret = target->TransferVariable( | 497 ResultCode ret = target->TransferVariable( |
| 465 "g_shared_delayed_integrity_level", | 498 "g_shared_delayed_integrity_level", |
| 466 &g_shared_delayed_integrity_level, | 499 &g_shared_delayed_integrity_level, |
| 467 sizeof(g_shared_delayed_integrity_level)); | 500 sizeof(g_shared_delayed_integrity_level)); |
| 468 g_shared_delayed_integrity_level = INTEGRITY_LEVEL_LAST; | 501 g_shared_delayed_integrity_level = INTEGRITY_LEVEL_LAST; |
| 469 if (SBOX_ALL_OK != ret) | 502 if (SBOX_ALL_OK != ret) |
| 470 return false; | 503 return false; |
| 471 | 504 |
| 505 // Add in delayed mitigations and pseudo-mitigations enforced at startup. |
| 506 g_shared_delayed_mitigations = delayed_mitigations_ | |
| 507 FilterPostStartupProcessMitigations(mitigations_); |
| 508 if (!CanSetProcessMitigationsPostStartup(g_shared_delayed_mitigations)) |
| 509 return false; |
| 510 |
| 511 ret = target->TransferVariable("g_shared_delayed_mitigations", |
| 512 &g_shared_delayed_mitigations, |
| 513 sizeof(g_shared_delayed_mitigations)); |
| 514 g_shared_delayed_mitigations = 0; |
| 515 if (SBOX_ALL_OK != ret) |
| 516 return false; |
| 517 |
| 472 AutoLock lock(&lock_); | 518 AutoLock lock(&lock_); |
| 473 targets_.push_back(target); | 519 targets_.push_back(target); |
| 474 return true; | 520 return true; |
| 475 } | 521 } |
| 476 | 522 |
| 477 bool PolicyBase::OnJobEmpty(HANDLE job) { | 523 bool PolicyBase::OnJobEmpty(HANDLE job) { |
| 478 AutoLock lock(&lock_); | 524 AutoLock lock(&lock_); |
| 479 TargetSet::iterator it; | 525 TargetSet::iterator it; |
| 480 for (it = targets_.begin(); it != targets_.end(); ++it) { | 526 for (it = targets_.begin(); it != targets_.end(); ++it) { |
| 481 if ((*it)->Job() == job) | 527 if ((*it)->Job() == job) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 | 624 |
| 579 // Finally, setup imports on the target so the interceptions can work. | 625 // Finally, setup imports on the target so the interceptions can work. |
| 580 return SetupNtdllImports(target); | 626 return SetupNtdllImports(target); |
| 581 } | 627 } |
| 582 | 628 |
| 583 bool PolicyBase::SetupHandleCloser(TargetProcess* target) { | 629 bool PolicyBase::SetupHandleCloser(TargetProcess* target) { |
| 584 return handle_closer_.InitializeTargetHandles(target); | 630 return handle_closer_.InitializeTargetHandles(target); |
| 585 } | 631 } |
| 586 | 632 |
| 587 } // namespace sandbox | 633 } // namespace sandbox |
| OLD | NEW |