Skrevet av Emne: setuid works but setgid fails with EPERM  (Lest 4247 ganger)

ATC

  • Gjest
setuid works but setgid fails with EPERM
« på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • The following code always fails saying
    "Error 1 setting group ID to nn" (where nn is the GID)

      if ( setuid((*user).pw_uid) == -1) {
        printf("Error %d setting user ID to %d\n", errno, (*user).pw_uid);
        return 1;
      }
      if ( setgid((*user).pw_gid) == -1) {
        printf("Error %d setting group ID to %d\n", errno, (*user).pw_gid);
        return 1;
      }  



    ATC

    • Gjest
    [Solved] setuid works but setgid fails with EPERM
    « Svar #1 på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • After the process has changed user id, it is no longer permitted to change group id. Simply swap the two, like this:

      if ( setgid((*user).pw_gid) == -1) {
        printf("Error %d setting group ID to %d\n", errno, (*user).pw_gid);
        return 1;
      }  
      if ( setuid((*user).pw_uid) == -1) {
        printf("Error %d setting user ID to %d\n", errno, (*user).pw_uid);
        return 1;
      }