1 // Copyright (c) 2010 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 #ifndef SANDBOX_SRC_RESTRICTED_TOKEN_H_
6 #define SANDBOX_SRC_RESTRICTED_TOKEN_H_
11 #include "base/basictypes.h"
12 #include "sandbox/win/src/restricted_token_utils.h"
13 #include "sandbox/win/src/security_level.h"
14 #include "sandbox/win/src/sid.h"
16 // Flags present in the Group SID list. These 2 flags are new in Windows Vista
17 #ifndef SE_GROUP_INTEGRITY
18 #define SE_GROUP_INTEGRITY (0x00000020L)
20 #ifndef SE_GROUP_INTEGRITY_ENABLED
21 #define SE_GROUP_INTEGRITY_ENABLED (0x00000040L)
26 // Handles the creation of a restricted token using the effective token or
29 // RestrictedToken restricted_token;
30 // unsigned err_code = restricted_token.Init(NULL); // Use the current
32 // if (ERROR_SUCCESS != err_code) {
36 // restricted_token.AddRestrictingSid(ATL::Sids::Users().GetPSID());
37 // HANDLE token_handle;
38 // err_code = restricted_token.GetRestrictedTokenHandle(&token_handle);
39 // if (ERROR_SUCCESS != err_code) {
43 // CloseHandle(token_handle);
44 class RestrictedToken
{
46 // Init() has to be called before calling any other method in the class.
48 : init_(false), effective_token_(NULL
),
49 integrity_level_(INTEGRITY_LEVEL_LAST
) { }
53 CloseHandle(effective_token_
);
56 // Initializes the RestrictedToken object with effective_token.
57 // If effective_token is NULL, it initializes the RestrictedToken object with
58 // the effective token of the current process.
59 unsigned Init(HANDLE effective_token
);
61 // Creates a restricted token and returns its handle using the token_handle
62 // output parameter. This handle has to be closed by the caller.
63 // If the function succeeds, the return value is ERROR_SUCCESS. If the
64 // function fails, the return value is the win32 error code corresponding to
66 unsigned GetRestrictedTokenHandle(HANDLE
*token_handle
) const;
68 // Creates a restricted token and uses this new token to create a new token
69 // for impersonation. Returns the handle of this impersonation token using
70 // the token_handle output parameter. This handle has to be closed by
73 // If the function succeeds, the return value is ERROR_SUCCESS. If the
74 // function fails, the return value is the win32 error code corresponding to
77 // The sample usage is the same as the GetRestrictedTokenHandle function.
78 unsigned GetRestrictedTokenHandleForImpersonation(HANDLE
*token_handle
) const;
80 // Lists all sids in the token and mark them as Deny Only except for those
81 // present in the exceptions parameter. If there is no exception needed,
82 // the caller can pass an empty list or NULL for the exceptions
85 // If the function succeeds, the return value is ERROR_SUCCESS. If the
86 // function fails, the return value is the win32 error code corresponding to
90 // std::vector<Sid> sid_exceptions;
91 // sid_exceptions.push_back(ATL::Sids::Users().GetPSID());
92 // sid_exceptions.push_back(ATL::Sids::World().GetPSID());
93 // restricted_token.AddAllSidsForDenyOnly(&sid_exceptions);
94 // Note: A Sid marked for Deny Only in a token cannot be used to grant
95 // access to any resource. It can only be used to deny access.
96 unsigned AddAllSidsForDenyOnly(std::vector
<Sid
> *exceptions
);
98 // Adds a user or group SID for Deny Only in the restricted token.
99 // Parameter: sid is the SID to add in the Deny Only list.
100 // The return value is always ERROR_SUCCESS.
103 // restricted_token.AddSidForDenyOnly(ATL::Sids::Admins().GetPSID());
104 unsigned AddSidForDenyOnly(const Sid
&sid
);
106 // Adds the user sid of the token for Deny Only in the restricted token.
107 // If the function succeeds, the return value is ERROR_SUCCESS. If the
108 // function fails, the return value is the win32 error code corresponding to
110 unsigned AddUserSidForDenyOnly();
112 // Lists all privileges in the token and add them to the list of privileges
113 // to remove except for those present in the exceptions parameter. If
114 // there is no exception needed, the caller can pass an empty list or NULL
115 // for the exceptions parameter.
117 // If the function succeeds, the return value is ERROR_SUCCESS. If the
118 // function fails, the return value is the win32 error code corresponding to
122 // std::vector<std::wstring> privilege_exceptions;
123 // privilege_exceptions.push_back(SE_CHANGE_NOTIFY_NAME);
124 // restricted_token.DeleteAllPrivileges(&privilege_exceptions);
125 unsigned DeleteAllPrivileges(
126 const std::vector
<std::wstring
> *exceptions
);
128 // Adds a privilege to the list of privileges to remove in the restricted
130 // Parameter: privilege is the privilege name to remove. This is the string
131 // representing the privilege. (e.g. "SeChangeNotifyPrivilege").
132 // If the function succeeds, the return value is ERROR_SUCCESS. If the
133 // function fails, the return value is the win32 error code corresponding to
137 // restricted_token.DeletePrivilege(SE_LOAD_DRIVER_NAME);
138 unsigned DeletePrivilege(const wchar_t *privilege
);
140 // Adds a SID to the list of restricting sids in the restricted token.
141 // Parameter: sid is the sid to add to the list restricting sids.
142 // The return value is always ERROR_SUCCESS.
145 // restricted_token.AddRestrictingSid(ATL::Sids::Users().GetPSID());
146 // Note: The list of restricting is used to force Windows to perform all
147 // access checks twice. The first time using your user SID and your groups,
148 // and the second time using your list of restricting sids. The access has
149 // to be granted in both places to get access to the resource requested.
150 unsigned AddRestrictingSid(const Sid
&sid
);
152 // Adds the logon sid of the token in the list of restricting sids for the
155 // If the function succeeds, the return value is ERROR_SUCCESS. If the
156 // function fails, the return value is the win32 error code corresponding to
158 unsigned AddRestrictingSidLogonSession();
160 // Adds the owner sid of the token in the list of restricting sids for the
163 // If the function succeeds, the return value is ERROR_SUCCESS. If the
164 // function fails, the return value is the win32 error code corresponding to
166 unsigned AddRestrictingSidCurrentUser();
168 // Adds all group sids and the user sid to the restricting sids list.
170 // If the function succeeds, the return value is ERROR_SUCCESS. If the
171 // function fails, the return value is the win32 error code corresponding to
173 unsigned AddRestrictingSidAllSids();
175 // Sets the token integrity level. This is only valid on Vista. The integrity
176 // level cannot be higher than your current integrity level.
177 unsigned SetIntegrityLevel(IntegrityLevel integrity_level
);
180 // The list of restricting sids in the restricted token.
181 std::vector
<Sid
> sids_to_restrict_
;
182 // The list of privileges to remove in the restricted token.
183 std::vector
<LUID
> privileges_to_disable_
;
184 // The list of sids to mark as Deny Only in the restricted token.
185 std::vector
<Sid
> sids_for_deny_only_
;
186 // The token to restrict. Can only be set in a constructor.
187 HANDLE effective_token_
;
188 // The token integrity level. Only valid on Vista.
189 IntegrityLevel integrity_level_
;
190 // Tells if the object is initialized or not (if Init() has been called)
193 DISALLOW_COPY_AND_ASSIGN(RestrictedToken
);
196 } // namespace sandbox
198 #endif // SANDBOX_SRC_RESTRICTED_TOKEN_H_