From 0ab1c42eeae48530853ec22a8790a492c254006d Mon Sep 17 00:00:00 2001 From: Mary Date: Thu, 3 Dec 2020 19:19:10 +0100 Subject: [PATCH] Make sure to not leak copy handles passed in request (#1772) * Make sure to not leak copy handles passed in request Following last gdkchan's PR this make sure to close copy handles that are passed by guest when it should. * fix comment copy pasta --- .../Audio/AudioOutManager/IAudioOut.cs | 24 +++++++++++-------- .../HOS/Services/Audio/IAudioOutManager.cs | 2 +- Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs | 7 +++--- .../Services/Nv/NvDrvServices/NvDeviceFile.cs | 5 +++- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs index 86e5566aac..c941cf4fb0 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs @@ -1,6 +1,7 @@ using Ryujinx.Audio; using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Ipc; +using Ryujinx.HLE.HOS.Kernel; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Threading; using System; @@ -10,18 +11,20 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager { class IAudioOut : IpcService, IDisposable { - private readonly IAalOutput _audioOut; - private readonly KEvent _releaseEvent; - private int _releaseEventHandle; - private readonly int _track; - private readonly int _clientHandle; + private readonly KernelContext _kernelContext; + private readonly IAalOutput _audioOut; + private readonly KEvent _releaseEvent; + private int _releaseEventHandle; + private readonly int _track; + private readonly int _clientHandle; - public IAudioOut(IAalOutput audioOut, KEvent releaseEvent, int track, int clientHandle) + public IAudioOut(KernelContext kernelContext, IAalOutput audioOut, KEvent releaseEvent, int track, int clientHandle) { - _audioOut = audioOut; - _releaseEvent = releaseEvent; - _track = track; - _clientHandle = clientHandle; + _kernelContext = kernelContext; + _audioOut = audioOut; + _releaseEvent = releaseEvent; + _track = track; + _clientHandle = clientHandle; } [Command(0)] @@ -217,6 +220,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager { if (disposing) { + _kernelContext.Syscall.CloseHandle(_clientHandle); _audioOut.CloseTrack(_track); } } diff --git a/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs b/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs index 908bf14e69..6204a7beda 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs @@ -134,7 +134,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio int track = audioOut.OpenTrack(sampleRate, channels, callback); - MakeObject(context, new IAudioOut(audioOut, releaseEvent, track, context.Request.HandleDesc.ToCopy[0])); + MakeObject(context, new IAudioOut(context.Device.System.KernelContext, audioOut, releaseEvent, track, context.Request.HandleDesc.ToCopy[0])); context.ResponseData.Write(sampleRate); context.ResponseData.Write(channels); diff --git a/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs b/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs index 52fe3ea1f2..8b1deb97f3 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs @@ -329,6 +329,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv context.ResponseData.Write((uint)NvResult.Success); + // Close transfer memory immediately as we don't use it. + context.Device.System.KernelContext.Syscall.CloseHandle(transferMemHandle); + return ResultCode.Success; } @@ -384,9 +387,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv if (errorCode == NvResult.Success) { - KSharedMemory sharedMemory = context.Process.HandleTable.GetObject(sharedMemoryHandle); - - errorCode = ConvertInternalErrorCode(deviceFile.MapSharedMemory(sharedMemory, argument)); + errorCode = ConvertInternalErrorCode(deviceFile.MapSharedMemory(sharedMemoryHandle, argument)); } } diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvDeviceFile.cs b/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvDeviceFile.cs index 34ff4ccb43..f143f6d1c3 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvDeviceFile.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvDeviceFile.cs @@ -28,8 +28,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices return NvInternalResult.NotImplemented; } - public virtual NvInternalResult MapSharedMemory(KSharedMemory sharedMemory, uint argument) + public virtual NvInternalResult MapSharedMemory(int sharedMemoryHandle, uint argument) { + // Close shared memory immediately as we don't use it. + Context.Device.System.KernelContext.Syscall.CloseHandle(sharedMemoryHandle); + return NvInternalResult.NotImplemented; }