diff --git a/cmake/process_bpf_source_file.cmake b/cmake/process_bpf_source_file.cmake index 2e23a7b49..5705b0a81 100644 --- a/cmake/process_bpf_source_file.cmake +++ b/cmake/process_bpf_source_file.cmake @@ -11,7 +11,7 @@ function(add_bpftonative_command file_name kernel_mode unsafe_program) set (output_file ${output_dir}/${file_name}_um.dll) endif() - set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${output_dir}/Convert-BpfToNative.ps1 -IncludeDir ${CMAKE_SOURCE_DIR}/include -ProgramName ${file_name} -OutDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode}) + set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${output_dir}/Convert-BpfToNative.ps1 -IncludeDir ${CMAKE_SOURCE_DIR}/include -FileName ${file_name} -OutDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode}) set (input_file ${output_dir}/${file_name}.o) if (${unsafe_program}) diff --git a/docs/NativeCodeGeneration.md b/docs/NativeCodeGeneration.md index d1f2de3c7..2d5945547 100644 --- a/docs/NativeCodeGeneration.md +++ b/docs/NativeCodeGeneration.md @@ -25,7 +25,7 @@ image that can be loaded and used as an eBPF program. From the root of the eBPF-For-Windows project (from a VS Developer Command Prompt), after building the project, run: ``` -powershell scripts\Convert-BpfToNative.ps1 -ProgramName my_program +powershell scripts\Convert-BpfToNative.ps1 -FileName my_program ``` Where my_program is the name of your BPF program without the extension. This will produce a native image in x64\Release with name of my_program.sys. diff --git a/scripts/build_custom_sample_programs.ps1 b/scripts/build_custom_sample_programs.ps1 index cf2bf701c..771250339 100644 --- a/scripts/build_custom_sample_programs.ps1 +++ b/scripts/build_custom_sample_programs.ps1 @@ -17,8 +17,8 @@ if ($FileName -eq "bpf") $ProgramType = "xdp" } -.\Convert-BpfToNative.ps1 -ProgramName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $KernelConfiguration -KernelMode $True -.\Convert-BpfToNative.ps1 -ProgramName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $Configuration -KernelMode $False +.\Convert-BpfToNative.ps1 -FileName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $KernelConfiguration -KernelMode $True +.\Convert-BpfToNative.ps1 -FileName $Filename -Type $ProgramType -IncludeDir $IncludePath -Platform $Platform -Configuration $Configuration -KernelMode $False Pop-Location diff --git a/tests/sample/sample.vcxproj b/tests/sample/sample.vcxproj index 67c19845c..2937876d1 100644 --- a/tests/sample/sample.vcxproj +++ b/tests/sample/sample.vcxproj @@ -118,8 +118,8 @@ clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c %(Filename).c -o $(OutputPath)%(Filename).o pushd $(OutDir) - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false popd $(OutputPath)%(Filename).o;$(OutputPath)%(Filename)_um.dll;$(OutputPath)%(Filename).sys @@ -144,8 +144,8 @@ clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c unsafe\%(Filename).c -o $(OutputPath)%(Filename).o pushd $(OutDir) - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true popd $(OutputPath)%(Filename).o @@ -160,8 +160,8 @@ clang $(ClangFlags) -I../xdp -I../socket -I./ext/inc -c unsafe\%(Filename).c -o $(OutputPath)%(Filename).o pushd $(OutDir) - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true - powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -ProgramName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(KernelConfiguration) -KernelMode $true -SkipVerification $true + powershell -NonInteractive -ExecutionPolicy Unrestricted .\Convert-BpfToNative.ps1 -FileName %(Filename) -IncludeDir $(SolutionDir)\include -Platform $(Platform) -Configuration $(Configuration) -KernelMode $false -SkipVerification $true popd $(OutputPath)%(Filename).o diff --git a/tools/bpf2c/Convert-BpfToNative.ps1.template b/tools/bpf2c/Convert-BpfToNative.ps1.template index f037e7266..26583795e 100644 --- a/tools/bpf2c/Convert-BpfToNative.ps1.template +++ b/tools/bpf2c/Convert-BpfToNative.ps1.template @@ -1,7 +1,74 @@ # Copyright (c) Microsoft Corporation # SPDX-License-Identifier: MIT -param([parameter(Mandatory = $true)] [string] $ProgramName, +<# +.SYNOPSIS + Generate a native driver from a BPF program. + +.DESCRIPTION + This script generates a native driver (.sys file) from a BPF program. The native program can be used to load BPF + programs into the kernel when Hypervisor-protected Code Integrity (HVCI) is enabled. + +.PARAMETER FileName + Specifies the name of the ELF file containing the BPF program. + +.PARAMETER Type + Specifies the type of the BPF program. Valid values include "xdp", "bind", "sockops", and other BPF program types. + +.PARAMETER IncludeDir + Specifies the directory containing the bpf2c include files. + +.PARAMETER BinDir + Specifies the directory containing the bpf2c binaries. + +.PARAMETER OutDir + Specifies the directory where the generated driver will be placed. + +.PARAMETER Platform + Specifies the target platform. Valid values include "x64". + +.PARAMETER Configuration + Specifies the build configuration. Valid values include "Release", "FuzzerDebug", and "Debug". + +.PARAMETER SkipVerification + Specifies whether to skip verification of the generated driver. This parameter is only supported for Debug builds. + +.PARAMETER KernelMode + Specifies whether to generate a kernel-mode driver. If this parameter is false, a user DLL will be generated. + +.PARAMETER ResourceFile + Specifies the path to a resource file to embed in the generated driver. + +.EXAMPLE + .\Convert-BpfToNative.ps1 -FileName bindmonitor + + This example generates a native driver from the BPF program bindmonitor.o. + +.EXAMPLE + .\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind + + This example generates a native driver from the BPF program bindmonitor.o. The + program type is set to "bind". + +.EXAMPLE + .\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind -Configuration Debug + + This example generates a native driver from the BPF program bindmonitor.o. The program type is set to "bind". + The driver is built in Debug configuration. + +.EXAMPLE + .\Convert-BpfToNative.ps1 -FileName bindmonitor -Type bind -Configuration Debug -KernelMode $false + + This example generates a user-mode DLL from the BPF program bindmonitor.o. The program type is set to "bind". + The driver is built in Debug configuration. + +.NOTES + Author: Microsoft Corporation + Website: https://github.com/microsoft/ebpf-for-windows + +#> + +param([parameter(Mandatory = $true)] [string] $FileName, [parameter(Mandatory = $false)] [string] $Type, [parameter(Mandatory = $false)] [string] $IncludeDir = "$PSScriptRoot\..\include", [parameter(Mandatory = $false)] [string] $BinDir = "$PSScriptRoot", @@ -9,7 +76,8 @@ param([parameter(Mandatory = $true)] [string] $ProgramName, [parameter(Mandatory = $false)] [string] $Platform = "x64", [ValidateSet("Release", "FuzzerDebug", "Debug")][parameter(Mandatory = $false)] [string] $Configuration = "Release", [parameter(Mandatory = $false)] [bool] $SkipVerification = $false, - [parameter(Mandatory = $false)] [bool] $KernelMode = $true) + [parameter(Mandatory = $false)] [bool] $KernelMode = $true, + [parameter(Mandatory = $false)] [string] $ResourceFile = "") Push-Location $OutDir @@ -17,8 +85,8 @@ $KernelModeProject = '___KERNEL_MODE_VCXPROJ___' $UserModeProject = '___USER_MODE_VCXPROJ___' # If program name ends with .o, remove the suffix -if ($ProgramName.EndsWith(".o")) { - $ProgramName = $ProgramName.Substring(0, $ProgramName.Length - 2) +if ($FileName.EndsWith(".o")) { + $FileName = $FileName.Substring(0, $FileName.Length - 2) } # SkipVerification is only supported for Debug builds. @@ -30,13 +98,13 @@ if ($null -eq (Get-Command 'msbuild.exe' -ErrorAction SilentlyContinue)) { throw "Unable to locate msbuild.exe. This command needs to run within a 'Developer Command Prompt'" } -$fileExists = Test-Path -Path ("$ProgramName.o") +$fileExists = Test-Path -Path ("$FileName.o") if (!$fileExists) { - $errorString = "Can't find program file: " + "$ProgramName.o" + $errorString = "Can't find program file: " + "$FileName.o" throw $errorString } -$TempDir = "$OutDir\$ProgramName" +$TempDir = "$OutDir\$FileName" if ($KernelMode) { $TempDir += "_km" @@ -46,15 +114,15 @@ else { } mkdir -Force $TempDir -Copy-Item -Path "$ProgramName.o" -Destination "$TempDir\" +Copy-Item -Path "$FileName.o" -Destination "$TempDir\" cd $TempDir if ($KernelMode) { - $ProjectFile = "$ProgramName.vcxproj" + $ProjectFile = "$FileName.vcxproj" Set-Content -Path $ProjectFile -Value $KernelModeProject } else { - $ProjectFile = "$ProgramName_um.vcxproj" + $ProjectFile = "$FileName_um.vcxproj" Set-Content -Path $ProjectFile -Value $UserModeProject } @@ -64,10 +132,10 @@ if ($PSBoundParameters.ContainsKey("Type")) { $AdditionalOptions += " --type $Type" } -msbuild /p:BinDir="$BinDir\" /p:OutDir="$OutDir\" /p:IncludeDir="$IncludeDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile +msbuild /p:BinDir="$BinDir\" /p:OutDir="$OutDir\" /p:IncludeDir="$IncludeDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:FileName="$FileName" /p:AdditionalOptions="$AdditionalOptions" /p:ResourceFile="$ResourceFile" $ProjectFile if ($LASTEXITCODE -ne 0) { - throw "Build failed for $ProgramName.o" + throw "Build failed for $FileName.o" } Pop-Location diff --git a/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj b/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj index ad51d568b..bdbe3d059 100644 --- a/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj +++ b/tools/bpf2c/templates/kernel_mode_bpf2c.vcxproj @@ -37,9 +37,9 @@ 12.0 Debug Win32 - $(ProgramName) + $(FileName) $(LatestTargetPlatformVersion) - $(ProgramName)_km + $(FileName)_km @@ -60,11 +60,11 @@ DbgengKernelDebugger - $(ProgramName) + $(FileName) - $(BinDir)bpf2c --bpf $(ProgramName).o --sys $(AdditionalOptions) >$(ProgramName)_driver.c + $(BinDir)bpf2c --bpf $(FileName).o --sys $(AdditionalOptions) >$(FileName)_driver.c $(IncludeDir);%(AdditionalIncludeDirectories) @@ -83,9 +83,12 @@ - + + + + diff --git a/tools/bpf2c/templates/user_mode_bpf2c.vcxproj b/tools/bpf2c/templates/user_mode_bpf2c.vcxproj index 771ef430a..112dc9920 100644 --- a/tools/bpf2c/templates/user_mode_bpf2c.vcxproj +++ b/tools/bpf2c/templates/user_mode_bpf2c.vcxproj @@ -19,15 +19,15 @@ - + 16.0 Win32Proj {1baf5f74-b71f-4088-8677-f27070302b59} - $(ProgramName)dll + $(FileName)dll 10.0 - $(ProgramName)_um + $(FileName)_um @@ -47,7 +47,7 @@ false - $(ProgramName)_um + $(FileName)_um @@ -64,7 +64,7 @@ false - $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) >$(FileName)_dll.c @@ -82,7 +82,7 @@ false - $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) >$(FileName)_dll.c @@ -102,9 +102,12 @@ false - $(BinDir)bpf2c.exe --bpf $(ProgramName).o --dll $(AdditionalOptions) >$(ProgramName)_dll.c + $(BinDir)bpf2c.exe --bpf $(FileName).o --dll $(AdditionalOptions) >$(FileName)_dll.c + + +