Debian Codegraph @

src/shared/base-filesystem.c

raw
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 __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(__hppa__)
80 /* No /lib32 or /lib64 on hppa. The linker is /usr/lib/hppa-linux-gnu/ld.so.1. */
81 # define KNOW_LIB64_DIRS 1
82 #elif defined(__i386__) || defined(__x86_64__)
83 { "lib64", 0, "usr/lib64\0"
84 "usr/lib\0", "ld-linux-x86-64.so.2" },
85 # define KNOW_LIB64_DIRS 1
86 #elif defined(__ia64__)
87 #elif defined(__loongarch_lp64)
88 # define KNOW_LIB64_DIRS 1
89 # if defined(__loongarch_double_float)
90 { "lib64", 0, "usr/lib64\0"
91 "usr/lib\0", "ld-linux-loongarch-lp64d.so.1" },
92 # elif defined(__loongarch_single_float)
93 { "lib64", 0, "usr/lib64\0"
94 "usr/lib\0", "ld-linux-loongarch-lp64f.so.1" },
95 # elif defined(__loongarch_soft_float)
96 { "lib64", 0, "usr/lib64\0"
97 "usr/lib\0", "ld-linux-loongarch-lp64s.so.1" },
98 # else
99 # error "Unknown LoongArch ABI"
100 # endif
101 #elif defined(__m68k__)
102 /* No link needed. */
103 # define KNOW_LIB64_DIRS 1
104 #elif defined(_MIPS_SIM)
105 # if _MIPS_SIM == _MIPS_SIM_ABI32
106 # elif _MIPS_SIM == _MIPS_SIM_NABI32
107 # elif _MIPS_SIM == _MIPS_SIM_ABI64
108 # else
109 # error "Unknown MIPS ABI"
110 # endif
111 #elif defined(__powerpc__)
112 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
113 { "lib64", 0, "usr/lib64\0"
114 "usr/lib\0", "ld64.so.2" },
115 # define KNOW_LIB64_DIRS 1
116 # elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
117 /* powerpc64-linux-gnu */
118 # else
119 /* powerpc-linux-gnu */
120 /* No /lib32 or /lib64 on powerpc. The linker is /usr/lib/powerpc-linux-gnu/ld.so.1. */
121 # define KNOW_LIB64_DIRS 1
122 # endif
123 #elif defined(__riscv)
124 # if __riscv_xlen == 32
125 # elif __riscv_xlen == 64
126 /* Same situation as for aarch64 */
127 { "lib64", 0, "usr/lib64\0"
128 "usr/lib\0", "ld-linux-riscv64-lp64d.so.1", BASE_FILESYSTEM_EMPTY_ONLY },
129 # define KNOW_LIB64_DIRS 1
130 # else
131 # error "Unknown RISC-V ABI"
132 # endif
133 #elif defined(__s390x__)
134 /* Same situation as for aarch64 */
135 { "lib64", 0, "usr/lib64\0"
136 "usr/lib\0", "ld-lsb-s390x.so.3", BASE_FILESYSTEM_EMPTY_ONLY },
137 # define KNOW_LIB64_DIRS 1
138 #elif defined(__s390__)
139 /* s390-linux-gnu */
140 #elif defined(__sparc__)
141 #endif
142 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
143 };
144
145 #ifndef KNOW_LIB64_DIRS
146 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
147 #endif
148
149 int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
150 bool empty_fs = false;
151 int r;
152
153 assert(fd >= 0);
154 assert(root);
155
156 /* The "root" parameter is decoration only – it's only used as part of log messages */
157
158 FOREACH_ELEMENT(i, table) {
159 if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_ONLY) && !empty_fs)
160 continue;
161
162 if (faccessat(fd, i->dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
163 continue;
164
165 if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_MARKER))
166 empty_fs = true;
167
168 if (i->target) { /* Create as symlink? */
169 const char *target = NULL;
170
171 /* check if one of the targets exists */
172 NULSTR_FOREACH(s, i->target) {
173 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
174 continue;
175
176 /* check if a specific file exists at the target path */
177 if (i->exists) {
178 _cleanup_free_ char *p = NULL;
179
180 p = path_join(s, i->exists);
181 if (!p)
182 return log_oom();
183
184 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
185 continue;
186 }
187
188 target = s;
189 break;
190 }
191
192 if (!target)
193 continue;
194
195 r = RET_NERRNO(symlinkat(target, fd, i->dir));
196 } else {
197 /* Create as directory. */
198 WITH_UMASK(0000)
199 r = RET_NERRNO(mkdirat(fd, i->dir, i->mode));
200 }
201 if (r < 0) {
202 bool ignore = IN_SET(r, -EEXIST, -EROFS) || FLAGS_SET(i->flags, BASE_FILESYSTEM_IGNORE_ON_FAILURE);
203 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
204 "Failed to create %s/%s: %m", root, i->dir);
205 if (ignore)
206 continue;
207
208 return r;
209 }
210
211 if (uid_is_valid(uid) || gid_is_valid(gid))
212 if (fchownat(fd, i->dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
213 return log_error_errno(errno, "Failed to chown %s/%s: %m", root, i->dir);
214 }
215
216 return 0;
217 }
218
219 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
220 _cleanup_close_ int fd = -EBADF;
221
222 fd = open(ASSERT_PTR(root), O_DIRECTORY|O_CLOEXEC);
223 if (fd < 0)
224 return log_error_errno(errno, "Failed to open root file system: %m");
225
226 return base_filesystem_create_fd(fd, root, uid, gid);
227 }