From ef9ad90801b6570bd08b9a10cb64c9e2eea01328 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:31:44 +0000 Subject: [PATCH] Net::Server::PreFork: create lock_file with mode 0600 With serialize set to flock and a lock_file configured, each PreFork and PreForkSimple child opened the lock file with open(..., ">", ...), which creates it with mode 0666 less the umask. With the usual umask of 022 the file is readable by every local user. flock needs only a read-only handle, so any local user could open the file, take LOCK_EX and hold it, and every child would then block before accept and the server would stop answering. Open the lock file with sysopen, O_WRONLY | O_CREAT and mode 0600, so that a lock file created by the server can only be opened by the server's user. The file is no longer truncated, which does not matter as its content is never used. An existing lock file keeps its mode. The POD for lock_file now says so. Notes for deciding whether to apply this patch: - Only a lock file created by the server gets mode 0600. An existing lock_file keeps its mode and must be restricted by hand. - A lock_file in a directory other local users can write to can still be created by them first. Keep it in a directory only the server's user can write to. - The lock file is no longer truncated when opened. Its content is never used. - Not tested on a running server. It was checked by review and by applying it to Net-Server 2.018. - Applies on its own to Net-Server 2.018, and together with the other CPANSec patches for Net-Server 2.018 when they are applied in CVE-ID order. --- lib/Net/Server/PreFork.pm | 3 ++- lib/Net/Server/PreForkSimple.pm | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Net/Server/PreFork.pm b/lib/Net/Server/PreFork.pm index 262c1da..5229ab3 100644 --- a/lib/Net/Server/PreFork.pm +++ b/lib/Net/Server/PreFork.pm @@ -21,6 +21,7 @@ use strict; use base qw(Net::Server::PreForkSimple); use Net::Server::SIG qw(register_sig check_sigs); use POSIX qw(WNOHANG); +use Fcntl (); use IO::Select (); use Time::HiRes qw(time); @@ -208,7 +209,7 @@ sub run_child { # Open in child at start if ($prop->{'serialize'} eq 'flock') { - open $prop->{'lock_fh'}, ">", $prop->{'lock_file'} + sysopen $prop->{'lock_fh'}, $prop->{'lock_file'}, Fcntl::O_WRONLY() | Fcntl::O_CREAT(), 0600 or $self->fatal("Couldn't open lock file \"$prop->{'lock_file'}\"[$!]"); } diff --git a/lib/Net/Server/PreForkSimple.pm b/lib/Net/Server/PreForkSimple.pm index 9afce1f..b593784 100644 --- a/lib/Net/Server/PreForkSimple.pm +++ b/lib/Net/Server/PreForkSimple.pm @@ -175,7 +175,7 @@ sub run_child { my $needs_lock = ($prop->{'serialize'} eq 'flock') ? 1 : 0; if ($needs_lock) { - open($prop->{'lock_fh'}, ">", $prop->{'lock_file'}) + sysopen($prop->{'lock_fh'}, $prop->{'lock_file'}, Fcntl::O_WRONLY() | Fcntl::O_CREAT(), 0600) or $self->fatal("Couldn't open lock file \"$prop->{'lock_file'}\"[$!]"); } @@ -447,7 +447,10 @@ then a the default serialization will be used instead of "none." Filename to use in flock serialized accept in order to serialize the accept sequence between the children. This will default to a generated temporary filename. If default value is used the lock_file will be -removed when the server closes. +removed when the server closes. A lock_file that does not exist yet is +created with mode 0600, so that other local users cannot open it and +hold the lock. An existing file keeps its mode; make sure that only the +server's user can read it. =item check_for_dead -- 2.53.0