Skip to content

Commit

Permalink
Service exception fix + README update
Browse files Browse the repository at this point in the history
- Fix a DirectoryNotFoundException if the log directory doesn't exist

- Update the WinRing0 vulnerability FAQ in the README
  • Loading branch information
Sparronator9999 committed Jul 24, 2024
1 parent 48860ec commit 4212897
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
84 changes: 43 additions & 41 deletions MSIFanControl.Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ private string LogString(string text, LogLevel level, bool showDate) =>
/// <summary>
/// The path to which the log file will be written.
/// </summary>
public string LogPath = Path.Combine(
public string LogDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Sparronator9999", "MSI Fan Control", "Logs",
AppDomain.CurrentDomain.FriendlyName);
"Sparronator9999", "MSI Fan Control", "Logs");

private string LogPath => Path.Combine(LogDir, AppDomain.CurrentDomain.FriendlyName);

//public string LogPath = Process.GetCurrentProcess().MainModule.FileName;

/// <summary>
Expand Down Expand Up @@ -317,56 +319,56 @@ private void WriteConsole(string message, LogLevel level)
/// </summary>
private void InitLogFile()
{
void ArchiveOldLogs()
// Rename old log files, and delete the oldest file if
// there's too many log files
for (int i = MaxArchivedLogs; i >= 0; i--)
{
// Rename old log files, and delete the oldest file if
// there's too many log files
for (int i = MaxArchivedLogs; i >= 0; i--)
try
{
try
if (i == MaxArchivedLogs)
{
File.Delete($"{LogPath}.{i}.log.gz");
}
else
{
if (i == MaxArchivedLogs)
{
File.Delete($"{LogPath}.{i}.log.gz");
}
else
{
File.Move($"{LogPath}.{i}.log.gz", $"{LogPath}.{i + 1}.log.gz");
}
File.Move($"{LogPath}.{i}.log.gz", $"{LogPath}.{i + 1}.log.gz");
}
catch (FileNotFoundException) { }
catch (DirectoryNotFoundException) { }
}
catch (FileNotFoundException) { }
catch (DirectoryNotFoundException) { }
}

try
{
FileInfo fi = new FileInfo($"{LogPath}.log");
try
{
FileInfo fi = new FileInfo($"{LogPath}.log");

// Set up file streams
FileStream original = fi.OpenRead();
FileStream compressed = File.Create($"{LogPath}.{1}.log.gz");
GZipStream gzStream = new GZipStream(compressed, CompressionLevel.Optimal);
// Set up file streams
FileStream original = fi.OpenRead();
FileStream compressed = File.Create($"{LogPath}.{1}.log.gz");
GZipStream gzStream = new GZipStream(compressed, CompressionLevel.Optimal);

// Compress the file
original.CopyTo(gzStream);
// Compress the file
original.CopyTo(gzStream);

// Close file streams
gzStream.Close();
compressed.Close();
original.Close();
// Close file streams
gzStream.Close();
compressed.Close();
original.Close();

// Delete the unarchived copy of the log
fi.Delete();
}
catch (FileNotFoundException)
{
// Log files probably don't exist yet,
// return without renaming old logs
return;
}
// Delete the unarchived copy of the log
fi.Delete();
}
catch (FileNotFoundException)
{
// Log files probably don't exist yet,
// return without renaming old logs
return;
}

ArchiveOldLogs();
// why can't windows just create the
// directory structure for me >:(
Directory.CreateDirectory(LogDir);

LogWriter = new StreamWriter($"{LogPath}.log")
{
AutoFlush = true
Expand Down
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ Converting to .NET *should* be simple for most components, except for the fan co

### Doesn't WinRing0 have security issues?

[Yes](https://voidsec.com/crucial-mod-utility-lpe-cve-2021-41285/), that is correct.

I would use the [updated fork](https://github.com/GermanAizek/WinRing0), but they don't provide
binary releases due to Microsoft's driver signing requirements, and I'm too smooth-brained
to write my own EC access kernel driver (plus, I'd have to get it signed, which costs my time
and money).
[Yes](https://voidsec.com/crucial-mod-utility-lpe-cve-2021-41285/), however MSI Fan Control
installs the driver such that only programs with administrator privileges can access the driver
functions (something that should have been done in the first place by the driver itself),
largely mitigating this vulnerability.

However, if MSI Fan Control finds the driver already installed, it will use that
(potentially vulnerable) version instead. If it was installed with, e.g.
[LibreHardwareMonitor](https://github.com/LibreHardwareMonitor/LibreHardwareMonitor),
you should be fine, as they implement the same fix.

The [updated fork of WinRing0](https://github.com/GermanAizek/WinRing0) updates the driver itself
(`WinRing0.sys`) to apply this fix, however binary releases of the driver aren't provided due to
Microsoft's driver signing requirements, and I'm too smooth-brained to write my own EC access
kernel driver (but apparently not an entire fan control utility from scratch, including the
WinRing0 interface code...), and I'd have to get it signed anyways.

Please read the [disclaimer](#disclaimers), especially the bold text, if you haven't already.

Expand Down

0 comments on commit 4212897

Please sign in to comment.