|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Contract: CVE-2026-53359 KVM-nested-virtualization mitigation in the |
| 16 | +// cozystack and generic presets. kvm_intel/kvm_amd are built into the |
| 17 | +// Talos kernel, so nested= is settable only from the kernel command |
| 18 | +// line; both presets pin it via machine.install.extraKernelArgs. |
| 19 | +// |
| 20 | +// On Talos >1.11 the generated base config defaults |
| 21 | +// machine.install.grubUseUKICmdline to true (UKI cmdline), and Talos |
| 22 | +// rejects extraKernelArgs alongside it. The presets therefore pin |
| 23 | +// grubUseUKICmdline:false wherever the base would set it true. The |
| 24 | +// full-pipeline test below renders through engine.Render (base bundle + |
| 25 | +// preset patch) and runs Talos's own validation to prove the merged |
| 26 | +// config the operator applies does not trip the UKI conflict. |
| 27 | + |
| 28 | +package engine |
| 29 | + |
| 30 | +import ( |
| 31 | + "context" |
| 32 | + "strings" |
| 33 | + "testing" |
| 34 | + |
| 35 | + helmEngine "github.com/cozystack/talm/pkg/engine/helm" |
| 36 | + "github.com/siderolabs/talos/pkg/machinery/config/configloader" |
| 37 | +) |
| 38 | + |
| 39 | +// kvmNestedRuntimeMode is a minimal validation.RuntimeMode implementation. |
| 40 | +// RequiresInstall() true selects the install-mode validation branch that |
| 41 | +// enforces the extraKernelArgs/grubUseUKICmdline mutual exclusion. |
| 42 | +type kvmNestedRuntimeMode struct{ requiresInstall bool } |
| 43 | + |
| 44 | +func (m kvmNestedRuntimeMode) String() string { return "kvm-nested-contract" } |
| 45 | +func (m kvmNestedRuntimeMode) RequiresInstall() bool { return m.requiresInstall } |
| 46 | +func (kvmNestedRuntimeMode) InContainer() bool { return false } |
| 47 | + |
| 48 | +// Contract: both presets always emit the KVM nested-virt kernel args |
| 49 | +// under machine.install, for every chart × schema × machineType cell. |
| 50 | +// A regression that drops them silently re-exposes the guest-to-host |
| 51 | +// escape on every node the preset installs. |
| 52 | +func TestContract_Machine_Install_KVMNestedArgs(t *testing.T) { |
| 53 | + for _, cell := range allCells() { |
| 54 | + t.Run(cell.name, func(t *testing.T) { |
| 55 | + out := renderChartTemplate(t, cell.chartPath, cell.templateFile, cell.talosVersion) |
| 56 | + assertContains(t, out, "extraKernelArgs:") |
| 57 | + assertContains(t, out, "kvm_intel.nested=0") |
| 58 | + assertContains(t, out, "kvm_amd.nested=0") |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Contract: grubUseUKICmdline:false is emitted exactly where the |
| 64 | +// generated base config would default it to true — i.e. when no |
| 65 | +// --talos-version is pinned (the bundle falls back to the current |
| 66 | +// contract, >1.11) or when the pinned version is >=1.12. For an |
| 67 | +// explicit pre-1.12 version the field is omitted, both because the base |
| 68 | +// does not set it and because older Talos schemas reject the key. |
| 69 | +func TestContract_Machine_Install_GrubUKICmdlineGate(t *testing.T) { |
| 70 | + for _, tc := range []struct { |
| 71 | + talosVersion string |
| 72 | + wantField bool |
| 73 | + }{ |
| 74 | + {"", true}, // empty → base uses current contract (>1.11) → pin false |
| 75 | + {"v1.12.0", true}, // >=1.12 → base defaults true → pin false |
| 76 | + {"v1.13.0", true}, // >=1.12 → base defaults true → pin false |
| 77 | + {"v1.11.0", false}, // <1.12 → base leaves it unset → do not emit |
| 78 | + } { |
| 79 | + name := tc.talosVersion |
| 80 | + if name == "" { |
| 81 | + name = "empty" |
| 82 | + } |
| 83 | + t.Run(name, func(t *testing.T) { |
| 84 | + out := renderChartTemplate(t, cozystackChartPath, controlplaneTpl, tc.talosVersion) |
| 85 | + if tc.wantField { |
| 86 | + assertContains(t, out, "grubUseUKICmdline: false") |
| 87 | + } else { |
| 88 | + assertNotContains(t, out, "grubUseUKICmdline") |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Contract: the FULL config an operator applies — base bundle merged |
| 95 | +// with the preset patch, exactly what `talm apply` serialises (Full: |
| 96 | +// true) — passes Talos validation without the UKI/extraKernelArgs |
| 97 | +// conflict, for both presets and both machine types on v1.12+. |
| 98 | +// |
| 99 | +// This is the regression guard for the whole mitigation: the raw Helm |
| 100 | +// output above never carries grubUseUKICmdline (the base bundle injects |
| 101 | +// it), so only a full-pipeline render surfaces the collision. Offline |
| 102 | +// discovery leaves install.disk empty, which trips a separate |
| 103 | +// validation error, so the assertion is scoped to the UKI conflict |
| 104 | +// specifically rather than "no errors at all". |
| 105 | +func TestContract_Machine_Install_FullConfigValidates_NoUKIConflict(t *testing.T) { |
| 106 | + // Offline render still consults the Helm `lookup`; reset it to the |
| 107 | + // empty stub so no live discovery is attempted. |
| 108 | + helmEngine.LookupFunc = func(string, string, string) (map[string]any, error) { |
| 109 | + return map[string]any{}, nil |
| 110 | + } |
| 111 | + |
| 112 | + const ukiConflict = "grubUseUKICmdline" |
| 113 | + |
| 114 | + cells := []struct { |
| 115 | + name string |
| 116 | + chartPath string |
| 117 | + templateFile string |
| 118 | + }{ |
| 119 | + {"cozystack/controlplane", cozystackChartPath, controlplaneTpl}, |
| 120 | + {"cozystack/worker", cozystackChartPath, workerTpl}, |
| 121 | + {"generic/controlplane", genericChartPath, controlplaneTpl}, |
| 122 | + {"generic/worker", genericChartPath, workerTpl}, |
| 123 | + } |
| 124 | + |
| 125 | + for _, cell := range cells { |
| 126 | + t.Run(cell.name, func(t *testing.T) { |
| 127 | + out, err := Render(context.Background(), nil, Options{ |
| 128 | + Offline: true, |
| 129 | + Full: true, |
| 130 | + Root: cell.chartPath, |
| 131 | + TalosVersion: "v1.12.0", |
| 132 | + TemplateFiles: []string{cell.templateFile}, |
| 133 | + Values: []string{ |
| 134 | + "endpoint=" + testEndpoint, |
| 135 | + "advertisedSubnets={" + testAdvertisedSubnet + "}", |
| 136 | + }, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("full render failed: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + got := string(out) |
| 143 | + assertContains(t, got, "grubUseUKICmdline: false") |
| 144 | + assertContains(t, got, "kvm_intel.nested=0") |
| 145 | + if strings.Contains(got, "grubUseUKICmdline: true") { |
| 146 | + t.Fatalf("full config still carries grubUseUKICmdline: true — UKI cmdline not disabled:\n%s", got) |
| 147 | + } |
| 148 | + |
| 149 | + cfg, lerr := configloader.NewFromBytes(out) |
| 150 | + if lerr != nil { |
| 151 | + t.Fatalf("config loader rejected the rendered config: %v", lerr) |
| 152 | + } |
| 153 | + |
| 154 | + _, verr := cfg.Validate(kvmNestedRuntimeMode{requiresInstall: true}) |
| 155 | + if verr != nil && strings.Contains(verr.Error(), ukiConflict) { |
| 156 | + t.Fatalf("Talos validation reports the UKI/extraKernelArgs conflict — apply would fail: %v", verr) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments