Coverage Report

Created: 2025-08-28 06:46

/src/systemd/src/shared/base-filesystem.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3
#include <fcntl.h>
4
#include <sys/stat.h>
5
#include <syslog.h>
6
#include <unistd.h>
7
8
#ifdef ARCH_MIPS
9
#include <asm/sgidefs.h>
10
#endif
11
12
#include "alloc-util.h"
13
#include "base-filesystem.h"
14
#include "errno-util.h"
15
#include "fd-util.h"
16
#include "log.h"
17
#include "nulstr-util.h"
18
#include "path-util.h"
19
#include "umask-util.h"
20
#include "user-util.h"
21
22
typedef enum BaseFilesystemFlags {
23
        BASE_FILESYSTEM_IGNORE_ON_FAILURE = 1 << 0,
24
        BASE_FILESYSTEM_EMPTY_MARKER      = 1 << 1, /* If this is missing, then we are booting on an empty filesystem - see comment below */
25
        BASE_FILESYSTEM_EMPTY_ONLY        = 1 << 2, /* If booting on an empty filesystem create this, otherwise skip it - see comment below */
26
} BaseFilesystemFlags;
27
28
typedef struct BaseFilesystem {
29
        const char *dir;      /* directory or symlink to create */
30
        mode_t mode;
31
        const char *target;   /* if non-NULL create as symlink to this target */
32
        const char *exists;   /* conditionalize this entry on existence of this file */
33
        BaseFilesystemFlags flags; /* various modifiers for behaviour on creation */
34
} BaseFilesystem;
35
36
/* Note that as entries are processed in order, entries with BASE_FILESYSTEM_EMPTY_MARKER must be listed
37
 * before entries with BASE_FILESYSTEM_EMPTY_ONLY. */
38
static const BaseFilesystem table[] = {
39
        { "bin",      0, "usr/bin\0",                  NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
40
        { "lib",      0, "usr/lib\0",                  NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
41
        { "root",  0750, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
42
        { "sbin",     0, "usr/sbin\0",                 NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
43
        { "usr",   0755, NULL,                         NULL },
44
        { "var",   0755, NULL,                         NULL },
45
        { "etc",   0755, NULL,                         NULL },
46
        { "proc",  0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
47
        { "sys",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
48
        { "dev",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
49
        { "run",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
50
        /* We don't add /tmp/ here for now (even though it's necessary for regular operation), because we
51
         * want to support both cases where /tmp/ is a mount of its own (in which case we probably should set
52
         * the mode to 1555, to indicate that no one should write to it, not even root) and when it's part of
53
         * the rootfs (in which case we should set mode 1777), and we simply don't know what's right. */
54
55
        /* Various architecture ABIs define the path to the dynamic loader via the /lib64/ subdirectory of
56
         * the root directory. When booting from an otherwise empty root file system (where only /usr/ has
57
         * been mounted into) it is thus necessary to create a symlink pointing to the right subdirectory of
58
         * /usr/ first — otherwise we couldn't invoke any dynamic binary. Let's detect this case here, and
59
         * create the symlink as needed should it be missing. We prefer doing this consistently with Debian's
60
         * multiarch logic, but support Fedora-style and Arch-style multilib too. */
61
#if defined(__aarch64__)
62
        /* aarch64 ELF ABI actually says dynamic loader is in /lib/, but Fedora puts it in /lib64/ anyway and
63
         * just symlinks /lib/ld-linux-aarch64.so.1 to ../lib64/ld-linux-aarch64.so.1. For this to work
64
         * correctly, /lib64/ must be symlinked to /usr/lib64/. On the flip side, we must not create /lib64/
65
         * on Debian and derivatives as they expect the target to be different from what Fedora et al. use,
66
         * which is problematic for example when nspawn from some other distribution boots a Debian
67
         * container with only /usr/, so we only create this symlink when at least one other symlink is
68
         * missing, and let the image builder/package manager worry about not creating incomplete persistent
69
         * filesystem hierarchies instead. The key purpose of this code is to ensure we can bring up a system
70
         * with a volatile root filesystem after all. */
71
        { "lib64",    0, "usr/lib64\0"
72
                         "usr/lib\0",                "ld-linux-aarch64.so.1",       BASE_FILESYSTEM_EMPTY_ONLY        },
73
#  define KNOW_LIB64_DIRS 1
74
#elif defined(__alpha__)
75
#elif defined(__arc__) || defined(__tilegx__)
76
#elif defined(__arm__)
77
        /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */
78
#  define KNOW_LIB64_DIRS 1
79
#elif defined(__i386__) || defined(__x86_64__)
80
        { "lib64",    0, "usr/lib64\0"
81
                         "usr/lib\0",                "ld-linux-x86-64.so.2" },
82
#  define KNOW_LIB64_DIRS 1
83
#elif defined(__ia64__)
84
#elif defined(__loongarch_lp64)
85
#  define KNOW_LIB64_DIRS 1
86
#  if defined(__loongarch_double_float)
87
        { "lib64",    0, "usr/lib64\0"
88
                         "usr/lib\0",                "ld-linux-loongarch-lp64d.so.1" },
89
#  elif defined(__loongarch_single_float)
90
        { "lib64",    0, "usr/lib64\0"
91
                         "usr/lib\0",                "ld-linux-loongarch-lp64f.so.1" },
92
#  elif defined(__loongarch_soft_float)
93
        { "lib64",    0, "usr/lib64\0"
94
                         "usr/lib\0",                "ld-linux-loongarch-lp64s.so.1" },
95
#  else
96
#    error "Unknown LoongArch ABI"
97
#  endif
98
#elif defined(__m68k__)
99
        /* No link needed. */
100
#  define KNOW_LIB64_DIRS 1
101
#elif defined(_MIPS_SIM)
102
#  if _MIPS_SIM == _MIPS_SIM_ABI32
103
#  elif _MIPS_SIM == _MIPS_SIM_NABI32
104
#  elif _MIPS_SIM == _MIPS_SIM_ABI64
105
#  else
106
#    error "Unknown MIPS ABI"
107
#  endif
108
#elif defined(__powerpc__)
109
#  if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
110
        { "lib64",    0, "usr/lib64\0"
111
                         "usr/lib\0",                "ld64.so.2" },
112
#    define KNOW_LIB64_DIRS 1
113
#  elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
114
        /* powerpc64-linux-gnu */
115
#  else
116
        /* powerpc-linux-gnu */
117
#  endif
118
#elif defined(__riscv)
119
#  if __riscv_xlen == 32
120
#  elif __riscv_xlen == 64
121
        /* Same situation as for aarch64 */
122
        { "lib64",    0, "usr/lib64\0"
123
                         "usr/lib\0",                "ld-linux-riscv64-lp64d.so.1", BASE_FILESYSTEM_EMPTY_ONLY        },
124
#    define KNOW_LIB64_DIRS 1
125
#  else
126
#    error "Unknown RISC-V ABI"
127
#  endif
128
#elif defined(__s390x__)
129
        /* Same situation as for aarch64 */
130
        { "lib64",    0, "usr/lib64\0"
131
                         "usr/lib\0",                "ld-lsb-s390x.so.3",           BASE_FILESYSTEM_EMPTY_ONLY        },
132
#    define KNOW_LIB64_DIRS 1
133
#elif defined(__s390__)
134
        /* s390-linux-gnu */
135
#elif defined(__sparc__)
136
#endif
137
        /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
138
};
139
140
#ifndef KNOW_LIB64_DIRS
141
#  pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
142
#endif
143
144
0
int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
145
0
        bool empty_fs = false;
146
0
        int r;
147
148
0
        assert(fd >= 0);
149
0
        assert(root);
150
151
        /* The "root" parameter is decoration only – it's only used as part of log messages */
152
153
0
        FOREACH_ELEMENT(i, table) {
154
0
                if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_ONLY) && !empty_fs)
155
0
                        continue;
156
157
0
                if (faccessat(fd, i->dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
158
0
                        continue;
159
160
0
                if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_MARKER))
161
0
                        empty_fs = true;
162
163
0
                if (i->target) { /* Create as symlink? */
164
0
                        const char *target = NULL;
165
166
                        /* check if one of the targets exists */
167
0
                        NULSTR_FOREACH(s, i->target) {
168
0
                                if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
169
0
                                        continue;
170
171
                                /* check if a specific file exists at the target path */
172
0
                                if (i->exists) {
173
0
                                        _cleanup_free_ char *p = NULL;
174
175
0
                                        p = path_join(s, i->exists);
176
0
                                        if (!p)
177
0
                                                return log_oom();
178
179
0
                                        if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
180
0
                                                continue;
181
0
                                }
182
183
0
                                target = s;
184
0
                                break;
185
0
                        }
186
187
0
                        if (!target)
188
0
                                continue;
189
190
0
                        r = RET_NERRNO(symlinkat(target, fd, i->dir));
191
0
                } else {
192
                        /* Create as directory. */
193
0
                        WITH_UMASK(0000)
194
0
                                r = RET_NERRNO(mkdirat(fd, i->dir, i->mode));
195
0
                }
196
0
                if (r < 0) {
197
0
                        bool ignore = IN_SET(r, -EEXIST, -EROFS) || FLAGS_SET(i->flags, BASE_FILESYSTEM_IGNORE_ON_FAILURE);
198
0
                        log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
199
0
                                       "Failed to create %s/%s: %m", root, i->dir);
200
0
                        if (ignore)
201
0
                                continue;
202
203
0
                        return r;
204
0
                }
205
206
0
                if (uid_is_valid(uid) || gid_is_valid(gid))
207
0
                        if (fchownat(fd, i->dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
208
0
                                return log_error_errno(errno, "Failed to chown %s/%s: %m", root, i->dir);
209
0
        }
210
211
0
        return 0;
212
0
}
213
214
0
int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
215
0
        _cleanup_close_ int fd = -EBADF;
216
217
0
        fd = open(ASSERT_PTR(root), O_DIRECTORY|O_CLOEXEC);
218
0
        if (fd < 0)
219
0
                return log_error_errno(errno, "Failed to open root file system: %m");
220
221
0
        return base_filesystem_create_fd(fd, root, uid, gid);
222
0
}