Nocturnus has asked for the wisdom of the Perl Monks concerning the following question:
Dear seekers of wisdom,
I have found the following piece of code here:
0 use English; 1 my @temp = ($EUID, $EGID); 2 my $orig_uid = $UID; 3 my $orig_gid = $GID; 4 $EUID = $UID; 5 $EGID = $GID; 6 # Drop privileges 7 $UID = $orig_uid; 8 $GID = $orig_gid; 9 # Make sure privs are really gone 10 ($EUID, $EGID) = @temp; 11 die "Can't drop privileges" 12 unless $UID == $EUID && $GID eq $EGID;
I have slightly shortened it and have added line numbers.
Although I have tried very hard for several hours, I am not able to understand how it works. I believe that I understand it from line 9 on: Obviously, in line 10, we try to gain back EUID and EGID. This should not be possible if we really have dropped privileges. It it was possible, EUID now is different from UID or EGID now is different from GID. That means that if the conditions in line 12 are true the assignment in line 10 has failed, which is what we expect since we have already dropped privileges.
My main problem are lines 2 and 3 in conjunction with lines 7 and 8. Focusing on the user id only, we here have basically the sequence 2. $orig_UID = $UID followed by 7. $UID = $orig_UID. That does not make any sense IMHO, unless $UID is altered somewhere in between. The only place where this could happen is probably line 4. However, I can't see how the statement $EUID = $UID could alter $UID.
My tests seem to confirm that point of view. I have made a test environment in Linux (Debian bullseye) where I can run the script setsuid-root or setsuid-other_user, and have inserted print statements after every assignment. Regardless of what my real user id was, and regardless of the setsuid of the script, I never encountered a situation where $UID and $orig_UID were different immediately before executing line 7.
Please note that I am aware that it is normally not sufficient to set the setuid flag on script files to actually make the script run as the respective user; this is because the interpreter usually does not care about that flag. However, I am using suid-wrapper to run the script setsuid-root or setsuid-other_user.
So I guess that I'd like to know what the "# Drop privileges" part (lines 7 and 8) actually effect and how they work.
Thank you very in advance, and best regards ...
|
---|