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

systemd / systemd / 18810271929

25 Oct 2025 04:50PM UTC coverage: 72.26%. Remained the same
18810271929

push

github

YHNdnzj
core/exec-invoke: relax restriction for process name length

Previously, we limit the length of process name by 8.
This relax the restriction then at least process comm or
program_invocation_name contains the untrucated process name.

Closes #38367.

24 of 28 new or added lines in 3 files covered. (85.71%)

486 existing lines in 52 files now uncovered.

304829 of 421852 relevant lines covered (72.26%)

1095984.64 hits per line

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

80.66
/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(
645✔
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 */
645✔
36

37
        assert(variable);
645✔
38

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

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

46
        _cleanup_close_ int fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
1,290✔
47
        if (fd < 0)
645✔
48
                return log_debug_errno(errno, "open(\"%s\") failed: %m", p);
189✔
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) {
417✔
158
        _cleanup_free_ void *s = NULL, *x = NULL;
417✔
159
        size_t ss = 0;
417✔
160
        int r;
417✔
161

162
        assert(variable);
417✔
163

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

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

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

175
        return 0;
176
}
177

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

181
        assert(variable);
30✔
182

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

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

190
        return r;
191
}
192

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

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

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

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

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

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

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

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

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

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

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

245
        saved_flags_valid = r >= 0;
49✔
246

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

253
                return 0;
254
        }
255

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

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

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

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

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

280
        r = 0;
281

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

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

295
        return r;
296
}
297

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

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

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

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

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

321
        return cache;
743✔
322
}
323

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

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

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

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

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

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

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

358
        return cache > 0;
4✔
359
}
360

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

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

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

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

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

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

387
char *efi_tilt_backslashes(char *s) {
40✔
388
        return string_replace_char(s, '\\', '/');
40✔
389
}
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