1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/win/src/job.h"
7 #include "base/win/windows_version.h"
8 #include "sandbox/win/src/restricted_token.h"
12 Job::Job() : job_handle_(NULL
) {
18 DWORD
Job::Init(JobLevel security_level
,
19 const wchar_t* job_name
,
21 size_t memory_limit
) {
22 if (job_handle_
.IsValid())
23 return ERROR_ALREADY_INITIALIZED
;
25 job_handle_
.Set(::CreateJobObject(NULL
, // No security attribute
27 if (!job_handle_
.IsValid())
28 return ::GetLastError();
30 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli
= {};
31 JOBOBJECT_BASIC_UI_RESTRICTIONS jbur
= {};
33 // Set the settings for the different security levels. Note: The higher levels
34 // inherit from the lower levels.
35 switch (security_level
) {
37 jeli
.BasicLimitInformation
.LimitFlags
|=
38 JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION
;
40 case JOB_RESTRICTED
: {
41 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_WRITECLIPBOARD
;
42 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_READCLIPBOARD
;
43 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_HANDLES
;
44 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_GLOBALATOMS
;
46 case JOB_LIMITED_USER
: {
47 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_DISPLAYSETTINGS
;
48 jeli
.BasicLimitInformation
.LimitFlags
|= JOB_OBJECT_LIMIT_ACTIVE_PROCESS
;
49 jeli
.BasicLimitInformation
.ActiveProcessLimit
= 1;
51 case JOB_INTERACTIVE
: {
52 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS
;
53 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_DESKTOP
;
54 jbur
.UIRestrictionsClass
|= JOB_OBJECT_UILIMIT_EXITWINDOWS
;
56 case JOB_UNPROTECTED
: {
58 jeli
.BasicLimitInformation
.LimitFlags
|=
59 JOB_OBJECT_LIMIT_PROCESS_MEMORY
;
60 jeli
.ProcessMemoryLimit
= memory_limit
;
63 jeli
.BasicLimitInformation
.LimitFlags
|=
64 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
;
68 return ERROR_BAD_ARGUMENTS
;
72 if (FALSE
== ::SetInformationJobObject(job_handle_
.Get(),
73 JobObjectExtendedLimitInformation
,
76 return ::GetLastError();
79 jbur
.UIRestrictionsClass
= jbur
.UIRestrictionsClass
& (~ui_exceptions
);
80 if (FALSE
== ::SetInformationJobObject(job_handle_
.Get(),
81 JobObjectBasicUIRestrictions
,
84 return ::GetLastError();
90 DWORD
Job::UserHandleGrantAccess(HANDLE handle
) {
91 if (!job_handle_
.IsValid())
94 if (!::UserHandleGrantAccess(handle
,
96 TRUE
)) { // Access allowed.
97 return ::GetLastError();
100 return ERROR_SUCCESS
;
103 base::win::ScopedHandle
Job::Take() {
104 return job_handle_
.Pass();
107 DWORD
Job::AssignProcessToJob(HANDLE process_handle
) {
108 if (!job_handle_
.IsValid())
109 return ERROR_NO_DATA
;
111 if (FALSE
== ::AssignProcessToJobObject(job_handle_
.Get(), process_handle
))
112 return ::GetLastError();
114 return ERROR_SUCCESS
;
117 } // namespace sandbox