Обсуждение: GetTokenInformation() and FreeSid() at port/exec.c

Поиск
Список
Период
Сортировка

GetTokenInformation() and FreeSid() at port/exec.c

От
TAKATSUKA Haruka
Дата:
Hi.

We found the unbalance of xxAlloc and xxFree at AddUserToDacl() in
src/port/exec.c (of current HEAD code).

psidUser is a pointer of the element of a TOKEN_USER structure
allocated by  HeapAlloc().  The FreeSid() frees a SID allocated by
AllocateAndInitializeSid().  I think that it is correct to use
HeapFree(GetProcessHeap(), 0, pTokenUser).

At present, a specific error, crash or trouble seems not to have happened.


src/port/exec.c:748     AddUserToDacl()
src/port/exec.c:841      GetUserSid()
        pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);

src/port/exec.c:807      AddUserToDacl()
    FreeSid(psidUser);

______________________________________________________________________
 TAKATSUKA Haruka  harukat@sraoss.co.jp
 SRA OSS, Inc  http://www.sraoss.co.jp

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
TAKATSUKA Haruka wrote:
> Hi.
>
> We found the unbalance of xxAlloc and xxFree at AddUserToDacl() in
> src/port/exec.c (of current HEAD code).
>
> psidUser is a pointer of the element of a TOKEN_USER structure
> allocated by  HeapAlloc().  The FreeSid() frees a SID allocated by
> AllocateAndInitializeSid().  I think that it is correct to use
> HeapFree(GetProcessHeap(), 0, pTokenUser).
>
> At present, a specific error, crash or trouble seems not to have happened.
>
>
> src/port/exec.c:748     AddUserToDacl()
> src/port/exec.c:841      GetUserSid()
>         pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);
>
> src/port/exec.c:807      AddUserToDacl()
>     FreeSid(psidUser);

I quickly poked around and found what I believe to be two memory issues.

1. GetUserSid() uses HeapAlloc() to allocate a TOKEN_USER, but never calls
HeapFree() if the function succeeds.  Instead, it pulls out the token's SID and
returns it.  This is a memory leak.

2. The SID returned by GetUserSid() is incorrectly being passed to FreeSid()
within AddUserToDacl()'s cleanup section.  This memory belongs to the TOKEN_USER
allocated by HeapAlloc() in GetUserSid(), it cannot be passed to FreeSid.

Quick question, Why HeapAlloc and LocalAlloc.  Why not use malloc?

One solution would be to return a copy of the SID from GetUserSid and HeapFree
the TOKEN_USER.

Replace GetUserSid() line 869

*ppSidUser = pTokenUser->User.Sid;
return TRUE;

With the below (error checking excluded)

DWORD len = GetLengthSid(pTokenUser->User.Sid)
*ppSidUser = (PSID) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
CopySid(len, *ppSidUser, pTokenUser->User.Sid);

// SID is copied, free TOKEN_USER
HeapFree(GetProcessHeap(), 0, pTokenUser);
return TRUE;

Also, AddUserToDacl() line 807

FreeSid(psidUser) should be HeapFree(GetProcessHeap(), 0, psidUser)

in order to work with my suggested changes in GetUserSid().

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
>> At present, a specific error, crash or trouble seems not to have
>> happened.

The reason its not crashing is that most, if not all, windows allocation
functions know which addresses belong to them.  FreeSid is actually
documented as returning NULL on success.  On failure it returns the
address you tried to free.

Although the FreeSid call causes no harm because its defensive, there is
still the issue of leaking the TOKEN_USER structure.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Magnus Hagander
Дата:
Andrew Chernow wrote:
> TAKATSUKA Haruka wrote:
>> Hi.
>>
>> We found the unbalance of xxAlloc and xxFree at AddUserToDacl() in
>> src/port/exec.c (of current HEAD code).
>>
>> psidUser is a pointer of the element of a TOKEN_USER structure
>> allocated by  HeapAlloc().  The FreeSid() frees a SID allocated by
>> AllocateAndInitializeSid().  I think that it is correct to use
>> HeapFree(GetProcessHeap(), 0, pTokenUser).
>>
>> At present, a specific error, crash or trouble seems not to have
>> happened.
>>
>>
>> src/port/exec.c:748     AddUserToDacl()
>> src/port/exec.c:841      GetUserSid()
>>         pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(),
>> HEAP_ZERO_MEMORY, dwLength);
>>
>> src/port/exec.c:807      AddUserToDacl()
>>     FreeSid(psidUser);
>
> I quickly poked around and found what I believe to be two memory issues.
>
> 1. GetUserSid() uses HeapAlloc() to allocate a TOKEN_USER, but never
> calls HeapFree() if the function succeeds.  Instead, it pulls out the
> token's SID and returns it.  This is a memory leak.
>
> 2. The SID returned by GetUserSid() is incorrectly being passed to
> FreeSid() within AddUserToDacl()'s cleanup section.  This memory belongs
> to the TOKEN_USER allocated by HeapAlloc() in GetUserSid(), it cannot be
> passed to FreeSid.
>
> Quick question, Why HeapAlloc and LocalAlloc.  Why not use malloc?
>
> One solution would be to return a copy of the SID from GetUserSid and
> HeapFree the TOKEN_USER.
>
> Replace GetUserSid() line 869
>
> *ppSidUser = pTokenUser->User.Sid;
> return TRUE;
>
> With the below (error checking excluded)
>
> DWORD len = GetLengthSid(pTokenUser->User.Sid)
> *ppSidUser = (PSID) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
> CopySid(len, *ppSidUser, pTokenUser->User.Sid);
>
> // SID is copied, free TOKEN_USER
> HeapFree(GetProcessHeap(), 0, pTokenUser);
> return TRUE;
>
> Also, AddUserToDacl() line 807
>
> FreeSid(psidUser) should be HeapFree(GetProcessHeap(), 0, psidUser)
>
> in order to work with my suggested changes in GetUserSid().

How about something like this? I switched to using LocalAlloc() in all
places to be consistent, instead of mixing heap and local. (Though per
doc, LocalAlloc is actually a wrapper for HeapAlloc in win32).

--
 Magnus Hagander
 Self: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/
*** a/src/port/exec.c
--- b/src/port/exec.c
***************
*** 804,810 **** AddUserToDacl(HANDLE hProcess)

  cleanup:
      if (psidUser)
!         FreeSid(psidUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);
--- 804,810 ----

  cleanup:
      if (psidUser)
!         LocalFree((HLOCAL) psidUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);
***************
*** 822,827 **** cleanup:
--- 822,830 ----
   * GetUserSid*PSID *ppSidUser, HANDLE hToken)
   *
   * Get the SID for the current user
+  *
+  * The caller of this function is responsible for calling LocalFree() on the
+  * returned SID area.
   */
  static BOOL
  GetUserSid(PSID *ppSidUser, HANDLE hToken)
***************
*** 838,844 **** GetUserSid(PSID *ppSidUser, HANDLE hToken)
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);

              if (pTokenUser == NULL)
              {
--- 841,847 ----
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             pTokenUser = (PTOKEN_USER) LocalAlloc(LPTR, dwLength);

              if (pTokenUser == NULL)
              {
***************
*** 859,872 **** GetUserSid(PSID *ppSidUser, HANDLE hToken)
                               dwLength,
                               &dwLength))
      {
!         HeapFree(GetProcessHeap(), 0, pTokenUser);
          pTokenUser = NULL;

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

!     *ppSidUser = pTokenUser->User.Sid;
      return TRUE;
  }

--- 862,895 ----
                               dwLength,
                               &dwLength))
      {
!         LocalFree(pTokenUser);
          pTokenUser = NULL;

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

!     /*
!      * Need to copy the data to a separate buffer, so we can free our buffer
!      * and let the caller free only the sid part.
!      */
!     dwLength = GetLengthSid(pTokenUser->User.Sid);
!     *ppSidUser = (PSID) LocalAlloc(LPTR, dwLength);
!     if (!*ppSidUser)
!     {
!         LocalFree(pTokenUser);
!         log_error("could not allocate %lu bytes of memory", dwLength);
!         return FALSE;
!     }
!     if (!CopySid(dwLength, *ppSidUser, pTokenUser->User.Sid))
!     {
!         LocalFree(*ppSidUser);
!         LocalFree(pTokenUser);
!         log_error("could not copy SID: %lu", GetLastError());
!         return FALSE;
!     }
!
!     LocalFree(pTokenUser);
      return TRUE;
  }


Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
>
> DWORD len = GetLengthSid(pTokenUser->User.Sid)
> *ppSidUser = (PSID) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
> CopySid(len, *ppSidUser, pTokenUser->User.Sid);
>

I attached a patch for this.  Although, I did not use CopySid.  Instead,
I changed GetUserSid to GetTokenUser.  AddUserToDacl() is the only
function making use of GetUserSid(), so this change won't break
anything.  The benefit to this approach over my first suggestion is that
it avoids an unneeded HeapAlloc(sid), CopySid(sid) ... and its cleaner.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/
Index: src/port/exec.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/port/exec.c,v
retrieving revision 1.63
diff -C6 -r1.63 exec.c
*** src/port/exec.c    11 Jun 2009 14:49:15 -0000    1.63
--- src/port/exec.c    23 Jun 2009 14:57:46 -0000
***************
*** 53,65 ****

  static int    validate_exec(const char *path);
  static int    resolve_symlinks(char *path);
  static char *pipe_read_line(char *cmd, char *line, int maxsize);

  #ifdef WIN32
! static BOOL GetUserSid(PSID *ppSidUser, HANDLE hToken);
  #endif

  /*
   * validate_exec -- validate "path" as an executable file
   *
   * returns 0 if the file is found and no error is encountered.
--- 53,65 ----

  static int    validate_exec(const char *path);
  static int    resolve_symlinks(char *path);
  static char *pipe_read_line(char *cmd, char *line, int maxsize);

  #ifdef WIN32
! static BOOL GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser);
  #endif

  /*
   * validate_exec -- validate "path" as an executable file
   *
   * returns 0 if the file is found and no error is encountered.
***************
*** 694,706 ****
      ACCESS_ALLOWED_ACE *pace;
      DWORD        dwNewAclSize;
      DWORD        dwSize = 0;
      DWORD        dwTokenInfoLength = 0;
      HANDLE        hToken = NULL;
      PACL        pacl = NULL;
!     PSID        psidUser = NULL;
      TOKEN_DEFAULT_DACL tddNew;
      TOKEN_DEFAULT_DACL *ptdd = NULL;
      TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
      BOOL        ret = FALSE;

      /* Get the token for the process */
--- 694,706 ----
      ACCESS_ALLOWED_ACE *pace;
      DWORD        dwNewAclSize;
      DWORD        dwSize = 0;
      DWORD        dwTokenInfoLength = 0;
      HANDLE        hToken = NULL;
      PACL        pacl = NULL;
!     PTOKEN_USER pTokenUser = NULL;
      TOKEN_DEFAULT_DACL tddNew;
      TOKEN_DEFAULT_DACL *ptdd = NULL;
      TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
      BOOL        ret = FALSE;

      /* Get the token for the process */
***************
*** 741,761 ****
                             AclSizeInformation))
      {
          log_error("could not get ACL information: %lu", GetLastError());
          goto cleanup;
      }

!     /* Get the SID for the current user. We need to add this to the ACL. */
!     if (!GetUserSid(&psidUser, hToken))
      {
!         log_error("could not get user SID: %lu", GetLastError());
          goto cleanup;
      }

      /* Figure out the size of the new ACL */
!     dwNewAclSize = asi.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidUser) -sizeof(DWORD);

      /* Allocate the ACL buffer & initialize it */
      pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
      if (pacl == NULL)
      {
          log_error("could not allocate %lu bytes of memory", dwNewAclSize);
--- 741,764 ----
                             AclSizeInformation))
      {
          log_error("could not get ACL information: %lu", GetLastError());
          goto cleanup;
      }

!     /* Get the user token for the current user. This provides us with the
!      * user's SID which is needed for creating the ACL.
!      */
!     if (!GetTokenUser(hToken, &pTokenUser))
      {
!         log_error("could not get user token: %lu", GetLastError());
          goto cleanup;
      }

      /* Figure out the size of the new ACL */
!     dwNewAclSize = asi.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) +
!         GetLengthSid(pTokenUser->User.Sid) - sizeof(DWORD);

      /* Allocate the ACL buffer & initialize it */
      pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
      if (pacl == NULL)
      {
          log_error("could not allocate %lu bytes of memory", dwNewAclSize);
***************
*** 782,794 ****
              log_error("could not add ACE: %lu", GetLastError());
              goto cleanup;
          }
      }

      /* Add the new ACE for the current user */
!     if (!AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, psidUser))
      {
          log_error("could not add access allowed ACE: %lu", GetLastError());
          goto cleanup;
      }

      /* Set the new DACL in the token */
--- 785,797 ----
              log_error("could not add ACE: %lu", GetLastError());
              goto cleanup;
          }
      }

      /* Add the new ACE for the current user */
!     if (!AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, pTokenUser->User.Sid))
      {
          log_error("could not add access allowed ACE: %lu", GetLastError());
          goto cleanup;
      }

      /* Set the new DACL in the token */
***************
*** 800,813 ****
          goto cleanup;
      }

      ret = TRUE;

  cleanup:
!     if (psidUser)
!         FreeSid(psidUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);

      if (ptdd)
          LocalFree((HLOCAL) ptdd);
--- 803,816 ----
          goto cleanup;
      }

      ret = TRUE;

  cleanup:
!     if (pTokenUser)
!         HeapFree(GetProcessHeap(), 0, pTokenUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);

      if (ptdd)
          LocalFree((HLOCAL) ptdd);
***************
*** 816,873 ****
          CloseHandle(hToken);

      return ret;
  }

  /*
!  * GetUserSid*PSID *ppSidUser, HANDLE hToken)
   *
!  * Get the SID for the current user
   */
  static BOOL
! GetUserSid(PSID *ppSidUser, HANDLE hToken)
  {
      DWORD        dwLength;
!     PTOKEN_USER pTokenUser = NULL;
!

!     if (!GetTokenInformation(hToken,
!                              TokenUser,
!                              pTokenUser,
!                              0,
!                              &dwLength))
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);

!             if (pTokenUser == NULL)
              {
                  log_error("could not allocate %lu bytes of memory", dwLength);
                  return FALSE;
              }
          }
          else
          {
              log_error("could not get token information buffer size: %lu", GetLastError());
              return FALSE;
          }
      }

!     if (!GetTokenInformation(hToken,
!                              TokenUser,
!                              pTokenUser,
!                              dwLength,
!                              &dwLength))
      {
!         HeapFree(GetProcessHeap(), 0, pTokenUser);
!         pTokenUser = NULL;

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

-     *ppSidUser = pTokenUser->User.Sid;
      return TRUE;
  }

  #endif
--- 819,871 ----
          CloseHandle(hToken);

      return ret;
  }

  /*
!  * GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
   *
!  * Get the user's token information from a process token.  If the
!  * function succeeds, it returns TRUE and *ppTokenUser is assigned
!  * to memory allocated with HeapAlloc() ... free with HeapFree().
!  * If the function fails, it returns FALSE and *ppTokenUser will
!  * point to NULL.
   */
  static BOOL
! GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
  {
      DWORD        dwLength;
!
!     *ppTokenUser = NULL;

!     if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength))
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             *ppTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);

!             if (*ppTokenUser == NULL)
              {
                  log_error("could not allocate %lu bytes of memory", dwLength);
                  return FALSE;
              }
          }
          else
          {
              log_error("could not get token information buffer size: %lu", GetLastError());
              return FALSE;
          }
      }

!     if (!GetTokenInformation(hToken, TokenUser, *ppTokenUser, dwLength, &dwLength))
      {
!         HeapFree(GetProcessHeap(), 0, *ppTokenUser);
!         *ppTokenUser = NULL;

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

      return TRUE;
  }

  #endif

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
>
> How about something like this? I switched to using LocalAlloc() in all
> places to be consistent, instead of mixing heap and local. (Though per
> doc, LocalAlloc is actually a wrapper for HeapAlloc in win32).

Our patches crossed.  Although, in my patch I left the allocation scheme
alone since I wasn't sure if someone wanted that way.  I'd suggest
malloc and free if your going to change it.  The only time I use an MS
allocater is when a win32 api function specifically states it must be used.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Magnus Hagander
Дата:
Andrew Chernow wrote:
>
>>
>> How about something like this? I switched to using LocalAlloc() in all
>> places to be consistent, instead of mixing heap and local. (Though per
>> doc, LocalAlloc is actually a wrapper for HeapAlloc in win32).
>
> Our patches crossed.  Although, in my patch I left the allocation scheme
> alone since I wasn't sure if someone wanted that way.  I'd suggest
> malloc and free if your going to change it.  The only time I use an MS
> allocater is when a win32 api function specifically states it must be used.

Attached is a mix of our two patches. How does that look to you?


--
 Magnus Hagander
 Self: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/
*** a/src/port/exec.c
--- b/src/port/exec.c
***************
*** 56,62 **** static int    resolve_symlinks(char *path);
  static char *pipe_read_line(char *cmd, char *line, int maxsize);

  #ifdef WIN32
! static BOOL GetUserSid(PSID *ppSidUser, HANDLE hToken);
  #endif

  /*
--- 56,62 ----
  static char *pipe_read_line(char *cmd, char *line, int maxsize);

  #ifdef WIN32
! static BOOL GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser);
  #endif

  /*
***************
*** 697,703 **** AddUserToDacl(HANDLE hProcess)
      DWORD        dwTokenInfoLength = 0;
      HANDLE        hToken = NULL;
      PACL        pacl = NULL;
!     PSID        psidUser = NULL;
      TOKEN_DEFAULT_DACL tddNew;
      TOKEN_DEFAULT_DACL *ptdd = NULL;
      TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
--- 697,703 ----
      DWORD        dwTokenInfoLength = 0;
      HANDLE        hToken = NULL;
      PACL        pacl = NULL;
!     PTOKEN_USER    pTokenUser = NULL;
      TOKEN_DEFAULT_DACL tddNew;
      TOKEN_DEFAULT_DACL *ptdd = NULL;
      TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
***************
*** 744,758 **** AddUserToDacl(HANDLE hProcess)
          goto cleanup;
      }

!     /* Get the SID for the current user. We need to add this to the ACL. */
!     if (!GetUserSid(&psidUser, hToken))
      {
!         log_error("could not get user SID: %lu", GetLastError());
          goto cleanup;
      }

      /* Figure out the size of the new ACL */
!     dwNewAclSize = asi.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidUser) -sizeof(DWORD);

      /* Allocate the ACL buffer & initialize it */
      pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
--- 744,762 ----
          goto cleanup;
      }

!     /*
!      * Get the user token for the current user, which provides us with the
!      * SID that is needed for creating the ACL.
!      */
!     if (!GetTokenUser(hToken, &pTokenUser))
      {
!         log_error("could not get user token: %lu", GetLastError());
          goto cleanup;
      }

      /* Figure out the size of the new ACL */
!     dwNewAclSize = asi.AclBytesInUse + sizeof(ACCESS_ALLOWED_ACE) +
!         GetLengthSid(pTokenUser->User.Sid) -sizeof(DWORD);

      /* Allocate the ACL buffer & initialize it */
      pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
***************
*** 785,791 **** AddUserToDacl(HANDLE hProcess)
      }

      /* Add the new ACE for the current user */
!     if (!AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, psidUser))
      {
          log_error("could not add access allowed ACE: %lu", GetLastError());
          goto cleanup;
--- 789,795 ----
      }

      /* Add the new ACE for the current user */
!     if (!AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL, pTokenUser->User.Sid))
      {
          log_error("could not add access allowed ACE: %lu", GetLastError());
          goto cleanup;
***************
*** 803,810 **** AddUserToDacl(HANDLE hProcess)
      ret = TRUE;

  cleanup:
!     if (psidUser)
!         FreeSid(psidUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);
--- 807,814 ----
      ret = TRUE;

  cleanup:
!     if (pTokenUser)
!         LocalFree((HLOCAL) pTokenUser);

      if (pacl)
          LocalFree((HLOCAL) pacl);
***************
*** 819,846 **** cleanup:
  }

  /*
!  * GetUserSid*PSID *ppSidUser, HANDLE hToken)
   *
!  * Get the SID for the current user
   */
  static BOOL
! GetUserSid(PSID *ppSidUser, HANDLE hToken)
  {
      DWORD        dwLength;
-     PTOKEN_USER pTokenUser = NULL;


      if (!GetTokenInformation(hToken,
                               TokenUser,
!                              pTokenUser,
                               0,
                               &dwLength))
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             pTokenUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);

!             if (pTokenUser == NULL)
              {
                  log_error("could not allocate %lu bytes of memory", dwLength);
                  return FALSE;
--- 823,853 ----
  }

  /*
!  * GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
!  *
!  * Get the users token information from a process token.
   *
!  * The caller of this function is responsible for calling LocalFree() on the
!  * returned TOKEN_USER memory.
   */
  static BOOL
! GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
  {
      DWORD        dwLength;

+     *ppTokenUser = NULL;

      if (!GetTokenInformation(hToken,
                               TokenUser,
!                              NULL,
                               0,
                               &dwLength))
      {
          if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
          {
!             *ppTokenUser = (PTOKEN_USER) LocalAlloc(LPTR, dwLength);

!             if (*ppTokenUser == NULL)
              {
                  log_error("could not allocate %lu bytes of memory", dwLength);
                  return FALSE;
***************
*** 855,872 **** GetUserSid(PSID *ppSidUser, HANDLE hToken)

      if (!GetTokenInformation(hToken,
                               TokenUser,
!                              pTokenUser,
                               dwLength,
                               &dwLength))
      {
!         HeapFree(GetProcessHeap(), 0, pTokenUser);
!         pTokenUser = NULL;

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

!     *ppSidUser = pTokenUser->User.Sid;
      return TRUE;
  }

--- 862,879 ----

      if (!GetTokenInformation(hToken,
                               TokenUser,
!                              *ppTokenUser,
                               dwLength,
                               &dwLength))
      {
!         LocalFree(*ppTokenUser);
!         *ppTokenUser = NULL;

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

!     /* Memory in *ppTokenUser is LocalFree():d by the caller */
      return TRUE;
  }


Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
>
> Attached is a mix of our two patches. How does that look to you?
>

looks good.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Magnus Hagander
Дата:
Andrew Chernow wrote:
>>
>> Attached is a mix of our two patches. How does that look to you?
>>
>
> looks good.

The next obvious question is, is this something we care to backpatch (at
this point, 8.4 is considered backpatch from that perspective), or
should we hold for 8.5?

The only issue now is a small leak of memory in a function that is only
called a fixed (and very small) number of times per process. Given this,
I'm inclined to say we should put it on hold for 8.5. Thoughts?


--
 Magnus Hagander
 Self: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Andrew Chernow
Дата:
> The only issue now is a small leak of memory in a function that is only
> called a fixed (and very small) number of times per process. Given this,
> I'm inclined to say we should put it on hold for 8.5. Thoughts?
>
>

Doesn't sound urgent to me.  If it were my decision, I'd punt it to 8.5.

Hard to keep that win32 acl stuff leak free.  There is always a cleanup
goto because you need 6 billion objects to answer any question :o

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Magnus Hagander
Дата:
Andrew Chernow wrote:
>
>> The only issue now is a small leak of memory in a function that is only
>> called a fixed (and very small) number of times per process. Given this,
>> I'm inclined to say we should put it on hold for 8.5. Thoughts?
>>
>>
>
> Doesn't sound urgent to me.  If it were my decision, I'd punt it to 8.5.

Ok. Agreed then - added to the commitfest page.


> Hard to keep that win32 acl stuff leak free.  There is always a cleanup
> goto because you need 6 billion objects to answer any question :o

Yeah, true.


--
 Magnus Hagander
 Self: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/

Re: GetTokenInformation() and FreeSid() at port/exec.c

От
Magnus Hagander
Дата:
On Wed, Jun 24, 2009 at 16:01, Magnus Hagander<magnus@hagander.net> wrote:
> Andrew Chernow wrote:
>>
>>> The only issue now is a small leak of memory in a function that is only
>>> called a fixed (and very small) number of times per process. Given this,
>>> I'm inclined to say we should put it on hold for 8.5. Thoughts?
>>>
>>>
>>
>> Doesn't sound urgent to me. =A0If it were my decision, I'd punt it to 8.=
5.
>
> Ok. Agreed then - added to the commitfest page.

Applied to HEAD.


--=20
 Magnus Hagander
 Self: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/