machine-id-setup: actually unlink /run/machine-id if write/mount fails (#36105)

Follow-up for 64d52739c5

Fixes CID#1590374 and CID#1590375
This commit is contained in:
Lennart Poettering
2025-01-22 21:41:57 +01:00
committed by GitHub
+8 -8
View File
@@ -181,7 +181,7 @@ int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlag
fd = xopenat_full(inode_fd, /* path= */ NULL, O_RDWR|O_CLOEXEC, XO_REGULAR, 0444);
if (fd < 0) {
log_debug_errno(fd, "Failed to topen '%s' in writable mode, retrying in read-only mode: %m", etc_machine_id);
log_debug_errno(fd, "Failed to open '%s' in writable mode, retrying in read-only mode: %m", etc_machine_id);
/* If that didn't work, convert it into a readable file */
fd = xopenat_full(inode_fd, /* path= */ NULL, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
@@ -260,8 +260,10 @@ int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlag
WITH_UMASK(0022) {
r = id128_write_at(run_fd, "machine-id", ID128_FORMAT_PLAIN, machine_id);
if (r < 0)
if (r < 0) {
(void) unlinkat(run_fd, "machine-id", /* flags = */ 0);
return log_error_errno(r, "Cannot write '%s': %m", run_machine_id);
}
}
unlink_run_machine_id = true;
@@ -273,20 +275,18 @@ int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlag
/* And now, let's mount it over */
r = mount_follow_verbose(LOG_ERR, run_machine_id, FORMAT_PROC_FD_PATH(fd), /* fstype= */ NULL, MS_BIND, /* options= */ NULL);
if (r < 0)
if (r < 0) {
if (unlink_run_machine_id)
(void) unlinkat(ASSERT_FD(run_fd), "machine-id", /* flags = */ 0);
return r;
}
log_full(FLAGS_SET(flags, MACHINE_ID_SETUP_FORCE_TRANSIENT) ? LOG_DEBUG : LOG_INFO, "Installed transient '%s' file.", etc_machine_id);
unlink_run_machine_id = false;
/* Mark the mount read-only (note: we are not going via FORMAT_PROC_FD_PATH() here because that fd is not updated to our new bind mount) */
(void) mount_follow_verbose(LOG_WARNING, /* source= */ NULL, etc_machine_id, /* fstype= */ NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, /* options= */ NULL);
finish:
if (unlink_run_machine_id)
(void) unlinkat(ASSERT_FD(run_fd), "machine-id", /* flags= */ 0);
if (!in_initrd())
(void) sd_notifyf(/* unset_environment= */ false, "X_SYSTEMD_MACHINE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(machine_id));