Manipulating process credentials on Unix systems is difficult. I highly recommend getting a complete picture of how identifiers of real, effective, and stored sets are interconnected. It is very easy to drown out the "discarding privileges."
As for your specific observations ... I wonder if there is a simple reason that you may have missed. Your code first creates conflicting tests, and you did not specify the exact permissions for the files in /etc/sudoers and /etc/group- . Perhaps you could behave as described if /etc/sudoers has permission mode = 440, uid = root, gid = root (which are the default permissions for my system) and if /etc/group- has mode = 400.
You do not change the process GID, so if /etc/sudoers is readable in a group, this explains why it is always readable. fork() does not change the credentials of the process. However, this may look like the one shown in your sample code, since you are checking different files in the parent and child. If /etc/group- does not have permission to access the group where /etc/sudoers , this explains the obvious problem.
If all you are trying to do is drop privileges, use the following code:
os.setgid( NEW_GID ) os.setuid( NEW_UID )
Generally speaking, you only want to use an effective user ID if your process should turn it on and off throughout the process. If you just need to perform some configuration operations with root privileges, but will no longer require them after completing these installation operations, simply use the code above to delete them irrevocably.
Oh, and a useful debugging utility for manipulating process credentials on Linux is to print the output of /proc/self/status , the Uid and Gid lines of this file display the real, effective, saved, and file identifiers stored in the current process (in that order). Python APIs can be used to extract the same information, but you can view the contents of this file as “truth data” and avoid any possible complications from the cross-platform Python APIs.