fix(system_baseline): run the apt upgrade with TMPDIR outside /tmp and /var/tmp
Build / build (pull_request) Successful in 31s
Lint / lint (pull_request) Canceled after 1m40s
Molecule / molecule (pull_request) Canceled after 1m13s

An openssh-server upgrade restarts ssh.service. With PrivateTmp=yes systemd
then deletes that unit's /tmp and /var/tmp, which removes the files apt
staged there while the upgrade is still running, so dpkg fails and leaves
packages unpacked but not configured.

The update flow now creates system_baseline_apt_tmpdir (root, mode 0700),
runs the upgrade and the autoremove with TMPDIR set to it, and opens a new
connection afterwards. The input is validated in the main flow and in the
update entry point: absolute, no "..", not under /tmp or /var/tmp.

Closes #374

Assisted-by: Claude Code:claude-sonnet-5-5
This commit is contained in:
Dragonish committed 2026-11-25 19:21:01 +00:00
1 parent 008c5ab8a4
commit 1bbf3368ee
11 files changed
+151 -1

No files matched your search

+25
View File
@@ -6,6 +6,21 @@
- name: Validate the apt upgrade mode
ansible.builtin.import_tasks: validate_apt_upgrade.yml
- name: Validate the apt scratch directory
ansible.builtin.import_tasks: validate_apt_tmpdir.yml
# apt and the package module put their temporary files, including the staged .deb files, in TMPDIR. A restart of ssh.service during the upgrade
# (an openssh-server update) deletes a PrivateTmp /tmp, so TMPDIR points at a directory systemd leaves alone. Created in check mode too,
# because the autoremove below runs there with the same TMPDIR.
- name: Create the apt scratch directory
ansible.builtin.file:
path: "{{ system_baseline_apt_tmpdir }}"
state: directory
owner: root
group: root
mode: "0700"
become: true
# safe is the apt module's name for the plain upgrade (apt-get upgrade --with-new-pkgs), what `true` meant here before; dist runs
# apt-get dist-upgrade (profile key apt_upgrade, `pve` by default).
- name: Update & upgrade apt packages
@@ -13,6 +28,8 @@
update_cache: true
upgrade: "{{ system_baseline_apt_upgrade }}"
cache_valid_time: 3600
environment:
TMPDIR: "{{ system_baseline_apt_tmpdir }}"
become: true
when: not (ansible_check_mode and __system_baseline_main_flow | default(false))
@@ -21,4 +38,12 @@
ansible.builtin.package:
autoremove: true
autoclean: true
environment:
TMPDIR: "{{ system_baseline_apt_tmpdir }}"
become: true
# A restart of ssh.service ends the connection that was open during the upgrade; a new one gets a new private /tmp, which later tasks
# of the play (and a caller's own tasks) may use.
- name: Open a new connection after the upgrade
ansible.builtin.meta: reset_connection
when: not (ansible_check_mode and __system_baseline_main_flow | default(false))
+4
View File
@@ -39,6 +39,10 @@
ansible.builtin.import_tasks: validate_apt_upgrade.yml
tags: always
- name: Validate the apt scratch directory
ansible.builtin.import_tasks: validate_apt_tmpdir.yml
tags: always
- name: Validate the reboot inputs
ansible.builtin.import_tasks: validate_reboot.yml
tags: always
@@ -0,0 +1,15 @@
---
# Shared by validate.yml and update.yml: the tasks_from: update entry point does not run validate.yml. The directory becomes TMPDIR of the apt
# upgrade, so it must not be one that an ssh.service restart (PrivateTmp=yes) deletes, and it is created as root: a relative path, a `..` or an
# odd character must stop the run before anything changes.
- name: Validate the apt scratch directory
ansible.builtin.assert:
that:
- system_baseline_apt_tmpdir is string
- system_baseline_apt_tmpdir is match('^/[A-Za-z0-9._/-]+\Z')
- "'..' not in system_baseline_apt_tmpdir"
- system_baseline_apt_tmpdir is not match('^/(var/)?tmp(/|\Z)')
fail_msg: >-
system_baseline_apt_tmpdir must be an absolute path of letters, digits, . _ - and /, without .., and not /tmp, /var/tmp or a path below them.
quiet: true
tags: always