Fix a permissions bug when creating system save data

This commit is contained in:
Alex Barney 2021-06-29 13:00:32 -07:00
parent 496858c7a7
commit 20dcbf8664
2 changed files with 48 additions and 6 deletions

View file

@ -965,22 +965,22 @@ namespace LibHac.FsSrv
if (!IsStaticSaveDataIdValueRange(attribute.StaticSaveDataId)) if (!IsStaticSaveDataIdValueRange(attribute.StaticSaveDataId))
return ResultFs.InvalidArgument.Log(); return ResultFs.InvalidArgument.Log();
SaveDataCreationInfo newCreationInfo = creationInfo; SaveDataCreationInfo tempCreationInfo = creationInfo;
if (newCreationInfo.OwnerId == 0) if (tempCreationInfo.OwnerId == 0)
{ {
newCreationInfo.OwnerId = programInfo.ProgramIdValue; tempCreationInfo.OwnerId = programInfo.ProgramIdValue;
} }
rc = SaveDataAccessibilityChecker.CheckCreate(in attribute, in creationInfo, programInfo, rc = SaveDataAccessibilityChecker.CheckCreate(in attribute, in tempCreationInfo, programInfo,
programInfo.ProgramId); programInfo.ProgramId);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
// Static system saves don't usually have meta files // Static system saves don't usually have meta files
SaveDataMetaInfo dataMetaInfo = default; SaveDataMetaInfo metaInfo = default;
Optional<HashSalt> hashSalt = default; Optional<HashSalt> hashSalt = default;
return CreateSaveDataFileSystemCore(in attribute, in creationInfo, in dataMetaInfo, in hashSalt); return CreateSaveDataFileSystemCore(in attribute, in tempCreationInfo, in metaInfo, in hashSalt);
} }
public Result ExtendSaveDataFileSystem(SaveDataSpaceId spaceId, ulong saveDataId, long dataSize, public Result ExtendSaveDataFileSystem(SaveDataSpaceId spaceId, ulong saveDataId, long dataSize,

View file

@ -141,6 +141,48 @@ namespace LibHac.Tests.Fs.FileSystemClientTests.ShimTests
Assert.Equal(SaveDataState.Normal, info[0].State); Assert.Equal(SaveDataState.Normal, info[0].State);
} }
[Theory]
[InlineData(AccessControlBits.Bits.SystemSaveData)]
[InlineData(AccessControlBits.Bits.None)]
public void CreateSystemSaveData_HasBuiltInSystemPermission_SaveIsCreatedInSystem(AccessControlBits.Bits permissions)
{
ulong saveId = 0x8000000001234000;
Horizon hos = FileSystemServerFactory.CreateHorizonServer();
var mainProgramId = new ProgramId(0x123456);
HorizonClient client = hos.CreateHorizonClient(new ProgramLocation(mainProgramId, StorageId.BuiltInSystem),
permissions);
HorizonClient privilegedClient = hos.CreatePrivilegedHorizonClient();
// Create the save
if (permissions.HasFlag(AccessControlBits.Bits.SystemSaveData))
{
Assert.Success(client.Fs.CreateSystemSaveData(saveId, 0x1000, 0x1000, SaveDataFlags.None));
}
else
{
// Creation should fail if we don't have the right permissions.
Assert.Failure(client.Fs.CreateSystemSaveData(saveId, 0x1000, 0x1000, SaveDataFlags.None));
return;
}
// Make sure it was placed in the System save space with the right info.
Assert.Success(privilegedClient.Fs.OpenSaveDataIterator(out SaveDataIterator iterator, SaveDataSpaceId.System));
var info = new SaveDataInfo[2];
Assert.Success(iterator.ReadSaveDataInfo(out long entriesRead, info));
Assert.Equal(1, entriesRead);
Assert.Equal(SaveDataType.System, info[0].Type);
Assert.Equal(SaveDataSpaceId.System, info[0].SpaceId);
Assert.Equal(saveId, info[0].StaticSaveDataId);
Assert.Equal(saveId, info[0].SaveDataId);
Assert.Equal(SaveDataState.Normal, info[0].State);
}
[Fact] [Fact]
public void CreateSaveData_DoesNotExist_SaveIsCreated() public void CreateSaveData_DoesNotExist_SaveIsCreated()
{ {