From f389506714969025d2b2d61370648c0e58b8c0a2 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 22 Jan 2025 02:16:15 +0100 Subject: [PATCH 1/2] machine-id-setup: actually unlink /run/machine-id if write/mount fails Follow-up for 64d52739c59e28a5eb453f749e24f23b2d2ec06a Fixes CID#1590374 and CID#1590375 --- src/shared/machine-id-setup.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c index 83f609c43f..19d841a913 100644 --- a/src/shared/machine-id-setup.c +++ b/src/shared/machine-id-setup.c @@ -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)); From c8650d0dbe9a70dfdc057c30be7e9892834571c6 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 22 Jan 2025 14:47:57 +0100 Subject: [PATCH 2/2] machine-id-setup: fix typo --- src/shared/machine-id-setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c index 19d841a913..d444ea06e9 100644 --- a/src/shared/machine-id-setup.c +++ b/src/shared/machine-id-setup.c @@ -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);