Check out perlsec
, which explains it all. Basically, since you'll be running this as root (with information that is supplied by the user), you need to be certain $USER doesn't contain any evil or harmful characters. If you let the user specify a username of, like, "../../bin", you'd be creating directories and things in very bad places. A simple sanity check should suffice:
($USER) = $cgi->param('user') =~ /(\w+)/;
This would only permit normal alphanumeric characters into $USER, and un-taint it in the process. With taint-checking enabled (-T
), Perl will die before letting you use arbitrary user-supplied (or potentially unsafe) information in any critical system calls (like chdir
Other posts below advocate using a separate script to perform the actual updates as root, and I agree with them 100%. It's infinitely more secure if you keep the user from interacting directly with a setuid script at all. A buffer (in the form of semaphore files or a socket connection) is a better solution to your problem.