Skip to content

Write Galera state files atomically with restrictive modes - #147

Open
abg wants to merge 1 commit into
mainfrom
galera-init-state-file-perms
Open

abg wants to merge 1 commit into
mainfrom
galera-init-state-file-perms

Conversation

@abg

@abg abg commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

galera-healthcheck wrote state.txt with os.WriteFile(..., 0777), and galera-init rewrote grastate.dat with ioutil.WriteFile(..., 0777). Both files could end up world-writable (the final mode depends on umask).

Write state.txt with 0600 and grastate.dat with 0640 (what mysqld creates), using renameio so the replacement is atomic and the galera-agent bootstrap/join/single-node endpoints behave like galera-init's existing state writes.

renameio.WriteFile preserves the mode of an existing file, so add os_helper.WriteFileAtomic (and an equivalent helper in healthcheck) built on WithStaticPermissions. It forces the mode regardless of umask and corrects files left loose by earlier releases. galera-init's WriteStringToFile, whose only caller writes state.txt, now uses 0600 so both writers agree. The grastate.dat safe_to_bootstrap rewrite also uses the helper, replacing the ioutil read/write in the node starter.

This is hardening / defense in depth: bpm already restricts /var/vcap/store/pxc-mysql to 0700, so a non-vcap local user could not reach these files. Only the galera-agent bootstrap path was still creating state.txt with loose permissions.

galera-healthcheck wrote state.txt with os.WriteFile(..., 0777), and
galera-init rewrote grastate.dat with ioutil.WriteFile(..., 0777). Both
files could end up world-writable (the final mode depends on umask).

Write state.txt with 0600 and grastate.dat with 0640 (what mysqld
creates), using renameio so the replacement is atomic and the
galera-agent bootstrap/join/single-node endpoints behave like
galera-init's existing state writes.

renameio.WriteFile preserves the mode of an existing file, so add
os_helper.WriteFileAtomic (and an equivalent helper in healthcheck) built
on WithStaticPermissions. It forces the mode regardless of umask and
corrects files left loose by earlier releases. galera-init's
WriteStringToFile, whose only caller writes state.txt, now uses 0600 so
both writers agree. The grastate.dat safe_to_bootstrap rewrite also uses
the helper, replacing the ioutil read/write in the node starter.

This is hardening / defense in depth: bpm already restricts
/var/vcap/store/pxc-mysql to 0700, so a non-vcap local user could not
reach these files. Only the galera-agent bootstrap path was still
creating state.txt with loose permissions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant