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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.