Skip to content

Fix GetFileAccessFromRights bitwise logic for compound FileSystemRights flags#122406

Merged
stephentoub merged 4 commits into
mainfrom
copilot/fix-fileinfo-create-access-rights
Jan 13, 2026
Merged

Fix GetFileAccessFromRights bitwise logic for compound FileSystemRights flags#122406
stephentoub merged 4 commits into
mainfrom
copilot/fix-fileinfo-create-access-rights

Conversation

Copilot AI commented Dec 10, 2025

Copy link
Copy Markdown
Contributor

FileInfo.Create() with FileSystemRights.Read incorrectly returned a FileStream with CanWrite = true because the underlying GetFileAccessFromRights method used faulty bitwise logic to check for FullControl and Modify flags.

Root Cause

The method checked if ANY bits overlapped:

if ((rights & FileSystemRights.FullControl) != 0)
    return FileAccess.ReadWrite;

Since FileSystemRights.Read (0x20089) shares bits with FileSystemRights.FullControl (0x1F01FF), this incorrectly mapped ReadReadWrite.

Changes

  • Fixed bitwise comparison to check if ALL bits are present:

    if ((rights & FileSystemRights.FullControl) == FileSystemRights.FullControl)
        return FileAccess.ReadWrite;
  • Added regression test validating that FileStream.CanWrite is false when opened with FileSystemRights.Read

Impact

Rights Before After
Read ReadWrite Read
Write ReadWrite Write
ReadAndExecute ReadWrite Read
Modify ReadWrite ReadWrite
FullControl ReadWrite ReadWrite
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] FileInfo.Create wraps file handle with wrong FileSystemRights.</issue_title>
<issue_description>### Description

Calling FileInfo.Create extension method with FileSystemRights.Read as access rights to create a FileStream.

Reproduction Steps

FileInfo fi = new(path);
FileStream stream = fi.Create(
    FileMode.Open,
    FileSystemRights.Read, // <---- Open with read-only access.
    FileShare.ReadWrite | FileShare.Delete,
    4 * KB,
    FileOptions.None,
    null);

// Check stream.CanWrite!

Expected behavior

  • A read-only file handle is opened and wrapped it into a FileStream.
  • The stream file access should be set to FileAccess.Read.
  • The stream CanWrite property should read to false.

Actual behavior

  • A read-only file handle is opened and wrapped it into a FileStream (so far so good).
  • The FileStream file access is wrongly set internally to FileAccess.ReadWrite.
  • As a result, CanWrite is true for the stream. The underlying handle with throw an exception if we try to write.

Regression?

No response

Known Workarounds

No response

Configuration

Windows 10, .NET SDK 7.0.203, using System.IO.FileSystem.AccessControl package.

Other information

The issue is most probably within the FileSystemAclExtensions.GetFileAccessFromRights method.

Almost any value for the FileSystemRights will match the first condition, and most of them will also match the second.

See:

// rights = FileSystemRights.Read;

private static FileAccess GetFileAccessFromRights(FileSystemRights rights)
{
	FileAccess fileAccess = (FileAccess)0;
	if ((rights & FileSystemRights.FullControl) != 0 || (rights & FileSystemRights.Modify) != 0)
	{
		return FileAccess.ReadWrite; // Returns here.
	}
```</issue_description>

<agent_instructions>Please fix the bug and add a test to an existing test class that validates the behavior. Please make sure to validate that the test fails before the fix and passes after.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@jeffhandley</author><body>
Assigned to @jozkee for triage.</body></comment_new>
<comment_new><author>@jozkee</author><body>
Regression introduced in https://github.com/dotnet/runtime/pull/61297. It works as you expect it in 6.0.</body></comment_new>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 10, 2025 21:34
…Access

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix FileInfo.Create to use correct FileSystemRights Dec 10, 2025
Copilot AI requested a review from stephentoub December 10, 2025 21:42
@stephentoub stephentoub marked this pull request as ready for review December 10, 2025 21:46
Copilot AI review requested due to automatic review settings December 10, 2025 21:47
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug where FileInfo.Create() with FileSystemRights.Read incorrectly returned a FileStream with CanWrite = true. The root cause was faulty bitwise logic in GetFileAccessFromRights that checked for ANY overlapping bits instead of checking that ALL bits of compound flags like FullControl and Modify were present.

Key changes:

  • Fixed bitwise comparison from (rights & flag) != 0 to (rights & flag) == flag for compound FileSystemRights flags (FullControl and Modify)
  • Added regression test validating that FileStream.CanWrite is false when opened with FileSystemRights.Read

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs Corrected bitwise logic in GetFileAccessFromRights to check for exact match of compound flags instead of partial overlap
src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs Added regression test validating FileStream properties when opened with FileSystemRights.Read
@stephentoub stephentoub enabled auto-merge (squash) December 12, 2025 05:07
@stephentoub stephentoub disabled auto-merge December 12, 2025 05:07
@stephentoub stephentoub enabled auto-merge (squash) December 12, 2025 05:07
@stephentoub stephentoub requested a review from jozkee January 9, 2026 05:14
@stephentoub stephentoub merged commit f71826b into main Jan 13, 2026
90 checks passed
@jozkee jozkee deleted the copilot/fix-fileinfo-create-access-rights branch January 13, 2026 18:35
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

4 participants