Skip to content

Commit a5c9d9d

Browse files
authored
Add tasks for poisoning and checking for poison in packages. (#797)
* Add tasks for poisoning and checking for poison in packages. * Fix false positives in archives. * Address code review comments and remove debug code.
1 parent 2d81fcd commit a5c9d9d

15 files changed

+857
-2
lines changed

build-source-tarball.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -euo pipefail
33
IFS=$'\n\t'
44

55
usage() {
6-
echo "usage: $0 <path-to-tarball-root> [--skip-build]"
6+
echo "usage: $0 <path-to-tarball-root> [--skip-build] [--enable-leak-detection]"
77
echo ""
88
}
99

@@ -16,6 +16,8 @@ TARBALL_ROOT=$1
1616
shift
1717

1818
SKIP_BUILD=0
19+
INCLUDE_LEAK_DETECTION=0
20+
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
1921

2022
while :; do
2123
if [ $# -le 0 ]; then
@@ -31,6 +33,9 @@ while :; do
3133
--skip-build)
3234
SKIP_BUILD=1
3335
;;
36+
--enable-leak-detection)
37+
INCLUDE_LEAK_DETECTION=1
38+
;;
3439
*)
3540
echo "Unrecognized argument '$1'"
3641
usage
@@ -158,6 +163,12 @@ SOURCE_BUILT_SDK_TOOLS_DIR="$TARBALL_ROOT/Tools/source-built/$ROSLYN_TOOLS_PACKA
158163
cp "$REPO_TOOLSET_PACKAGE_DIR/tools/"*.props "$SOURCE_BUILT_SDK_TOOLS_DIR"
159164
cp "$REPO_TOOLSET_PACKAGE_DIR/tools/"*.targets "$SOURCE_BUILT_SDK_TOOLS_DIR"
160165

166+
if [ $INCLUDE_LEAK_DETECTION -eq 1 ]; then
167+
echo 'Building leak detection MSBuild tasks...'
168+
./Tools/dotnetcli/dotnet restore $SCRIPT_ROOT/tools-local/tasks/Microsoft.DotNet.SourceBuild.Tasks.LeakDetection/Microsoft.DotNet.SourceBuild.Tasks.LeakDetection.csproj --source $FULL_TARBALL_ROOT/prebuilt/source-built --source $FULL_TARBALL_ROOT/prebuilt/nuget-packages
169+
./Tools/dotnetcli/dotnet publish -o $FULL_TARBALL_ROOT/tools-local/tasks/Microsoft.DotNet.SourceBuild.Tasks.LeakDetection $SCRIPT_ROOT/tools-local/tasks/Microsoft.DotNet.SourceBuild.Tasks.LeakDetection/Microsoft.DotNet.SourceBuild.Tasks.LeakDetection.csproj
170+
fi
171+
161172
echo 'Recording commits for the source-build repo and all submodules, to aid in reproducibility...'
162173

163174
cat >$TARBALL_ROOT/source-build-info.txt << EOF

build.proj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<Project ToolsVersion="15.0" InitialTargets="PrepareOutput" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="dir.props" />
44

5+
<UsingTask AssemblyFile="$(LeakDetectionTasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.LeakDetection.dll" TaskName="CheckForPoison" />
6+
57
<Target Name="Build" DependsOnTargets="PrepareOutput;InitBuild">
68
<Message Text="Build Environment: $(Platform) $(Configuration) $(TargetOS) $(TargetRid)" />
79

@@ -45,6 +47,18 @@
4547
<MSBuild Projects="repos\$(RootRepo).proj" Targets="ReportPrebuiltUsage" />
4648
</Target>
4749

50+
<Target Name="ReportPoisonUsage"
51+
AfterTargets="Build"
52+
Condition="'$(EnablePoison)' == 'true' and '$(OfflineBuild)' == 'true'">
53+
<ItemGroup>
54+
<FinalCliTarball Include="$(SourceBuiltTarBallPath)**/*$(TarBallExtension)" />
55+
</ItemGroup>
56+
<CheckForPoison FilesToCheck="@(FinalCliTarball)"
57+
HashCatalogFilePath="$(PoisonReportDataFile)"
58+
MarkerFileName="$(PoisonMarkerFile)"
59+
PoisonReportOutputFilePath="$(PoisonUsageReportFile)" />
60+
</Target>
61+
4862
<Target Name="RunSmokeTest" DependsOnTargets="GetProdConBlobFeedUrl">
4963
<!--
5064
Pass prodConBlobFeedUrl via EnvironmentVariables because it has '//' in it, which is

dir.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ToolsLocalDir>$(ProjectDir)tools-local/</ToolsLocalDir>
4848
<TaskDirectory>$(ToolsLocalDir)tasks/</TaskDirectory>
4949
<TasksBinDir>$(TaskDirectory)Microsoft.DotNet.SourceBuild.Tasks/bin/Debug/netstandard1.5/</TasksBinDir>
50+
<LeakDetectionTasksBinDir>$(TaskDirectory)Microsoft.DotNet.SourceBuild.Tasks.LeakDetection/</LeakDetectionTasksBinDir>
5051
<BaseIntermediatePath>$(BaseOutputPath)obj/</BaseIntermediatePath>
5152
<OutputPath>$(BaseOutputPath)$(Platform)/$(Configuration)/</OutputPath>
5253
<IntermediatePath>$(BaseIntermediatePath)$(Platform)/$(Configuration)/</IntermediatePath>
@@ -70,6 +71,11 @@
7071
<GitInfoOfflineDir>$(ProjectDir)git-info/</GitInfoOfflineDir>
7172
<PackageReportDir>$(BaseOutputPath)prebuilt-report/</PackageReportDir>
7273
<PackageReportDataFile>$(PackageReportDir)prebuilt-usage.xml</PackageReportDataFile>
74+
<PoisonUsageReportFile>$(PackageReportDir)poison-usage.xml</PoisonUsageReportFile>
75+
<PoisonReportDataFile>$(PackageReportDir)poison-catalog.xml</PoisonReportDataFile>
76+
<PoisonMarkerFile>.prebuilt.xml</PoisonMarkerFile>
77+
<SourceBuiltPoisonReportDataFile>$(PackageReportDir)poison-source-built-catalog.xml</SourceBuiltPoisonReportDataFile>
78+
<SourceBuiltPoisonMarkerFile>.source-built.xml</SourceBuiltPoisonMarkerFile>
7379
<ProjectAssetsJsonArchiveFile>$(PackageReportDir)all-project-assets-json-files.zip</ProjectAssetsJsonArchiveFile>
7480
<ProdConManifestFile>$(PackageReportDir)prodcon-build.xml</ProdConManifestFile>
7581
<PoisonedReportFile>$(PackageReportDir)poisoned.txt</PoisonedReportFile>

tools-local/init-build.proj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Import Project="..\dir.props" />
88

99
<UsingTask AssemblyFile="$(TasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.dll" TaskName="GetHostInformation" />
10+
<UsingTask AssemblyFile="$(LeakDetectionTasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.LeakDetection.dll" TaskName="MarkAndCatalogPackages" />
1011

1112
<PropertyGroup>
1213
<BuildCompetedSuccessSemaphore>$(BaseIntermediatePath)/init-build-proj.complete</BuildCompetedSuccessSemaphore>
@@ -17,7 +18,7 @@
1718
Inputs="$(TargetInfoProps)"
1819
Outputs="$(BuildCompetedSuccessSemaphore)"
1920
>
20-
<CallTarget Targets="BuildTasks;InstallSourceBuildSdkResolver;WriteDynamicPropsToStaticPropsFiles;GenerateRootFs;CreateAllGitInfoProps;ApplyPatches" />
21+
<CallTarget Targets="BuildTasks;InstallSourceBuildSdkResolver;WriteDynamicPropsToStaticPropsFiles;GenerateRootFs;CreateAllGitInfoProps;ApplyPatches;PoisonPrebuiltPackages" />
2122
<Touch Files="$(BuildCompetedSuccessSemaphore)" AlwaysCreate="true" />
2223
</Target>
2324

@@ -93,4 +94,13 @@
9394
<WriteLinesToFile File="$(TargetInfoProps)" Lines="$(TargetInfoPropsContent)" Overwrite="True" />
9495
</Target>
9596

97+
<Target Name="PoisonPrebuiltPackages" Condition="'$(EnablePoison)' == 'true' and '$(OfflineBuild)' == 'true'">
98+
<ItemGroup>
99+
<PrebuiltPackages Include="$(PrebuiltPackagesPath)**/*.nupkg" />
100+
<PrebuiltSourceBuiltPackages Include="$(PrebuiltSourceBuiltPackagesPath)**/*.nupkg" />
101+
</ItemGroup>
102+
<MarkAndCatalogPackages PackagesToMark="@(PrebuiltPackages)" CatalogOutputFilePath="$(PoisonReportDataFile)" MarkerFileName="$(PoisonMarkerFile)" />
103+
<MarkAndCatalogPackages PackagesToMark="@(PrebuiltSourceBuiltPackages)" CatalogOutputFilePath="$(SourceBuiltPoisonReportDataFile)" MarkerFileName="$(SourceBuiltPoisonMarkerFile)" />
104+
</Target>
105+
96106
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using System.Xml.Linq;
9+
10+
namespace Microsoft.DotNet.SourceBuild.Tasks.LeakDetection
11+
{
12+
internal class CatalogFileEntry
13+
{
14+
const string ElementName = "File";
15+
16+
internal string Path { get; set; }
17+
internal byte[] OriginalHash { get; set; }
18+
internal byte[] PoisonedHash { get; set; }
19+
20+
public XElement ToXml() => new XElement(ElementName,
21+
new XAttribute(nameof(Path), Path),
22+
new XAttribute(nameof(OriginalHash), OriginalHash.ToHexString()),
23+
PoisonedHash == null ? null : new XAttribute(nameof(PoisonedHash), PoisonedHash.ToHexString())
24+
);
25+
}
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Xml.Linq;
10+
11+
namespace Microsoft.DotNet.SourceBuild.Tasks.LeakDetection
12+
{
13+
internal class CatalogPackageEntry
14+
{
15+
const string ElementName = "Package";
16+
17+
internal string Path { get; set; }
18+
internal string Id { get; set; }
19+
internal string Version { get; set; }
20+
internal byte[] OriginalHash { get; set; }
21+
internal byte[] PoisonedHash { get; set; }
22+
internal List<CatalogFileEntry> Files { get; }
23+
24+
public CatalogPackageEntry()
25+
{
26+
this.Files = new List<CatalogFileEntry>();
27+
}
28+
29+
public XElement ToXml() => new XElement(ElementName,
30+
new XAttribute(nameof(Path), Path),
31+
new XAttribute(nameof(Id), Id),
32+
new XAttribute(nameof(Version), Version),
33+
new XAttribute(nameof(OriginalHash), OriginalHash.ToHexString()),
34+
PoisonedHash == null ? null : new XAttribute(nameof(PoisonedHash), PoisonedHash.ToHexString()),
35+
Files.Select(f => f.ToXml())
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)