]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/machine-id-setup.c
tree-wide: check if return value of lseek() and friends is negative
[thirdparty/systemd.git] / src / shared / machine-id-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sched.h>
5 #include <sys/mount.h>
6 #include <unistd.h>
7
8 #include "sd-id128.h"
9
10 #include "alloc-util.h"
11 #include "chase.h"
12 #include "creds-util.h"
13 #include "fd-util.h"
14 #include "id128-util.h"
15 #include "io-util.h"
16 #include "log.h"
17 #include "machine-id-setup.h"
18 #include "macro.h"
19 #include "mkdir.h"
20 #include "mount-util.h"
21 #include "mountpoint-util.h"
22 #include "namespace-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "stat-util.h"
26 #include "string-util.h"
27 #include "sync-util.h"
28 #include "umask-util.h"
29 #include "virt.h"
30
31 static int acquire_machine_id_from_credential(sd_id128_t *ret) {
32 _cleanup_free_ char *buf = NULL;
33 int r;
34
35 r = read_credential_with_decryption("system.machine_id", (void**) &buf, /* ret_size= */ NULL);
36 if (r < 0)
37 return log_warning_errno(r, "Failed to read system.machine_id credential, ignoring: %m");
38 if (r == 0) /* not found */
39 return -ENXIO;
40
41 r = sd_id128_from_string(buf, ret);
42 if (r < 0)
43 return log_warning_errno(r, "Failed to parse system.machine_id credential, ignoring: %m");
44
45 log_info("Initializing machine ID from credential.");
46 return 0;
47 }
48
49 static int generate_machine_id(const char *root, sd_id128_t *ret) {
50 _cleanup_close_ int fd = -EBADF;
51 int r;
52
53 assert(ret);
54
55 /* First, try reading the D-Bus machine id, unless it is a symlink */
56 fd = chase_and_open("/var/lib/dbus/machine-id", root, CHASE_PREFIX_ROOT | CHASE_NOFOLLOW, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
57 if (fd >= 0 && id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret) >= 0) {
58 log_info("Initializing machine ID from D-Bus machine ID.");
59 return 0;
60 }
61
62 if (isempty(root) && running_in_chroot() <= 0) {
63 /* Let's use a system credential for the machine ID if we can */
64 r = acquire_machine_id_from_credential(ret);
65 if (r >= 0)
66 return r;
67
68 /* If that didn't work, see if we are running in a container,
69 * and a machine ID was passed in via $container_uuid the way
70 * libvirt/LXC does it */
71
72 if (detect_container() > 0) {
73 _cleanup_free_ char *e = NULL;
74
75 if (getenv_for_pid(1, "container_uuid", &e) > 0 &&
76 sd_id128_from_string(e, ret) >= 0) {
77 log_info("Initializing machine ID from container UUID.");
78 return 0;
79 }
80
81 } else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU)) {
82
83 /* If we are not running in a container, see if we are running in a VM that provides
84 * a system UUID via the SMBIOS/DMI interfaces. Such environments include QEMU/KVM
85 * with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */
86
87 if (id128_get_product(ret) >= 0) {
88 log_info("Initializing machine ID from VM UUID.");
89 return 0;
90 }
91 }
92 }
93
94 /* If that didn't work, generate a random machine id */
95 r = sd_id128_randomize(ret);
96 if (r < 0)
97 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
98
99 log_info("Initializing machine ID from random generator.");
100 return 0;
101 }
102
103 int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_id, sd_id128_t *ret) {
104 const char *etc_machine_id, *run_machine_id;
105 _cleanup_close_ int fd = -EBADF;
106 bool writable;
107 int r;
108
109 etc_machine_id = prefix_roota(root, "/etc/machine-id");
110
111 WITH_UMASK(0000) {
112 /* We create this 0444, to indicate that this isn't really
113 * something you should ever modify. Of course, since the file
114 * will be owned by root it doesn't matter much, but maybe
115 * people look. */
116
117 (void) mkdir_parents(etc_machine_id, 0755);
118 fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
119 if (fd < 0) {
120 int old_errno = errno;
121
122 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
123 if (fd < 0) {
124 if (old_errno == EROFS && errno == ENOENT)
125 return log_error_errno(errno,
126 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\n"
127 "Booting up is supported only when:\n"
128 "1) /etc/machine-id exists and is populated.\n"
129 "2) /etc/machine-id exists and is empty.\n"
130 "3) /etc/machine-id is missing and /etc is writable.\n");
131 else
132 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
133 }
134
135 writable = false;
136 } else
137 writable = true;
138 }
139
140 /* A we got a valid machine ID argument, that's what counts */
141 if (sd_id128_is_null(machine_id)) {
142
143 /* Try to read any existing machine ID */
144 if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0)
145 return 0;
146
147 /* Hmm, so, the id currently stored is not useful, then let's generate one */
148 r = generate_machine_id(root, &machine_id);
149 if (r < 0)
150 return r;
151 }
152
153 if (writable) {
154 if (lseek(fd, 0, SEEK_SET) < 0)
155 return log_error_errno(errno, "Failed to seek %s: %m", etc_machine_id);
156
157 if (ftruncate(fd, 0) < 0)
158 return log_error_errno(errno, "Failed to truncate %s: %m", etc_machine_id);
159
160 /* If the caller requested a transient machine-id, write the string "uninitialized\n" to
161 * disk and overmount it with a transient file.
162 *
163 * Otherwise write the machine-id directly to disk. */
164 if (force_transient) {
165 r = loop_write(fd, "uninitialized\n", SIZE_MAX);
166 if (r < 0)
167 return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
168
169 r = fsync_full(fd);
170 if (r < 0)
171 return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
172 } else {
173 r = id128_write_fd(fd, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, machine_id);
174 if (r < 0)
175 return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
176 else
177 goto finish;
178 }
179 }
180
181 fd = safe_close(fd);
182
183 /* Hmm, we couldn't or shouldn't write the machine-id to /etc?
184 * So let's write it to /run/machine-id as a replacement */
185
186 run_machine_id = prefix_roota(root, "/run/machine-id");
187
188 WITH_UMASK(0022)
189 r = id128_write(run_machine_id, ID128_FORMAT_PLAIN, machine_id);
190 if (r < 0) {
191 (void) unlink(run_machine_id);
192 return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
193 }
194
195 /* And now, let's mount it over */
196 r = mount_follow_verbose(LOG_ERR, run_machine_id, etc_machine_id, NULL, MS_BIND, NULL);
197 if (r < 0) {
198 (void) unlink(run_machine_id);
199 return r;
200 }
201
202 log_full(force_transient ? LOG_DEBUG : LOG_INFO, "Installed transient %s file.", etc_machine_id);
203
204 /* Mark the mount read-only */
205 r = mount_follow_verbose(LOG_WARNING, NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
206 if (r < 0)
207 return r;
208
209 finish:
210 if (ret)
211 *ret = machine_id;
212
213 return 0;
214 }
215
216 int machine_id_commit(const char *root) {
217 _cleanup_close_ int fd = -EBADF, initial_mntns_fd = -EBADF;
218 const char *etc_machine_id;
219 sd_id128_t id;
220 int r;
221
222 /* Before doing anything, sync everything to ensure any changes by first-boot units are persisted.
223 *
224 * First, explicitly sync the file systems we care about and check if it worked. */
225 FOREACH_STRING(sync_path, "/etc/", "/var/") {
226 r = syncfs_path(AT_FDCWD, sync_path);
227 if (r < 0)
228 return log_error_errno(r, "Cannot sync %s: %m", sync_path);
229 }
230
231 /* Afterwards, sync() the rest too, but we can't check the return value for these. */
232 sync();
233
234 /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
235 * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
236 * original mount namespace, thus revealing the file that was just created. */
237
238 etc_machine_id = prefix_roota(root, "/etc/machine-id");
239
240 r = path_is_mount_point(etc_machine_id, NULL, 0);
241 if (r < 0)
242 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
243 if (r == 0) {
244 log_debug("%s is not a mount point. Nothing to do.", etc_machine_id);
245 return 0;
246 }
247
248 /* Read existing machine-id */
249 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
250 if (fd < 0)
251 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
252
253 r = fd_is_temporary_fs(fd);
254 if (r < 0)
255 return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
256 if (r == 0)
257 return log_error_errno(SYNTHETIC_ERRNO(EROFS),
258 "%s is not on a temporary file system.",
259 etc_machine_id);
260
261 r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
262 if (r < 0)
263 return log_error_errno(r, "We didn't find a valid machine ID in %s: %m", etc_machine_id);
264
265 fd = safe_close(fd);
266
267 /* Store current mount namespace */
268 r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL, NULL);
269 if (r < 0)
270 return log_error_errno(r, "Can't fetch current mount namespace: %m");
271
272 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
273 r = detach_mount_namespace();
274 if (r < 0)
275 return log_error_errno(r, "Failed to set up new mount namespace: %m");
276
277 r = umount_verbose(LOG_ERR, etc_machine_id, 0);
278 if (r < 0)
279 return r;
280
281 /* Update a persistent version of etc_machine_id */
282 r = id128_write(etc_machine_id, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, id);
283 if (r < 0)
284 return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
285
286 /* Return to initial namespace and proceed a lazy tmpfs unmount */
287 r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
288 if (r < 0)
289 return log_warning_errno(r, "Failed to switch back to initial mount namespace: %m.\nWe'll keep transient %s file until next reboot.", etc_machine_id);
290
291 if (umount2(etc_machine_id, MNT_DETACH) < 0)
292 return log_warning_errno(errno, "Failed to unmount transient %s file: %m.\nWe keep that mount until next reboot.", etc_machine_id);
293
294 return 0;
295 }