• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 18578253386

16 Oct 2025 06:50PM UTC coverage: 72.363% (+0.3%) from 72.072%
18578253386

push

github

web-flow
core/mount: properly handle REMOUNTING_* states in mount_stop() (#39269)

5 of 9 new or added lines in 1 file covered. (55.56%)

3694 existing lines in 74 files now uncovered.

304611 of 420946 relevant lines covered (72.36%)

1092905.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/src/basic/efivars.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <stdlib.h>
5
#include <sys/stat.h>
6
#include <sys/uio.h>
7
#include <time.h>
8
#include <unistd.h>
9

10
#include "alloc-util.h"
11
#include "chattr-util.h"
12
#include "efivars.h"
13
#include "fd-util.h"
14
#include "io-util.h"
15
#include "log.h"
16
#include "memory-util.h"
17
#include "string-util.h"
18
#include "time-util.h"
19
#include "utf8.h"
20
#include "virt.h"
21

22
#if ENABLE_EFI
23

24
/* Reads from efivarfs sometimes fail with EINTR. Retry that many times. */
25
#define EFI_N_RETRIES_NO_DELAY 20
26
#define EFI_N_RETRIES_TOTAL 25
27
#define EFI_RETRY_DELAY (50 * USEC_PER_MSEC)
28

29
int efi_get_variable(
649✔
30
                const char *variable,
31
                uint32_t *ret_attribute,
32
                void **ret_value,
33
                size_t *ret_size) {
34

35
        usec_t begin = 0; /* Unnecessary initialization to appease gcc */
649✔
36

37
        assert(variable);
649✔
38

39
        const char *p = strjoina("/sys/firmware/efi/efivars/", variable);
3,245✔
40

41
        if (DEBUG_LOGGING) {
649✔
42
                log_debug("Reading EFI variable %s.", p);
385✔
43
                begin = now(CLOCK_MONOTONIC);
385✔
44
        }
45

46
        _cleanup_close_ int fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
1,298✔
47
        if (fd < 0)
649✔
48
                return log_debug_errno(errno, "open(\"%s\") failed: %m", p);
193✔
49

50
        uint32_t attr;
51
        _cleanup_free_ char *buf = NULL;
456✔
52
        ssize_t n;
53

54
        /* The kernel ratelimits reads from the efivarfs because EFI is inefficient, and we'll occasionally
55
         * fail with EINTR here. A slowdown is better than a failure for us, so retry a few times and
56
         * eventually fail with -EBUSY.
57
         *
58
         * See https://github.com/torvalds/linux/blob/master/fs/efivarfs/file.c#L75 and
59
         * https://github.com/torvalds/linux/commit/bef3efbeb897b56867e271cdbc5f8adaacaeb9cd.
60
         *
61
         * The variable may also be overwritten between the stat and read. If we find out that the new
62
         * contents are longer, try again.
63
         */
64
        for (unsigned try = 0;; try++) {
×
65
                struct stat st;
456✔
66

67
                if (fstat(fd, &st) < 0)
456✔
68
                        return log_debug_errno(errno, "fstat(\"%s\") failed: %m", p);
×
69
                if (st.st_size == 0)
456✔
70
                        return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
×
71
                                               "EFI variable %s is uncommitted", p);
72
                if (st.st_size < 4)
456✔
73
                        return log_debug_errno(SYNTHETIC_ERRNO(ENODATA), "EFI variable %s is shorter than 4 bytes, refusing.", p);
×
74
                if (st.st_size > 4*1024*1024 + 4)
456✔
75
                        return log_debug_errno(SYNTHETIC_ERRNO(E2BIG), "EFI variable %s is ridiculously large, refusing.", p);
×
76

77
                if (!ret_attribute && !ret_value) {
456✔
78
                        /* No need to read anything, return the reported size. */
79
                        n = st.st_size;
80
                        break;
456✔
81
                }
82

83
                /* We want +1 for the read call, and +3 for the additional terminating bytes added below. */
84
                char *t = realloc(buf, (size_t) st.st_size + MAX(1, 3));
449✔
85
                if (!t)
449✔
86
                        return -ENOMEM;
87
                buf = t;
449✔
88

89
                const struct iovec iov[] = {
449✔
90
                        { &attr, sizeof(attr) },
91
                        { buf, (size_t) st.st_size + 1 },
449✔
92
                };
93

94
                n = readv(fd, iov, 2);
449✔
95
                assert(n <= st.st_size + 1);
449✔
96
                if (n == st.st_size + 1)
449✔
97
                        /* We need to try again with a bigger buffer. */
98
                        continue;
×
99
                if (n >= 0)
449✔
100
                        break;
101

102
                log_debug_errno(errno, "Reading from \"%s\" failed: %m", p);
×
103
                if (errno != EINTR)
×
104
                        return -errno;
×
105
                if (try >= EFI_N_RETRIES_TOTAL)
×
106
                        return -EBUSY;
107
                if (try >= EFI_N_RETRIES_NO_DELAY)
×
108
                        (void) usleep_safe(EFI_RETRY_DELAY);
×
109
        }
110

111
        /* Unfortunately kernel reports EOF if there's an inconsistency between efivarfs var list and
112
         * what's actually stored in firmware, c.f. #34304. A zero size env var is not allowed in EFI
113
         * and hence the variable doesn't really exist in the backing store as long as it is zero
114
         * sized, and the kernel calls this "uncommitted". Hence we translate EOF back to ENOENT
115
         * here, as with kernel behavior before
116
         * https://github.com/torvalds/linux/commit/3fab70c165795431f00ddf9be8b84ddd07bd1f8f.
117
         *
118
         * If the kernel changes behaviour (to flush dentries on resume), we can drop this at some
119
         * point in the future. But note that the commit is 11 years old at this point so we'll need
120
         * to deal with the current behaviour for a long time.
121
         */
122
        if (n == 0)
456✔
123
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
×
124
                                       "EFI variable %s is uncommitted", p);
125
        if (n < 4)
456✔
126
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
×
127
                                       "Read %zi bytes from EFI variable %s, expected >= 4", n, p);
128

129
        if (ret_attribute)
456✔
130
                *ret_attribute = attr;
8✔
131
        if (ret_value) {
456✔
132
                assert(buf);
449✔
133
                /* Always NUL-terminate (3 bytes, to properly protect UTF-16, even if truncated in
134
                 * the middle of a character) */
135
                buf[n - 4] = 0;
449✔
136
                buf[n - 4 + 1] = 0;
449✔
137
                buf[n - 4 + 2] = 0;
449✔
138
                *ret_value = TAKE_PTR(buf);
449✔
139
        }
140

141
        if (DEBUG_LOGGING) {
456✔
142
                usec_t end = now(CLOCK_MONOTONIC);
277✔
143
                if (end > begin + EFI_RETRY_DELAY)
277✔
UNCOV
144
                        log_debug("Detected slow EFI variable read access on %s: %s",
×
145
                                  variable, FORMAT_TIMESPAN(end - begin, 1));
146
        }
147

148
        /* Note that efivarfs interestingly doesn't require ftruncate() to update an existing EFI variable
149
         * with a smaller value. */
150

151
        if (ret_size)
456✔
152
                *ret_size = n - 4;
456✔
153

154
        return 0;
155
}
156

157
int efi_get_variable_string(const char *variable, char **ret) {
421✔
158
        _cleanup_free_ void *s = NULL;
421✔
159
        size_t ss = 0;
421✔
160
        char *x;
421✔
161
        int r;
421✔
162

163
        assert(variable);
421✔
164

165
        r = efi_get_variable(variable, NULL, &s, &ss);
421✔
166
        if (r < 0)
421✔
167
                return r;
168

169
        x = utf16_to_utf8(s, ss);
274✔
170
        if (!x)
274✔
171
                return -ENOMEM;
172

173
        if (ret)
274✔
174
                *ret = x;
274✔
175

176
        return 0;
177
}
178

179
int efi_get_variable_path(const char *variable, char **ret) {
30✔
180
        int r;
30✔
181

182
        assert(variable);
30✔
183

184
        r = efi_get_variable_string(variable, ret);
30✔
185
        if (r < 0)
30✔
186
                return r;
187

188
        if (ret)
30✔
189
                efi_tilt_backslashes(*ret);
30✔
190

191
        return r;
192
}
193

194
static int efi_verify_variable(const char *variable, uint32_t attr, const void *value, size_t size) {
22✔
195
        _cleanup_free_ void *buf = NULL;
22✔
196
        size_t n;
22✔
197
        uint32_t a;
22✔
198
        int r;
22✔
199

200
        assert(variable);
22✔
201
        assert(value || size == 0);
22✔
202

203
        r = efi_get_variable(variable, &a, &buf, &n);
22✔
204
        if (r < 0)
22✔
205
                return r;
206

207
        return a == attr && memcmp_nn(buf, n, value, size) == 0;
8✔
208
}
209

210
int efi_set_variable(const char *variable, const void *value, size_t size) {
49✔
211
        static const uint32_t attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
49✔
212

213
        _cleanup_free_ struct var {
49✔
214
                uint32_t attr;
215
                char buf[];
216
        } _packed_ *buf = NULL;
49✔
217
        _cleanup_close_ int fd = -EBADF;
49✔
218
        bool saved_flags_valid = false;
49✔
219
        unsigned saved_flags;
49✔
220
        int r;
49✔
221

222
        assert(variable);
49✔
223
        assert(value || size == 0);
49✔
224

225
        /* size 0 means removal, empty variable would not be enough for that */
226
        if (size > 0 && efi_verify_variable(variable, attr, value, size) > 0) {
49✔
227
                log_debug("Variable '%s' is already in wanted state, skipping write.", variable);
×
228
                return 0;
×
229
        }
230

231
        const char *p = strjoina("/sys/firmware/efi/efivars/", variable);
245✔
232

233
        /* Newer efivarfs protects variables that are not in an allow list with FS_IMMUTABLE_FL by default,
234
         * to protect them for accidental removal and modification. We are not changing these variables
235
         * accidentally however, hence let's unset the bit first. */
236

237
        r = chattr_full(AT_FDCWD, p,
49✔
238
                        /* value = */ 0,
239
                        /* mask = */ FS_IMMUTABLE_FL,
240
                        /* ret_previous = */ &saved_flags,
241
                        /* ret_final = */ NULL,
242
                        /* flags = */ 0);
243
        if (r < 0 && r != -ENOENT)
49✔
244
                log_debug_errno(r, "Failed to drop FS_IMMUTABLE_FL flag from '%s', ignoring: %m", p);
×
245

246
        saved_flags_valid = r >= 0;
49✔
247

248
        if (size == 0) {
49✔
249
                if (unlink(p) < 0) {
27✔
250
                        r = -errno;
21✔
251
                        goto finish;
21✔
252
                }
253

254
                return 0;
255
        }
256

257
        fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644);
22✔
258
        if (fd < 0) {
22✔
259
                r = -errno;
×
260
                goto finish;
×
261
        }
262

263
        buf = malloc(sizeof(uint32_t) + size);
22✔
264
        if (!buf) {
22✔
265
                r = -ENOMEM;
×
266
                goto finish;
×
267
        }
268

269
        buf->attr = attr;
22✔
270
        memcpy(buf->buf, value, size);
22✔
271

272
        r = loop_write(fd, buf, sizeof(uint32_t) + size);
22✔
273
        if (r < 0)
22✔
274
                goto finish;
×
275

276
        /* For some reason efivarfs doesn't update mtime automatically. Let's do it manually then. This is
277
         * useful for processes that cache EFI variables to detect when changes occurred. */
278
        if (futimens(fd, /* times = */ NULL) < 0)
22✔
279
                log_debug_errno(errno, "Failed to update mtime/atime on %s, ignoring: %m", p);
11✔
280

281
        r = 0;
282

283
finish:
43✔
284
        if (saved_flags_valid) {
43✔
285
                int q;
8✔
286

287
                /* Restore the original flags field, just in case */
288
                if (fd < 0)
8✔
289
                        q = chattr_path(p, saved_flags, FS_IMMUTABLE_FL);
×
290
                else
291
                        q = chattr_fd(fd, saved_flags, FS_IMMUTABLE_FL);
8✔
292
                if (q < 0)
8✔
293
                        log_debug_errno(q, "Failed to restore FS_IMMUTABLE_FL on '%s', ignoring: %m", p);
49✔
294
        }
295

296
        return r;
297
}
298

299
int efi_set_variable_string(const char *variable, const char *value) {
×
300
        _cleanup_free_ char16_t *u16 = NULL;
×
301

302
        u16 = utf8_to_utf16(value, SIZE_MAX);
×
303
        if (!u16)
×
304
                return -ENOMEM;
305

306
        return efi_set_variable(variable, u16, (char16_strlen(u16) + 1) * sizeof(char16_t));
×
307
}
308

309
bool is_efi_boot(void) {
713✔
310
        static int cache = -1;
713✔
311

312
        if (cache < 0) {
713✔
313
                if (detect_container() > 0)
320✔
314
                        cache = false;
52✔
315
                else {
316
                        cache = access("/sys/firmware/efi/", F_OK) >= 0;
268✔
317
                        if (!cache && errno != ENOENT)
268✔
318
                                log_debug_errno(errno, "Unable to test whether /sys/firmware/efi/ exists, assuming EFI not available: %m");
×
319
                }
320
        }
321

322
        return cache;
713✔
323
}
324

325
static int read_flag(const char *variable) {
50✔
326
        _cleanup_free_ void *v = NULL;
50✔
327
        uint8_t b;
50✔
328
        size_t s;
50✔
329
        int r;
50✔
330

331
        if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */
50✔
332
                return 0;
333

334
        r = efi_get_variable(variable, NULL, &v, &s);
48✔
335
        if (r < 0)
48✔
336
                return r;
337

338
        if (s != 1)
24✔
339
                return -EINVAL;
340

341
        b = *(uint8_t *)v;
24✔
342
        return !!b;
24✔
343
}
344

345
bool is_efi_secure_boot(void) {
4✔
346
        static int cache = -1;
4✔
347
        int r;
4✔
348

349
        if (cache < 0) {
4✔
350
                r = read_flag(EFI_GLOBAL_VARIABLE_STR("SecureBoot"));
2✔
351
                if (r == -ENOENT)
2✔
352
                        cache = false;
×
353
                else if (r < 0)
2✔
354
                        log_debug_errno(r, "Error reading SecureBoot EFI variable, assuming not in SecureBoot mode: %m");
×
355
                else
356
                        cache = r;
2✔
357
        }
358

359
        return cache > 0;
4✔
360
}
361

362
SecureBootMode efi_get_secure_boot_mode(void) {
12✔
363
        static SecureBootMode cache = _SECURE_BOOT_INVALID;
12✔
364

365
        if (cache != _SECURE_BOOT_INVALID)
12✔
366
                return cache;
367

368
        int secure = read_flag(EFI_GLOBAL_VARIABLE_STR("SecureBoot"));
12✔
369
        if (secure < 0) {
12✔
370
                if (secure != -ENOENT)
×
371
                        log_debug_errno(secure, "Error reading SecureBoot EFI variable, assuming not in SecureBoot mode: %m");
×
372

373
                return (cache = SECURE_BOOT_UNSUPPORTED);
×
374
        }
375

376
        /* We can assume false for all these if they are abscent (AuditMode and
377
         * DeployedMode may not exist on older firmware). */
378
        int audit    = read_flag(EFI_GLOBAL_VARIABLE_STR("AuditMode"));
12✔
379
        int deployed = read_flag(EFI_GLOBAL_VARIABLE_STR("DeployedMode"));
12✔
380
        int setup    = read_flag(EFI_GLOBAL_VARIABLE_STR("SetupMode"));
12✔
381
        log_debug("Secure boot variables: SecureBoot=%d AuditMode=%d DeployedMode=%d SetupMode=%d",
12✔
382
                  secure, audit, deployed, setup);
383

384
        return (cache = decode_secure_boot_mode(secure, audit > 0, deployed > 0, setup > 0));
12✔
385
}
386
#endif
387

388
char *efi_tilt_backslashes(char *s) {
40✔
389
        return string_replace_char(s, '\\', '/');
40✔
390
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc