Skip to content

Commit 062c7ef

Browse files
committed
List the crates Rust's std brings into the binaries in their SBOMs
std is built from crates.io crates (gimli, addr2line, miniz_oxide, hashbrown...), and which ones end up in a binary depends on the platform. The SBOM script finds them by the source paths their panic messages carry, adds the ones known to be there without such paths, and takes the versions from the pinned toolchain's own lockfile, failing on any crate it does not know the license of. compiler-builtins is listed on its own, as part of it is under Apache-2.0 with the LLVM exception.
1 parent 26f782a commit 062c7ef

3 files changed

Lines changed: 74 additions & 16 deletions

File tree

‎.github/scripts/sbom.sh‎

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!/bin/bash
22
# Writes a CycloneDX SBOM (<binary>.cdx.json) for each binary in $1, from the <binary>.toolchain file its build
33
# left there, then removes the toolchain files. Nothing in the output depends on when or where this runs.
4+
# Needs the pinned Rust toolchain with its rust-src component, for the versions of the crates std is built from.
45
set -euo pipefail
56

67
dir=$1
8+
sysroot=$(cd native && rustc --print sysroot)
9+
rustc_version=$(cd native && rustc --version | cut -d' ' -f2)
10+
std_lock="$sysroot/lib/rustlib/src/rust/library/Cargo.lock"
11+
[ -f "$std_lock" ] || { echo "no $std_lock, install the rust-src component" >&2; exit 1; }
712
version=$(sed -n '1,/^\[/!b; s/^version = "\(.*\)"$/\1/p' native/Cargo.toml)
813
zstd=$(sed -n 's/.* v\([0-9.]*\) release.*/\1/p' native/zstd/README.md)
914
zstd_sha=$(sed -n 's/^SHA-256: //p' native/zstd/README.md)
@@ -29,13 +34,57 @@ tool() {
2934
jq -n --arg name "$1" --arg version "$2" '{type: "application", name: $name, version: $version}'
3035
}
3136

32-
# the Rust standard library, linked statically on every platform
33-
rust_std() {
34-
jq -n --arg version "$(tc rustc)" '{
35-
type: "library", "bom-ref": "rust-std", name: "Rust standard library", version: $version,
36-
licenses: [{expression: "MIT OR Apache-2.0"}],
37-
purl: "pkg:github/rust-lang/rust@\($version)"
38-
}'
37+
# crates.io crates std is built from, with their licenses
38+
declare -A crate_license=(
39+
[addr2line]="Apache-2.0 OR MIT"
40+
[adler2]="0BSD OR MIT OR Apache-2.0"
41+
[gimli]="MIT OR Apache-2.0"
42+
[hashbrown]="MIT OR Apache-2.0"
43+
[libc]="MIT OR Apache-2.0"
44+
[miniz_oxide]="MIT OR Zlib OR Apache-2.0"
45+
[object]="Apache-2.0 OR MIT"
46+
[rustc-demangle]="MIT OR Apache-2.0"
47+
)
48+
49+
# Rust's std and the crates it brings into the binary. Crates with panic messages carry their source path,
50+
# /rust/deps/<name>-<version>/, the others are known to be there: hashbrown backs HashMap, libc and object are
51+
# used by std everywhere but on Windows, adler2 by miniz_oxide.
52+
rust_components() {
53+
local bin=$1 windows=$2 crates
54+
crates=$(LC_ALL=C grep -aoE '/rust/deps[/\\][A-Za-z0-9_-]+-[0-9][0-9A-Za-z.+-]*[/\\]' "$bin" | sed -E 's#^/rust/deps.##; s#.$##' | sort -u)
55+
crates=$(printf '%s\n' "$crates" hashbrown-auto)
56+
[ "$windows" = 1 ] || crates=$(printf '%s\n' "$crates" libc-auto object-auto)
57+
case $crates in *miniz_oxide-*) crates=$(printf '%s\n' "$crates" adler2-auto) ;; esac
58+
[ "$(tc rustc)" = "$rustc_version" ] || { echo "$bin: built with rustc $(tc rustc), but $rustc_version is installed" >&2; exit 1; }
59+
local list=() crate name found lock_version
60+
for crate in $(printf '%s\n' "$crates" | sed '/^$/d' | sort -u); do
61+
name=${crate%-*}
62+
found=${crate##*-}
63+
lock_version=$(awk -v n="$name" '$0 == "name = \"" n "\"" { getline; gsub(/version = |"/, ""); print; exit }' "$std_lock")
64+
[ -n "$lock_version" ] || { echo "$bin: $name is not one of std's dependencies" >&2; exit 1; }
65+
[ "$found" = auto ] || [ "$found" = "$lock_version" ] || { echo "$bin: $name $found, std uses $lock_version" >&2; exit 1; }
66+
[ -n "${crate_license[$name]:-}" ] || { echo "$bin: unknown crate $name, add its license to sbom.sh" >&2; exit 1; }
67+
list+=("$(jq -n --arg name "$name" --arg version "$lock_version" --arg license "${crate_license[$name]}" '{
68+
type: "library", "bom-ref": "crate-\($name)", name: $name, version: $version,
69+
description: "Part of the Rust standard library build",
70+
licenses: [{expression: $license}],
71+
purl: "pkg:cargo/\($name)@\($version)"
72+
}')")
73+
done
74+
jq -n --arg version "$rustc_version" '[
75+
{
76+
type: "library", "bom-ref": "rust-std", name: "Rust standard library", version: $version,
77+
licenses: [{expression: "(MIT OR Apache-2.0) AND Unicode-3.0"}],
78+
purl: "pkg:github/rust-lang/rust@\($version)",
79+
cpe: "cpe:2.3:a:rust-lang:rust:\($version):*:*:*:*:*:*:*"
80+
},
81+
{
82+
type: "library", "bom-ref": "compiler-builtins", name: "compiler-builtins", version: $version,
83+
description: "Part of the Rust toolchain, with code ported from LLVM compiler-rt",
84+
licenses: [{expression: "MIT AND Apache-2.0 WITH LLVM-exception AND (MIT OR Apache-2.0)"}],
85+
purl: "pkg:github/rust-lang/rust@\($version)#library/compiler-builtins"
86+
}
87+
] + ($ARGS.positional | map(fromjson))' --args "${list[@]}"
3988
}
4089

4190
for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-darwin zstd-extract-windows-x86_64.exe; do
@@ -48,8 +97,7 @@ for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-da
4897
linked=$(jq -n '$ARGS.positional | map(fromjson)' --args \
4998
"$(apk musl musl MIT)" \
5099
"$(apk llvm-libunwind llvm-libunwind-static 'Apache-2.0 WITH LLVM-exception')" \
51-
"$(apk fortify-headers fortify-headers 0BSD)" \
52-
"$(rust_std)")
100+
"$(apk fortify-headers fortify-headers 0BSD)")
53101
tools=$(jq -n '$ARGS.positional | map(fromjson)' --args \
54102
"$(tool rustc "$(tc rustc)")" \
55103
"$(apk gcc gcc 'GPL-3.0-or-later' | jq '.type = "application" | del(."bom-ref")')" \
@@ -58,21 +106,22 @@ for name in zstd-extract-linux-x86_64 zstd-extract-linux-aarch64 zstd-extract-da
58106
;;
59107
*-darwin)
60108
# libSystem is linked dynamically, so only zstd and Rust's std are part of the binary
61-
linked=$(jq -n '$ARGS.positional | map(fromjson)' --args "$(rust_std)")
109+
linked='[]'
62110
tools=$(jq -n '$ARGS.positional | map(fromjson)' --args \
63111
"$(tool rustc "$(tc rustc)")" "$(tool 'Apple clang' "$(tc clang)")" "$(tool 'macOS SDK' "$(tc sdk)")")
64112
;;
65113
*-windows-*)
66114
license='{"license": {"name": "Microsoft Visual Studio and Windows SDK license terms"}}'
67-
linked=$(jq -n --arg msvc "$(tc msvc)" --arg ucrt "$(tc ucrt)" --argjson license "$license" --argjson std "$(rust_std)" '[
115+
linked=$(jq -n --arg msvc "$(tc msvc)" --arg ucrt "$(tc ucrt)" --argjson license "$license" '[
68116
{type: "library", "bom-ref": "vcruntime", name: "Microsoft Visual C++ runtime (static)", version: $msvc, licenses: [$license]},
69-
{type: "library", "bom-ref": "ucrt", name: "Microsoft Universal C runtime (static)", version: $ucrt, licenses: [$license]},
70-
$std
117+
{type: "library", "bom-ref": "ucrt", name: "Microsoft Universal C runtime (static)", version: $ucrt, licenses: [$license]}
71118
]')
72119
tools=$(jq -n '$ARGS.positional | map(fromjson)' --args \
73120
"$(tool rustc "$(tc rustc)")" "$(tool 'MSVC' "$(tc msvc)")" "$(tool 'Windows SDK' "$(tc sdk)")")
74121
;;
75122
esac
123+
case $name in *-windows-*) windows=1 ;; *) windows=0 ;; esac
124+
linked=$(jq -n --argjson linked "$linked" --argjson rust "$(rust_components "$dir/$name" "$windows")" '$linked + $rust')
76125

77126
jq -n --arg name "$name" --arg version "$version" --arg sha "$(sha256sum "$dir/$name" | cut -d' ' -f1)" \
78127
--arg zstd "$zstd" --arg zstd_sha "$zstd_sha" --argjson linked "$linked" --argjson tools "$tools" '{

‎.github/workflows/build.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ jobs:
131131
pattern: '{linux-*,darwin,windows-*}'
132132
merge-multiple: true
133133

134+
# the pinned toolchain's rust-src has the versions of the crates std is built from
135+
- name: Install rust-src
136+
working-directory: native
137+
run: rustup toolchain install && rustup component add rust-src
138+
134139
- name: SBOMs and checksums
135140
run: |
136141
.github/scripts/sbom.sh "$RUNNER_TEMP/bin"

‎README.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ gh attestation verify bin/zstd-extract-linux-x86_64 --repo composer/zstd
206206
```
207207

208208
Each binary comes with a CycloneDX SBOM, `bin/<binary>.cdx.json`, listing what is linked into it: the zstd
209-
decoder, the Rust standard library, on Linux musl and libunwind with their exact Alpine package versions, and on
210-
Windows the Microsoft C runtime.
209+
decoder, the Rust standard library with the crates it is built from (found in the binary, versions from the
210+
toolchain's lockfile), on Linux musl and libunwind with their exact Alpine package versions, and on Windows the
211+
Microsoft C runtime.
211212
Dependency scanners cannot see these otherwise, as static binaries carry no package metadata. The SBOMs are
212213
generated by the build, compared like the binaries, and attested by the release workflow:
213214

@@ -338,7 +339,10 @@ composer/zstd is licensed under the MIT License, see the LICENSE file for detail
338339
The bundled binaries also contain:
339340

340341
- zstd (decoder only), used under its BSD license (zstd is dual-licensed BSD/GPLv2): `bin/LICENSE.zstd`
341-
- the Rust standard library, used under its MIT license (it is dual-licensed MIT/Apache-2.0): `bin/LICENSE.rust`
342+
- the Rust standard library, used under its MIT license (it is dual-licensed MIT/Apache-2.0), including Unicode
343+
data tables under the Unicode license: `bin/LICENSE.rust`. The crates it is built from, listed in the SBOMs, are
344+
all available under MIT as well. Its compiler-builtins part contains code under Apache-2.0 with the LLVM
345+
exception, which waives the notice requirements for binaries
342346
- on Linux, musl libc, MIT licensed: `bin/LICENSE.musl`, and LLVM's libunwind, whose LLVM exception waives the
343347
notice requirements for binaries
344348
- on Windows, the statically linked Microsoft C runtime, which Microsoft's Visual C++ license allows to be

0 commit comments

Comments
 (0)