]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3577de7a KS |
2 | |
3 | #include <errno.h> | |
a8fbdf54 TA |
4 | #include <fcntl.h> |
5 | #include <stdbool.h> | |
3577de7a | 6 | #include <stdlib.h> |
07630cea | 7 | #include <sys/stat.h> |
a8fbdf54 | 8 | #include <syslog.h> |
3577de7a KS |
9 | #include <unistd.h> |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
a965a319 | 12 | #include "architecture.h" |
affb60b1 LP |
13 | #include "base-filesystem.h" |
14 | #include "fd-util.h" | |
3577de7a KS |
15 | #include "log.h" |
16 | #include "macro.h" | |
d8b4d14d | 17 | #include "nulstr-util.h" |
65290fbf | 18 | #include "path-util.h" |
07630cea | 19 | #include "string-util.h" |
affb60b1 | 20 | #include "umask-util.h" |
ee104e11 | 21 | #include "user-util.h" |
3577de7a KS |
22 | |
23 | typedef struct BaseFilesystem { | |
04ba1bb0 | 24 | const char *dir; /* directory or symlink to create */ |
3577de7a | 25 | mode_t mode; |
04ba1bb0 | 26 | const char *target; /* if non-NULL create as symlink to this target */ |
a6f44d61 | 27 | const char *exists; /* conditionalize this entry on existence of this file */ |
6404ecc8 | 28 | bool ignore_failure; |
3577de7a KS |
29 | } BaseFilesystem; |
30 | ||
31 | static const BaseFilesystem table[] = { | |
30d7c9c4 HH |
32 | { "bin", 0, "usr/bin\0", NULL }, |
33 | { "lib", 0, "usr/lib\0", NULL }, | |
93cbc9ca | 34 | { "root", 0750, NULL, NULL, true }, |
30d7c9c4 | 35 | { "sbin", 0, "usr/sbin\0", NULL }, |
03cfe0d5 LP |
36 | { "usr", 0755, NULL, NULL }, |
37 | { "var", 0755, NULL, NULL }, | |
38 | { "etc", 0755, NULL, NULL }, | |
10404d52 DH |
39 | { "proc", 0755, NULL, NULL, true }, |
40 | { "sys", 0755, NULL, NULL, true }, | |
41 | { "dev", 0755, NULL, NULL, true }, | |
6f32005f | 42 | |
04ba1bb0 LP |
43 | /* Various architecture ABIs define the path to the dynamic loader via the /lib64/ subdirectory of |
44 | * the root directory. When booting from an otherwise empty root file system (where only /usr/ has | |
45 | * been mounted into) it is thus necessary to create a symlink pointing to the right subdirectory of | |
46 | * /usr/ first — otherwise we couldn't invoke any dynamic binary. Let's detect this case here, and | |
47 | * create the symlink as needed should it be missing. We prefer doing this consistently with Debian's | |
0b75493d | 48 | * multiarch logic, but support Fedora-style multilib too. */ |
6f32005f | 49 | #if defined(__aarch64__) |
dcc87c68 ZJS |
50 | /* aarch64 ELF ABI actually says dynamic loader is in /lib/, but Fedora puts it in /lib64/ anyway and |
51 | * just symlinks /lib/ld-linux-aarch64.so.1 to ../lib64/ld-linux-aarch64.so.1. For this to work | |
52 | * correctly, /lib64/ must be symlinked to /usr/lib64/. */ | |
a965a319 | 53 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" |
dcc87c68 ZJS |
54 | "usr/lib64\0", "ld-linux-aarch64.so.1" }, |
55 | # define KNOW_LIB64_DIRS 1 | |
6f32005f ZJS |
56 | #elif defined(__alpha__) |
57 | #elif defined(__arc__) || defined(__tilegx__) | |
58 | #elif defined(__arm__) | |
2db409ce ZJS |
59 | /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */ |
60 | # define KNOW_LIB64_DIRS 1 | |
6f32005f | 61 | #elif defined(__i386__) || defined(__x86_64__) |
a965a319 | 62 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" |
30d7c9c4 | 63 | "usr/lib64\0", "ld-linux-x86-64.so.2" }, |
6f32005f ZJS |
64 | # define KNOW_LIB64_DIRS 1 |
65 | #elif defined(__ia64__) | |
01722893 XW |
66 | #elif defined(__loongarch64) |
67 | # define KNOW_LIB64_DIRS 1 | |
68 | # if defined(__loongarch_double_float) | |
69 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" | |
70 | "usr/lib64\0", "ld-linux-loongarch-lp64d.so.1" }, | |
71 | # elif defined(__loongarch_single_float) | |
72 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" | |
73 | "usr/lib64\0", "ld-linux-loongarch-lp64f.so.1" }, | |
74 | # elif defined(__loongarch_soft_float) | |
75 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" | |
76 | "usr/lib64\0", "ld-linux-loongarch-lp64s.so.1" }, | |
77 | # else | |
78 | # error "Unknown LoongArch ABI" | |
79 | # endif | |
6f32005f | 80 | #elif defined(__m68k__) |
996eaea5 ZJS |
81 | /* No link needed. */ |
82 | # define KNOW_LIB64_DIRS 1 | |
6f32005f ZJS |
83 | #elif defined(_MIPS_SIM) |
84 | # if _MIPS_SIM == _MIPS_SIM_ABI32 | |
85 | # elif _MIPS_SIM == _MIPS_SIM_NABI32 | |
86 | # elif _MIPS_SIM == _MIPS_SIM_ABI64 | |
87 | # else | |
88 | # error "Unknown MIPS ABI" | |
89 | # endif | |
90 | #elif defined(__powerpc__) | |
e98157b9 | 91 | # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
a965a319 | 92 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" |
e98157b9 ZJS |
93 | "usr/lib64\0", "ld64.so.2" }, |
94 | # define KNOW_LIB64_DIRS 1 | |
95 | # elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
96 | /* powerpc64-linux-gnu */ | |
97 | # else | |
98 | /* powerpc-linux-gnu */ | |
99 | # endif | |
6f32005f ZJS |
100 | #elif defined(__riscv) |
101 | # if __riscv_xlen == 32 | |
102 | # elif __riscv_xlen == 64 | |
761e9382 | 103 | /* Same situation as for aarch64 */ |
a965a319 | 104 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" |
761e9382 ZJS |
105 | "usr/lib64\0", "ld-linux-riscv64-lp64d.so.1" }, |
106 | # define KNOW_LIB64_DIRS 1 | |
6f32005f ZJS |
107 | # else |
108 | # error "Unknown RISC-V ABI" | |
109 | # endif | |
110 | #elif defined(__s390__) | |
fe037986 | 111 | /* s390-linux-gnu */ |
6f32005f | 112 | #elif defined(__s390x__) |
a965a319 | 113 | { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0" |
4167e213 | 114 | "usr/lib64\0", "ld-lsb-s390x.so.3" }, |
fe037986 | 115 | # define KNOW_LIB64_DIRS 1 |
6f32005f | 116 | #elif defined(__sparc__) |
e1ae9755 | 117 | #endif |
6f32005f | 118 | /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */ |
3577de7a KS |
119 | }; |
120 | ||
6f32005f ZJS |
121 | #ifndef KNOW_LIB64_DIRS |
122 | # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links." | |
04ba1bb0 LP |
123 | #endif |
124 | ||
03cfe0d5 | 125 | int base_filesystem_create(const char *root, uid_t uid, gid_t gid) { |
254d1313 | 126 | _cleanup_close_ int fd = -EBADF; |
8a383bf2 | 127 | int r; |
3577de7a KS |
128 | |
129 | fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); | |
4a62c710 MS |
130 | if (fd < 0) |
131 | return log_error_errno(errno, "Failed to open root file system: %m"); | |
3577de7a | 132 | |
9b4aba10 | 133 | for (size_t i = 0; i < ELEMENTSOF(table); i++) { |
6f4f8056 HH |
134 | if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0) |
135 | continue; | |
136 | ||
3577de7a | 137 | if (table[i].target) { |
12e2b70f | 138 | const char *target = NULL; |
e1ae9755 KS |
139 | |
140 | /* check if one of the targets exists */ | |
141 | NULSTR_FOREACH(s, table[i].target) { | |
142 | if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0) | |
143 | continue; | |
144 | ||
3fd165e5 KS |
145 | /* check if a specific file exists at the target path */ |
146 | if (table[i].exists) { | |
147 | _cleanup_free_ char *p = NULL; | |
148 | ||
65290fbf | 149 | p = path_join(s, table[i].exists); |
3fd165e5 KS |
150 | if (!p) |
151 | return log_oom(); | |
152 | ||
153 | if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0) | |
154 | continue; | |
155 | } | |
156 | ||
e1ae9755 KS |
157 | target = s; |
158 | break; | |
159 | } | |
160 | ||
161 | if (!target) | |
3577de7a KS |
162 | continue; |
163 | ||
8258578f LP |
164 | if (symlinkat(target, fd, table[i].dir) < 0) { |
165 | log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno, | |
166 | "Failed to create symlink at %s/%s: %m", root, table[i].dir); | |
167 | ||
168 | if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure) | |
169 | continue; | |
170 | ||
171 | return -errno; | |
172 | } | |
03cfe0d5 | 173 | |
9b4aba10 | 174 | if (uid_is_valid(uid) || gid_is_valid(gid)) |
03cfe0d5 LP |
175 | if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) |
176 | return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir); | |
03cfe0d5 | 177 | |
3577de7a KS |
178 | continue; |
179 | } | |
180 | ||
2053593f | 181 | WITH_UMASK(0000) |
3577de7a | 182 | r = mkdirat(fd, table[i].dir, table[i].mode); |
8258578f LP |
183 | if (r < 0) { |
184 | log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno, | |
6404ecc8 LP |
185 | "Failed to create directory at %s/%s: %m", root, table[i].dir); |
186 | ||
8258578f LP |
187 | if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure) |
188 | continue; | |
d1d59eeb | 189 | |
8258578f | 190 | return -errno; |
6404ecc8 | 191 | } |
03cfe0d5 | 192 | |
264caae2 | 193 | if (uid_is_valid(uid) || gid_is_valid(gid)) |
03cfe0d5 LP |
194 | if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) |
195 | return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir); | |
3577de7a KS |
196 | } |
197 | ||
198 | return 0; | |
199 | } |