-
Notifications
You must be signed in to change notification settings - Fork 86
/
sbctl.go
248 lines (211 loc) · 5.42 KB
/
sbctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package sbctl
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"slices"
"github.com/foxboron/sbctl/backend"
"github.com/foxboron/sbctl/config"
"github.com/foxboron/sbctl/hierarchy"
"github.com/spf13/afero"
)
var (
// TODO: Remove this at some point
// Only here for legacy reasons to denote the old path
DatabasePath = "/usr/share/secureboot/"
Version = "unknown"
)
// Functions that doesn't fit anywhere else
type LsblkEntry struct {
Parttype string `json:"parttype"`
Mountpoint string `json:"mountpoint"`
Mountpoints []string `json:"mountpoints"`
Pttype string `json:"pttype"`
Fstype string `json:"fstype"`
Children []*LsblkEntry `json:"children"`
}
type LsblkRoot struct {
Blockdevices []*LsblkEntry `json:"blockdevices"`
}
var espLocations = []string{
"/efi",
"/boot",
"/boot/efi",
}
var ErrNoESP = errors.New("failed to find EFI system partition")
func findESP(b []byte) (string, error) {
var lsblkRoot LsblkRoot
if err := json.Unmarshal(b, &lsblkRoot); err != nil {
return "", fmt.Errorf("failed to parse json: %v", err)
}
for _, lsblkEntry := range lsblkRoot.Blockdevices {
// This is our check function, that also checks mountpoints
checkDev := func(e *LsblkEntry, pttype string) *LsblkEntry {
if e.Pttype != "gpt" && (e.Pttype != "" && pttype != "gpt") {
return nil
}
if e.Fstype != "vfat" {
return nil
}
if e.Parttype != "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" {
return nil
}
if slices.Contains(espLocations, e.Mountpoint) {
return e
}
for _, esp := range espLocations {
n := slices.Index(e.Mountpoints, esp)
if n == -1 {
continue
}
// Replace the top-level Mountpoint with a valid one from mountpoints
e.Mountpoint = e.Mountpoints[n]
return e
}
return nil
}
// First check top-level devices
p := checkDev(lsblkEntry, "")
if p != nil {
return p.Mountpoint, nil
}
// Check children, this is not recursive.
for _, ce := range lsblkEntry.Children {
p := checkDev(ce, lsblkEntry.Pttype)
if p != nil {
return p.Mountpoint, nil
}
}
}
return "", ErrNoESP
}
// Slightly more advanced check
func GetESP(vfs afero.Fs) (string, error) {
for _, env := range []string{"SYSTEMD_ESP_PATH", "ESP_PATH"} {
envEspPath, found := os.LookupEnv(env)
if found {
return envEspPath, nil
}
}
for _, location := range espLocations {
// "Read" a file inside all candiadate locations to trigger an
// automount if there's an automount partition.
_, _ = vfs.Stat(fmt.Sprintf("%s/does-not-exist", location))
}
out, err := exec.Command(
"lsblk",
"--json",
"--tree",
"--output", "PARTTYPE,MOUNTPOINT,PTTYPE,FSTYPE").Output()
if err != nil {
return "", err
}
return findESP(out)
}
func Sign(state *config.State, keys *backend.KeyHierarchy, file, output string, enroll bool) error {
file, err := filepath.Abs(file)
if err != nil {
return err
}
if output == "" {
output = file
} else {
output, err = filepath.Abs(output)
if err != nil {
return err
}
}
kh, err := backend.GetKeyHierarchy(state.Fs, state)
if err != nil {
return err
}
files, err := ReadFileDatabase(state.Fs, state.Config.FilesDb)
if err != nil {
return fmt.Errorf("couldn't open database: %s", state.Config.FilesDb)
}
if enroll {
files[file] = &SigningEntry{File: file, OutputFile: output}
if err := WriteFileDatabase(state.Fs, state.Config.FilesDb, files); err != nil {
return err
}
}
if entry, ok := files[file]; ok && output == entry.OutputFile {
err = SignFile(state, kh, hierarchy.Db, entry.File, entry.OutputFile)
// return early if signing fails
if err != nil {
return err
}
files[file] = entry
if err := WriteFileDatabase(state.Fs, state.Config.FilesDb, files); err != nil {
return err
}
} else {
err = SignFile(state, kh, hierarchy.Db, file, output)
// return early if signing fails
if err != nil {
return err
}
}
return err
}
func CombineFiles(vfs afero.Fs, microcode, initramfs string) (afero.File, error) {
for _, file := range []string{microcode, initramfs} {
if _, err := vfs.Stat(file); err != nil {
return nil, fmt.Errorf("%s: %w", file, errors.Unwrap(err))
}
}
tmpFile, err := afero.TempFile(vfs, "/var/tmp", "initramfs-")
if err != nil {
return nil, err
}
one, err := vfs.Open(microcode)
if err != nil {
return nil, err
}
defer one.Close()
two, err := vfs.Open(initramfs)
if err != nil {
return nil, err
}
defer two.Close()
_, err = io.Copy(tmpFile, one)
if err != nil {
return nil, fmt.Errorf("failed to append microcode file to output: %w", err)
}
_, err = io.Copy(tmpFile, two)
if err != nil {
return nil, fmt.Errorf("failed to append initramfs file to output: %w", err)
}
return tmpFile, nil
}
func CreateBundle(state *config.State, bundle Bundle) error {
var microcode string
make_bundle := false
if bundle.IntelMicrocode != "" {
microcode = bundle.IntelMicrocode
make_bundle = true
} else if bundle.AMDMicrocode != "" {
microcode = bundle.AMDMicrocode
make_bundle = true
}
if make_bundle {
tmpFile, err := CombineFiles(state.Fs, microcode, bundle.Initramfs)
if err != nil {
return err
}
defer state.Fs.Remove(tmpFile.Name())
bundle.Initramfs = tmpFile.Name()
}
out, err := GenerateBundle(state.Fs, &bundle)
if err != nil {
return err
}
if !out {
return fmt.Errorf("failed to generate bundle %s", bundle.Output)
}
return nil
}