I have a large shell script that I want to make setuid. Someone suggested rewriting it in C. I would prefer to rewrite it in Perl, if possible. Generally, are setuid programs more secure in C than in Perl? Specifically, I plan to make the following steps. If anyone sees any reason why this will be less secure than a pure C solution, please let me know. Other suggestions are also welcome.
wr.c (C wrapper): #include <unistd.h> main(int argc, char* argv[]) { char* sArgv[8]; sArgv[0] = (char*)"/usr/bin/perl"; sArgv[1] = (char*)"-wT"; sArgv[2] = (char*)"/home/fred/setuid/t1.pl"; sArgv[3] = argv[1]; sArgv[4] = 0; execv("/usr/bin/perl", sArgv); }
For various reasons, I prefer not to use a shebang line in the Perl script, and will not use /usr/bin/perl but my own custom version.
Example Perl script t1.pl: use strict; sub dump_user_details { my $uid = shift; my ($user, $u, $pgid, $home, $shell) = (getpwuid($uid))[0,2,3,7,8]; print "ruid='$<' euid='$>' uid='$uid' u='$u' pgid='$pgid'\n"; print "user='$user' home='$home' shell='$shell'\n"; my @gids = ( $pgid ); while (my ($name, $pw, $gid, $members) = getgrent) { push(@gids, $gid) if grep($_ eq $user, split(' ', $members)); } endgrent(); print "gids:@gids:\n"; } my $arg1 = shift; print "arg1='$arg1'\n"; dump_user_details($<); dump_user_details($>) if $> != $<;
Then issue the following commands:
cc -o wr wr.c chown root wr t1.pl chmod 700 t1.pl chmod 4710 wr ./wr test
In reply to setuid: Perl v C by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |