2021-06-13 04:54:55 +00:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
2021-11-23 17:32:44 +00:00
|
|
|
, fetchzip
|
2021-06-13 04:54:55 +00:00
|
|
|
, fetchFromGitHub
|
|
|
|
, callPackage
|
|
|
|
, autoconf
|
|
|
|
, automake
|
|
|
|
, binutils
|
|
|
|
, cmake
|
|
|
|
, file
|
2021-11-15 02:00:27 +00:00
|
|
|
, gdb
|
2021-06-13 04:54:55 +00:00
|
|
|
, git
|
|
|
|
, libtool
|
|
|
|
, nasm
|
|
|
|
, ocaml
|
|
|
|
, ocamlPackages
|
|
|
|
, openssl
|
|
|
|
, perl
|
|
|
|
, python3
|
|
|
|
, texinfo
|
2021-11-15 00:57:40 +00:00
|
|
|
, validatePkgConfig
|
2021-06-13 04:54:55 +00:00
|
|
|
, writeShellScript
|
2021-11-15 17:56:28 +00:00
|
|
|
, writeText
|
2021-06-13 04:54:55 +00:00
|
|
|
}:
|
2021-11-19 00:04:18 +00:00
|
|
|
with lib;
|
2021-06-13 04:54:55 +00:00
|
|
|
stdenv.mkDerivation rec {
|
|
|
|
pname = "sgx-sdk";
|
2021-11-19 00:04:18 +00:00
|
|
|
version = "2.14.100.2";
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-23 17:32:44 +00:00
|
|
|
versionTag = concatStringsSep "." (take 2 (splitVersion version));
|
|
|
|
|
2021-06-13 04:54:55 +00:00
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "intel";
|
|
|
|
repo = "linux-sgx";
|
2021-11-23 17:32:44 +00:00
|
|
|
rev = "sgx_${versionTag}";
|
2021-11-23 16:12:44 +00:00
|
|
|
hash = "sha256-D/QZWBUe1gRbbjWnV10b7IPoM3utefAsOEKnQuasIrM=";
|
2021-06-13 04:54:55 +00:00
|
|
|
fetchSubmodules = true;
|
|
|
|
};
|
|
|
|
|
2021-11-23 17:32:44 +00:00
|
|
|
postUnpack =
|
|
|
|
let
|
|
|
|
optlibName = "optimized_libs_${versionTag}.tar.gz";
|
|
|
|
optimizedLibs = fetchzip {
|
|
|
|
url = "https://download.01.org/intel-sgx/sgx-linux/${versionTag}/${optlibName}";
|
|
|
|
hash = "sha256-FjNhNV9+KDMvBYdWXZbua6qYOc3Z1/jtcF4j52TSxQY=";
|
|
|
|
stripRoot = false;
|
|
|
|
};
|
|
|
|
sgxIPPCryptoHeader = "${optimizedLibs}/external/ippcp_internal/inc/sgx_ippcp.h";
|
|
|
|
in
|
|
|
|
''
|
|
|
|
# Make sure this is the right version of linux-sgx
|
|
|
|
grep -q '"${version}"' "$src/common/inc/internal/se_version.h" \
|
|
|
|
|| (echo "Could not find expected version ${version} in linux-sgx source" >&2 && exit 1)
|
|
|
|
|
|
|
|
# Make sure we use the correct version to build IPP Crypto
|
|
|
|
grep -q 'optlib_name=${optlibName}' "$src/download_prebuilt.sh" \
|
2021-11-23 23:11:19 +00:00
|
|
|
|| (echo "Could not find expected optimized libs ${optlibName} in linux-sgx source" >&2 && exit 1)
|
2021-11-23 17:32:44 +00:00
|
|
|
|
|
|
|
# Add missing sgx_ippcp.h: https://github.com/intel/linux-sgx/pull/752
|
|
|
|
ln -s ${sgxIPPCryptoHeader} "$sourceRoot/external/ippcp_internal/inc/sgx_ippcp.h"
|
|
|
|
'';
|
2021-11-19 00:04:18 +00:00
|
|
|
|
2021-06-13 04:54:55 +00:00
|
|
|
postPatch = ''
|
2021-11-14 23:32:36 +00:00
|
|
|
# https://github.com/intel/linux-sgx/pull/730
|
|
|
|
substituteInPlace buildenv.mk --replace '/bin/cp' 'cp'
|
|
|
|
|
2021-11-15 00:36:16 +00:00
|
|
|
patchShebangs linux/installer/bin/build-installpkg.sh \
|
|
|
|
linux/installer/common/sdk/createTarball.sh \
|
|
|
|
linux/installer/common/sdk/install.sh
|
2021-06-13 04:54:55 +00:00
|
|
|
'';
|
|
|
|
|
2021-11-15 00:13:24 +00:00
|
|
|
# We need `cmake` as a build input but don't use it to kick off the build phase
|
|
|
|
dontUseCmakeConfigure = true;
|
2021-06-13 04:54:55 +00:00
|
|
|
|
|
|
|
# SDK built with stackprotector produces broken enclaves which crash at runtime.
|
|
|
|
# Disable all to be safe, SDK build configures compiler mitigations manually.
|
|
|
|
hardeningDisable = [ "all" ];
|
|
|
|
|
|
|
|
nativeBuildInputs = [
|
2021-11-15 00:13:24 +00:00
|
|
|
autoconf
|
|
|
|
automake
|
2021-06-13 04:54:55 +00:00
|
|
|
cmake
|
2021-11-15 00:13:24 +00:00
|
|
|
file
|
2021-06-13 04:54:55 +00:00
|
|
|
git
|
|
|
|
ocaml
|
|
|
|
ocamlPackages.ocamlbuild
|
|
|
|
perl
|
|
|
|
python3
|
|
|
|
texinfo
|
2021-11-15 00:57:40 +00:00
|
|
|
validatePkgConfig
|
2021-06-13 04:54:55 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
buildInputs = [
|
|
|
|
libtool
|
|
|
|
openssl
|
|
|
|
];
|
|
|
|
|
|
|
|
BINUTILS_DIR = "${binutils}/bin";
|
|
|
|
|
|
|
|
# Build external/ippcp_internal first. The Makefile is rewritten to make the
|
|
|
|
# build faster by splitting different versions of ipp-crypto builds and to
|
|
|
|
# avoid patching the Makefile for reproducibility issues.
|
2021-11-15 00:13:24 +00:00
|
|
|
preBuild =
|
2021-11-14 23:10:36 +00:00
|
|
|
let
|
2021-11-15 00:13:24 +00:00
|
|
|
ipp-crypto-no_mitigation = callPackage ./ipp-crypto.nix { };
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-14 23:10:36 +00:00
|
|
|
sgx-asm-pp = "python ${src}/build-scripts/sgx-asm-pp.py --assembler=nasm";
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-14 23:10:36 +00:00
|
|
|
nasm-load = writeShellScript "nasm-load" "${sgx-asm-pp} --MITIGATION-CVE-2020-0551=LOAD $@";
|
2021-11-15 00:13:24 +00:00
|
|
|
ipp-crypto-cve_2020_0551_load = callPackage ./ipp-crypto.nix {
|
2021-11-14 23:10:36 +00:00
|
|
|
extraCmakeFlags = [ "-DCMAKE_ASM_NASM_COMPILER=${nasm-load}" ];
|
|
|
|
};
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-14 23:10:36 +00:00
|
|
|
nasm-cf = writeShellScript "nasm-cf" "${sgx-asm-pp} --MITIGATION-CVE-2020-0551=CF $@";
|
2021-11-15 00:13:24 +00:00
|
|
|
ipp-crypto-cve_2020_0551_cf = callPackage ./ipp-crypto.nix {
|
2021-11-14 23:10:36 +00:00
|
|
|
extraCmakeFlags = [ "-DCMAKE_ASM_NASM_COMPILER=${nasm-cf}" ];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
''
|
2021-11-15 09:33:00 +00:00
|
|
|
header "Setting up IPP crypto build artifacts"
|
|
|
|
|
2021-11-15 00:13:24 +00:00
|
|
|
pushd 'external/ippcp_internal'
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:36:16 +00:00
|
|
|
install ${ipp-crypto-no_mitigation}/include/* inc/
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:36:16 +00:00
|
|
|
install -D -m a+rw ${ipp-crypto-no_mitigation}/lib/intel64/libippcp.a \
|
|
|
|
lib/linux/intel64/no_mitigation/libippcp.a
|
|
|
|
install -D -m a+rw ${ipp-crypto-cve_2020_0551_load}/lib/intel64/libippcp.a \
|
|
|
|
lib/linux/intel64/cve_2020_0551_load/libippcp.a
|
|
|
|
install -D -m a+rw ${ipp-crypto-cve_2020_0551_cf}/lib/intel64/libippcp.a \
|
|
|
|
lib/linux/intel64/cve_2020_0551_cf/libippcp.a
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:36:16 +00:00
|
|
|
rm inc/ippcp.h
|
|
|
|
patch ${ipp-crypto-no_mitigation}/include/ippcp.h -i inc/ippcp20u3.patch -o inc/ippcp.h
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:36:16 +00:00
|
|
|
install -D ${ipp-crypto-no_mitigation.src}/LICENSE license/LICENSE
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:13:24 +00:00
|
|
|
popd
|
2021-11-15 00:36:16 +00:00
|
|
|
'';
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 00:13:24 +00:00
|
|
|
buildFlags = [
|
|
|
|
"sdk_install_pkg"
|
|
|
|
];
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-12-04 13:24:32 +00:00
|
|
|
enableParallelBuilding = true;
|
|
|
|
|
2021-06-13 04:54:55 +00:00
|
|
|
postBuild = ''
|
2021-11-19 00:04:18 +00:00
|
|
|
patchShebangs linux/installer/bin/sgx_linux_x64_sdk_${version}.bin
|
2021-06-13 04:54:55 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
2021-11-15 00:13:24 +00:00
|
|
|
runHook preInstall
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 10:08:19 +00:00
|
|
|
installDir=$TMPDIR
|
2021-11-19 00:04:18 +00:00
|
|
|
./linux/installer/bin/sgx_linux_x64_sdk_${version}.bin -prefix $installDir
|
2021-11-15 10:08:19 +00:00
|
|
|
installDir=$installDir/sgxsdk
|
|
|
|
|
|
|
|
header "Move files created by installer"
|
|
|
|
|
2021-11-15 12:49:54 +00:00
|
|
|
mkdir -p $out/bin
|
2021-11-15 10:08:19 +00:00
|
|
|
pushd $out
|
|
|
|
|
|
|
|
mv $installDir/bin/sgx-gdb $out/bin
|
|
|
|
mkdir $out/bin/x64
|
|
|
|
for file in $installDir/bin/x64/*; do
|
|
|
|
mv $file bin/
|
|
|
|
ln -sr bin/$(basename $file) bin/x64/
|
|
|
|
done
|
|
|
|
rmdir $installDir/bin/{x64,}
|
|
|
|
|
|
|
|
# Move `lib64` to `lib` and symlink `lib64`
|
|
|
|
mv $installDir/lib64 lib
|
|
|
|
ln -s lib/ lib64
|
|
|
|
|
|
|
|
mv $installDir/include/ .
|
|
|
|
|
|
|
|
mkdir -p share/
|
|
|
|
mv $installDir/{SampleCode,licenses} share/
|
|
|
|
|
|
|
|
mkdir -p share/bin
|
|
|
|
mv $installDir/{environment,buildenv.mk} share/bin/
|
|
|
|
ln -s share/bin/{environment,buildenv.mk} .
|
|
|
|
|
|
|
|
# pkgconfig should go to lib/
|
|
|
|
mv $installDir/pkgconfig lib/
|
|
|
|
ln -s lib/pkgconfig/ .
|
|
|
|
|
|
|
|
# Also create the `sdk_libs` for compat. All the files
|
|
|
|
# link to libraries in `lib64/`, we shouldn't link the entire
|
|
|
|
# directory, however, as there seems to be some ambiguity between
|
|
|
|
# SDK and PSW libraries.
|
|
|
|
mkdir sdk_libs/
|
|
|
|
for file in $installDir/sdk_libs/*; do
|
|
|
|
ln -sr lib/$(basename $file) sdk_libs/
|
|
|
|
rm $file
|
|
|
|
done
|
|
|
|
rmdir $installDir/sdk_libs
|
|
|
|
|
|
|
|
# No uninstall script required
|
|
|
|
rm $installDir/uninstall.sh
|
|
|
|
|
|
|
|
# Create an `sgxsdk` symlink which points to `$out` for compat
|
|
|
|
ln -sr . sgxsdk
|
|
|
|
|
|
|
|
# Make sure we didn't forget any files
|
|
|
|
rmdir $installDir || (echo "Error: The directory $installDir still contains unhandled files: $(ls -A $installDir)" >&2 && exit 1)
|
|
|
|
|
|
|
|
popd
|
2021-11-15 00:13:24 +00:00
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
2021-06-13 04:54:55 +00:00
|
|
|
|
2021-11-15 10:08:19 +00:00
|
|
|
|
2021-11-15 00:57:40 +00:00
|
|
|
preFixup = ''
|
2021-11-15 10:08:19 +00:00
|
|
|
header "Strip sgxsdk prefix"
|
2021-11-15 12:49:54 +00:00
|
|
|
for path in "$out/share/bin/environment" "$out/bin/sgx-gdb"; do
|
2021-11-15 10:08:19 +00:00
|
|
|
substituteInPlace $path --replace "$TMPDIR/sgxsdk" "$out"
|
|
|
|
done
|
2021-11-15 02:00:27 +00:00
|
|
|
|
2021-11-15 09:33:00 +00:00
|
|
|
header "Fixing pkg-config files"
|
2021-11-15 10:08:19 +00:00
|
|
|
sed -i "s|prefix=.*|prefix=$out|g" $out/lib/pkgconfig/*.pc
|
|
|
|
|
|
|
|
header "Fixing SGX_SDK default in samples"
|
|
|
|
substituteInPlace $out/share/SampleCode/LocalAttestation/buildenv.mk \
|
|
|
|
--replace '/opt/intel/sgxsdk' "$out"
|
|
|
|
for file in $out/share/SampleCode/*/Makefile; do
|
|
|
|
substituteInPlace $file \
|
|
|
|
--replace '/opt/intel/sgxsdk' "$out" \
|
|
|
|
--replace '$(SGX_SDK)/buildenv.mk' "$out/share/bin/buildenv.mk"
|
|
|
|
done
|
|
|
|
|
2021-11-15 10:12:51 +00:00
|
|
|
header "Fixing BINUTILS_DIR in buildenv.mk"
|
2021-11-15 10:08:19 +00:00
|
|
|
substituteInPlace $out/share/bin/buildenv.mk \
|
2021-11-15 12:04:05 +00:00
|
|
|
--replace 'BINUTILS_DIR ?= /usr/local/bin' \
|
|
|
|
'BINUTILS_DIR ?= ${BINUTILS_DIR}'
|
2021-11-15 02:00:27 +00:00
|
|
|
|
2021-11-15 10:12:51 +00:00
|
|
|
header "Fixing GDB path in bin/sgx-gdb"
|
2021-11-15 10:08:19 +00:00
|
|
|
substituteInPlace $out/bin/sgx-gdb --replace '/usr/local/bin/gdb' '${gdb}/bin/gdb'
|
2021-11-15 00:57:40 +00:00
|
|
|
'';
|
|
|
|
|
2021-06-13 04:54:55 +00:00
|
|
|
doInstallCheck = true;
|
2021-11-15 10:12:51 +00:00
|
|
|
|
2021-06-13 04:54:55 +00:00
|
|
|
installCheckPhase = ''
|
2021-11-15 00:13:24 +00:00
|
|
|
runHook preInstallCheck
|
|
|
|
|
2021-11-15 10:12:51 +00:00
|
|
|
# Make sure all symlinks are valid
|
|
|
|
output=$(find "$out" -type l -exec test ! -e {} \; -print)
|
|
|
|
if [[ -n "$output" ]]; then
|
|
|
|
echo "Broken symlinks:"
|
|
|
|
echo "$output"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-11-15 00:13:24 +00:00
|
|
|
runHook postInstallCheck
|
2021-06-13 04:54:55 +00:00
|
|
|
'';
|
|
|
|
|
2021-11-15 17:56:28 +00:00
|
|
|
setupHook = writeText "setup-hook.sh" ''
|
|
|
|
sgxsdk() {
|
|
|
|
export SGX_SDK=@out@
|
|
|
|
}
|
|
|
|
|
|
|
|
postHooks+=(sgxsdk)
|
|
|
|
'';
|
|
|
|
|
2021-11-15 17:57:27 +00:00
|
|
|
passthru.tests = callPackage ./samples.nix { };
|
|
|
|
|
2021-11-19 00:04:18 +00:00
|
|
|
meta = {
|
2021-06-13 04:54:55 +00:00
|
|
|
description = "Intel SGX SDK for Linux built with IPP Crypto Library";
|
|
|
|
homepage = "https://github.com/intel/linux-sgx";
|
2021-11-15 10:26:50 +00:00
|
|
|
maintainers = with maintainers; [ sbellem arturcygan veehaitch ];
|
2021-06-13 04:54:55 +00:00
|
|
|
platforms = [ "x86_64-linux" ];
|
|
|
|
license = with licenses; [ bsd3 ];
|
|
|
|
};
|
|
|
|
}
|