Ano-Tech Computers
Enter keyword:

setuid works but setgid fails with EPERM
Problem:
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; }
 
Solution:
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;
}

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles