Accessing Commands and Files Under Reduced Privileges
The following discussion pertains to some nifty security facilities of Unix-like systems. Users of other systems may safely (or rather, unsafely) skip this section.
If you’re running set-id, whenever possible, try to arrange
that you do dangerous operations with the privileges of the user,
not the privileges of the program. That is, whenever you’re going to
call open, sysopen, system, backticks, and any other file or process operations, you can protect
yourself by setting your effective UID or GID back to the real UID
or GID. In Perl, you can do this for setuid scripts by saying $> = $< (or $EUID = $UID if you use English) and for setgid scripts by saying $) =
$( ($EGID = $GID). If
both IDs are set, you should reset both. However, sometimes this
isn’t feasible because you might still need those increased
privileges later in your program.
For those cases, Perl provides a reasonably safe way to open a
file or pipe from within a set-id program. First, fork a child using
the special open syntax that
connects the parent and child by a pipe. In the child, reset the
user and group IDs back to their original or known safe values. You
also get to modify any of the child’s per-process attributes without
affecting the parent, letting you change the working directory, set
the file creation mask, or fiddle with environment variables. No
longer executing under extra privileges, the child process at last
calls open and passes whatever data it ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access