I have a tool which downloads photos from a digital camera which appears as a removable USB device.  It includes the following code:

my $u = $<; setuid(0) || die "Cannot change UID from $u to 0"; system(split(' ','/sbin/modprobe usb-storage')); setuid($u) || die "Cannot drop privileges"; system(split(' ','/sbin/mount /mnt/usb/flash')); [do some file manipulation in here] chdir ('/'); system(split(' ','/sbin/umount /mnt/usb/flash')); sleep (2); # this is pure raging paranoia setuid(0) || die "Cannot change UID from $u to 0"; system(split(' ','/sbin/modprobe -r usb-storage')); setuid($u) || die "Cannot drop privileges";

The tool is mode 6775 root:console.  Invariably, the first setuid(0) succeeds, as does the first setuid($u).  Also invariably, the second setuid(0) fails, as in the following example:

babylon5:alaric:~:17 $ getpics Will copy images from camera canon /mnt/usb/flash/dcim is mounted /mnt/usb/flash/dcim/104canon/img_0458.jpg -> /minbar/camera/canon/img_ +0458.jpg /mnt/usb/flash/dcim/104canon/img_0459.jpg -> /minbar/camera/canon/img_ +0459.jpg /mnt/usb/flash/dcim/104canon/img_0460.jpg -> /minbar/camera/canon/img_ +0460.jpg Cannot become root from UID 1000! at /usr/local/bin/getpics line 133. babylon5:alaric:~:18 $

Can anyone offer any explanation of why this might happen?


In reply to 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.