-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvars_linux.go
288 lines (246 loc) · 6.88 KB
/
vars_linux.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2020-2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package efi
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"golang.org/x/sys/unix"
internal_unix "github.com/canonical/go-efilib/internal/unix"
)
func efivarfsPath() string {
return "/sys/firmware/efi/efivars"
}
type varFile interface {
io.ReadWriteCloser
Readdir(n int) ([]os.FileInfo, error)
GetInodeFlags() (uint, error)
SetInodeFlags(flags uint) error
}
func makeVarFileMutable(f varFile) (restore func() error, err error) {
const immutableFlag = 0x00000010
flags, err := f.GetInodeFlags()
if err != nil {
return nil, err
}
if flags&immutableFlag == 0 {
// Nothing to do
return func() error { return nil }, nil
}
if err := f.SetInodeFlags(flags &^ immutableFlag); err != nil {
return nil, err
}
return func() error {
return f.SetInodeFlags(flags)
}, nil
}
type realVarFile struct {
*os.File
}
func (f *realVarFile) GetInodeFlags() (uint, error) {
flags, err := internal_unix.IoctlGetUint(int(f.Fd()), unix.FS_IOC_GETFLAGS)
if err != nil {
return 0, &os.PathError{Op: "ioctl", Path: f.Name(), Err: err}
}
return flags, nil
}
func (f *realVarFile) SetInodeFlags(flags uint) error {
if err := internal_unix.IoctlSetPointerUint(int(f.Fd()), unix.FS_IOC_SETFLAGS, flags); err != nil {
return &os.PathError{Op: "ioctl", Path: f.Name(), Err: err}
}
return nil
}
func realOpenVarFile(path string, flags int, perm os.FileMode) (varFile, error) {
f, err := os.OpenFile(path, flags, perm)
if err != nil {
return nil, err
}
return &realVarFile{f}, nil
}
var guidLength = len("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
func probeEfivarfs() bool {
var st unix.Statfs_t
if err := unixStatfs(efivarfsPath(), &st); err != nil {
return false
}
if uint(st.Type) != uint(unix.EFIVARFS_MAGIC) {
return false
}
return true
}
func maybeRetry(n int, fn func() (bool, error)) error {
for i := 1; ; i++ {
retry, err := fn()
switch {
case i > n:
return err
case !retry:
return err
case err == nil:
return nil
}
}
}
func processEfivarfsFileAccessError(err error) (retry bool, errOut error) {
if os.IsPermission(err) {
var se syscall.Errno
if !errors.As(err, &se) {
// This shouldn't happen, but just return ErrVarPermission
// in this case and don't retry.
return false, ErrVarPermission
}
if se == syscall.EACCES {
// open will fail with EACCES if we lack the privileges
// to write to the file.
// open will fail with EACCES if we lack the privileges
// to write to the parent directory in the case where we
// need to create a new file.
// unlink will fail with EACCES if we lack the privileges
// to write to the parent directory.
// Don't retry in these cases.
return false, ErrVarPermission
}
// open and unlink will fail with EPERM if the file exists but
// it is immutable. This might happen as a result of a race with
// another process that might have been writing to the variable
// or may have deleted and recreated it, making the underlying
// inode immutable again.
// Retry in this case.
return true, ErrVarPermission
}
// Don't retry for any other error.
return false, err
}
func writeEfivarfsFile(path string, attrs VariableAttributes, data []byte) (retry bool, err error) {
// Open for reading to make the inode mutable
r, err := openVarFile(path, os.O_RDONLY, 0)
switch {
case os.IsNotExist(err):
case os.IsPermission(err):
return false, ErrVarPermission
case err != nil:
return false, err
default:
defer r.Close()
restoreImmutable, err := makeVarFileMutable(r)
switch {
case os.IsPermission(err):
return false, ErrVarPermission
case err != nil:
return false, err
}
defer restoreImmutable()
}
if len(data) == 0 {
// short-cut for unauthenticated variable delete - efivarfs will perform a
// zero-byte write to delete the variable if we unlink the entry here.
err := removeVarFile(path)
if os.IsNotExist(err) {
// it shouldn't be an error if the variable already doesn't exist.
return false, nil
}
return processEfivarfsFileAccessError(err)
}
flags := os.O_WRONLY | os.O_CREATE
if attrs&AttributeAppendWrite != 0 {
flags |= os.O_APPEND
}
w, err := openVarFile(path, flags, 0644)
if err != nil {
return processEfivarfsFileAccessError(err)
}
defer w.Close()
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, attrs)
buf.Write(data)
_, err = buf.WriteTo(w)
return false, err
}
type efivarfsVarsBackend struct{}
func (v efivarfsVarsBackend) Get(name string, guid GUID) (VariableAttributes, []byte, error) {
path := filepath.Join(efivarfsPath(), fmt.Sprintf("%s-%s", name, guid))
f, err := openVarFile(path, os.O_RDONLY, 0)
switch {
case os.IsNotExist(err):
return 0, nil, ErrVarNotExist
case os.IsPermission(err):
return 0, nil, ErrVarPermission
case err != nil:
return 0, nil, err
}
defer f.Close()
var attrs VariableAttributes
if err := binary.Read(f, binary.LittleEndian, &attrs); err != nil {
if err == io.EOF {
return 0, nil, ErrVarNotExist
}
return 0, nil, err
}
data, err := io.ReadAll(f)
if err != nil {
return 0, nil, err
}
return attrs, data, nil
}
func (v efivarfsVarsBackend) Set(name string, guid GUID, attrs VariableAttributes, data []byte) error {
path := filepath.Join(efivarfsPath(), fmt.Sprintf("%s-%s", name, guid))
return maybeRetry(4, func() (bool, error) { return writeEfivarfsFile(path, attrs, data) })
}
func (v efivarfsVarsBackend) List() ([]VariableDescriptor, error) {
f, err := openVarFile(efivarfsPath(), os.O_RDONLY, 0)
switch {
case os.IsNotExist(err):
return nil, ErrVarsUnavailable
case os.IsPermission(err):
return nil, ErrVarPermission
case err != nil:
return nil, err
}
defer f.Close()
dirents, err := f.Readdir(-1)
if err != nil {
return nil, err
}
var entries []VariableDescriptor
for _, dirent := range dirents {
if !dirent.Mode().IsRegular() {
// Skip non-regular files
continue
}
if len(dirent.Name()) < guidLength+1 {
// Skip files with a basename that isn't long enough
// to contain a GUID and a hyphen
continue
}
if dirent.Name()[len(dirent.Name())-guidLength-1] != '-' {
// Skip files where the basename doesn't contain a
// hyphen between the name and GUID
continue
}
if dirent.Size() == 0 {
// Skip files with zero size. These are variables that
// have been deleted by writing an empty payload
continue
}
name := dirent.Name()[:len(dirent.Name())-guidLength-1]
guid, err := DecodeGUIDString(dirent.Name()[len(name)+1:])
if err != nil {
continue
}
entries = append(entries, VariableDescriptor{Name: name, GUID: guid})
}
return entries, nil
}
func addDefaultVarsBackend(ctx context.Context) context.Context {
if !probeEfivarfs() {
return withVarsBackend(ctx, nullVarsBackend{})
}
return withVarsBackend(ctx, efivarfsVarsBackend{})
}