Skip to content

Prevent symlink cycle - #7685

Merged
Forgind merged 9 commits into
dotnet:mainfrom
Forgind:prevent-symlink-cycle
Jul 17, 2022
Merged

Forgind merged 9 commits into
dotnet:mainfrom
Forgind:prevent-symlink-cycle

Conversation

@Forgind

@Forgind Forgind commented Jun 7, 2022 •

Copy link
Copy Markdown
Contributor

Fixes-ish AB#1513887

Context

A symlink in folder A pointing to A will recurse eternally (hang). This fixes that problem in a fairly simple way.

Changes Made

Check if a symlink would take us to a folder above the one we're in before following it.

Testing

I'd rather not introduce something that, if broken, just hangs.

@rainersigwald rainersigwald left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd rather not introduce something that, if broken, just hangs.

Please write the test. A deterministic test hang is strictly worse than a cleanly failing test but strictly better than a product regression.

Comment thread src/Shared/FileMatcher.cs Outdated
Dictionary<string, List<RecursionState>> searchesToExcludeInSubdirs,
TaskOptions taskOptions)
{
#if NET6_0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a rule, never use exact-match checks in ifdefs. If you mean "if this API is available" you could say

Suggested change
#if NET6_0
#if NET6_0_OR_GREATER

but the preferred approach is to create a FEATURE_* flag and set it when relevant, checking it here. That's nice because it allows future maintainers to more easily understand the nature of the conditional inclusion: it was like this because LinkTarget was not available on .NET 4.7.2.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can certainly do NET6_0_OR_GREATER, but I'm a bit hesitant about a new FEATURE_* for a few reasons. First, making MSBuild logic for "or greater" isn't easy for this sort of thing. I'd have to parse out the version from the target framework then do a version comparison with 6. That messiness is presumably why our other flags that would use NET6_0_OR_GREATER-dependent feature flags use NET6_0_OR_GREATER rather than making new flags, and I do like consistency.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can put it in this section:

<PropertyGroup Condition="'$(NetCoreBuild)'=='true'">
<CompilerToolsDir>$([System.IO.Path]::Combine($(ToolPackagesDir)Microsoft.Net.Compilers, $(CompilerToolsVersion), "tools"))$([System.IO.Path]::DirectorySeparatorChar)</CompilerToolsDir>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLYLOADCONTEXT</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_RUNTIMEINFORMATION</DefineConstants>
<DefineConstants>$(DefineConstants);USE_MSBUILD_DLL_EXTN</DefineConstants>
<DefineConstants>$(DefineConstants);WORKAROUND_COREFX_19110</DefineConstants>
</PropertyGroup>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But that isn't sufficient, right? It isn't even available in NET5, according to the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I know we just use net6, netstandard2.0, and net472, but that feels a little like lying.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also maybe true for netstandard2.0?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the standard approach in our codebase. It does not seem important to me to support a theoretical future downgrade to an end-of-life target, and I don't think it's "lying" to say "when building for the current .NET Core TFM, include this code that is present in today's version of that TFM".

Comment thread src/Shared/FileMatcher.cs Outdated
Comment on lines +847 to +848
DirectoryInfo baseDirectoryInfo = new(recursionState.BaseDirectory);
if (baseDirectoryInfo.LinkTarget is not null && baseDirectoryInfo.FullName.Contains(baseDirectoryInfo.LinkTarget))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you could make this a little less allocatey:

Suggested change
DirectoryInfo baseDirectoryInfo = new(recursionState.BaseDirectory);
if (baseDirectoryInfo.LinkTarget is not null && baseDirectoryInfo.FullName.Contains(baseDirectoryInfo.LinkTarget))
FileSystemInfo linkTarget = Directory.ResolveLinkTarget(recursionState.BaseDirectory, returnFinalTarget: true);
if (linkTarget is not null && baseDirectoryInfo.FullName.Contains(linkTarget.FullName))

Comment thread src/Shared/FileMatcher.cs
// GetFiles in folder C that contains a symlink that points folder D, which contains C as well as other files
// and most obviously, frameworks other than net6.0
// The solution I'd propose for the first two, if necessary, would be maintaining a set of symlinks and verifying, before following it,
// that we had not followed it previously. The third would require a more involved P/invoke-style fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Link to #703 and describe this codepath there, please.

Comment thread src/Shared/FileMatcher.cs Outdated
#if NET6_0
// This is a pretty quick, simple check, but it misses some cases:
// symlink in folder A pointing to folder B and symlink in folder B pointing to folder A
// GetFiles in folder C that contains a symlink that points folder D, which contains C as well as other files

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand this case, can you elaborate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"If folder C contains file Foo.cs and folder D, and folder D contains a symlink pointing to folder C, calling GetFilesRecursive and passing in folder D would currently find Foo.cs, whereas this would make us miss it."

Is that a clearer description?

That sounds like a valid case, but I feel fine about ignoring it for now, since it would hang with current MSBuild anyway, so we know no one is doing it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I see. Could you avoid the recursion into the "bad" symlink, instead of just returning from it quickly? That feels like the same basic code, but would solve that case (which I think is important).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How would that help? Either we follow the symlink or we don't, and if we do, then we can only avoid following it repeatedly if we store the visited directories; if we don't, we can't possibly find Foo.cs in that case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given:

  • C
    • D
      • AAA (link to C)
      • Foo.cs

With your code, you're saying it would behave like this

  1. Start at D
  2. Examine AAA's link status
  3. return early

With my approach:

  1. Start at D
  2. (start foreach child item)
  3. Examine AAA's link status
  4. Decide not to recurse into AAA/back to C
  5. Examine Foo.cs's link status
  6. Include Foo.cs

This doesn't require storing anything; the "just don't follow symlinks directly up" approach can stand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No; with my approach, that scenario would work. It's fairly easy to modify my test to prove that. The scenario that wouldn't work is:

  • C
    • D
      • AAA (link to C)
    • Foo.cs

The idea then would be that it would start at D, check AAA, and not follow it, and be finished. Ideally, it would start at D, follow AAA to C, find Foo.cs, check D again, see AAA again but not follow it this time, and stop. That ideal isn't possible now, though, which is why I think this solution is fine.

@Forgind

Forgind commented Jun 8, 2022

Copy link
Copy Markdown
Contributor Author

Test seem to be failing because they're essentially passing recursionState.BaseDirectory as "". Is that ever a real case? (It doesn't sound very real to me.) If it is, I'm assuming I can still rely on projectDirectory, though that's also empty in the failing tests. I'm tempted to just disable this change for those tests, but that feels wrong.

@rainersigwald rainersigwald added the Area: Engine Issues impacting the core execution of targets and tasks. label Jun 8, 2022
Comment thread src/Shared/FileMatcher.cs Outdated
}
}
// This fails in tests with the MockFileSystem when they don't have real paths.
catch (Exception) { }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Narrow exception?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

At least to IOException

Forgind added 2 commits June 13, 2022 15:03
Directory.ResolveLinkTarget claims to only throw IOExceptions, but apparently it can also throw ArgumentExceptions. I think it's best to catch both just in case.
@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Jun 29, 2022
@Forgind
Forgind merged commit 06b0eb6 into dotnet:main Jul 17, 2022
@Forgind
Forgind deleted the prevent-symlink-cycle branch July 17, 2022 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Engine Issues impacting the core execution of targets and tasks. merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants