Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3694250

Browse files
committed
Fix minor memory leak in Win32 SID handling functions. Not a big issue
since it's only called during process startup, thus no backpatch.Found by TAKATSUKA Haruka, patch by Magnus Hagander andAndrew Chernow
1 parent220e36c commit3694250

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

‎src/port/exec.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/port/exec.c,v 1.63 2009/06/11 14:49:15 momjian Exp $
12+
* $PostgreSQL: pgsql/src/port/exec.c,v 1.64 2009/07/27 08:46:10 mha Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -56,7 +56,7 @@ static intresolve_symlinks(char *path);
5656
staticchar*pipe_read_line(char*cmd,char*line,intmaxsize);
5757

5858
#ifdefWIN32
59-
staticBOOLGetUserSid(PSID*ppSidUser,HANDLEhToken);
59+
staticBOOLGetTokenUser(HANDLEhToken,PTOKEN_USER*ppTokenUser);
6060
#endif
6161

6262
/*
@@ -697,7 +697,7 @@ AddUserToDacl(HANDLE hProcess)
697697
DWORDdwTokenInfoLength=0;
698698
HANDLEhToken=NULL;
699699
PACLpacl=NULL;
700-
PSIDpsidUser=NULL;
700+
PTOKEN_USERpTokenUser=NULL;
701701
TOKEN_DEFAULT_DACLtddNew;
702702
TOKEN_DEFAULT_DACL*ptdd=NULL;
703703
TOKEN_INFORMATION_CLASStic=TokenDefaultDacl;
@@ -744,15 +744,19 @@ AddUserToDacl(HANDLE hProcess)
744744
gotocleanup;
745745
}
746746

747-
/* Get the SID for the current user. We need to add this to the ACL. */
748-
if (!GetUserSid(&psidUser,hToken))
747+
/*
748+
* Get the user token for the current user, which provides us with the
749+
* SID that is needed for creating the ACL.
750+
*/
751+
if (!GetTokenUser(hToken,&pTokenUser))
749752
{
750-
log_error("could not get userSID: %lu",GetLastError());
753+
log_error("could not get usertoken: %lu",GetLastError());
751754
gotocleanup;
752755
}
753756

754757
/* Figure out the size of the new ACL */
755-
dwNewAclSize=asi.AclBytesInUse+sizeof(ACCESS_ALLOWED_ACE)+GetLengthSid(psidUser)-sizeof(DWORD);
758+
dwNewAclSize=asi.AclBytesInUse+sizeof(ACCESS_ALLOWED_ACE)+
759+
GetLengthSid(pTokenUser->User.Sid)-sizeof(DWORD);
756760

757761
/* Allocate the ACL buffer & initialize it */
758762
pacl= (PACL)LocalAlloc(LPTR,dwNewAclSize);
@@ -785,7 +789,7 @@ AddUserToDacl(HANDLE hProcess)
785789
}
786790

787791
/* Add the new ACE for the current user */
788-
if (!AddAccessAllowedAce(pacl,ACL_REVISION,GENERIC_ALL,psidUser))
792+
if (!AddAccessAllowedAce(pacl,ACL_REVISION,GENERIC_ALL,pTokenUser->User.Sid))
789793
{
790794
log_error("could not add access allowed ACE: %lu",GetLastError());
791795
gotocleanup;
@@ -803,8 +807,8 @@ AddUserToDacl(HANDLE hProcess)
803807
ret= TRUE;
804808

805809
cleanup:
806-
if (psidUser)
807-
FreeSid(psidUser);
810+
if (pTokenUser)
811+
LocalFree((HLOCAL)pTokenUser);
808812

809813
if (pacl)
810814
LocalFree((HLOCAL)pacl);
@@ -819,28 +823,31 @@ AddUserToDacl(HANDLE hProcess)
819823
}
820824

821825
/*
822-
* GetUserSid*PSID *ppSidUser, HANDLE hToken)
826+
* GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
827+
*
828+
* Get the users token information from a process token.
823829
*
824-
* Get the SID for the current user
830+
* The caller of this function is responsible for calling LocalFree() on the
831+
* returned TOKEN_USER memory.
825832
*/
826833
staticBOOL
827-
GetUserSid(PSID*ppSidUser,HANDLEhToken)
834+
GetTokenUser(HANDLEhToken,PTOKEN_USER*ppTokenUser)
828835
{
829836
DWORDdwLength;
830-
PTOKEN_USERpTokenUser=NULL;
831837

838+
*ppTokenUser=NULL;
832839

833840
if (!GetTokenInformation(hToken,
834841
TokenUser,
835-
pTokenUser,
842+
NULL,
836843
0,
837844
&dwLength))
838845
{
839846
if (GetLastError()==ERROR_INSUFFICIENT_BUFFER)
840847
{
841-
pTokenUser= (PTOKEN_USER)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwLength);
848+
*ppTokenUser= (PTOKEN_USER)LocalAlloc(LPTR,dwLength);
842849

843-
if (pTokenUser==NULL)
850+
if (*ppTokenUser==NULL)
844851
{
845852
log_error("could not allocate %lu bytes of memory",dwLength);
846853
return FALSE;
@@ -855,18 +862,18 @@ GetUserSid(PSID *ppSidUser, HANDLE hToken)
855862

856863
if (!GetTokenInformation(hToken,
857864
TokenUser,
858-
pTokenUser,
865+
*ppTokenUser,
859866
dwLength,
860867
&dwLength))
861868
{
862-
HeapFree(GetProcessHeap(),0,pTokenUser);
863-
pTokenUser=NULL;
869+
LocalFree(*ppTokenUser);
870+
*ppTokenUser=NULL;
864871

865872
log_error("could not get token information: %lu",GetLastError());
866873
return FALSE;
867874
}
868875

869-
*ppSidUser=pTokenUser->User.Sid;
876+
/* Memory in *ppTokenUser is LocalFree():d by the caller */
870877
return TRUE;
871878
}
872879

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp