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

systemd / systemd / 22045760807

16 Feb 2026 12:10AM UTC coverage: 72.384% (-0.3%) from 72.694%
22045760807

push

github

web-flow
boot: fix buffer alignment when doing block I/O (#40465)

UEFI Block I/O Protocol has `Media->IoAlign` field dictating the minimum
alignment for I/O buffer. It's quite surprising this has been lingering
here unnoticed for years, seems like most UEFI implementations have
small or no alignment requirements. U-Boot is not the case here, and
requires at least 512 byte alignment, hence attempt to read GPT
partition table fail and in effect systemd-boot can not find XBOOTLDR
partition.

These patches allow to boot from XBOOTLDR partition on U-Boot - tested
with latest systemd revision and U-Boot master
(`8de6e8f8a`) on x64 and ARM32, of which
both are failing without the patch.

Also fixes Bitlocker probing logic, which is the only other place where
raw block I/O is used, however this is untested.

311273 of 430029 relevant lines covered (72.38%)

1216197.92 hits per line

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

89.31
/src/basic/locale-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

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

10
#include "dirent-util.h"
11
#include "env-util.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "locale-util.h"
15
#include "log.h"
16
#include "path-util.h"
17
#include "process-util.h"
18
#include "set.h"
19
#include "string-table.h"
20
#include "string-util.h"
21
#include "strv.h"
22
#include "utf8.h"
23

24
static char* normalize_locale(const char *name) {
24✔
25
        const char *e;
24✔
26

27
        /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
28
         * lowercases everything, and removes most special chars. This means the official .UTF-8 suffix
29
         * becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse
30
         * operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do
31
         * that for UTF-8 however, since it's kinda the only charset that matters. */
32

33
        e = endswith(name, ".utf8");
24✔
34
        if (e) {
24✔
35
                _cleanup_free_ char *prefix = NULL;
19✔
36

37
                prefix = strndup(name, e - name);
19✔
38
                if (!prefix)
19✔
39
                        return NULL;
40

41
                return strjoin(prefix, ".UTF-8");
19✔
42
        }
43

44
        e = strstr(name, ".utf8@");
5✔
45
        if (e) {
5✔
46
                _cleanup_free_ char *prefix = NULL;
×
47

48
                prefix = strndup(name, e - name);
×
49
                if (!prefix)
×
50
                        return NULL;
51

52
                return strjoin(prefix, ".UTF-8@", e + 6);
×
53
        }
54

55
        return strdup(name);
5✔
56
}
57

58
static const char* get_locale_dir(void) {
20✔
59
        return secure_getenv("SYSTEMD_LOCALE_DIRECTORY") ?:
20✔
60
#ifdef __GLIBC__
61
                "/usr/lib/locale/";
62
#else
63
                "/usr/share/i18n/locales/musl/";
64
#endif
65
}
66

67
#ifdef __GLIBC__
68
static int add_locales_from_archive(Set *locales) {
10✔
69
        /* Stolen from glibc... */
70

71
        struct locarhead {
10✔
72
                uint32_t magic;
73
                /* Serial number.  */
74
                uint32_t serial;
75
                /* Name hash table.  */
76
                uint32_t namehash_offset;
77
                uint32_t namehash_used;
78
                uint32_t namehash_size;
79
                /* String table.  */
80
                uint32_t string_offset;
81
                uint32_t string_used;
82
                uint32_t string_size;
83
                /* Table with locale records.  */
84
                uint32_t locrectab_offset;
85
                uint32_t locrectab_used;
86
                uint32_t locrectab_size;
87
                /* MD5 sum hash table.  */
88
                uint32_t sumhash_offset;
89
                uint32_t sumhash_used;
90
                uint32_t sumhash_size;
91
        };
92

93
        struct namehashent {
10✔
94
                /* Hash value of the name.  */
95
                uint32_t hashval;
96
                /* Offset of the name in the string table.  */
97
                uint32_t name_offset;
98
                /* Offset of the locale record.  */
99
                uint32_t locrec_offset;
100
        };
101

102
        int r;
10✔
103

104
        assert(locales);
10✔
105

106
        _cleanup_free_ char *locale_archive_file = path_join(get_locale_dir(), "locale-archive");
20✔
107
        if (!locale_archive_file)
10✔
108
                return -ENOMEM;
109

110
        _cleanup_close_ int fd = open(locale_archive_file, O_RDONLY|O_NOCTTY|O_CLOEXEC);
20✔
111
        if (fd < 0)
10✔
112
                return errno == ENOENT ? 0 : -errno;
3✔
113

114
        struct stat st;
7✔
115
        if (fstat(fd, &st) < 0)
7✔
116
                return -errno;
×
117

118
        if (!S_ISREG(st.st_mode))
7✔
119
                return -EBADMSG;
120

121
        if (st.st_size < (off_t) sizeof(struct locarhead))
7✔
122
                return -EBADMSG;
123

124
        if (file_offset_beyond_memory_size(st.st_size))
7✔
125
                return -EFBIG;
126

127
        void *p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
7✔
128
        if (p == MAP_FAILED)
7✔
129
                return -errno;
×
130

131
        const struct namehashent *e;
7✔
132
        const struct locarhead *h = p;
7✔
133
        if (h->magic != 0xde020109 ||
7✔
134
            h->namehash_offset + h->namehash_size > st.st_size ||
7✔
135
            h->string_offset + h->string_size > st.st_size ||
7✔
136
            h->locrectab_offset + h->locrectab_size > st.st_size ||
7✔
137
            h->sumhash_offset + h->sumhash_size > st.st_size) {
7✔
138
                r = -EBADMSG;
×
139
                goto finish;
×
140
        }
141

142
        e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
7✔
143
        for (size_t i = 0; i < h->namehash_size; i++) {
6,356✔
144
                char *z;
6,349✔
145

146
                if (e[i].locrec_offset == 0)
6,349✔
147
                        continue;
6,340✔
148

149
                if (!utf8_is_valid((char*) p + e[i].name_offset))
9✔
150
                        continue;
×
151

152
                z = normalize_locale((char*) p + e[i].name_offset);
9✔
153
                if (!z) {
9✔
154
                        r = -ENOMEM;
×
155
                        goto finish;
×
156
                }
157

158
                r = set_consume(locales, z);
9✔
159
                if (r < 0)
9✔
160
                        goto finish;
×
161
        }
162

163
        r = 0;
164

165
finish:
7✔
166
        if (p != MAP_FAILED)
7✔
167
                munmap(p, st.st_size);
7✔
168

169
        return r;
7✔
170
}
171

172
static int add_locales_from_libdir(Set *locales) {
10✔
173
        _cleanup_closedir_ DIR *dir = NULL;
10✔
174
        int r;
10✔
175

176
        assert(locales);
10✔
177

178
        dir = opendir(get_locale_dir());
10✔
179
        if (!dir)
10✔
180
                return errno == ENOENT ? 0 : -errno;
×
181

182
        FOREACH_DIRENT(de, dir, return -errno) {
52✔
183
                char *z;
22✔
184

185
                if (de->d_type != DT_DIR)
22✔
186
                        continue;
7✔
187

188
                z = normalize_locale(de->d_name);
15✔
189
                if (!z)
15✔
190
                        return -ENOMEM;
191

192
                r = set_consume(locales, z);
15✔
193
                if (r < 0)
15✔
194
                        return r;
195
        }
196

197
        return 0;
198
}
199

200
#else
201

202
static int add_locales_for_musl(Set *locales) {
203
        int r;
204

205
        assert(locales);
206

207
        _cleanup_closedir_ DIR *dir = opendir(get_locale_dir());
208
        if (!dir)
209
                return errno == ENOENT ? 0 : -errno;
210

211
        FOREACH_DIRENT(de, dir, return -errno) {
212
                if (de->d_type != DT_REG)
213
                        continue;
214

215
                char *z = normalize_locale(de->d_name);
216
                if (!z)
217
                        return -ENOMEM;
218

219
                r = set_consume(locales, z);
220
                if (r < 0)
221
                        return r;
222
        }
223

224
        return 0;
225
}
226
#endif
227

228
int get_locales(char ***ret) {
10✔
229
        _cleanup_set_free_ Set *locales = NULL;
10✔
230
        int r;
10✔
231

232
        locales = set_new(&string_hash_ops_free);
10✔
233
        if (!locales)
10✔
234
                return -ENOMEM;
235

236
#ifdef __GLIBC__
237
        r = add_locales_from_archive(locales);
10✔
238
        if (r < 0 && r != -ENOENT)
10✔
239
                return r;
240

241
        r = add_locales_from_libdir(locales);
10✔
242
        if (r < 0)
10✔
243
                return r;
244
#else
245
        r = add_locales_for_musl(locales);
246
        if (r < 0)
247
                return r;
248
#endif
249

250
        char *locale;
10✔
251
        SET_FOREACH(locale, locales) {
45✔
252
                r = locale_is_installed(locale);
25✔
253
                if (r < 0)
25✔
254
                        return r;
×
255
                if (r == 0)
25✔
256
                        free(set_remove(locales, locale));
5✔
257
        }
258

259
        _cleanup_strv_free_ char **l = set_to_strv(&locales);
20✔
260
        if (!l)
10✔
261
                return -ENOMEM;
262

263
        r = getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
10✔
264
        if (r <= 0) {
10✔
265
                if (!IN_SET(r, -ENXIO, 0))
10✔
266
                        log_debug_errno(r, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean, ignoring: %m");
×
267

268
                /* Filter out non-UTF-8 locales, because it's 2019, by default */
269
                char **b = l;
10✔
270
                STRV_FOREACH(a, l)
29✔
271
                        if (endswith(*a, "UTF-8") || strstr(*a, ".UTF-8@"))
19✔
272
                                *(b++) = *a;
19✔
273
                        else
274
                                free(*a);
×
275

276
                *b = NULL;
10✔
277
        }
278

279
        strv_sort(l);
10✔
280

281
        *ret = TAKE_PTR(l);
10✔
282

283
        return 0;
10✔
284
}
285

286
bool locale_is_valid(const char *name) {
82✔
287

288
        if (isempty(name))
82✔
289
                return false;
290

291
        if (strlen(name) >= 128)
80✔
292
                return false;
293

294
        if (!utf8_is_valid(name))
80✔
295
                return false;
296

297
        if (!filename_is_valid(name))
80✔
298
                return false;
299

300
        /* Locales look like: ll_CC.ENC@variant, where ll and CC are alphabetic, ENC is alphanumeric with
301
         * dashes, and variant seems to be alphabetic.
302
         * See: https://www.gnu.org/software/gettext/manual/html_node/Locale-Names.html */
303
        if (!in_charset(name, ALPHANUMERICAL "_.-@"))
78✔
304
                return false;
2✔
305

306
        return true;
307
}
308

309
int locale_is_installed(const char *name) {
48✔
310
        if (!locale_is_valid(name))
48✔
311
                return false;
48✔
312

313
        if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
45✔
314
                return true;
6✔
315

316
#ifdef __GLIBC__
317
        _cleanup_(freelocalep) locale_t loc = newlocale(LC_ALL_MASK, name, (locale_t) 0);
39✔
318
        if (loc == (locale_t) 0)
39✔
319
                return errno == ENOMEM ? -ENOMEM : false;
12✔
320

321
        return true;
322
#else
323
        /* musl also has C.UTF-8 as builtin */
324
        if (streq(name, "C.UTF-8"))
325
                return true;
326

327
        /* musl's newlocale() always succeeds and provides a fake locale object even when the locale does
328
         * not exist. Hence, we need to explicitly check if the locale file exists. */
329
        _cleanup_free_ char *p = path_join(get_locale_dir(), name);
330
        if (!p)
331
                return -ENOMEM;
332

333
        return access(p, F_OK) >= 0;
334
#endif
335
}
336

337
static bool is_locale_utf8_impl(void) {
11,387✔
338
        const char *set;
11,387✔
339
        int r;
11,387✔
340

341
        /* Note that we default to 'true' here, since today UTF8 is pretty much supported everywhere. */
342

343
        r = secure_getenv_bool("SYSTEMD_UTF8");
11,387✔
344
        if (r >= 0)
11,387✔
345
                return r;
133✔
346
        if (r != -ENXIO)
11,254✔
347
                log_debug_errno(r, "Failed to parse $SYSTEMD_UTF8, ignoring: %m");
×
348

349
        /* This function may be called from libsystemd, and setlocale() is not thread safe. Assuming yes. */
350
        if (!is_main_thread())
11,254✔
351
                return true;
352

353
        if (!setlocale(LC_ALL, ""))
11,230✔
354
                return true;
355

356
        set = nl_langinfo(CODESET);
11,230✔
357
        if (!set || streq(set, "UTF-8"))
11,230✔
358
                return true;
359

360
        set = setlocale(LC_CTYPE, NULL);
2,915✔
361
        if (!set)
2,915✔
362
                return true;
363

364
        /* Unless LC_CTYPE is explicitly overridden, return true. Because here CTYPE is effectively unset
365
         * and everything can do to UTF-8 nowadays. */
366
        return STR_IN_SET(set, "C", "POSIX") &&
5,830✔
367
                !getenv("LC_ALL") &&
5,830✔
368
                !getenv("LC_CTYPE") &&
8,745✔
369
                !getenv("LANG");
2,914✔
370
}
371

372
bool is_locale_utf8(void) {
847,031✔
373
        static int cached = -1;
847,031✔
374

375
        if (cached < 0)
847,031✔
376
                cached = is_locale_utf8_impl();
11,387✔
377

378
        return cached;
847,031✔
379
}
380

381
void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
9✔
382
        free_many_charp(l, _VARIABLE_LC_MAX);
9✔
383
}
9✔
384

385
void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]) {
286✔
386
        assert(l);
286✔
387

388
        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
4,290✔
389
                if (p == VARIABLE_LANG)
4,004✔
390
                        continue;
286✔
391
                if (isempty(l[p]) || streq_ptr(l[VARIABLE_LANG], l[p]))
3,736✔
392
                        l[p] = mfree(l[p]);
3,702✔
393
        }
394
}
286✔
395

396
static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
397
        [VARIABLE_LANG]              = "LANG",
398
        [VARIABLE_LANGUAGE]          = "LANGUAGE",
399
        [VARIABLE_LC_CTYPE]          = "LC_CTYPE",
400
        [VARIABLE_LC_NUMERIC]        = "LC_NUMERIC",
401
        [VARIABLE_LC_TIME]           = "LC_TIME",
402
        [VARIABLE_LC_COLLATE]        = "LC_COLLATE",
403
        [VARIABLE_LC_MONETARY]       = "LC_MONETARY",
404
        [VARIABLE_LC_MESSAGES]       = "LC_MESSAGES",
405
        [VARIABLE_LC_PAPER]          = "LC_PAPER",
406
        [VARIABLE_LC_NAME]           = "LC_NAME",
407
        [VARIABLE_LC_ADDRESS]        = "LC_ADDRESS",
408
        [VARIABLE_LC_TELEPHONE]      = "LC_TELEPHONE",
409
        [VARIABLE_LC_MEASUREMENT]    = "LC_MEASUREMENT",
410
        [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
411
};
412

413
DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
16,302✔
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