Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Fix missing .NET Core argument validation for PrivateFontCollection.AddFontFile #21707

Merged
merged 2 commits into from Jul 3, 2017
Merged

Fix missing .NET Core argument validation for PrivateFontCollection.AddFontFile #21707

merged 2 commits into from Jul 3, 2017

Conversation

hughbe
Copy link

@hughbe hughbe commented Jun 29, 2017

This is a cleaned up copy of the netfx source code.

However, is the whole IntSecurity.DemandReadFileIO(filename) stuff necessary

Or maybe I could just replace that with filename = Path.GetFullPath(filename) and delete IntSecurity.cs

Fixes #21558

@stephentoub
Copy link
Member

is the whole IntSecurity.DemandReadFileIO(filename) stuff necessary

Nope :)

@danmoseley
Copy link
Member

@hughbe I believe all use of System.Security.Permissions.* (and using statements) can be removed from Drawing. (Almost) all of our implmentations of those types are stubs.

@hughbe
Copy link
Author

hughbe commented Jun 30, 2017

I asked before but it may have gone unnoticed. Does that apply even for Mono?

I've updated this PR after Stephen commented to get rid of the security stuff

@mellinoe
Copy link
Contributor

This is a cleaned up copy of the netfx source code.

Maybe I'm misinterpreting this, but I don't think the .NET Framework version does this normalization. Could you clear that up? It looks like an okay change, but TBH I'd rather just keep 100% behavior compat with .NET Framework until we have excellent test coverage.

@hughbe
Copy link
Author

hughbe commented Jul 1, 2017

Before this cleanup, it used IntSecurity.DemandReadFileIO. This was unnecessary in netcoreapp as Stephen mentioned so I could just shorten it to Path.GetFullPath

@karelz karelz modified the milestone: 2.1.0 Jul 1, 2017
@mellinoe
Copy link
Contributor

mellinoe commented Jul 2, 2017

I get that it used IntSecurity.DemandReadFileIO(filename); before, but that shouldn't affect which value is passed into GdipPrivateAddFontFile, right? From my understanding DemandReadFileIO will just do a security / permissions check on that full path, and that's the part we no longer care about.

@hughbe
Copy link
Author

hughbe commented Jul 2, 2017

The code in netfx is:

public void AddFontFile (string filename) {
    IntSecurity.DemandReadFileIO(filename);
    int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, nativeFontCollection), filename);

    if (status != SafeNativeMethods.Gdip.Ok)
        throw SafeNativeMethods.Gdip.StatusException(status);
        
    // Register private font with GDI as well so pure GDI-based controls (TextBox, Button for instance) can access it.
    SafeNativeMethods.AddFontFile( filename );
}

We got rid of the IntSecurity.DemandReadFileIO(filename); part presumably because .NET Core doesn't need the security/permissions bit.

However, this method has side effects that weren't evident at the time of porting due to no tests.

Namely, that it called UnsafeGetFullPath before demanding the permissions

internal static void DemandReadFileIO(string fileName) {
  string full = fileName;
  
  full = UnsafeGetFullPath(fileName);

  new FileIOPermission(FileIOPermissionAccess.Read, full).Demand();
}

[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
internal static string UnsafeGetFullPath(string fileName) {
    string full = fileName;

    FileIOPermission fiop = new FileIOPermission(PermissionState.None);
    fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
    fiop.Assert();

    try {
        full = Path.GetFullPath(fileName);
    } finally {
        CodeAccessPermission.RevertAssert();
    }

    return full;
}

So we'd get to the call full = Path.GetFullPath(fileName);, which can and does throw in the situation this PR fixes.

but that shouldn't affect which value is passed into GdipPrivateAddFontFile, right?

The value passed to GdipPrivateAddFontFile is not affected by DemadReadFileIO, correct

@mellinoe
Copy link
Contributor

mellinoe commented Jul 2, 2017

The value passed to GdipPrivateAddFontFile is not affected by DemadReadFileIO, correct

I'm trying to point out that this change does affect the call to GdipPrivateAddFontFile:

filename = Path.GetFullPath(filename);
int status = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, _nativeFontCollection), filename);

@hughbe
Copy link
Author

hughbe commented Jul 2, 2017

Ohhhhh dammit! Sorry I wasn't really getting that. It shouldn't actually matter as GdipPrivateAddFontFile handles relative file paths anyways (and we have a test for that already)

But I removed the assignment :)

@mellinoe
Copy link
Contributor

mellinoe commented Jul 3, 2017

LGTM. Thanks, @hughbe

@mellinoe mellinoe merged commit 5fd039d into dotnet:master Jul 3, 2017
@hughbe hughbe deleted the add-font-file-compat branch July 4, 2017 00:52
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Fix missing .NET Core argument validation for PrivateFontCollection.AddFontFile

Commit migrated from dotnet/corefx@5fd039d
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants