Skip to content

Commit

Permalink
[SCSI MODE] Fix handling of corrupt/invalid MODE SENSE (6) block desc…
Browse files Browse the repository at this point in the history
…riptors. Fixes #842
  • Loading branch information
claunia committed Apr 30, 2024
1 parent 06d6a7c commit 0d140fa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
8 changes: 5 additions & 3 deletions Aaru.Core/Devices/Dumping/CompactDisc/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ void RetryCdUserData(ExtentsULong audioExtents, uint blockSize, DumpHardware cur
sense = _dev.ModeSense6(out cmdBuf, out _, false, ScsiModeSensePageControl.Current, 0x01, _dev.Timeout,
out _);

if(sense)
Modes.DecodedMode? dcMode6 = null;
if(!sense)
dcMode6 = Modes.DecodeMode6(cmdBuf, PeripheralDeviceTypes.MultiMediaDevice);

if(sense || dcMode6 is null)
{
sense = _dev.ModeSense10(out cmdBuf, out _, false, ScsiModeSensePageControl.Current, 0x01, _dev.Timeout,
out _);
Expand All @@ -133,8 +137,6 @@ void RetryCdUserData(ExtentsULong audioExtents, uint blockSize, DumpHardware cur
}
else
{
Modes.DecodedMode? dcMode6 = Modes.DecodeMode6(cmdBuf, PeripheralDeviceTypes.MultiMediaDevice);

if(dcMode6?.Pages != null)
{
foreach(Modes.ModePage modePage in dcMode6.Value.Pages.Where(modePage => modePage is
Expand Down
8 changes: 5 additions & 3 deletions Aaru.Core/Devices/Dumping/PlayStationPortable/MemoryStick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ void DumpMs()
sense = _dev.ModeSense6(out readBuffer, out _, false, ScsiModeSensePageControl.Current, 0x01,
_dev.Timeout, out _);

if(sense)
Modes.DecodedMode? dcMode6 = null;
if(!sense)
dcMode6 = Modes.DecodeMode6(readBuffer, _dev.ScsiType);

if(sense || dcMode6 is null)
{
sense = _dev.ModeSense10(out readBuffer, out _, false, ScsiModeSensePageControl.Current, 0x01,
_dev.Timeout, out _);
Expand All @@ -392,8 +396,6 @@ void DumpMs()
}
else
{
Modes.DecodedMode? dcMode6 = Modes.DecodeMode6(readBuffer, _dev.ScsiType);

if(dcMode6.HasValue)
{
foreach(Modes.ModePage modePage in dcMode6.Value.Pages.Where(modePage =>
Expand Down
8 changes: 5 additions & 3 deletions Aaru.Core/Devices/Dumping/Sbc/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ void RetrySbcData(Reader scsiReader, DumpHardware currentTry, ExtentsULong
sense = _dev.ModeSense6(out buffer, out _, false, ScsiModeSensePageControl.Current, 0x01, _dev.Timeout,
out _);

if(sense)
Modes.DecodedMode? dcMode6 = null;
if(!sense)
dcMode6 = Modes.DecodeMode6(buffer, _dev.ScsiType);

if(sense || dcMode6 is null)
{
sense = _dev.ModeSense10(out buffer, out _, false, ScsiModeSensePageControl.Current, 0x01, _dev.Timeout,
out _);
Expand All @@ -96,8 +100,6 @@ void RetrySbcData(Reader scsiReader, DumpHardware currentTry, ExtentsULong
}
else
{
Modes.DecodedMode? dcMode6 = Modes.DecodeMode6(buffer, _dev.ScsiType);

if(dcMode6?.Pages != null)
{
foreach(Modes.ModePage modePage in dcMode6.Value.Pages.Where(modePage => modePage is
Expand Down
11 changes: 5 additions & 6 deletions Aaru.Core/Devices/Dumping/XGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,6 @@ void Xgd(Dictionary<MediaTagType, byte[]> mediaTags, MediaType dskType)
Humanize()));

UpdateStatus?.Invoke(string.Format(Localization.Core.Average_write_speed_0,
ByteSize.FromBytes(blockSize * (blocks + 1)).
Per(imageWriteDuration.Seconds()).
Humanize()));

_dumpLog.WriteLine(string.Format(Localization.Core.Dump_finished_in_0,
_dumpStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));

Check failure on line 976 in Aaru.Core/Devices/Dumping/XGD.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 976 in Aaru.Core/Devices/Dumping/XGD.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 976 in Aaru.Core/Devices/Dumping/XGD.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

) expected

Check failure on line 976 in Aaru.Core/Devices/Dumping/XGD.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

) expected

Check failure on line 976 in Aaru.Core/Devices/Dumping/XGD.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

) expected
Expand Down Expand Up @@ -1071,7 +1068,11 @@ void Xgd(Dictionary<MediaTagType, byte[]> mediaTags, MediaType dskType)
sense = _dev.ModeSense6(out readBuffer, out _, false, ScsiModeSensePageControl.Current, 0x01,
_dev.Timeout, out _);

if(sense)
Modes.DecodedMode? dcMode6 = null;
if(!sense)
dcMode6 = Modes.DecodeMode6(readBuffer, PeripheralDeviceTypes.MultiMediaDevice);

if(sense || dcMode6 is null)
{
sense = _dev.ModeSense10(out readBuffer, out _, false, ScsiModeSensePageControl.Current, 0x01,
_dev.Timeout, out _);
Expand All @@ -1091,8 +1092,6 @@ void Xgd(Dictionary<MediaTagType, byte[]> mediaTags, MediaType dskType)
}
else
{
Modes.DecodedMode? dcMode6 = Modes.DecodeMode6(readBuffer, PeripheralDeviceTypes.MultiMediaDevice);

if(dcMode6.HasValue)
{
foreach(Modes.ModePage modePage in dcMode6.Value.Pages.Where(modePage =>
Expand Down
5 changes: 5 additions & 0 deletions Aaru.Decoders/SCSI/Modes/Mode6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static partial class Modes

if(modeResponse[3] > 0)
{
// An incorrect size field, we cannot know if the following bytes are really the pages (probably not),
// so consider the MODE SENSE(6) response as invalid
if(modeResponse[3] + 4 > modeResponse.Length)
return null;

header.BlockDescriptors = new BlockDescriptor[modeResponse[3] / 8];

for(var i = 0; i < header.BlockDescriptors.Length; i++)
Expand Down
14 changes: 7 additions & 7 deletions Aaru.Images/ZZZRawImage/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,10 @@ public ErrorNumber Open(IFilter imageFilter)

Modes.DecodedMode? decMode = null;

if(_mediaTags.TryGetValue(MediaTagType.SCSI_MODESENSE_6, out byte[] mode6))
decMode = Modes.DecodeMode6(mode6, devType);
else if(_mediaTags.TryGetValue(MediaTagType.SCSI_MODESENSE_10, out byte[] mode10))
if(_mediaTags.TryGetValue(MediaTagType.SCSI_MODESENSE_10, out byte[] mode10))
decMode = Modes.DecodeMode10(mode10, devType);
else if(_mediaTags.TryGetValue(MediaTagType.SCSI_MODESENSE_6, out byte[] mode6))
decMode = Modes.DecodeMode6(mode6, devType);

byte mediumType = 0;
byte densityCode = 0;
Expand Down Expand Up @@ -1067,8 +1067,8 @@ public ErrorNumber Open(IFilter imageFilter)
_imageInfo.MediaType = MediaTypeFromDevice.GetFromScsi((byte)devType, _imageInfo.DriveManufacturer,
_imageInfo.DriveModel, mediumType, densityCode,
_imageInfo.Sectors, _imageInfo.SectorSize,
_mediaTags.ContainsKey(MediaTagType.
USB_Descriptors), _rawCompactDisc);
_mediaTags.ContainsKey(MediaTagType
.USB_Descriptors), _rawCompactDisc);
}

if(_imageInfo.MediaType == MediaType.Unknown)
Expand Down Expand Up @@ -1164,8 +1164,8 @@ public ErrorNumber Open(IFilter imageFilter)
var fs = new FileStream(basename + ".metadata.json", FileMode.Open);

AaruMetadata =
(JsonSerializer.Deserialize(fs, typeof(MetadataJson), MetadataJsonContext.Default) as MetadataJson)?
.AaruMetadata;
(JsonSerializer.Deserialize(fs, typeof(MetadataJson), MetadataJsonContext.Default) as MetadataJson)
?.AaruMetadata;

fs.Close();
}
Expand Down

0 comments on commit 0d140fa

Please sign in to comment.