Client: Improved handling of invalid FILETIME

This commit is contained in:
Tal Aloni 2025-04-19 15:20:58 +03:00
parent 2c4e42d956
commit 1172dfb043
6 changed files with 41 additions and 32 deletions

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2014-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -14,6 +13,21 @@ namespace SMBLibrary
{
public static readonly DateTime MinFileTimeValue = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly long MaxFileTimeIntegerValue = DateTime.MaxValue.ToFileTimeUtc();
public static DateTime ReadFileTimeSafe(byte[] buffer, int offset)
{
long span = LittleEndianConverter.ToInt64(buffer, offset);
if (span >= 0 && span <= MaxFileTimeIntegerValue)
{
return DateTime.FromFileTimeUtc(span);
}
else
{
return DateTime.MaxValue;
}
}
public static DateTime ReadFileTime(byte[] buffer, int offset)
{
long span = LittleEndianConverter.ToInt64(buffer, offset);

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -37,10 +36,10 @@ namespace SMBLibrary
public FileBothDirectoryInformation(byte[] buffer, int offset) : base(buffer, offset)
{
CreationTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 8));
LastAccessTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 16));
LastWriteTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 24));
ChangeTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 32));
CreationTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 8);
LastAccessTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 16);
LastWriteTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 24);
ChangeTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 32);
EndOfFile = LittleEndianConverter.ToInt64(buffer, offset + 40);
AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + 48);
FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 56);

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -33,10 +32,10 @@ namespace SMBLibrary
public FileDirectoryInformation(byte[] buffer, int offset) : base(buffer, offset)
{
CreationTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 8));
LastAccessTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 16));
LastWriteTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 24));
ChangeTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 32));
CreationTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 8);
LastAccessTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 16);
LastWriteTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 24);
ChangeTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 32);
EndOfFile = LittleEndianConverter.ToInt64(buffer, offset + 40);
AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + 48);
FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 56);

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -34,10 +33,10 @@ namespace SMBLibrary
public FileFullDirectoryInformation(byte[] buffer, int offset) : base(buffer, offset)
{
CreationTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 8));
LastAccessTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 16));
LastWriteTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 24));
ChangeTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 32));
CreationTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 8);
LastAccessTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 16);
LastWriteTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 24);
ChangeTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 32);
EndOfFile = LittleEndianConverter.ToInt64(buffer, offset + 40);
AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + 48);
FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 56);

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -39,10 +38,10 @@ namespace SMBLibrary
public FileIdBothDirectoryInformation(byte[] buffer, int offset) : base(buffer, offset)
{
CreationTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 8));
LastAccessTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 16));
LastWriteTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 24));
ChangeTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 32));
CreationTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 8);
LastAccessTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 16);
LastWriteTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 24);
ChangeTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 32);
EndOfFile = LittleEndianConverter.ToInt64(buffer, offset + 40);
AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + 48);
FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 56);

View file

@ -1,11 +1,10 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2025 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary
@ -36,10 +35,10 @@ namespace SMBLibrary
public FileIdFullDirectoryInformation(byte[] buffer, int offset) : base(buffer, offset)
{
CreationTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 8));
LastAccessTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 16));
LastWriteTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 24));
ChangeTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + 32));
CreationTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 8);
LastAccessTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 16);
LastWriteTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 24);
ChangeTime = FileTimeHelper.ReadFileTimeSafe(buffer, offset + 32);
EndOfFile = LittleEndianConverter.ToInt64(buffer, offset + 40);
AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + 48);
FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 56);