@@ -2826,31 +2826,44 @@ static void setup_windows_environment(void)
2826
2826
}
2827
2827
}
2828
2828
2829
- static PSID get_current_user_sid (void )
2829
+ static void get_current_user_sid (PSID * sid , HANDLE * linked_token )
2830
2830
{
2831
2831
HANDLE token ;
2832
2832
DWORD len = 0 ;
2833
- PSID result = NULL ;
2833
+ TOKEN_ELEVATION_TYPE elevationType ;
2834
+ DWORD size ;
2835
+
2836
+ * sid = NULL ;
2837
+ * linked_token = NULL ;
2834
2838
2835
2839
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY , & token ))
2836
- return NULL ;
2840
+ return ;
2837
2841
2838
2842
if (!GetTokenInformation (token , TokenUser , NULL , 0 , & len )) {
2839
2843
TOKEN_USER * info = xmalloc ((size_t )len );
2840
2844
if (GetTokenInformation (token , TokenUser , info , len , & len )) {
2841
2845
len = GetLengthSid (info -> User .Sid );
2842
- result = xmalloc (len );
2843
- if (!CopySid (len , result , info -> User .Sid )) {
2846
+ * sid = xmalloc (len );
2847
+ if (!CopySid (len , * sid , info -> User .Sid )) {
2844
2848
error (_ ("failed to copy SID (%ld)" ),
2845
2849
GetLastError ());
2846
- FREE_AND_NULL (result );
2850
+ FREE_AND_NULL (* sid );
2847
2851
}
2848
2852
}
2849
2853
FREE_AND_NULL (info );
2850
2854
}
2851
- CloseHandle (token );
2852
2855
2853
- return result ;
2856
+ if (GetTokenInformation (token , TokenElevationType , & elevationType , sizeof (elevationType ), & size ) &&
2857
+ elevationType == TokenElevationTypeLimited ) {
2858
+ /*
2859
+ * The current process is run by a member of the Administrators
2860
+ * group, but is not running elevated.
2861
+ */
2862
+ if (!GetTokenInformation (token , TokenLinkedToken , linked_token , sizeof (* linked_token ), & size ))
2863
+ linked_token = NULL ; /* there is no linked token */
2864
+ }
2865
+
2866
+ CloseHandle (token );
2854
2867
}
2855
2868
2856
2869
static BOOL user_sid_to_user_name (PSID sid , LPSTR * str )
@@ -2931,18 +2944,22 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
2931
2944
else if (sid && IsValidSid (sid )) {
2932
2945
/* Now, verify that the SID matches the current user's */
2933
2946
static PSID current_user_sid ;
2947
+ static HANDLE linked_token ;
2934
2948
BOOL is_member ;
2935
2949
2936
2950
if (!current_user_sid )
2937
- current_user_sid = get_current_user_sid ();
2951
+ get_current_user_sid (& current_user_sid , & linked_token );
2938
2952
2939
2953
if (current_user_sid &&
2940
2954
IsValidSid (current_user_sid ) &&
2941
2955
EqualSid (sid , current_user_sid ))
2942
2956
result = 1 ;
2943
2957
else if (IsWellKnownSid (sid , WinBuiltinAdministratorsSid ) &&
2944
- CheckTokenMembership (NULL , sid , & is_member ) &&
2945
- is_member )
2958
+ ((CheckTokenMembership (NULL , sid , & is_member ) &&
2959
+ is_member ) ||
2960
+ (linked_token &&
2961
+ CheckTokenMembership (linked_token , sid , & is_member ) &&
2962
+ is_member )))
2946
2963
/*
2947
2964
* If owned by the Administrators group, and the
2948
2965
* current user is an administrator, we consider that
0 commit comments