Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative: use unveil rather than chroot+setuid #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 8 additions & 36 deletions minimalist.pl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Carp;
use Net::Config qw(%NetConfig);
use Net::SMTP;
use OpenBSD::Unveil;
require File::Spec;
require Digest::MD5;
require MIME::QuotedPrint;
Expand Down Expand Up @@ -197,45 +198,16 @@
$maketext = Translation::en->new() || die "Language?";


################################################################
# >>>>>>>>>>>>>>> CHROOT AND DROP PRIVILEGES <<<<<<<<<<<<<<<<< #
################################################################
#####################################################
# >>>>>>>>>>>>>>> DROP PRIVILEGES <<<<<<<<<<<<<<<<< #
#####################################################

-d $conf{directory} or die "$conf{directory} is not a directory.";

# $< and $( are real [ug]id, $> and $) are effective [ug]id
if ($> == 0 && $< != 0) {
my $uid;
my $gid = getgrnam('nogroup');
defined $gid or $gid = getgrnam('nobody');
defined $gid or die "Could not get gid of nogroup.";
if (defined $conf{user}) {
$uid = getpwnam($conf{user});
defined $uid or die "Could not get uid of $conf{user}.";
}
else { $uid = $< }

# drop group privileges
$) = "$gid $gid";
die "Error while dropping group privileges: $!" unless($) eq "$gid $gid");
POSIX::setgid($gid) or die "setgid failed: $!";;

# chroot
chroot $conf{directory} or die "Could not chroot to $conf{directory}: $!";
chdir '/' or die "Cannot chdir to / in chroot ($conf{directory}).";
$conf{directory} = '/';

# drop user privileges
POSIX::setuid($uid) or die "setuid failed: $!";

# This may be too paranoid, but check
# that privileges are really dropped permanently
POSIX::setuid(0);
POSIX::setgid(0);
my $groups = qr/^$gid( $gid)?$/;
if ($< == 0 || $> == 0 || $( !~ $groups || $) !~ $groups) {
die "Could not drop privileges permanently." }
}
unveil( $conf{directory}, "crw" ) or die "Unable to unveil: $!";
unveil() || die "Unable to lock unveil: $!";
chdir "$conf{directory}" or die "Cannot chdir to ($conf{directory}).";

@INC = ('.');

####################################################################
Expand Down