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

Commit

Permalink
Remove no-op file security (#8611)
Browse files Browse the repository at this point in the history
Deletes FileSecurityState and pulls redundant methods. Also
removes DriveInfo, which isn't in use in Core.
  • Loading branch information
JeremyKuhne authored and jkotas committed Dec 13, 2016
1 parent 4c27831 commit 4f1a90f
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 1,325 deletions.
4 changes: 2 additions & 2 deletions src/mscorlib/corefx/System/IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ public FileStream(string path, FileMode mode, FileAccess access, FileShare share
{ }

internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, string msgPath, bool bFromProxy)
: this(path, mode, access, share, bufferSize, options, msgPath, bFromProxy, useLongPath: false, checkHost: false)
: this(path, mode, access, share, bufferSize, options, msgPath, bFromProxy, useLongPath: false)
{
}

internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost)
internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, string msgPath, bool bFromProxy, bool useLongPath)
: this(path, mode, access, share, bufferSize, options)
{
// msgPath is the path that is handed back to untrusted code, CoreCLR is always full trust
Expand Down
3 changes: 0 additions & 3 deletions src/mscorlib/mscorlib.shared.sources.props
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@
<IoSources Include="$(BclSourcesRoot)\System\IO\DirectoryInfo.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\SearchOption.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\DirectoryNotFoundException.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\DriveInfo.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\DriveNotFoundException.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\EncodingCache.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\EndOfStreamException.cs" />
Expand Down Expand Up @@ -836,8 +835,6 @@
<IoSources Include="$(BclSourcesRoot)\System\IO\UnmanagedMemoryAccessor.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\UnmanagedMemoryStream.cs" />
<IoSources Include="$(BclSourcesRoot)\System\IO\UnmanagedMemoryStreamWrapper.cs" />
<IoSources Condition="'$(FeatureCoreclr)' == 'true'" Include="$(BclSourcesRoot)\System\IO\FileSecurityState.cs" />
<IoSources Condition="'$(FeatureCoreclr)' == 'true'" Include="$(BclSourcesRoot)\System\IO\FileSecurityStateAccess.cs" />
</ItemGroup>
<ItemGroup>
<SecuritySources Include="$(BclSourcesRoot)\System\Security\Attributes.cs" />
Expand Down
223 changes: 20 additions & 203 deletions src/mscorlib/src/System/IO/Directory.cs

Large diffs are not rendered by default.

95 changes: 10 additions & 85 deletions src/mscorlib/src/System/IO/DirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@
**
===========================================================*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Security;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Diagnostics;
Expand All @@ -35,36 +29,19 @@ namespace System.IO
[ComVisible(true)]
public sealed class DirectoryInfo : FileSystemInfo
{
private String[] demandDir;

// Migrating InheritanceDemands requires this default ctor, so we can annotate it.
#if FEATURE_CORESYSTEM
#else
#endif //FEATURE_CORESYSTEM
private DirectoryInfo(){}


public static DirectoryInfo UnsafeCreateDirectoryInfo(String path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
Contract.EndContractBlock();

DirectoryInfo di = new DirectoryInfo();
di.Init(path, false);
return di;
}

public DirectoryInfo(String path)
{
if (path==null)
throw new ArgumentNullException(nameof(path));
Contract.EndContractBlock();

Init(path, true);
Init(path);
}

private void Init(String path, bool checkHost)
private void Init(String path)
{
// Special case "<DriveLetter>:" to point to "<CurrentDirectory>" instead
if ((path.Length == 2) && (path[1] == ':'))
Expand All @@ -76,23 +53,10 @@ private void Init(String path, bool checkHost)
OriginalPath = path;
}

// Must fully qualify the path for the security check
String fullPath = Path.GetFullPath(path);

demandDir = new String[] {Directory.GetDemandDir(fullPath, true)};

if (checkHost)
{
FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, OriginalPath, fullPath);
state.EnsureState();
}

FullPath = fullPath;
FullPath = Path.GetFullPath(path); ;
DisplayPath = GetDisplayName(OriginalPath, FullPath);
}

#if FEATURE_CORESYSTEM
#endif //FEATURE_CORESYSTEM
internal DirectoryInfo(String fullPath, bool junk)
{
Debug.Assert(PathInternal.GetRootLength(fullPath) > 0, "fullPath must be fully qualified!");
Expand All @@ -101,7 +65,6 @@ internal DirectoryInfo(String fullPath, bool junk)

FullPath = fullPath;
DisplayPath = GetDisplayName(OriginalPath, FullPath);
demandDir = new String[] {Directory.GetDemandDir(fullPath, true)};
}

private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
Expand All @@ -125,16 +88,12 @@ public DirectoryInfo Parent {
// those cases, as well as avoiding mangling "c:\".
String s = FullPath;
if (s.Length > 3 && s.EndsWith(Path.DirectorySeparatorChar))
s = FullPath.Substring(0, FullPath.Length - 1);
s = FullPath.Substring(0, FullPath.Length - 1);
parentName = Path.GetDirectoryName(s);
if (parentName==null)
return null;
DirectoryInfo dir = new DirectoryInfo(parentName,false);

FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.PathDiscovery | FileSecurityStateAccess.Read, String.Empty, dir.demandDir[0]);
state.EnsureState();

return dir;
return new DirectoryInfo(parentName, false);
}
}

Expand Down Expand Up @@ -167,11 +126,6 @@ private DirectoryInfo CreateSubdirectoryHelper(String path, Object directorySecu
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSubPath", path, displayPath));
}

// Ensure we have permission to create this subdirectory.
String demandDirForCreation = Directory.GetDemandDir(fullPath, true);
FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, OriginalPath, demandDirForCreation);
state.EnsureState();

Directory.InternalCreateDirectory(fullPath, path, directorySecurity);

// Check for read permission to directory we hand back by calling this constructor.
Expand All @@ -180,14 +134,10 @@ private DirectoryInfo CreateSubdirectoryHelper(String path, Object directorySecu

public void Create()
{
Directory.InternalCreateDirectory(FullPath, OriginalPath, null, true);
Directory.InternalCreateDirectory(FullPath, OriginalPath, null);
}

// Tests if the given path refers to an existing DirectoryInfo on disk.
//
// Your application must have Read permission to the directory's
// contents.
//
public override bool Exists {
get
{
Expand All @@ -197,7 +147,6 @@ public override bool Exists {
Refresh();
if (_dataInitialised != 0) // Refresh was unable to initialise the data
return false;

return _data.fileAttributes != -1 && (_data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0;
}
catch
Expand Down Expand Up @@ -435,7 +384,7 @@ private IEnumerable<FileSystemInfo> InternalEnumerateFileSystemInfos(String sear

return FileSystemEnumerableFactory.CreateFileSystemInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
}

// Returns the root portion of the given path. The resulting string
// consists of those rightmost characters of the path that constitute the
// root of the path. Possible patterns for the resulting string are: An
Expand All @@ -444,18 +393,11 @@ private IEnumerable<FileSystemInfo> InternalEnumerateFileSystemInfos(String sear
// where X is the drive letter), "X:\" (an absolute path on a given drive),
// and "\\server\share" (a UNC path for a given server and share name).
// The resulting string is null if path is null.
//

public DirectoryInfo Root {
get
{
String demandPath;
int rootLength = PathInternal.GetRootLength(FullPath);
String rootPath = FullPath.Substring(0, rootLength);
demandPath = Directory.GetDemandDir(rootPath, true);

FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPath);
sourceState.EnsureState();

return new DirectoryInfo(rootPath);
}
Expand All @@ -468,25 +410,10 @@ public void MoveTo(String destDirName) {
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), nameof(destDirName));
Contract.EndContractBlock();

FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Write | FileSecurityStateAccess.Read, DisplayPath, Directory.GetDemandDir(FullPath, true));
sourceState.EnsureState();

String fullDestDirName = Path.GetFullPath(destDirName);
String demandPath;
if (!fullDestDirName.EndsWith(Path.DirectorySeparatorChar))
fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar;

demandPath = fullDestDirName + '.';

// Demand read & write permission to destination. The reason is
// we hand back a DirectoryInfo to the destination that would allow
// you to read a directory listing from that directory. Sure, you
// had the ability to read the file contents in the old location,
// but you technically also need read permissions to the new
// location as well, and write is not a true superset of read.
FileSecurityState destState = new FileSecurityState(FileSecurityStateAccess.Write, destDirName, demandPath);
destState.EnsureState();

String fullSourcePath;
if (FullPath.EndsWith(Path.DirectorySeparatorChar))
fullSourcePath = FullPath;
Expand Down Expand Up @@ -519,20 +446,19 @@ public void MoveTo(String destDirName) {
FullPath = fullDestDirName;
OriginalPath = destDirName;
DisplayPath = GetDisplayName(OriginalPath, FullPath);
demandDir = new String[] { Directory.GetDemandDir(FullPath, true) };

// Flush any cached information about the directory.
_dataInitialised = -1;
}

public override void Delete()
{
Directory.Delete(FullPath, OriginalPath, false, true);
Directory.Delete(FullPath, OriginalPath, false);
}

public void Delete(bool recursive)
{
Directory.Delete(FullPath, OriginalPath, recursive, true);
Directory.Delete(FullPath, OriginalPath, recursive);
}

// Returns the fully qualified path
Expand Down Expand Up @@ -580,7 +506,6 @@ private static String GetDirName(String fullPath)
}
return dirName;
}

}
}
}

Loading

0 comments on commit 4f1a90f

Please sign in to comment.