From 9872f4bfa2ae17d8c3e36d8285b802bc6693f064 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:30:31 +0000 Subject: [PATCH] Net::Server: drop root's groups when user is set without group When the group option is not set, post_bind defaults it to the current group list, $). In a server started as root that list is root's own, so the later "group ne $)" check is false and set_gid never runs. Only the uid is changed: the real, effective and saved gid stay 0 and the supplementary groups of the starting process are kept, so code running after the privilege drop keeps access to files open to group 0 and to root's supplementary groups. The documentation says group defaults to "nobody". When user is set and group is not, resolve group from the run-as user instead: its primary gid from the passwd entry, plus every group that lists the user as a member. The gid change and setgroups then run as part of the normal privilege drop. A uid with no passwd entry is now a fatal error asking for an explicit group. When neither option is set, or user is the current user, the current groups are kept as before. Document the new default for group. Notes for deciding whether to apply this patch: - When user is set and group is not, the server now drops to the run-as user's primary group and the supplementary groups that list it, instead of keeping gid 0 and root's groups. Code that relied on group 0 access after the drop loses it. - A numeric user with no passwd entry and no group set now stops the server at startup, asking for an explicit group. - The supplementary groups are found with getgrent, which can be slow or incomplete on large or LDAP-backed group databases. Setting group explicitly avoids the lookup. - The documentation still says user defaults to "nobody", which does not match the code. That is not changed here. - 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 | 28 ++++++++++++++++++++++++++-- lib/Net/Server.pod | 10 +++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/Net/Server.pm b/lib/Net/Server.pm index 17353d3..5bc3333 100644 --- a/lib/Net/Server.pm +++ b/lib/Net/Server.pm @@ -366,9 +366,14 @@ sub post_bind { # secure the process and background it my $self = shift; my $prop = $self->{'server'}; + my $group_from_user; if (! defined $prop->{'group'}) { - $self->log(1, "Group Not Defined. Defaulting to EGID '$)'"); - $prop->{'group'} = $); + if (defined $prop->{'user'}) { + $group_from_user = 1; # resolved from the user's groups below + } else { + $self->log(1, "Group Not Defined. Defaulting to EGID '$)'"); + $prop->{'group'} = $); + } } elsif ($prop->{'group'} =~ /^([\w.-]+(?:[ ,][\w.-]+)*)$/) { $prop->{'group'} = eval { get_gid($1) }; $self->fatal(my $e = $@) if $@; @@ -386,6 +391,25 @@ sub post_bind { # secure the process and background it $self->fatal("Invalid user \"$prop->{'user'}\""); } + # user without group: use the user's own groups, not the starting process's (root's) groups + if ($group_from_user) { + if ($prop->{'user'} ne $>) { + my ($name, $pgid) = (getpwuid($prop->{'user'}))[0, 3]; + $self->fatal("No passwd entry for uid \"$prop->{'user'}\" - set group explicitly") if ! defined $pgid; + my @gids = ($pgid); + setgrent(); + while (my ($gid, $members) = (getgrent())[2, 3]) { + push @gids, $gid if $gid != $pgid && grep {$_ eq $name} split /\s+/, $members; + } + endgrent(); + $prop->{'group'} = join(" ", $pgid, @gids); + $self->log(1, "Group Not Defined. Defaulting to groups of user '$name' ($prop->{'group'})"); + } else { + $self->log(1, "Group Not Defined. Defaulting to EGID '$)'"); + $prop->{'group'} = $); + } + } + # chown any files or sockets that we need to if ($prop->{'group'} ne $) || $prop->{'user'} ne $>) { my @chown_files; diff --git a/lib/Net/Server.pod b/lib/Net/Server.pod index 960cfb4..c2cedbc 100644 --- a/lib/Net/Server.pod +++ b/lib/Net/Server.pod @@ -459,7 +459,7 @@ base class.) pid_file "filename" undef chroot "directory" undef user (uid|username) "nobody" - group (gid|group) "nobody" + group (gid|group) user's groups background 1 undef setsid 1 undef @@ -895,8 +895,12 @@ you will have to specify C equal to "root". =item group Groupid or groupname to become after the bind process has occurred. -Defaults to "nobody." If you would like the server to run as root, -you will have to specify C equal to "root". +If C is not set but C is, defaults to the primary group +of C together with the groups that list C as a member, so +that the groups of the starting process (such as root's) are not +kept. If neither is set, the current groups are kept. If you would +like the server to run as root, you will have to specify C +equal to "root". =item background -- 2.53.0