fix(system_baseline): run the apt upgrade with TMPDIR outside /tmp and /var/tmp
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:
1 parent
008c5ab8a4
commit
1bbf3368ee
11 files changed
+151
-1
No files matched your search
@@ -0,0 +1,12 @@
|
||||
---
|
||||
bugfixes:
|
||||
- >-
|
||||
system_baseline - the apt upgrade no longer loses its staged packages when it upgrades ``openssh-server`` on a host whose
|
||||
``ssh.service`` has ``PrivateTmp=yes``. The restart of that unit deleted ``/tmp`` and ``/var/tmp`` in the middle of the upgrade, so
|
||||
dpkg failed with "cannot access archive" and left packages unpacked but not configured. The upgrade and the autoremove now run with
|
||||
``TMPDIR`` set to ``system_baseline_apt_tmpdir``, and the role opens a new connection afterwards.
|
||||
minor_changes:
|
||||
- >-
|
||||
system_baseline - add ``system_baseline_apt_tmpdir`` (default ``/var/cache/system-baseline-apt-tmp``), the directory the apt upgrade and
|
||||
autoremove use as ``TMPDIR``. The role creates it (root, mode ``0700``) when it runs the ``update`` flow, including the ``update`` entry
|
||||
point, and refuses a value under ``/tmp`` or ``/var/tmp``, a relative path or a ``..``.
|
||||
@@ -52,6 +52,7 @@ templating error instead of the role's own message.
|
||||
| `system_baseline_reboot_throttle` | `0` | no | Hosts that reboot at the same time; `0` is no limit, `1` one at a time (see "Rebooting one host at a time") |
|
||||
| `system_baseline_reboot_post_delay` | `0` | no | Seconds to wait after a rebooted host answers again |
|
||||
| `system_baseline_apt_daily_timeout` | empty | no | Systemd time span written as `TimeoutStartSec` of `apt-daily.service`; empty leaves the host alone (see "Bounding a hung apt-daily.service") |
|
||||
| `system_baseline_apt_tmpdir` | `/var/cache/system-baseline-apt-tmp` | no | Directory the apt upgrade uses as `TMPDIR`; absolute, never `/tmp` or `/var/tmp` (see "Apt scratch directory") |
|
||||
| `system_baseline_update` | `true` | no | Run the package update in the main flow; `false` skips it (the `update` entry point ignores it) |
|
||||
|
||||
A flat `system_baseline_*` input replaces that value entirely. To change one key of a profile dict, use `system_baseline_profile_overrides`, for example to let unattended-upgrades reboot a group on its own:
|
||||
@@ -93,7 +94,7 @@ The two task files besides `main` are a supported interface, for a caller that d
|
||||
|
||||
### `update`
|
||||
|
||||
`tasks_from: update`: apt update, upgrade, autoremove and autoclean, nothing else. It does not read `system_baseline_update`: calling the entry point is the request to update. In check mode it previews the upgrade; the main flow does not (see "Check mode").
|
||||
`tasks_from: update`: apt update, upgrade, autoremove and autoclean, nothing else. It does not read `system_baseline_update`: calling the entry point is the request to update. In check mode it previews the upgrade; the main flow does not (see "Check mode"). It reads `system_baseline_apt_tmpdir` (see "Apt scratch directory").
|
||||
|
||||
### `reboot`
|
||||
|
||||
@@ -129,6 +130,7 @@ The scenario `molecule/default/` runs in CI only, on one platform: `${MOLECULE_I
|
||||
- Converge then applies the `ubuntu_lxc` profile, with tmux autostart and `system_baseline_apt_daily_timeout: 1h`, and only resolves the `pve` and `ubuntu_vm` profiles and a consumer override, asserting the resolved inputs.
|
||||
- Verify checks the installed and removed packages, the journal size cap, the apt settings in `20auto-upgrades` and `50unattended-upgrades`, the locale, and that systemd loaded the `apt-daily.service` timeout.
|
||||
- Verify checks the tmux snippet: root-owned, valid shell, the session and quoted window command, no effect without a terminal, a session name or window command ending in a newline refused, and the file removed when autostart is off.
|
||||
- Converge also checks that a bad `system_baseline_apt_tmpdir` (under `/tmp` or `/var/tmp`, relative, with `..`, empty) is refused in the main flow and in the `update` entry point. Verify reads `tasks/update.yml` and checks that the scratch directory is created with mode `0700`, that both apt tasks set `TMPDIR` to it, and that the connection is reset afterwards: the scenario skips the `update` tag, so the upgrade itself never runs there.
|
||||
- Verify runs the `reboot` entry point with `system_baseline_reboot_if_required: false`, without and with `/var/run/reboot-required`, and checks the fact and that the host did not reboot.
|
||||
- The `pve` origins pin is applied to the Ubuntu container and read back with `apt-config dump`, then removed again. The package blacklist is written, read back as a list, left alone by a second run and removed by an empty list.
|
||||
|
||||
@@ -193,6 +195,14 @@ The profile key `apt_upgrade` (`safe` or `dist`, resolved to `system_baseline_ap
|
||||
- The `update` entry point follows the same input: it upgrades with `dist` when `system_baseline_system_type` is `pve`. Without a `system_baseline_system_type` (the entry point does not require one) it stays on `safe`.
|
||||
- Only the apt upgrade changes. Autoremove and autoclean, the check-mode skip and unattended-upgrades are as before; the example blacklist below keeps unattended-upgrades away from the Proxmox packages.
|
||||
|
||||
### Apt scratch directory
|
||||
|
||||
The upgrade runs over the SSH connection Ansible uses. An `openssh-server` upgrade restarts `ssh.service`, and a unit with `PrivateTmp=yes` (the `systemd_hardening` role sets it) has its own `/tmp` and `/var/tmp`, which systemd deletes when the unit stops. The files apt staged there for the running upgrade then disappear and dpkg fails, leaving packages unpacked but not configured. So the `update` flow 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, so that later tasks get a fresh private `/tmp`.
|
||||
|
||||
- The value must be an absolute path of letters, digits, `.`, `_`, `-` and `/`, without `..`, and not `/tmp`, `/var/tmp` or a path below them; anything else stops the run before a change.
|
||||
- The directory is also created in check mode (the autoremove runs there with the same `TMPDIR`), so a dry run reports it once, on a host that does not have it yet.
|
||||
- Changing the value does not remove the old directory.
|
||||
|
||||
### Check mode
|
||||
|
||||
In a dry run (`--check`) the main flow does not run the apt upgrade: a package that is only waiting for the next patch run, or a package cache older than an hour, is not drift, and the package module reports both as `changed`, so a scheduled dry run would flag every host whenever a package landed in between. The autoremove and autoclean step still runs, and is checked: a leftover package it would remove is reported as `changed`, on purpose. `apt-get autoclean` runs in this step in a real run, in the main flow and in the `update` entry point. The `update` entry point is unchanged: it is an explicit request to update, so in check mode it still previews the upgrade.
|
||||
|
||||
@@ -11,6 +11,10 @@ system_baseline_reboot_throttle: 0
|
||||
system_baseline_reboot_post_delay: 0
|
||||
# Systemd time span (for example 1h) written as TimeoutStartSec of apt-daily.service; empty leaves the host alone (tasks/apt_daily_timeout.yml).
|
||||
system_baseline_apt_daily_timeout: ""
|
||||
# Directory the apt upgrade and autoremove use as TMPDIR (tasks/update.yml): absolute, never /tmp or /var/tmp. An openssh-server upgrade
|
||||
# restarts ssh.service, and with PrivateTmp=yes systemd then deletes that unit's /tmp and /var/tmp while the upgrade still needs its staged files.
|
||||
# Created root-owned, mode 0700.
|
||||
system_baseline_apt_tmpdir: /var/cache/system-baseline-apt-tmp
|
||||
# Run the package update (tasks/update.yml) in the main flow. false skips it, for hosts that are patched by a separate,
|
||||
# operator-run flow; the tasks_from: update entry point does not read it.
|
||||
system_baseline_update: true
|
||||
|
||||
@@ -129,6 +129,16 @@ argument_specs:
|
||||
C(/etc/systemd/system/apt-daily.service.d/10-timeout.conf), so a hung apt run is stopped. Empty leaves the host alone; clearing
|
||||
the value later does not remove a file already written. A value of C(0) means no timeout to systemd.
|
||||
C(apt-daily-upgrade.service) is not changed. Tag C(apt-daily-timeout).
|
||||
system_baseline_apt_tmpdir:
|
||||
type: str
|
||||
default: /var/cache/system-baseline-apt-tmp
|
||||
description:
|
||||
- >-
|
||||
Directory the apt upgrade and autoremove use as C(TMPDIR). It must be an absolute path made of letters, digits, C(.), C(_), C(-)
|
||||
and C(/), without C(..), and not C(/tmp) or C(/var/tmp) or anything below them: an C(openssh-server) upgrade restarts
|
||||
C(ssh.service), and when that unit has C(PrivateTmp=yes) systemd deletes its C(/tmp) and C(/var/tmp) while the upgrade still
|
||||
needs the files apt staged there. The role creates it, owned by root with mode C(0700). An invalid value stops the run before
|
||||
any change. Changing the value later does not remove the old directory.
|
||||
system_baseline_unattended_upgrade_package_blacklist:
|
||||
type: list
|
||||
elements: str
|
||||
@@ -162,6 +172,10 @@ argument_specs:
|
||||
- >-
|
||||
A supported entry point (C(tasks_from=update)) for a caller that drives its own reboot, for example a
|
||||
Kubernetes node patch flow that drains the node first.
|
||||
- >-
|
||||
It creates C(system_baseline_apt_tmpdir), runs the upgrade and the autoremove with C(TMPDIR) set to it, and then opens a new
|
||||
connection, so that a restart of C(ssh.service) during the upgrade does not remove the files apt needs. It reads that input
|
||||
although the entry point has no argument specification of its own.
|
||||
reboot:
|
||||
short_description: Compute system_baseline_reboot_required and reboot when allowed
|
||||
description:
|
||||
|
||||
@@ -64,6 +64,23 @@
|
||||
that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
# The apt scratch directory becomes TMPDIR of the upgrade: a path under /tmp or /var/tmp (which a restart of ssh.service can delete), a relative
|
||||
# path and a `..` each stop the run before any change, in the main flow and in the update entry point (which does not run validate.yml).
|
||||
- name: A bad apt scratch directory is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Refuse each bad value
|
||||
ansible.builtin.include_tasks: tasks/refuse_apt_tmpdir_entries.yml
|
||||
vars:
|
||||
system_baseline_refused_tmpdir: "{{ item }}"
|
||||
loop:
|
||||
- /tmp
|
||||
- /var/tmp/apt
|
||||
- /var/cache/../../tmp/x
|
||||
- var/cache/apt-tmp
|
||||
- ""
|
||||
|
||||
# The reboot inputs are whole numbers, 0 or more: a negative number and a boolean each stop the run before a host reboots,
|
||||
# in the main flow and in the reboot entry point (which does not run validate.yml).
|
||||
- name: A bad reboot throttle or post-reboot delay is refused
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
# One refusal per entry point: the main flow and the update entry point must both stop on validation, with a message that names the variable.
|
||||
- name: Run the role with a bad apt scratch directory
|
||||
block:
|
||||
- name: Include system_baseline
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.system_baseline
|
||||
tasks_from: "{{ system_baseline_refused_entry }}"
|
||||
vars:
|
||||
system_baseline_system_type: ubuntu_lxc
|
||||
system_baseline_apt_tmpdir: "{{ system_baseline_refused_tmpdir }}"
|
||||
- name: Unreachable when validation works
|
||||
ansible.builtin.fail:
|
||||
msg: system_baseline ran with an invalid system_baseline_apt_tmpdir
|
||||
rescue:
|
||||
- name: The validation stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'system_baseline_apt_tmpdir must be' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Refuse the value in the main flow and in the update entry point
|
||||
ansible.builtin.include_tasks: refuse_apt_tmpdir.yml
|
||||
vars:
|
||||
system_baseline_refused_entry: "{{ entry }}"
|
||||
loop:
|
||||
- main
|
||||
- update
|
||||
loop_control:
|
||||
loop_var: entry
|
||||
@@ -101,6 +101,26 @@
|
||||
- system_baseline_verify_apt_daily_show.stdout_lines is contains('TimeoutStartUSec=1h')
|
||||
quiet: true
|
||||
|
||||
# The scenario skips the `update` tag (molecule.yml), so the upgrade never runs here and its scratch directory is never created. This reads the
|
||||
# task file on the controller and checks that the directory is private and that both apt tasks use it as TMPDIR.
|
||||
- name: The update tasks give the apt upgrade a private scratch directory
|
||||
hosts: localhost
|
||||
connection: local
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the update tasks
|
||||
ansible.builtin.set_fact:
|
||||
system_baseline_verify_update_tasks: "{{ lookup('ansible.builtin.file', playbook_dir ~ '/../../tasks/update.yml') }}"
|
||||
|
||||
- name: The scratch directory is created private and both apt tasks use it
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- system_baseline_verify_update_tasks is search('(?m)^ path:\s"\{\{ system_baseline_apt_tmpdir \}\}"$')
|
||||
- system_baseline_verify_update_tasks is search('(?m)^ mode:\s"0700"$')
|
||||
- system_baseline_verify_update_tasks | regex_findall('(?m)^ TMPDIR:\s"\{\{ system_baseline_apt_tmpdir \}\}"$') | length == 2
|
||||
- system_baseline_verify_update_tasks is search('(?m)^ ansible.builtin.meta:\sreset_connection$')
|
||||
quiet: true
|
||||
|
||||
# The reboot entry point is a supported interface: with system_baseline_reboot_if_required
|
||||
# false it only sets system_baseline_reboot_required. Here and not in converge, because it
|
||||
# creates and removes the marker file, which the idempotence run would report as a change.
|
||||
|
||||
@@ -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))
|
||||
@@ -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
|
||||
Reference in new issue
Block a user