1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_ACCESS_TOKEN_H_
#define BASE_WIN_ACCESS_TOKEN_H_
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/win/access_control_list.h"
#include "base/win/scoped_handle.h"
#include "base/win/sid.h"
#include "base/win/windows_types.h"
namespace base::win {
// Impersonation level for the token.
enum class SecurityImpersonationLevel {
kAnonymous,
kIdentification,
kImpersonation,
kDelegation
};
// This class is used to access the information for a Windows access token.
class BASE_EXPORT AccessToken {
public:
// This class represents an access token group.
class BASE_EXPORT Group {
public:
// Get the group SID.
const Sid& GetSid() const LIFETIME_BOUND { return sid_; }
// Get the group attribute flags.
DWORD GetAttributes() const { return attributes_; }
// Returns true if the group is an integrity level.
bool IsIntegrity() const;
// Returns true if the group is enabled.
bool IsEnabled() const;
// Returns true if the group is deny only.
bool IsDenyOnly() const;
// Returns true if the group is the logon ID.
bool IsLogonId() const;
Group(Sid&& sid, DWORD attributes);
Group(Group&&);
~Group();
private:
Sid sid_;
DWORD attributes_;
};
// This class represents an access token privilege.
class BASE_EXPORT Privilege {
public:
// Get the privilege LUID.
CHROME_LUID GetLuid() const { return luid_; }
// Get the privilege attribute flags.
DWORD GetAttributes() const { return attributes_; }
// Get the name of the privilege.
std::wstring GetName() const;
// Returns true if the privilege is enabled.
bool IsEnabled() const;
Privilege(CHROME_LUID luid, DWORD attributes);
private:
CHROME_LUID luid_;
DWORD attributes_;
};
// Creates an AccessToken object from a token handle.
// |token| the token handle. This handle will be duplicated for TOKEN_QUERY
// access, therefore the caller must be granted that access to the token
// object. The AccessToken object owns its own copy of the token handle so
// the original can be closed.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromToken(HANDLE token,
ACCESS_MASK desired_access = 0);
// Creates an AccessToken object from an existing token handle.
// |token| the token handle. The AccessToken object will take ownership of
// this handle without duplicating it. It must have been opened with at least
// TOKEN_QUERY access to succeed.
static std::optional<AccessToken> FromToken(ScopedHandle&& token);
// Creates an AccessToken object from a process handle.
// |process| the process handle. The handle needs to have
// PROCESS_QUERY_LIMITED_INFORMATION access to the handle and TOKEN_QUERY
// access to the token object.
// |impersonation| if true then the process token will be duplicated to an
// impersonation token. This allows you to call the IsMember API which
// requires an impersonation token. To duplicate TOKEN_DUPLICATE access is
// required.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromProcess(HANDLE process,
bool impersonation = false,
ACCESS_MASK desired_access = 0);
// Creates an AccessToken object for the current process.
// |impersonation| if true then the process token will be duplicated to an
// impersonation token. This allows you to call the IsMember API which
// requires an impersonation token. To duplicate TOKEN_DUPLICATE access is
// required.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromCurrentProcess(
bool impersonation = false,
ACCESS_MASK desired_access = 0);
// Creates an AccessToken object from a thread handle. The thread must be
// impersonating a token for this to succeed.
// |thread| the thread handle. The handle needs to have
// THREAD_QUERY_LIMITED_INFORMATION access and TOKEN_QUERY access to the
// token object.
// |open_as_self| open the token using the process token rather than the
// current thread's impersonated token.
// If the thread isn't impersonating it will return an empty value and the
// Win32 last error code will be ERROR_NO_TOKEN.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromThread(HANDLE thread,
bool open_as_self = true,
ACCESS_MASK desired_access = 0);
// Creates an AccessToken object from the current thread. The thread must be
// impersonating a token for this to succeed.
// |open_as_self| open the thread handle using the process token rather
// than the current thread's impersonated token.
// If the thread isn't impersonating it will return an empty value and the
// Win32 last error code will be ERROR_NO_TOKEN.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromCurrentThread(
bool open_as_self = true,
ACCESS_MASK desired_access = 0);
// Creates an AccessToken object for the current thread's effective token.
// If the thread is impersonating then it'll try and open the thread token,
// otherwise it'll open the process token.
// |desired_access| specifies additional access for the token handle,
// TOKEN_QUERY will always be requested.
static std::optional<AccessToken> FromEffective(
ACCESS_MASK desired_access = 0);
AccessToken(const AccessToken&) = delete;
AccessToken& operator=(const AccessToken&) = delete;
AccessToken(AccessToken&&);
AccessToken& operator=(AccessToken&&);
~AccessToken();
// Get the token's user SID.
Sid User() const;
// Get the token's user group.
Group UserGroup() const;
// Get the token's owner SID. This can be different to the user SID, it's
// used as the default owner for new secured objects.
Sid Owner() const;
// Get the token's primary group SID.
Sid PrimaryGroup() const;
// Get the token logon SID. Returns an empty value if the token doesn't have
// a logon SID. If the logon SID doesn't exist then the Win32 last error code
// will be ERROR_NOT_FOUND.
std::optional<Sid> LogonId() const;
// Get the token's integrity level. Returns MAXDWORD if the token doesn't
// have an integrity level.
DWORD IntegrityLevel() const;
// Set the token's integrity level. Token needs to have been opened with
// TOKEN_ADJUST_DEFAULT access.
bool SetIntegrityLevel(DWORD integrity_level);
// Get the token's session ID. Returns MAXDWORD if the token if the session
// ID can't be queried.
DWORD SessionId() const;
// The token's group list.
std::vector<Group> Groups() const;
// Get whether the token is a restricted.
bool IsRestricted() const;
// The token's restricted SIDs list. If not a restricted token this will
// return an empty vector.
std::vector<Group> RestrictedSids() const;
// Get whether the token is an appcontainer.
bool IsAppContainer() const;
// Get the token's appcontainer SID. If not an appcontainer token this will
// return an empty value.
std::optional<Sid> AppContainerSid() const;
// The token's capabilities. If not an appcontainer token this will return an
// empty vector.
std::vector<Group> Capabilities() const;
// Get the UAC linked token.
std::optional<AccessToken> LinkedToken() const;
// Get the default DACL for the token. Returns an empty value on error.
std::optional<AccessControlList> DefaultDacl() const;
// Set the default DACL of the token. Token needs to have been opened with
// TOKEN_ADJUST_DEFAULT access.
bool SetDefaultDacl(const AccessControlList& default_dacl);
// Get the token's ID.
CHROME_LUID Id() const;
// Get the token's authentication ID.
CHROME_LUID AuthenticationId() const;
// Get the token's privileges.
std::vector<Privilege> Privileges() const;
// Get whether the token is elevated.
bool IsElevated() const;
// Checks if the sid is a member of the token's groups. The token must be
// an impersonation token rather than a primary token. If the token is not an
// impersonation token then it returns false and the Win32 last error will be
// set to ERROR_NO_IMPERSONATION_TOKEN.
bool IsMember(const Sid& sid) const;
// Checks if the well known sid is a member of the token's groups. The token
// must be an impersonation token rather than a primary token. If the token
// is not an impersonation token then it returns false and the Win32 last
// error will be set to ERROR_NO_IMPERSONATION_TOKEN.
bool IsMember(WellKnownSid known_sid) const;
// Checks if the token is an impersonation token. If false then it's a primary
// token.
bool IsImpersonation() const;
// Checks if the token can only be used for identification. This is based on
// the security impersonation level of the token. If the level is less than
// or equal to SecurityIdentification this function returns true. Always
// returns false for a primary token.
bool IsIdentification() const;
// Get the current impersonation level. If the token is a primary token
// the function returns kImpersonation.
SecurityImpersonationLevel ImpersonationLevel() const;
// Duplicate the token to a new primary token.
// |desired_access| specifies additional access for the token handle.
// TOKEN_QUERY will always be requested.
// The original token must have TOKEN_DUPLICATE access to successfully
// duplicate the token.
std::optional<AccessToken> DuplicatePrimary(
ACCESS_MASK desired_access = 0) const;
// Duplicate the token to a new impersonation token.
// |impersonation_level| specifies the impersonation level for the token.
// |desired_access| specifies additional access for the token handle.
// TOKEN_QUERY will always be requested.
// The original token must have TOKEN_DUPLICATE access to successfully
// duplicate the token.
std::optional<AccessToken> DuplicateImpersonation(
SecurityImpersonationLevel impersonation_level =
SecurityImpersonationLevel::kImpersonation,
ACCESS_MASK desired_access = 0) const;
// Create a new restricted token from this token.
// |flags| can be set to a combination of DISABLE_MAX_PRIVILEGE,
// SANDBOX_INERT, LUA_TOKEN and WRITE_RESTRICTED.
// |sids_to_disable| is the list of SIDs to disable in the token.
// |privileges_to_delete| is the names of the privileges to delete.
// |sids_to_restrict| is the list of SIDs to add as restricted SIDs.
// |desired_access| specifies additional access for the token handle.
// The token needs to be opened with TOKEN_DUPLICATE access.
std::optional<AccessToken> CreateRestricted(
DWORD flags,
const std::vector<Sid>& sids_to_disable,
const std::vector<std::wstring>& privileges_to_delete,
const std::vector<Sid>& sids_to_restrict,
ACCESS_MASK desired_access = 0) const;
// Create a new AppContainer primary token from this token.
// |app_container_sid| the AppContainer package SID.
// |capabilities| the list of AppContainer capabilities.
// |desired_access| specifies additional access for the token handle.
// The token needs to be opened with TOKEN_DUPLICATE access.
std::optional<AccessToken> CreateAppContainer(
const Sid& appcontainer_sid,
const std::vector<Sid>& capabilities,
ACCESS_MASK desired_access = 0) const;
// Enable or disable a privilege.
// |name| the name of the privilege to change.
// |enable| specify whether to enable or disable the privilege.
// Returns the previous enable state of the privilege, or nullopt if failed.
// The token must be opened with TOKEN_ADJUST_PRIVILEGES access.
std::optional<bool> SetPrivilege(const std::wstring& name, bool enable);
// Remove a privilege permanently from the token.
// |name| the name of the privilege to remove.
// Returns true if successfully removed the privilege.
// The token must be opened with TOKEN_ADJUST_PRIVILEGES access.
bool RemovePrivilege(const std::wstring& name);
// Permanently remove all privileges from the token.
// Returns true if the operation was successful.
// The token must be opened with TOKEN_ADJUST_PRIVILEGES access.
bool RemoveAllPrivileges();
// Indicates if the AccessToken object is valid.
bool is_valid() const;
// Get the underlying token handle.
HANDLE get() const;
// Take ownership of the underlying token handle. Once released no other
// methods on this object should be called.
ScopedHandle release();
private:
explicit AccessToken(HANDLE token);
ScopedHandle token_;
};
} // namespace base::win
#endif // BASE_WIN_ACCESS_TOKEN_H_
|