Update to LibHac 0.13.1 (#2328)

Update the LibHac dependency to version 0.13.1. This brings a ton of improvements and changes such as:
- Refactor `FsSrv` to match the official refactoring done in FS.
- Change how the `Horizon` and `HorizonClient` classes are handled. Each client created represents a different process with its own process ID and client state.
- Add FS access control to handle permissions for FS service method calls.
- Add FS program registry to keep track of the program ID, location and permissions of each process.
- Add FS program index map info manager to track the program IDs and indexes of multi-application programs.
- Add all FS IPC interfaces.
- Rewrite `Fs.Fsa` code to be more accurate.
- Rewrite a lot of `FsSrv` code to be more accurate.
- Extend directory save data to store `SaveDataExtraData`
- Extend directory save data to lock the save directory to allow only one accessor at a time.
- Improve waiting and retrying when encountering access issues in `LocalFileSystem` and `DirectorySaveDataFileSystem`.
- More `IFileSystemProxy` methods should work now.
- Probably a bunch more stuff.

On the Ryujinx side:
- Forward most `IFileSystemProxy` methods to LibHac.
- Register programs and program index map info when launching an application.
- Remove hacks and workarounds for missing LibHac functionality.
- Recreate missing save data extra data found on emulator startup.
- Create system save data that wasn't indexed correctly on an older LibHac version.

`FsSrv` now enforces access control for each process. When a process tries to open a save data file system, FS reads the save's extra data to determine who the save owner is and if the caller has permission to open the save data. Previously-created save data did not have extra data created when the save was created.
With access control checks in place, this means that processes with no permissions (most games) wouldn't be able to access their own save data. The extra data can be partially created from data in the save data indexer, which should be enough for access control purposes.
This commit is contained in:
Alex Barney 2021-07-13 01:19:28 -07:00 committed by GitHub
parent 04dce402ac
commit 19afb3209c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 1795 additions and 574 deletions

View file

@ -1,4 +1,7 @@
using ARMeilleure.Translation.PTC;
using LibHac.Loader;
using LibHac.Ncm;
using LibHac.Util;
using Ryujinx.Common;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel;
@ -6,12 +9,25 @@ using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.HLE.Loaders.Npdm;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Npdm = LibHac.Loader.Npdm;
namespace Ryujinx.HLE.HOS
{
struct ProgramInfo
{
public string Name;
public ulong ProgramId;
public ProgramInfo(in Npdm npdm)
{
Name = StringUtils.Utf8ZToString(npdm.Meta.Value.ProgramName);
ProgramId = npdm.Aci.Value.ProgramId.Value;
}
}
static class ProgramLoader
{
private const bool AslrEnabled = true;
@ -125,11 +141,21 @@ namespace Ryujinx.HLE.HOS
return true;
}
public static bool LoadNsos(KernelContext context, out ProcessTamperInfo tamperInfo, Npdm metaData, byte[] arguments = null, params IExecutable[] executables)
public static bool LoadNsos(KernelContext context, out ProcessTamperInfo tamperInfo, MetaLoader metaData, ProgramInfo programInfo, byte[] arguments = null, params IExecutable[] executables)
{
LibHac.Result rc = metaData.GetNpdm(out var npdm);
if (rc.IsFailure())
{
tamperInfo = null;
return false;
}
ref readonly var meta = ref npdm.Meta.Value;
ulong argsStart = 0;
uint argsSize = 0;
ulong codeStart = metaData.Is64Bit ? 0x8000000UL : 0x200000UL;
ulong codeStart = (meta.Flags & 1) != 0 ? 0x8000000UL : 0x200000UL;
uint codeSize = 0;
var buildIds = executables.Select(e => (e switch
@ -182,18 +208,20 @@ namespace Ryujinx.HLE.HOS
int codePagesCount = (int)(codeSize / KPageTableBase.PageSize);
int personalMmHeapPagesCount = metaData.PersonalMmHeapSize / KPageTableBase.PageSize;
int personalMmHeapPagesCount = (int)(meta.SystemResourceSize / KPageTableBase.PageSize);
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
metaData.TitleName,
metaData.Version,
metaData.Aci0.TitleId,
programInfo.Name,
(int)meta.Version,
programInfo.ProgramId,
codeStart,
codePagesCount,
(ProcessCreationFlags)metaData.ProcessFlags | ProcessCreationFlags.IsApplication,
(ProcessCreationFlags)meta.Flags | ProcessCreationFlags.IsApplication,
0,
personalMmHeapPagesCount);
context.Device.System.LibHacHorizonManager.InitializeApplicationClient(new ProgramId(programInfo.ProgramId), in npdm);
KernelResult result;
KResourceLimit resourceLimit = new KResourceLimit(context);
@ -217,7 +245,7 @@ namespace Ryujinx.HLE.HOS
KProcess process = new KProcess(context);
MemoryRegion memoryRegion = (MemoryRegion)((metaData.Acid.Flags >> 2) & 0xf);
MemoryRegion memoryRegion = (MemoryRegion)((npdm.Acid.Value.Flags >> 2) & 0xf);
if (memoryRegion > MemoryRegion.NvServices)
{
@ -232,7 +260,7 @@ namespace Ryujinx.HLE.HOS
result = process.Initialize(
creationInfo,
metaData.Aci0.KernelAccessControl.Capabilities,
MemoryMarshal.Cast<byte, int>(npdm.KernelCapabilityData).ToArray(),
resourceLimit,
memoryRegion,
processContextFactory);
@ -262,9 +290,9 @@ namespace Ryujinx.HLE.HOS
}
}
process.DefaultCpuCore = metaData.DefaultCpuId;
process.DefaultCpuCore = meta.DefaultCpuId;
result = process.Start(metaData.MainThreadPriority, (ulong)metaData.MainThreadStackSize);
result = process.Start(meta.MainThreadPriority, meta.MainThreadStackSize);
if (result != KernelResult.Success)
{