title: RealUID, Saved UID, Effective UID. What's going on?
body: <p>This is a set-root-uid program</p>
  
  <pre><code>$ls -l
  -rwsr-sr-x 1 root root 7406 2011-12-13 22:37 ./x*
  </code></pre>
  
  <h1>The source code:</h1>
  
  <pre class="lang-c prettyprint-override"><code>int main(void) {
      printf(
          "         UID           GID  \n"
          "Real      %d  Real      %d  \n"
          "Effective %d  Effective %d  \n",
               getuid (),     getgid (),
               geteuid(),     getegid()
      );
  
  seteuid(600);
      printf(
          "         UID           GID  \n"
          "Real      %d  Real      %d  \n"
          "Effective %d  Effective %d  \n",
               getuid (),     getgid (),
               geteuid(),     getegid()
      );
  
  setuid(1000);
  
      printf(
          "         UID           GID  \n"
          "Real      %d  Real      %d  \n"
          "Effective %d  Effective %d  \n",
               getuid (),     getgid (),
               geteuid(),     getegid()
      );
  
  setuid(0); // HOW DOES THIS SUCCEED IN SETTING THE EUID BACK TO 0
      printf(
          "         UID           GID  \n"
          "Real      %d  Real      %d  \n"
          "Effective %d  Effective %d  \n",
               getuid (),     getgid (),
               geteuid(),     getegid()
      );
  
      return 0 ;       
  }
  </code></pre>
  
  <h1>OUTPUT</h1>
  
  <pre><code>         UID           GID  
  Real      1000  Real      1000  
  Effective 0  Effective 0  
           UID           GID  
  Real      1000  Real      1000  
  Effective 600  Effective 0  
           UID           GID  
  Real      1000  Real      1000  
  Effective 1000  Effective 1000  
           UID           GID  
  Real      1000  Real      1000  
  Effective 0  Effective 1000  
  </code></pre>
  
  <h1>My question</h1>
  
  <p>The man page states that setuid will change the real,saved and effective uid.
  So after the calling <code>setuid(1000)</code>, all three change to <code>1000</code>.
  How is that <code>setuid(0)</code> let's me change <code>euid</code> to <code>0</code>?</p>