1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
# Paranoid -- Paranoia support for safer programs
#
# $Id: lib/Paranoid.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
#
# (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
# (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
# This software is free software. Similar to Perl, you can redistribute it
# and/or modify it under the terms of either:
#
# a) the GNU General Public License
# <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
# Free Software Foundation <http://www.fsf.org/>; either version 1
# <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
# <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
# b) the Artistic License 2.0
# <https://opensource.org/licenses/Artistic-2.0>,
#
# subject to the following additional term: No trademark rights to
# "Paranoid" have been or are conveyed under any of the above licenses.
# However, "Paranoid" may be used fairly to describe this unmodified
# software, in good faith, but not as a trademark.
#
# (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
# (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid;
use 5.008;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
@EXPORT = qw(psecureEnv);
@EXPORT_OK = ( @EXPORT, qw(PTRUE_ZERO) );
%EXPORT_TAGS = ( all => [@EXPORT_OK], );
use constant PTRUE_ZERO => '0 but true';
use constant DEFAULT_PATH => '/bin:/sbin:/usr/bin:/usr/sbin';
#####################################################################
#
# Module code follows
#
#####################################################################
#BEGIN {
#die "This module requires taint mode to be enabled!\n" unless
# ${^TAINT} == 1;
#delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
#$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
#no ops qw(backtick system exec);
# :subprocess = system, backtick, exec, fork, glob
# :dangerous = syscall, dump, chroot
# :others = mostly IPC stuff
# :filesys_write = link, unlink, rename, mkdir, rmdir, chmod,
# chown, fcntl
# :sys_db = getpwnet, etc.
#}
sub psecureEnv (;$) {
# Purpose: To delete taint-unsafe environment variables and to sanitize
# the PATH variable
# Returns: True (1) -- no matter what
# Usage: psecureEnv();
my $path = shift;
$path = DEFAULT_PATH unless defined $path;
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{PATH} = $path;
if ( exists $ENV{TERM} ) {
if ( $ENV{TERM} =~ /^([\w\+\.\-]+)$/s ) {
$ENV{TERM} = $1;
} else {
$ENV{TERM} = 'vt100';
}
}
return 1;
}
{
my $errorMsg = '';
sub ERROR : lvalue {
# Purpose: To store/retrieve a string error message
# Returns: Scalar string
# Usage: $errMsg = Paranoid::ERROR;
# Usage: Paranoid::ERROR = $errMsg;
$errorMsg;
}
}
1;
__END__
=head1 NAME
Paranoid - Paranoia support for safer programs
=head1 VERSION
$Id: lib/Paranoid.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
=head1 SYNOPSIS
use Paranoid;
$errMsg = Paranoid::ERROR;
psecureEnv("/bin:/usr/bin");
sub foo {
# this function can return '0' as a valid return value
# that should be construed as a boolean true value
return PTRUE_ZERO;
}
=head1 DESCRIPTION
This collection of modules started out as modules which perform things
(debatably) in a safer and taint-safe manner. Since then it's also grown to
include functionality that fit into the same framework and conventions of the
original modules, including keeping the debug hooks for command-line
debugging.
All the modules below are intended to be used directly in your programs if you
need the functionality they provide.
This module does provide one function meant to secure your environment
enough to satisfy taint-enabled programs, and as a container which holds the
last reported error from any code in the Paranoid framework.
=head1 IMPORT LISTS
This module exports the following symbols by default:
psecureEnv
The following specialized import lists also exist:
List Members
--------------------------------------------------------
all @defaults PTRUE_ZERO
=head1 SUBROUTINES/METHODS
=head2 psecureEnv
psecureEnv("/bin:/usr/bin");
This function deletes some of the dangerous environment variables that can be
used to subvert perl when being run in setuid applications. It also sets the
path, either to the passed argument (if passed) or a default of
"/bin:/sbin:/usr/bin:/usr/sbin".
B<NOTE:> I did explicitly exclude I</usr/local> directories from the default
path for the following reason: I</usr/local> is often used by admins to stash
random scripts and programs which may not have undergone any serious scrutiny
and review for security issues. Only the base OS directories are presumed as
safe, and even that may be stretching the truth as it is. You can override
the defaults as desired.
=head2 Paranoid::ERROR
$errMsg = Paranoid::ERROR;
Paranoid::ERROR = $errMsg;
This lvalue function is not exported and must be referenced via the
B<Paranoid> namespace.
=head2 PTRUE_ZERO
This is a constant that evaluates to '0 but true', which allows I<0> to be
passed for boolean true use-cases.
=head1 TAINT NOTES
Taint-mode programming can be somewhat of an adventure until you know all the
places considered dangerous under perl's taint mode. The following functions
should generally have their arguments detainted before using:
exec system open glob
unlink mkdir chdir rmdir
chown chmod umask utime
link symlink kill eval
truncate ioctl fcntl chroot
setpgrp setpriority syscall socket
socketpair bind connect
=head1 DEPENDENCIES
While this module itself doesn't have any external dependencies various child
modules do. Please check their documentation for any particulars should you
use them.
=head1 SEE ALSO
The following modules are available for use. You should check their POD for
specifics on use:
=over
=item o
L<Paranoid::Args>: Command-line argument parsing functions
=item o
L<Paranoid::Data>: Misc. data manipulation functions
=item o
L<Paranoid::Data::AVLTree>: AVL-Balanced Tree Class
=item o
L<Paranoid::Data::AVLTree::AVLNode>: AVL-Balanced Tree Node Class
=item o
L<Paranoid::Debug>: Command-line debugging framework and functions
=item o
L<Paranoid::Filesystem>: Filesystem operation functions
=item o
L<Paranoid::Glob>: Paranoid Glob objects
=item o
L<Paranoid::IO>: File I/O wrappers for sysopen, etc.
=item o
L<Paranoid::IO::FileMultiplexer>: File Multiplexer Object
=item o
L<Paranoid::IO::FileMultiplexer::Block>: Block-level Allocator/Accessor
=item o
L<Paranoid::IO::FileMultiplexer::Block::BATHeader>: BAT Header Block
=item o
L<Paranoid::IO::FileMultiplexer::Block::FileHeader>: File Header Block
=item o
L<Paranoid::IO::FileMultiplexer::Block::StreamHeader>: Stream Header Block
=item o
L<Paranoid::IO::Line>: I/O functions for working with line-based files
=item o
L<Paranoid::IO::Lockfile>: I/O functions for working with lock files
=item o
L<Paranoid::Input>: Input-related functions (file reading, detainting)
=item o
L<Paranoid::Log>: Unified logging framework and functions
=item o
L<Paranoid::Log::Buffer>: Buffered-based logging mechanism
=item o
L<Paranoid::Log::File>: File-based logging mechanism
=item o
L<Paranoid::Module>: Run-time module loading functions
=item o
L<Paranoid::Network>: Network-related functions
=item o
L<Paranoid::Network::IPv4>: General IPv4-related functions
=item o
L<Paranoid::Network::IPv6>: General IPv6-related functions
=item o
L<Paranoid::Network::Socket>: Wrapper module for Socket & Socket6
=item o
L<Paranoid::Process>: Process management functions
=back
=head1 BUGS AND LIMITATIONS
=head1 AUTHOR
Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is free software. Similar to Perl, you can redistribute it
and/or modify it under the terms of either:
a) the GNU General Public License
<https://www.gnu.org/licenses/gpl-1.0.html> as published by the
Free Software Foundation <http://www.fsf.org/>; either version 1
<https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
<https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
b) the Artistic License 2.0
<https://opensource.org/licenses/Artistic-2.0>,
subject to the following additional term: No trademark rights to
"Paranoid" have been or are conveyed under any of the above licenses.
However, "Paranoid" may be used fairly to describe this unmodified
software, in good faith, but not as a trademark.
(c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
(tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
|