Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNOW-1156046] fix toctou vulnerability in EasyLogginConfig #925

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

sfc-gh-erojaslizano
Copy link
Contributor

@sfc-gh-erojaslizano sfc-gh-erojaslizano commented Apr 19, 2024

Description

Invert the order, first read the file and then check the permissions.

Checklist

  • Code compiles correctly
  • Code is formatted according to Coding Conventions
  • Created tests which fail without the change (if possible)
  • All tests passing (dotnet test)
  • Extended the README / documentation, if necessary
  • Provide JIRA issue id (if possible) or GitHub issue id in PR name

@sfc-gh-erojaslizano sfc-gh-erojaslizano force-pushed the SNOW-1156046-Fix-Toctou-race-condition branch from fa7df40 to cc5e12c Compare April 19, 2024 16:02
{
using (StreamReader reader = new StreamReader(fileStream))
{
CheckIfValidPermissions(filePath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it's still prone to race condition because the filepath is used. First, you open the FileStream and then (milliseconds later) you make the check calling _unixOperations.CheckFileHasAnyOfPermissions that opens the file again. It is possible that between these two opens the file under the path is changed: imagine the path to check is ./my.conf, which initially is a symbolic link to /tmp/bad.conf, and milliseconds later it is repointed to /home/user/valid.conf by the bad actor to make the check pass.

One way to do this securely is to open the file once and from this moment operate only on its handle. You can obtain the handle to the file using FileStream.SafeFileHandle and pass it to File.GetUnixFileMode(SafeFileHandle) to obtain permissions, and check them in a way that is not prone to race conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sfc-gh-kkatowiczkowalewski I made the change to use the SafeFileHandle but it only works on net8.0 so I make some conditional compilation to work around that.
I'm still working on the updating the failing test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 72.41379% with 8 lines in your changes missing coverage. Please review.

Project coverage is 85.13%. Comparing base (982a1e4) to head (58c4cd3).

Files Patch % Lines
...lake.Data/Configuration/EasyLoggingConfigParser.cs 66.66% 5 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #925      +/-   ##
==========================================
- Coverage   85.55%   85.13%   -0.42%     
==========================================
  Files         106      106              
  Lines       10777    10782       +5     
  Branches     1032     1033       +1     
==========================================
- Hits         9220     9179      -41     
- Misses       1307     1352      +45     
- Partials      250      251       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sfc-gh-erojaslizano sfc-gh-erojaslizano force-pushed the SNOW-1156046-Fix-Toctou-race-condition branch from 076b56c to 4b15d4d Compare May 15, 2024 16:50
@sfc-gh-erojaslizano
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@sfc-gh-erojaslizano sfc-gh-erojaslizano marked this pull request as ready for review May 15, 2024 22:54
@sfc-gh-erojaslizano sfc-gh-erojaslizano requested a review from a team as a code owner May 15, 2024 22:54
}

#if NET8_0_OR_GREATER
var unixFileMode = File.GetUnixFileMode(fileStream.SafeFileHandle);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we exclude Mono.Unix from dependencies for .NET 8? @sfc-gh-knozderko wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but there are many other places that uses de Mono.Unix. If this change is needed I prefer to do it in a a new task instead
Screenshot 2024-05-16 at 10 49 05 AM

var hasPermissions = !(((UnixFileMode.GroupWrite | UnixFileMode.OtherWrite) & unixFileMode) != 0);
#else
var entitlements = FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite;
var hasPermissions = !_unixOperations.CheckFileHasAnyOfPermissions(filePath, entitlements);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'hasPermissions' name suggests that the file has permissions specified. Maybe remove negations and if hasPermissions then throw an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the variable from 'haspermission' to a more clear 'hasGroupOrOtherWritePermissions' and inverted the if statement to throw the exception if true.
this was done in 9a2925b

@sfc-gh-erojaslizano sfc-gh-erojaslizano force-pushed the SNOW-1156046-Fix-Toctou-race-condition branch from 9a2925b to 29fa29f Compare July 10, 2024 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants