Skip to content

The friendly wrapper for GetNamedSecurityInfo() should return ppSecurityDescriptor as a LocalFreeSafeHandle. #2168

@mjr4077au

Description

@mjr4077au

Actual behavior

The docs for this say: "A pointer to a variable that receives a pointer to the security descriptor of the object. When you have finished using the pointer, free the returned buffer by calling the LocalFree function.".

Expected behavior

That the friendly overload for this (and any) function returning a value that requires freeing/releasing of resources be wrapped in the appropriate SafeHandle type, especially if CsWin32 already provides the SafeHandle type.

Repro steps

  1. NativeMethods.txt content:
GetNamedSecurityInfo
  1. NativeMethods.json content (if present): N/A

  2. Any of your own code that should be shared?

internal unsafe static WIN32_ERROR GetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, out SafeNoReleaseHandle? ppsidOwner, out SafeNoReleaseHandle? ppsidGroup, out LocalFreeSafeHandle? ppDacl, out LocalFreeSafeHandle? ppSacl, out LocalFreeSafeHandle ppSecurityDescriptor)
{
    fixed (char* pObjectNameLocal = pObjectName)
    {
        PSID psidOwner = default, pSidGroup = default; ACL* pDacl = null, pSacl = null; PSECURITY_DESCRIPTOR pSecurityDescriptor = default;
        var res = PInvoke.GetNamedSecurityInfo(pObjectNameLocal, ObjectType, SecurityInfo, &psidOwner, &pSidGroup, &pDacl, &pSacl, &pSecurityDescriptor);
        if (res != WIN32_ERROR.ERROR_SUCCESS)
        {
            throw ExceptionUtilities.GetExceptionForLastWin32Error(res);
        }
        if (pSecurityDescriptor == default)
        {
            throw new InvalidOperationException("Failed to retrieve security descriptor.");
        }
        ppsidOwner = psidOwner != default ? new((IntPtr)psidOwner.Value) : null;
        ppsidGroup = pSidGroup != default ? new((IntPtr)pSidGroup.Value) : null;
        ppDacl = pDacl is not null ? new((IntPtr)pDacl, false) : null;
        ppSacl = pSacl is not null ? new((IntPtr)pSacl, false) : null;
        ppSecurityDescriptor = new((IntPtr)pSecurityDescriptor, true);
        return res;
    }
}

Context

  • CsWin32 version: 0.3.253
  • Win32Metadata version (if explicitly set by project): N/A
  • Target Framework: net472
  • LangVersion (if explicitly set by project): N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions