Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/spin-machine-dev/verbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ func packed(t *testing.T, dir string) string {
put(t, filepath.Join(out, build.ShirudoSigning, "gen-image", f), f, 0o755)
}
put(t, op("share/shirudo/vmlinux.h"), "struct x;\n", 0o644)
for _, u := range build.ShirudoRootUnits {
put(t, filepath.Join(out, build.ShirudoUnits, u), "[Service]\nExecStart=/usr/bin/shirudo init\n", 0o644)
}
put(t, op("install"), "#!/bin/sh\n", 0o755)
put(t, op("licenses.txt"), "licences", 0o644)
put(t, op("debs/Packages"), "Package: dracut-core\n", 0o644)
Expand Down
5 changes: 5 additions & 0 deletions docs/shirudo.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ from (versions.yaml, `shirudo`; kernel/README.md, "shirudo's hooks"):
objects and makes the config and runtime images with it, and compiles nothing. `readelf`,
`llvm-strip`, `veritysetup` and `mkfs.ext4` come from the control plane's image (spin-stack/ami#257).
See "Signing as the control plane does".
- **The root's units**, `usr/share/spin-stack/shirudo/units/` in the main tarball: `shirudo.service`
and `shirudo-handover.service`, the pinned commit's `systemd/*.in` rendered as
`shirudo-operator.sh` renders them, `@bindir@` as `/usr/bin`. spin's control plane publishes these
in the `shirudo` extension and keeps no copies of its own (#905). The build refuses one that is
missing, keeps a placeholder, or does not run `/usr/bin/shirudo`.
- **The operator half**, `usr/share/spin-stack/shirudo-operator/` in the second tarball,
`spin-machine-VERSION-linux-x86_64.shirudo-operator.tar.gz`. It makes what a shirudo machine boots
with around the guest half: the operator's key, the signed config image, the dm-verity runtime
Expand Down
36 changes: 35 additions & 1 deletion internal/build/shirudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const (
var shirudoPins = strings.Fields("alpine ubuntu ubuntu-snapshot llvm llvm-license shirudo openssl curl tpm2-tss")

func makeShirudo(ctx context.Context, o Options, p Part) error {
if err := emptied(o.Out, ShirudoBinary, ShirudoLicenses, ShirudoPackages, ShirudoOperator, ShirudoOperatorPackages, ShirudoObjects, ShirudoSigning); err != nil {
if err := emptied(o.Out, ShirudoBinary, ShirudoLicenses, ShirudoPackages, ShirudoOperator, ShirudoOperatorPackages, ShirudoObjects, ShirudoSigning, ShirudoUnits); err != nil {
return err
}
args, err := partArgs(o, p)
Expand Down Expand Up @@ -280,6 +280,18 @@ const ShirudoObjects = "shirudo/objects"
// operator half's clang (#886).
const ShirudoSigning = "shirudo/libexec"

// ShirudoUnits is where the units a shirudo workspace's root runs are, under Out and under the
// main tarball's usr/share/spin-stack: the pinned commit's systemd/<unit>.in rendered as
// shirudo-operator.sh renders them, @bindir@ as /usr/bin. spin's control plane publishes these in
// the shirudo extension, so it keeps no copies that drift from the pin (#905).
const ShirudoUnits = "shirudo/units"

// ShirudoRootUnits are the units ShirudoUnits holds, as shirudo/Dockerfile renders them.
var ShirudoRootUnits = []string{"shirudo.service", "shirudo-handover.service"}

// placeholder is a substitution of shirudo's systemd/*.in, such as @bindir@, left unrendered.
var placeholder = regexp.MustCompile(`@[a-z_]+@`)

// A shirudoObject is one object an installation signs: the upatch name it is signed under, and the
// C file it is compiled from.
type shirudoObject struct{ name, source string }
Expand Down Expand Up @@ -425,5 +437,27 @@ func VerifyShirudo(o Options) error {
return fmt.Errorf("missing %s: %w", f, err)
}
}
if err := verifyUnits(o); err != nil {
return err
}
return verifyObjects(o)
}

// verifyUnits asserts each of ShirudoRootUnits is rendered: it runs the shirudo a workspace's root
// has, at /usr/bin, and leaves no substitution unmade.
func verifyUnits(o Options) error {
for _, u := range ShirudoRootUnits {
f := filepath.Join(ShirudoUnits, u)
b, err := os.ReadFile(filepath.Join(o.Out, f)) // #nosec G304 -- a unit this build rendered
if err != nil {
return fmt.Errorf("missing %s: %w", f, err)
}
if m := placeholder.Find(b); m != nil {
return fmt.Errorf("%s leaves %s unrendered", f, m)
}
if !bytes.Contains(b, []byte("\nExecStart=/usr/bin/shirudo ")) {
return fmt.Errorf("%s does not run /usr/bin/shirudo", f)
}
}
return nil
}
19 changes: 18 additions & 1 deletion internal/build/shirudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func shirudoTree(t *testing.T, out, bin string, v *versions.Versions) {
op("libexec/shirudo/gen-initrd/shirudo-gen-initrd"): []byte("#!/bin/sh\n"),
sign("shirudo-build"): b, sign("gen-image/shirudo-gen-image"): []byte("#!/bin/sh\n"), sign("gen-image/shirudo-gen-runtime"): []byte("#!/bin/sh\n"),
sign("gen-image/shirudo-gen-verity"): []byte("#!/bin/sh\n"), sign("gen-image/common.sh"): []byte("x=1\n"),
op("share/shirudo/vmlinux.h"): []byte("struct x;\n"),
op("share/shirudo/vmlinux.h"): []byte("struct x;\n"),
filepath.Join(ShirudoUnits, "shirudo.service"): []byte("[Service]\nExecStart=/usr/bin/shirudo init $SHIRUDO_INIT_ARGS\n"),
filepath.Join(ShirudoUnits, "shirudo-handover.service"): []byte("[Service]\nExecStart=/usr/bin/shirudo init $SHIRUDO_INIT_ARGS\n"),
op("share/shirudo/upatches/kernel-cve/sources/cve-2026-31431-crypto-algif-aead-copy-fail/mitigation/cve-2026-31431-af-alg-block.bpf.c"): []byte("af-alg-block\n"),
} {
if err := os.MkdirAll(filepath.Dir(filepath.Join(out, f)), 0o755); err != nil {
Expand Down Expand Up @@ -246,6 +248,7 @@ func TestShirudoVerifiesAsAStaticBinaryOfThePin(t *testing.T) {
filepath.Join(ShirudoOperator, ShirudoBuild): "missing shirudo-build",
filepath.Join(ShirudoSigning, "gen-image/shirudo-gen-runtime"): "missing " + filepath.Join(ShirudoSigning, "gen-image/shirudo-gen-runtime"),
filepath.Join(ShirudoSigning, "shirudo-build"): "missing shirudo-build",
filepath.Join(ShirudoUnits, "shirudo-handover.service"): "missing " + filepath.Join(ShirudoUnits, "shirudo-handover.service"),
} {
out := t.TempDir()
shirudoTree(t, out, named(t, load, e.Pin[:7]), v)
Expand Down Expand Up @@ -275,6 +278,20 @@ func TestShirudoVerifiesAsAStaticBinaryOfThePin(t *testing.T) {
if err := VerifyShirudo(Options{Root: repo, Out: dynamic, Pins: v}); err == nil || !strings.Contains(err.Error(), "is linked dynamically") {
t.Errorf("a dynamic shirudo-build: %v", err)
}
// A unit is the one a workspace's root runs: rendered, and running /usr/bin/shirudo.
for unit, want := range map[string]string{
"[Service]\nExecStart=@bindir@/shirudo init\n": "leaves @bindir@ unrendered",
"[Service]\nExecStart=/usr/local/bin/shirudo init\n": "does not run /usr/bin/shirudo",
} {
out := t.TempDir()
shirudoTree(t, out, named(t, load, e.Pin[:7]), v)
if err := os.WriteFile(filepath.Join(out, ShirudoUnits, "shirudo.service"), []byte(unit), 0o644); err != nil {
t.Fatal(err)
}
if err := VerifyShirudo(Options{Root: repo, Out: out, Pins: v}); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("a unit %q: %v, want %q", unit, err, want)
}
}
other := t.TempDir()
shirudoTree(t, other, named(t, load, e.Pin[:7]), v)
if err := os.WriteFile(filepath.Join(other, ShirudoOperator, ShirudoClang), []byte("#!/bin/sh\necho 22.1.0\n"), 0o755); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/release/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ func shipped(o Options, top, share string) ([]file, error) {
for _, p := range objects {
files = append(files, file{p, at(build.ShirudoObjects + "/" + filepath.Base(p)), "the shirudo object " + filepath.Base(p), 0o644})
}
// The units a shirudo workspace's root runs (build.ShirudoUnits), every one the directory holds.
units, _ := filepath.Glob(fromOut(build.ShirudoUnits + "/*"))
if len(units) == 0 {
return nil, fmt.Errorf("missing every shirudo unit in %s (spin-machine-dev build shirudo renders them)", fromOut(build.ShirudoUnits))
}
for _, p := range units {
files = append(files, file{p, at(build.ShirudoUnits + "/" + filepath.Base(p)), "the shirudo unit " + filepath.Base(p), 0o644})
}
// shirudo's signing subset (build.ShirudoSigning), whatever the directory holds, each a regular
// file because spin's install drops links. No clang: the control plane signs the objects above
// and compiles nothing.
Expand Down
33 changes: 30 additions & 3 deletions internal/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func tree(t *testing.T) (root, out string) {
put(t, filepath.Join(out, build.ShirudoLicenses), "shirudo's licences", 0o644)
put(t, filepath.Join(out, build.ShirudoObjects, "spin-seal.o"), "seal", 0o644)
put(t, filepath.Join(out, build.ShirudoObjects, "af-alg-block.o"), "af-alg-block", 0o644)
for _, u := range build.ShirudoRootUnits {
put(t, filepath.Join(out, build.ShirudoUnits, u), "ExecStart=/usr/bin/shirudo init\n", 0o644)
}
put(t, filepath.Join(out, build.ShirudoPackages), "libc6-dev 2.43-2ubuntu2.7 amd64\nlibelf-dev 0.194-4 amd64\n", 0o644)
op := filepath.Join(out, build.ShirudoOperator)
put(t, filepath.Join(op, "install"), "#!/bin/sh\n", 0o755)
Expand Down Expand Up @@ -178,8 +181,8 @@ func TestAReleaseShipsEveryPartAndSaysWhatItIs(t *testing.T) {
t.Errorf("%s: in the tarball %v, mode %o; want %o", f, ok, m, mode)
}
}
if len(got) != 51 {
t.Errorf("the tarball holds %d files, want 51: %v", len(got), slices.Sorted(func(yield func(string) bool) {
if len(got) != 53 {
t.Errorf("the tarball holds %d files, want 53: %v", len(got), slices.Sorted(func(yield func(string) bool) {
for k := range got {
if !yield(k) {
return
Expand Down Expand Up @@ -297,6 +300,29 @@ func TestTheObjectsRecordNamesTheShippedObjects(t *testing.T) {
}
}

// The units a workspace's root runs are in the main tarball, so spin publishes them from the
// release and keeps no copies (#905); a build that rendered none is refused.
func TestTheRootUnitsAreInTheMainTarball(t *testing.T) {
root, out := tree(t)
o := options(t, root, out)
packed, err := Pack(context.Background(), o)
if err != nil {
t.Fatal(err)
}
files := contents(t, packed.Tarball)
for _, u := range build.ShirudoRootUnits {
if body, ok := files[Name(o.Version)+"/usr/share/spin-stack/"+build.ShirudoUnits+"/"+u]; !ok || !strings.Contains(string(body), "ExecStart=/usr/bin/shirudo") {
t.Errorf("the main tarball's %s/%s: %q", build.ShirudoUnits, u, body)
}
}
if err := os.RemoveAll(filepath.Join(out, build.ShirudoUnits)); err != nil {
t.Fatal(err)
}
if _, err := Pack(context.Background(), o); err == nil || !strings.Contains(err.Error(), "missing every shirudo unit") {
t.Errorf("a build without the units packed: %v", err)
}
}

// contents is every regular file in the tarball at path, by its name.
func contents(t *testing.T, path string) map[string][]byte {
t.Helper()
Expand Down Expand Up @@ -337,7 +363,8 @@ func TestTheOperatorHalfIsATarballOfItsOwn(t *testing.T) {
subset := map[string]int64{share + "shirudo/libexec/shirudo-build": 0o755,
share + "shirudo/libexec/gen-image/shirudo-gen-image": 0o755, share + "shirudo/libexec/gen-image/shirudo-gen-runtime": 0o755,
share + "shirudo/libexec/gen-image/shirudo-gen-verity": 0o755, share + "shirudo/libexec/gen-image/common.sh": 0o644,
share + "shirudo/libexec/gen-image/runtime-service.conf.in": 0o644, share + "shirudo/libexec/gen-image/runtime-service-static.conf.in": 0o644}
share + "shirudo/libexec/gen-image/runtime-service.conf.in": 0o644, share + "shirudo/libexec/gen-image/runtime-service-static.conf.in": 0o644,
share + "shirudo/units/shirudo.service": 0o644, share + "shirudo/units/shirudo-handover.service": 0o644}
operator := map[string]bool{}
_ = filepath.WalkDir(filepath.Join(out, build.ShirudoOperator), func(p string, d fs.DirEntry, err error) error {
if err == nil && !d.IsDir() {
Expand Down
9 changes: 8 additions & 1 deletion shirudo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ RUN echo dracut-core zstd cpio cryptsetup-bin binutils llvm python3 > wanted &&
# root (shirudo/operator-install); every file a machine boots is untouched by it. Its shirudo-build
# and gen-image are copied to shirudo/libexec too, the signing subset the main tarball ships: what
# an installation's control plane signs the objects and makes the images with, without clang (#886).
# The units a workspace's root runs go to shirudo/units, rendered as shirudo-operator.sh renders
# them, for spin's control plane to publish (#905).
FROM build AS operator
ARG LLVM_VERSION
ARG LLVM_LICENSE_VERSION
Expand All @@ -192,7 +194,12 @@ RUN set -e; o=/out/shirudo-operator; l=$o/libexec/shirudo/llvm; \
test "$("$l/bin/clang" -dumpversion)" = "${LLVM_VERSION}"; \
install -m 0755 /spin/operator-install "$o/install"; \
mkdir -p /out/shirudo/libexec; \
cp -R "$o/libexec/shirudo/shirudo-build" "$o/libexec/shirudo/gen-image" /out/shirudo/libexec/
cp -R "$o/libexec/shirudo/shirudo-build" "$o/libexec/shirudo/gen-image" /out/shirudo/libexec/; \
mkdir -p /out/shirudo/units; \
for u in shirudo.service shirudo-handover.service; do \
sed 's|@bindir@|/usr/bin|g' "$o/share/shirudo/systemd/$u.in" >"/out/shirudo/units/$u"; \
done; \
! grep -n '@[a-z_]*@' /out/shirudo/units/*
COPY --from=operator-debs /debs /out/shirudo-operator/debs
COPY --from=operator-debs /out/sbom /out/sbom
# The licences of what the operator half carries beyond the debs, whose own are inside them: what
Expand Down