Read on perlvar, variables $> and $<. You can drop privileges temporarily assigning to $> (effective uid of the process), but if you assign to $< (real uid of the process) and $> a uid higher than 0, you don't get back to 0.
Setting $< or $> has no effect if neither real nor effective uid are 0.

#!/usr/bin/perl print "effective: $>, real: $<\n"; $> = 111; open(O,">foo") or die "Can't write foo: $!\n"; close O or die "Can't close O: $!\n"; $> = 0; open(O,">bar") or die "Can't write bar: $!\n"; close O or die "Can't close O: $!\n"; $> = 111 ; # this must fail. open(O,">bar") or warn "Can't write bar: $!\n"; # change real uid $< = 111; # oops, forgot to set $> to 0 print "effective: $>, real: $<\n"; $< = 0; # no effect print "effective: $>, real: $<\n";

This outputs:

effective: 0, real: 0 Can't write bar: Permission denied effective: 111, real: 111 effective: 111, real: 111

As you see, the second change of the real uid had no effect. Let's see what's in here:

quux [gm] /tmp/foo # ls -l total 4 -rw-r--r-- 1 root root 0 2006-06-26 02:31 bar -rw-r--r-- 1 111 root 0 2006-06-26 02:31 foo -rw-r--r-- 1 root root 338 2006-06-26 02:29 setuid.pl

Where do you get the function setuid from? can't find that in my perlfunc...

update: ah, POSIX.

setuid Sets the real user identifier and the effective user identi +- fier for this process. Similar to assigning a value to the Perl's builtin $< variable, see "$UID" in perlvar, except that the latter will change only the real user identifier.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: An odd failure of setuid(0) by shmem
in thread An odd failure of setuid(0) by Llew_Llaw_Gyffes

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.