Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Provenance: where a build came from

After this lesson: I can check a build's provenance against a policy before I deploy it.

60 min lesson12 commandsDevSecOps lab: build a security pipeline and a gate

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Signed, but built from what?

In 2020 it came out that attackers had been inside SolarWinds' build system. The update they changed was signed with the company's real key, and thousands of customers installed it.

The signature was genuine. What nobody checked was how, and from what source, the file had been built.

By the end of this lesson

Note

I can check a build's provenance against a policy before I deploy it.

Key words

WordWhat it meansExample
Provenancea record of how and from what a file was built
Attestationa signed statement about an artifact
Subjectthe artifact a statement is about, named by its digest
Builderthe system that ran the build
SLSAlevels of trust in how software is builtBuild L2
Policythe rules an artifact must meet to be deployed

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The rule for this lesson

Warning

You will forge a build record and swap a file. Do it only in your own lab copy, or on a system whose owner has said yes in writing. Tampering with someone else's build without permission is a crime under the Computer Misuse Act 1990.

Provenance says what, from where, by whom

Provenance is a statement written by the builder. Its subject is the artifact, named by its digest. It records the repository, the branch, the exact commit and the builder's id.

The common shape is an in-toto statement with a SLSA predicate. The builder signs it, so it is an attestation.

Clone the shop in the terminal

Command
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.git

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make the builder's key

Command
test -f builder_key || ssh-keygen -q -t ed25519 -N "" -C [email protected] -f builder_key; echo "[email protected] $(cat builder_key.pub)" > builders

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the build job

Command
cat > build.sh <<'EOF'
set -e
git -C tuckshop fetch -q origin
commit=$(git -C tuckshop rev-parse "origin/$1")
git -C tuckshop archive --format=tar.gz --prefix=tuckshop/ -o ../tuckshop.tar.gz "$commit"
digest=$(sha256sum tuckshop.tar.gz | cut -c1-64)
cat > provenance.json <<END
{"_type": "https://in-toto.io/Statement/v1",
"subject": [{"name": "tuckshop.tar.gz", "digest": {"sha256": "$digest"}}],
"predicateType": "https://slsa.dev/provenance/v1",
"predicate": {
"buildDefinition": {"buildType": "https://tuckshop.example/build/v1",
"externalParameters": {"repository": "http://gitea:3000/student/tuckshop", "ref": "refs/heads/$1"},
"resolvedDependencies": [{"uri": "git+http://gitea:3000/student/tuckshop", "digest": {"gitCommit": "$commit"}}]},
"runDetails": {"builder": {"id": "https://tuckshop.example/builder"}}}}
END
rm -f provenance.json.sig
ssh-keygen -q -Y sign -f builder_key -n slsa-provenance provenance.json
echo "Built $1 at $commit"
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Build main

Command
bash build.sh main

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the provenance

Command
cat provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Check the signature, then the policy

A user of the artifact checks two things, in order. First the signature: is this statement from the builder we trust? Then the policy: does what it says match our rules?

The shop's policy: the right digest, the trusted builder, the shop's repository and the main branch.

Write the policy check

Command
docker exec -i ci sh -c "cat > checkprov.py" <<'EOF'
# Checks provenance against the policy: python3 checkprov.py SHA256 < provenance.json
import json
import sys

statement = json.load(sys.stdin)
build = statement["predicate"]["buildDefinition"]
checks = [
    ("SLSA provenance", statement["predicateType"] == "https://slsa.dev/provenance/v1"),
    ("artifact digest", statement["subject"][0]["digest"]["sha256"] == sys.argv[1]),
    ("trusted builder", statement["predicate"]["runDetails"]["builder"]["id"] == "https://tuckshop.example/builder"),
    ("source repository", build["externalParameters"]["repository"] == "http://gitea:3000/student/tuckshop"),
    ("built from main", build["externalParameters"]["ref"] == "refs/heads/main"),
]
for name, passed in checks:
    print("PASS" if passed else "FAIL", name)
accepted = all(passed for name, passed in checks)
print("Verdict:", "ACCEPT" if accepted else "REJECT")
sys.exit(0 if accepted else 1)
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check the signature

Command
ssh-keygen -Y verify -f builders -I [email protected] -n slsa-provenance -s provenance.json.sig < provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check the policy

Command
docker exec -i ci python3 checkprov.py $(sha256sum tuckshop.tar.gz | cut -c1-64) < provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: read a statement

Code: python
statement = {
    "subject": [{"name": "tuckshop.tar.gz", "digest": {"sha256": "800cc9d8"}}],
    "predicate": {"buildDefinition": {"externalParameters": {"ref": "refs/heads/hotfix"}},
                  "runDetails": {"builder": {"id": "https://tuckshop.example/builder"}}},
}

ref = statement["predicate"]["buildDefinition"]["externalParameters"]["ref"]
builder = statement["predicate"]["runDetails"]["builder"]["id"]
print("built from", ref, "by", builder)
print("accept?", ref == "refs/heads/main")

# Change the ref to refs/heads/main and press Run. What changes?

Make a free account to save your work and track your progress.

Three ways round it, all refused

Attack your own copy three ways. Build a branch nobody reviewed. Edit the statement to claim it was main. Swap the artifact for another file.

Run the same two checks after each and read which one fails.

Push an unreviewed branch and build it

Command
git -C tuckshop switch -q -C hotfix origin/main && echo 'print("an untested change")' > tuckshop/hotfix.py && git -C tuckshop add hotfix.py && git -C tuckshop -c user.name="Sam Lab" -c [email protected] commit -q -m "Quick fix" && git -C tuckshop push -q -f origin hotfix 2> /dev/null && bash build.sh hotfix

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check the hotfix build's policy

Command
docker exec -i ci python3 checkprov.py $(sha256sum tuckshop.tar.gz | cut -c1-64) < provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Claim it was main, then check the signature

Command
sed -i 's#refs/heads/hotfix#refs/heads/main#' provenance.json && ssh-keygen -Y verify -f builders -I [email protected] -n slsa-provenance -s provenance.json.sig < provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Build main, then check a different file

Command
bash build.sh main && printf 'print("not from the shop")\n' > other.py && tar -czf other.tar.gz other.py && docker exec -i ci python3 checkprov.py $(sha256sum other.tar.gz | cut -c1-64) < provenance.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think any signed provenance is good provenance. Here you held the builder's key yourself, so you could have signed anything. Provenance is only as trustworthy as the builder: at SLSA Build L3 the build platform signs it, with a key no build step can reach.

SLSA levels say how far to trust it

SLSA, said "salsa", sets build levels. L1: provenance exists. L2: a hosted build platform makes and signs it. L3: the platform is hardened, so one build cannot touch another or the signing key.

This lab is L1 in substance. GitHub can make signed provenance for a workflow, and tools such as slsa-verifier run the checks you just ran.

Assignment: a provenance policy

A provenance statement is a dictionary shaped like the lab's provenance.json. A policy is a dictionary such as {"builders": [...], "repository": "...", "refs": [...]}. Write three functions.

facts(statement) returns a dictionary with the keys digest (the first subject's sha256), builder, repository, ref and commit (the first resolved dependency's gitCommit). problems(statement, digest, policy) returns a sorted list of the words "digest", "builder", "repository" and "ref" for each fact that breaks the policy: the digest must equal digest, the builder must be in builders, the repository must equal repository, and the ref must be in refs. decide(statement, digest, policy) returns "ACCEPT" when there are no problems and "REJECT" otherwise.

Code: python
def facts(statement):
    return {}


def problems(statement, digest, policy):
    return []


def decide(statement, digest, policy):
    return "ACCEPT"

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.