Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in error path #1

Open
JohnLaTwC opened this issue Nov 22, 2020 · 0 comments
Open

Memory leak in error path #1

JohnLaTwC opened this issue Nov 22, 2020 · 0 comments

Comments

@JohnLaTwC
Copy link

Issue 1

Memory leak due to missing call to Marshal.FreeHGlobal for oa.objectName in failure path.

You may want to put the code that releases resources in a finally block to close them if an exception is raised.

        public static void DRegHideManualMap(String hive = "HKCU", String subKey = @"\SOFTWARE", String keyName = "", String keyValue = "", bool hiddenKey = false, bool deleteKey = false)
        {
            DInvoke.Data.PE.PE_MANUAL_MAP mappedDLL = new DInvoke.Data.PE.PE_MANUAL_MAP();
            mappedDLL = DInvoke.ManualMap.Map.MapModuleToMemory(@"C:\Windows\System32\ntdll.dll");

            try
            {
...
                IntPtr oaObjectName = Marshal.AllocHGlobal(Marshal.SizeOf(UC_RegKey));
! Alloc'd before the call to `NtOpenKey`
                Marshal.StructureToPtr(UC_RegKey, oaObjectName, true);
                oa.Length = Marshal.SizeOf(oa);
                oa.Attributes = (uint)STRUCTS.OBJ_ATTRIBUTES.CASE_INSENSITIVE;
                oa.objectName = oaObjectName;
  ...
                retValue = (DInvoke.Data.Native.NTSTATUS)DInvoke.DynamicInvoke.Generic.CallMappedDLLModuleExport(mappedDLL.PEINFO, mappedDLL.ModuleBase, "NtOpenKey", typeof(DELEGATES.NtOpenKey), ntOpenKeyParams, false);
                keyHandle = (IntPtr)ntOpenKeyParams[0];

                if (retValue == DInvoke.Data.Native.NTSTATUS.Success)
                {
...

                    Marshal.FreeHGlobal(oa.objectName);
! `FreeHGlobal` is only called in the success path.  If `NtOpenKey` fails, this memory is leaked.
                    object[] ntCloseParams = { keyHandle };

                    retValue = (DInvoke.Data.Native.NTSTATUS)DInvoke.DynamicInvoke.Generic.CallMappedDLLModuleExport(mappedDLL.PEINFO, mappedDLL.ModuleBase, "NtClose", typeof(DELEGATES.NtClose), ntCloseParams, false);
                }
                else { Console.WriteLine("Regkey not found"); }

https://github.com/NVISO-BE/DInvisibleRegistry/blob/99f637a5a813cadffd859ba98d04f43daad8d270/Program.cs#L219

The same issue exists in DRegHideWithDeception() and DRegHide()

https://github.com/NVISO-BE/DInvisibleRegistry/blob/99f637a5a813cadffd859ba98d04f43daad8d270/Program.cs#L325
https://github.com/NVISO-BE/DInvisibleRegistry/blob/99f637a5a813cadffd859ba98d04f43daad8d270/Program.cs#L110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant