Bug 92

Summary: wishlist: make real UID visible in a secure way
Product: Sudo Reporter: era+sudo
Component: SudoAssignee: Todd C. Miller <Todd.Miller>
Status: RESOLVED WORKSFORME    
Severity: enhancement    
Priority: normal    
Version: 1.6.6   
Hardware: PC   
OS: Linux   

Description era+sudo 2002-07-29 04:44:08 MDT
For writing utilities that take advantage of the sudo functionality, it would be
great if sudo itself would make some information visible e.g. through
environment variables.

As it is now, there is no way to be sure that what is in the environment was put
there in a controlled fashion. If sudo would make sure to zap preexisting values
of certain variables, and fill them in with certifiably correct information, it
would be rather easy to write e.g. an "editor plug-in" to replace and enhance
visudo. (See bug #91.)

However, presently this is very hard to do securely using sudo -- you'd have to
essentially duplicate the privilege and authentication mechanisms of sudo to
provide something similar.

This is sketchy at this point, but I'd love to be able to make a script like the
following (off the top of my head -- not tested at all):

  #!/usr/bin/perl
  # something -- do something which requires root privs

  # do something which requires root privs
  
  chown ($ENV{"REALUID"}, $ENV{"REALGID"}, "somefile");

  # now drop privileges
  $< = $ENV{"REALUID"};
  $( = $ENV{"REALGID"};

  # now do something less secure
  system ($ENV{"EDITOR"} || $ENV{"VISUAL"}
     || "c:\\winnt\\system\\notepad.exe"  # just kidding :-)
      , "somefile") || die "Couldn't, sorry";

  # regain privileges
  $< = $>;
  $( = $);

  # restore ownership
  chown ($<, $>, "somefile");

which could then be invoked as

  sudo dosomething

and give a sane environment where REALUID, REALGID, and probably also VISUAL and
EDITOR are set up to contain sanitized values (or nothing). In case it's not
obvious, the REALUID and REALGID variables should contain the UID and GID of the
user who invoked sudo in the first place.
Comment 1 Todd C. Miller 2002-12-13 10:09:26 MST
Sudo currently sets the following environment variables:
    SUDO_COMMAND - the command run by sudo
    SUDO_USER    - the login name of the invoking user
    SUDO_UID     - the SUDO_USER's uid (from the password database)
    SUDO_GID     - the SUDO_USER's gid (from the password database)

Isn't that sufficient?
Comment 2 Todd C. Miller 2004-01-21 15:58:42 MST
Sudo already sets environment variables that have this info.