Skip to content

fix: create the rotation lock file 0644, and never through a symlink - #370

Merged
deemar merged 2 commits into
HardySimpson:masterfrom
yegorich:fix/lockfile-permissions
Oct 8, 2026
Merged

deemar merged 2 commits into
HardySimpson:masterfrom
yegorich:fix/lockfile-permissions

Conversation

@yegorich

Copy link
Copy Markdown
Contributor

lock_file() created it with S_IRWXU|S_IRWXG|S_IRWXO and followed whatever the name pointed at. Nothing is ever written to that file -- it exists to be fcntl locked -- so the execute bits and the world write bit were asking for permissions the library does not use.

It matters most for the default when there is no configuration file to lock against, /tmp/zlog.lock, which every such process shares: whoever creates the name first decides what it refers to, and a symlink planted there aimed the lock at a file of the attacker's choosing. Nothing is written through it, so this is a rotation denial of service rather than data loss, but there is no reason to leave it open. The fallback added recently for a read-only configuration file sends more processes to that path, which is what brought it up.

0644 and O_NOFOLLOW, the latter behind an ifdef for platforms without it. This is not a regression for rotation between users: with the usual umask of 022 the old mode produced 0755, so a second user could not take the lock either, and such a setup needs the file created and chowned by hand as the users guide says.

test_lockfile_perms checks the mode after a rotation, and that a symlink in place of the lock file stops the rotation instead of being followed. It fails without the change ("lock file is 0777, expected 0644"). 39/39 tests pass in Debug, 37/37 in Release.

yegorich and others added 2 commits September 30, 2026 10:30
lock_file() created it with S_IRWXU|S_IRWXG|S_IRWXO and followed whatever
the name pointed at. Nothing is ever written to that file -- it exists to
be fcntl locked -- so the execute bits and the world write bit were asking
for permissions the library does not use.

It matters most for the default when there is no configuration file to
lock against, /tmp/zlog.lock, which every such process shares: whoever
creates the name first decides what it refers to, and a symlink planted
there aimed the lock at a file of the attacker's choosing. Nothing is
written through it, so this is a rotation denial of service rather than
data loss, but there is no reason to leave it open. The fallback added
recently for a read-only configuration file sends more processes to that
path, which is what brought it up.

0644 and O_NOFOLLOW, the latter behind an ifdef for platforms without it.
This is not a regression for rotation between users: with the usual umask
of 022 the old mode produced 0755, so a second user could not take the
lock either, and such a setup needs the file created and chowned by hand
as the users guide says.

test_lockfile_perms checks the mode after a rotation, and that a symlink
in place of the lock file stops the rotation instead of being followed. It
fails without the change ("lock file is 0777, expected 0644"). 39/39 tests
pass in Debug, 37/37 in Release.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The users guide said zlog opens the lock file at zlog_init() with
read-write permission. It does not: the open happens the first time a
rotation is due, which is why zlog_init() succeeds against a read-only
configuration file and the trouble only shows up later, as a rotation
that never happens (HardySimpson#88, HardySimpson#69).

Bring the section up to what the code does:

  - the open is lazy, and the file is created 0644, with a symbolic link
    in its place refused rather than followed
  - a configuration file the process may only read cannot lock itself, so
    zlog falls back to /tmp/zlog.lock and warns; same when there is no
    configuration file at all
  - for rotation between users, say what actually has to be done -- create
    the lock file beforehand and grant write access -- rather than "make
    sure each program has permission", which 0644 does not give them

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@deemar
deemar merged commit bbcc587 into HardySimpson:master Oct 8, 2026
32 checks passed
@yegorich
yegorich deleted the fix/lockfile-permissions branch October 8, 2026 07:19
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.

2 participants