1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "updatelogging.h"
11 // See the MSDN documentation with title: Privilege Constants
12 // At the time of this writing, this documentation is located at:
13 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx
14 LPCTSTR
UACHelper::PrivsToDisable
[] =
16 SE_ASSIGNPRIMARYTOKEN_NAME
,
19 // CreateProcess will succeed but the app will fail to launch on some WinXP
20 // machines if SE_CHANGE_NOTIFY_NAME is disabled. In particular this happens
21 // for limited user accounts on those machines. The define is kept here as a
22 // reminder that it should never be re-added.
23 // This permission is for directory watching but also from MSDN: "This
24 // privilege also causes the system to skip all traversal access checks."
25 // SE_CHANGE_NOTIFY_NAME,
26 SE_CREATE_GLOBAL_NAME
,
27 SE_CREATE_PAGEFILE_NAME
,
28 SE_CREATE_PERMANENT_NAME
,
29 SE_CREATE_SYMBOLIC_LINK_NAME
,
32 SE_ENABLE_DELEGATION_NAME
,
34 SE_INC_BASE_PRIORITY_NAME
,
35 SE_INCREASE_QUOTA_NAME
,
36 SE_INC_WORKING_SET_NAME
,
39 SE_MACHINE_ACCOUNT_NAME
,
40 SE_MANAGE_VOLUME_NAME
,
41 SE_PROF_SINGLE_PROCESS_NAME
,
43 SE_REMOTE_SHUTDOWN_NAME
,
48 SE_SYSTEM_ENVIRONMENT_NAME
,
49 SE_SYSTEM_PROFILE_NAME
,
51 SE_TAKE_OWNERSHIP_NAME
,
54 SE_TRUSTED_CREDMAN_ACCESS_NAME
,
56 SE_UNSOLICITED_INPUT_NAME
60 * Opens a user token for the given session ID
62 * @param sessionID The session ID for the token to obtain
63 * @return A handle to the token to obtain which will be primary if enough
64 * permissions exist. Caller should close the handle.
67 UACHelper::OpenUserToken(DWORD sessionID
)
69 HMODULE module
= LoadLibraryW(L
"wtsapi32.dll");
70 HANDLE token
= nullptr;
71 decltype(WTSQueryUserToken
)* wtsQueryUserToken
=
72 (decltype(WTSQueryUserToken
)*) GetProcAddress(module
, "WTSQueryUserToken");
73 if (wtsQueryUserToken
)
75 wtsQueryUserToken(sessionID
, &token
);
82 * Opens a linked token for the specified token.
84 * @param token The token to get the linked token from
85 * @return A linked token or nullptr if one does not exist.
86 * Caller should close the handle.
89 UACHelper::OpenLinkedToken(HANDLE token
)
92 // UAC creates 2 tokens. One is the restricted token which we have.
93 // the other is the UAC elevated one. Since we are running as a service
94 // as the system account we have access to both.
95 TOKEN_LINKED_TOKEN tlt
;
96 HANDLE hNewLinkedToken
= nullptr;
98 if (GetTokenInformation(token
, (TOKEN_INFORMATION_CLASS
)TokenLinkedToken
,
99 &tlt
, sizeof(TOKEN_LINKED_TOKEN
), &len
))
101 token
= tlt
.LinkedToken
;
102 hNewLinkedToken
= token
;
104 return hNewLinkedToken
;
109 * Enables or disables a privilege for the specified token.
111 * @param token The token to adjust the privilege on.
112 * @param priv The privilege to adjust.
113 * @param enable Whether to enable or disable it
114 * @return TRUE if the token was adjusted to the specified value.
117 UACHelper::SetPrivilege(HANDLE token
, LPCTSTR priv
, BOOL enable
)
120 if (!LookupPrivilegeValue(nullptr, priv
, &luidOfPriv
))
125 TOKEN_PRIVILEGES tokenPriv
;
126 tokenPriv
.PrivilegeCount
= 1;
127 tokenPriv
.Privileges
[0].Luid
= luidOfPriv
;
128 tokenPriv
.Privileges
[0].Attributes
= enable
? SE_PRIVILEGE_ENABLED
: 0;
130 SetLastError(ERROR_SUCCESS
);
131 if (!AdjustTokenPrivileges(token
, false, &tokenPriv
,
132 sizeof(tokenPriv
), nullptr, nullptr))
137 return GetLastError() == ERROR_SUCCESS
;
141 * For each privilege that is specified, an attempt will be made to
142 * drop the privilege.
144 * @param token The token to adjust the privilege on.
145 * Pass nullptr for current token.
146 * @param unneededPrivs An array of unneeded privileges.
147 * @param count The size of the array
148 * @return TRUE if there were no errors
151 UACHelper::DisableUnneededPrivileges(HANDLE token
,
152 LPCTSTR
*unneededPrivs
,
155 HANDLE obtainedToken
= nullptr;
158 // Note: This handle is a pseudo-handle and need not be closed
159 HANDLE process
= GetCurrentProcess();
160 if (!OpenProcessToken(process
, TOKEN_ALL_ACCESS_P
, &obtainedToken
))
162 LOG_WARN(("Could not obtain token for current process, no "
163 "privileges changed. (%d)", GetLastError()));
166 token
= obtainedToken
;
170 for (size_t i
= 0; i
< count
; i
++)
172 if (SetPrivilege(token
, unneededPrivs
[i
], FALSE
))
174 LOG(("Disabled unneeded token privilege: %s.",
179 LOG(("Could not disable token privilege value: %s. (%d)",
180 unneededPrivs
[i
], GetLastError()));
187 CloseHandle(obtainedToken
);
193 * Disables privileges for the specified token.
194 * The privileges to disable are in PrivsToDisable.
195 * In the future there could be new privs and we are not sure if we should
196 * explicitly disable these or not.
198 * @param token The token to drop the privilege on.
199 * Pass nullptr for current token.
200 * @return TRUE if there were no errors
203 UACHelper::DisablePrivileges(HANDLE token
)
205 static const size_t PrivsToDisableSize
=
206 sizeof(UACHelper::PrivsToDisable
) / sizeof(UACHelper::PrivsToDisable
[0]);
208 return DisableUnneededPrivileges(token
, UACHelper::PrivsToDisable
,
213 * Check if the current user can elevate.
215 * @return true if the user can elevate.
219 UACHelper::CanUserElevate()
222 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
))
227 TOKEN_ELEVATION_TYPE elevationType
;
229 bool canElevate
= GetTokenInformation(token
, TokenElevationType
,
231 sizeof(elevationType
), &len
) &&
232 (elevationType
== TokenElevationTypeLimited
);