Skip to content

fix: handle concurrent/interrupted processes to avoid file corruption (LP: #2126805)#11

Open
jdstrand wants to merge 12 commits into
mainfrom
jdstrand/lp-2126805
Open

fix: handle concurrent/interrupted processes to avoid file corruption (LP: #2126805)#11
jdstrand wants to merge 12 commits into
mainfrom
jdstrand/lp-2126805

Conversation

@jdstrand

@jdstrand jdstrand commented Jul 3, 2026

Copy link
Copy Markdown
Owner

No description provided.

@jdstrand jdstrand force-pushed the jdstrand/lp-2126805 branch 5 times, most recently from 6c84835 to 5271a02 Compare July 3, 2026 15:07
jdstrand added 12 commits July 6, 2026 07:30
close_files() rewrote the target in place: shutil.copy() truncates the
live file and rewrites it through a buffered stream. Every 'ufw insert'
or 'ufw delete' therefore had a window where user.rules on disk was
empty or cut mid-line and concurrent 'ufw status' printed rules from
that broken state. Furthermore, a crash, power loss or SIGKILL mid-copy
left the truncation on disk.

Create the tempfile next to the target instead of in TMPDIR, fsync it,
preserve the original's mode/ownership, and os.rename() it over the
original (plus a best-effort directory fsync for power-loss safety).
Readers and crashes now only ever see a complete old or complete new
file. This also covers ufw.conf and the default policy files, which go
through the same helpers.
main_ufw() constructed the UFWFrontend before create_lock() and
UFWFronted parses user.rules, user6.rules and the configuration files.
Every ufw process therefore acted on a snapshot taken while another
process could be mid-update where a rule change could rewrite the whole
rules file from that stale snapshot.

Compute the lockfile path and take the lock before constructing the
frontend, so the whole read-modify-write cycle sits inside the critical
section.
@jdstrand jdstrand force-pushed the jdstrand/lp-2126805 branch from 5271a02 to 5e4bdc2 Compare July 6, 2026 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant