Repository navigation
fix(volumectl): protect move-volume from backups - #1426
Conversation
The finalize step of move-volume checked only the processes of the module agent service. A module backup is started by the node agent, so it was not detected: finalize stopped the user session, the backup was killed and the volume was moved without --force. Take the module-backup lock, shared with module-backup and the update-module scripts, and hold it until the move ends. If the lock is busy, refuse unless --force is given: then the lock holder is killed with the user session, and the lock is taken right after. A backup that starts during the move exits at once and the node run-backup command retries it later. Assisted-by: Claude Code:claude-sonnet-5-5 Assisted-by: Claude Code:claude-opus-5-5
During the finalize step of move-volume, a PAM session of the module user, like the one opened by runagent -m for a backup or for metrics, started the user manager again while the volume was being moved. Hold the user session with a transient unit for the whole finalize step. A new [email protected] drop-in skips the start of the user manager, and of [email protected], while the RuntimeDirectory of that unit exists under /run/nethserver/user-hold. It works with any session class and systemd version; the hold ends when the unit stops, or at reboot. The same unit keeps the runtime directory of the user, by requiring [email protected], instead of recreating it by hand. As the hold covers any session of the module user, Podman now runs through runagent also while the session is stopped, instead of runuser -u. The move is refused if systemd has not loaded the drop-in. Assisted-by: Claude Code:claude-opus-5-5
Only Ctrl-C rolled back an interrupted finalize step. A closed terminal, like a dropped SSH connection, or SIGTERM killed volumectl at once: the application stayed stopped, held by the transient unit, and the volume could be left half moved. Handle SIGHUP and SIGTERM like SIGINT, and ignore write errors on the closed terminal, so the original volume is restored and the application is started again. Assisted-by: Claude Code:claude-opus-5-5
run-backup and refresh-module-metrics run a command of each module with runagent -m. If the user session of a rootless module is stopped, for example while volumectl move-volume moves its data, the command runs without the user manager and the module may be half moved. Check the user session first. The backup of that module is retried later, like a module whose backup is already running. Its metrics keep their last output. Assisted-by: Claude Code:claude-opus-5-5
d0a269b to
12243dd
Compare
| lock_path = self.home / ".config/state/.module-backup.lock" | ||
| fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC, 0o644) | ||
| # module-backup runs as the module user: it must be able to | ||
| # open the file if we create it | ||
| os.fchown(fd, self.uid, self.gid) | ||
| self.backup_lock_fd = fd |
There was a problem hiding this comment.
lock_backup() runs as root, but the lock file lives under the module home, which the module user controls. If the module replaces .module-backup.lock (or .config/state) with a symlink to /etc/shadow, root opens the target and fchown() gives it to the module UID: privilege escalation from module to root.
Open the file as the module user instead: a symlink then only reaches files the module user can already write, the file is created with the right owner, and fchown() is not needed. O_NONBLOCK avoids hanging on a FIFO. Not tested on a node yet.
| lock_path = self.home / ".config/state/.module-backup.lock" | |
| fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC, 0o644) | |
| # module-backup runs as the module user: it must be able to | |
| # open the file if we create it | |
| os.fchown(fd, self.uid, self.gid) | |
| self.backup_lock_fd = fd | |
| lock_path = self.home / ".config/state/.module-backup.lock" | |
| # Open the file as the module user: its home is not trusted. | |
| # The file is created with the right owner, for module-backup | |
| groups = os.getgroups() | |
| os.setgroups([]) | |
| os.setegid(self.gid) | |
| os.seteuid(self.uid) | |
| try: | |
| fd = os.open(lock_path, os.O_WRONLY | os.O_CREAT | os.O_NONBLOCK | os.O_CLOEXEC, 0o644) | |
| finally: | |
| os.seteuid(0) | |
| os.setegid(0) | |
| os.setgroups(groups) | |
| self.backup_lock_fd = fd |
| """Stop the user manager of the module, and keep its runtime | ||
| directory: rootless Podman runs without the user manager. A | ||
| transient unit requires [email protected], otherwise the | ||
| directory is removed with the user manager on some distros. | ||
| While the unit is active, its own runtime directory keeps the | ||
| user manager stopped, see [email protected]/40-hold.conf: a PAM | ||
| session of the module user, like the one opened by runagent | ||
| for a backup, would start it again.""" | ||
| _log(f"stopping the user session of {self.mid}") | ||
| # Stop the leftover of an interrupted run, if any | ||
| subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL) | ||
| subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}", | ||
| "--service-type=oneshot", "--property=RemainAfterExit=yes", | ||
| f"--property=Requires=user-runtime-dir@{self.uid}.service", |
There was a problem hiding this comment.
With Requires=, cleanup_runtime() restarting user-runtime-dir@UID also restarts the holder unit. Its RuntimeDirectory /run/nethserver/user-hold/UID is removed and recreated, so for a moment 40-hold.conf does not block user@UID. A runagent -m MODULE ... (PAM session) in that window starts the user manager, and the module services can write to the volume while it is moved.
Wants= does not propagate the restart, and still keeps user-runtime-dir@ needed (StopWhenUnneeded=), so /run/user/UID stays. Checked with user-level units that copy this setup: with Requires= the hold directory is gone right after the restart, with Wants= it is untouched; in both cases the runtime-dir unit stops when the holder stops. Not tested on a node yet.
| """Stop the user manager of the module, and keep its runtime | |
| directory: rootless Podman runs without the user manager. A | |
| transient unit requires [email protected], otherwise the | |
| directory is removed with the user manager on some distros. | |
| While the unit is active, its own runtime directory keeps the | |
| user manager stopped, see [email protected]/40-hold.conf: a PAM | |
| session of the module user, like the one opened by runagent | |
| for a backup, would start it again.""" | |
| _log(f"stopping the user session of {self.mid}") | |
| # Stop the leftover of an interrupted run, if any | |
| subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL) | |
| subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}", | |
| "--service-type=oneshot", "--property=RemainAfterExit=yes", | |
| f"--property=Requires=user-runtime-dir@{self.uid}.service", | |
| """Stop the user manager of the module, and keep its runtime | |
| directory: rootless Podman runs without the user manager. A | |
| transient unit wants [email protected], otherwise the | |
| directory is removed with the user manager on some distros. | |
| Wants=, not Requires=: a restart of user-runtime-dir@ must not | |
| restart the unit, or its hold directory disappears for a moment. | |
| While the unit is active, its own runtime directory keeps the | |
| user manager stopped, see [email protected]/40-hold.conf: a PAM | |
| session of the module user, like the one opened by runagent | |
| for a backup, would start it again.""" | |
| _log(f"stopping the user session of {self.mid}") | |
| # Stop the leftover of an interrupted run, if any | |
| subprocess.run(["systemctl", "stop", self.holder], stderr=subprocess.DEVNULL) | |
| subprocess.run(["systemd-run", "--quiet", "--collect", f"--unit={self.holder}", | |
| "--service-type=oneshot", "--property=RemainAfterExit=yes", | |
| f"--property=Wants=user-runtime-dir@{self.uid}.service", |
gsanchietti
left a comment
There was a problem hiding this comment.
For me it looks good, IMO the suggestion about the lock file is could be good, but feel free to ignore both agent commands
QA of
volumectl move-volumefound a failure in the "busy application" test: when a backup of the application was running, the final step of the move stopped the application anyway, interrupted the backup and moved the volume even without--force. The backup runs outside the application agent, so the existing busy check did not see it. Also, while the application was stopped, any command run as the application user (for example a backup retry, or a metrics collection, throughrunagent) started the whole application again, in the middle of the move. NethServer/dev#8259Now the move refuses to start while a backup or an application update is running, unless
--forceis given. During the move, backups and updates are postponed, and the application stays stopped until the move ends, whatever command runs meanwhile. If the move is interrupted by a closed terminal (for example a dropped SSH connection) or bySIGTERM, it is rolled back like with Ctrl-C. Outside a move nothing changes, except that backups and metrics no longer run for an application whose user session is stopped.--forceafter it interrupts the running backup.volumectl-rundir-*, holds the application session during the move: a new systemd drop-in,[email protected]/40-hold.conf, skips any start of the application user manager while that unit is active. Podman now always runs throughrunagent, and the move is refused if the drop-in is not loaded.volumectlhandlesSIGHUPandSIGTERMlike Ctrl-C: the original volume is restored and the application is started again.run-backupandrefresh-module-metricscommands skip an application whose user session is stopped: its backup is retried later, its metrics keep their last value.Known limits: the code still has a few small race windows, on purpose, to keep the change simple. For example, a command run as the application user exactly while the move stops or starts the application, or a signal received at the very edges of the rollback. In the worst case the move fails and is rolled back, or the downtime is a few seconds longer: the volume data is not at risk.
How to test
Use a node with an additional disk and a rootless application with a data volume, as in the QA steps of NethServer/dev#8259, on Rocky Linux 9 and on Debian 13.
--sync: the command refuses and the backup completes. Run it again with--force: the backup is interrupted, the move completes, and a backup started during the move is retried later.journalctlshows any earlier start attempt as "skipped, unmet condition check").SIGTERMtovolumectl, while it copies the last changes: the volume stays at its original location and the application runs again.Generated by Claude Code:claude-opus-5-5 with nethserver-pr