From 70342a9d8dfdbce412c0fd1d1ff23604e7c0f1e6 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:32:39 +0000 Subject: [PATCH] Net::Server: do not follow symlinks when opening and chowning log and pid files A server started as root with a user to drop to opens log_file, pid_file and, in Net::Server::HTTP, access_log_file before the drop, then chowns each path to the run-as user. Both the open and the chown follow a symlink at the path. An account that can write to the directory holding these files, typically the run-as account itself, can replace one with a symlink: root then creates a missing target, or opens an existing one for the log files, and the chown hands the target to the run-as account at the next start as root. Add Net::Server::Daemonize::open_nofollow, which opens with O_NOFOLLOW (where available) so a symlink at the final path component is refused, and also refuses a regular file with more than one link. Use it for the log file, the access log and the pid file. Record the open handle rather than the path in chown_files and chown through the handle (fchown), and reopen the pid file the same way before chowning it, so the chown always applies to the file that was opened. Path entries in chown_files, such as UNIX socket paths, are chowned as before. The pid file is opened without O_TRUNC and truncated only after the link check, so a hardlinked file is not truncated before it is refused. Document the new behaviour for log_file, pid_file and access_log_file. Notes for deciding whether to apply this patch: - A log_file, pid_file or access_log_file path that is a symlink now stops the server at startup. This includes log_file set to /dev/stderr or another symlinked device path. The special STDERR names for access_log_file are not affected. - A log or pid file with more than one hard link is refused. - The chown goes through the open file handle, so platforms without fchown fail at startup when a user to drop to is set. - Files are opened with O_NONBLOCK where available. A FIFO used as a log may then return EAGAIN on writes. - Only the final path component is protected. Symlinks in parent directories are still followed. - lock_file and UNIX socket paths are still chowned by path. - 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.pm | 17 ++++++++++++----- lib/Net/Server.pod | 8 ++++++++ lib/Net/Server/Daemonize.pm | 32 ++++++++++++++++++++++++++++++-- lib/Net/Server/HTTP.pm | 8 ++++++-- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/lib/Net/Server.pm b/lib/Net/Server.pm index 17353d3..56657ce 100644 --- a/lib/Net/Server.pm +++ b/lib/Net/Server.pm @@ -46,7 +46,7 @@ use Net::Server::Proto qw[ safe_name_info ]; use Net::Server::Daemonize qw(check_pid_file create_pid_file safe_fork - get_uid get_gid set_uid set_gid); + get_uid get_gid set_uid set_gid open_nofollow); our $VERSION = '2.018'; @@ -237,10 +237,13 @@ sub initialize_logging { # regular file based logging croak "Unsecure filename \"$prop->{'log_file'}\"" if $prop->{'log_file'} !~ m|^([\:\w\.\-/\\]+)$|; $prop->{'log_file'} = $1; # open a logging file - open(_SERVER_LOG, ">>", $prop->{'log_file'}) + my $fh = eval { open_nofollow($prop->{'log_file'}, '>>') } + || croak "Couldn't open log file \"$prop->{'log_file'}\": $@"; + open(_SERVER_LOG, ">>&", $fh) || croak "Couldn't open log file \"$prop->{'log_file'}\" [$!]"; + close $fh; _SERVER_LOG->autoflush(1); - push @{ $prop->{'chown_files'} }, $prop->{'log_file'}; + push @{ $prop->{'chown_files'} }, [$prop->{'log_file'}, \*_SERVER_LOG]; # chown the handle, not the path } sub post_configure_hook {} @@ -390,13 +393,17 @@ sub post_bind { # secure the process and background it if ($prop->{'group'} ne $) || $prop->{'user'} ne $>) { my @chown_files; push @chown_files, map {$_->NS_port} grep {$_->NS_proto =~ /^UNIX/} @{ $prop->{'sock'} }; - push @chown_files, $prop->{'pid_file'} if $prop->{'pid_file_unlink'}; + if ($prop->{'pid_file_unlink'}) { # chown the handle, not the path + my $fh = eval { open_nofollow($prop->{'pid_file'}, '<') } || $self->fatal(my $e = $@); + push @chown_files, [$prop->{'pid_file'}, $fh]; + } push @chown_files, $prop->{'lock_file'} if $prop->{'lock_file_unlink'}; push @chown_files, @{ $prop->{'chown_files'} || [] }; my $uid = $prop->{'user'}; my $gid = (split /\ /, $prop->{'group'})[0]; foreach my $file (@chown_files){ - chown($uid, $gid, $file) || $self->fatal("Couldn't chown \"$file\" [$!]"); + my ($name, $target) = ref($file) eq 'ARRAY' ? @$file : ($file, $file); + chown($uid, $gid, $target) || $self->fatal("Couldn't chown \"$name\" [$!]"); } } diff --git a/lib/Net/Server.pod b/lib/Net/Server.pod index 960cfb4..bbff542 100644 --- a/lib/Net/Server.pod +++ b/lib/Net/Server.pod @@ -546,6 +546,11 @@ If a C is given or if C is set, STDIN and STDOUT will automatically be opened to /dev/null and STDERR will be opened to STDOUT. This will prevent any output from ending up at the terminal. +A log file is opened without following a symlink at its path, and a +regular file with more than one link is refused, because the file is +opened and chowned to C while the server is still running as +root. + =item log_function Can take a coderef or method name to call when a log event occurs. @@ -560,6 +565,9 @@ during process_request in situations where the tie_stdout is set Filename to store pid of parent process. Generally applies only to forking servers. Default is none (undef). +As with C, the pid file is opened without following a +symlink at its path. + =item port See L for further examples of configuration. diff --git a/lib/Net/Server/Daemonize.pm b/lib/Net/Server/Daemonize.pm index 1901eac..4ae21de 100644 --- a/lib/Net/Server/Daemonize.pm +++ b/lib/Net/Server/Daemonize.pm @@ -29,7 +29,8 @@ our $VERSION = "0.06"; our @EXPORT_OK = qw(check_pid_file create_pid_file unlink_pid_file is_root_user get_uid get_gid set_uid set_gid - set_user safe_fork daemonize); + set_user safe_fork daemonize + open_nofollow); ###----------------------------------------------------------------### @@ -69,7 +70,7 @@ sub create_pid_file ($) { check_pid_file($pid_file); - open my $fh, '>', $pid_file or croak "Couldn't open pid file \"$pid_file\" [$!]"; + my $fh = open_nofollow($pid_file, '>'); print $fh "$$\n"; close $fh; @@ -95,6 +96,33 @@ sub unlink_pid_file ($) { return 1; } +### open a file (modes '<', '>' or '>>') without following a +### symlink at the final path component, so that files created or +### chowned by root cannot be redirected elsewhere. A regular file +### with more than one link is refused as well. +sub open_nofollow { + my ($file, $mode) = @_; + require Fcntl; + my %flags = ('<' => Fcntl::O_RDONLY(), + '>' => Fcntl::O_WRONLY() | Fcntl::O_CREAT(), + '>>' => Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_APPEND()); + croak "Invalid open mode \"$mode\"" if ! defined $mode || ! exists $flags{$mode}; + my $flags = $flags{$mode}; + foreach my $name (qw(O_NOFOLLOW O_NONBLOCK)) { # not available everywhere + my $val = eval { no strict 'refs'; &{"Fcntl::$name"}() }; + $flags |= $val if $val; + } + + sysopen(my $fh, $file, $flags, 0666) or croak "Couldn't open \"$file\" [$!]"; + my @stat = stat $fh; + croak "Refusing to use \"$file\": it has more than one link" + if -f _ && $stat[3] > 1; + if ($mode eq '>') { + truncate($fh, 0) or croak "Couldn't truncate \"$file\" [$!]"; + } + return $fh; +} + ###----------------------------------------------------------------### sub is_root_user () { diff --git a/lib/Net/Server/HTTP.pm b/lib/Net/Server/HTTP.pm index d186e06..bf085b7 100644 --- a/lib/Net/Server/HTTP.pm +++ b/lib/Net/Server/HTTP.pm @@ -104,9 +104,10 @@ sub _init_access_log { } elsif ($log eq 'STDERR' || $log eq '/dev/stderr') { $prop->{'access_log_function'} = sub { print STDERR @_,"\n" }; } else { - open my $fh, '>>', $log or die "Could not open access_log_file \"$log\": $!"; + my $fh = eval { Net::Server::Daemonize::open_nofollow($log, '>>') } + or die "Could not open access_log_file \"$log\": $@"; $fh->autoflush(1); - push @{ $prop->{'chown_files'} }, $log; + push @{ $prop->{'chown_files'} }, [$log, $fh]; # chown the handle, not the path $prop->{'access_log_function'} = sub { print $fh @_,"\n" }; } } @@ -1059,6 +1060,9 @@ the same location as the ERROR log. If a special value of STDOUT or F is passed, the access log entry will be written to standard out. +Any other file is opened without following a symlink at its path, as +with C in L. + =item access_log_function Can take a coderef or method name to call when an log_http_request -- 2.53.0