bool DropPrivs()
{
- /* uid will be 0 in the end, but gid might be different anyway */
+ // uid will be 0 in the end, but gid might be different anyway
uid_t old_uid = getuid();
gid_t old_gid = getgid();
if(ret < 0)
_error->Warning("PR_SET_NO_NEW_PRIVS failed with %i", ret);
#endif
-
+ // Do not change the order here, it might break things
if (setgroups(1, &pw->pw_gid))
return _error->Errno("setgroups", "Failed to setgroups");
if (setuid(pw->pw_uid) != 0)
return _error->Errno("setuid", "Failed to setuid");
+
// the seteuid() is probably uneeded (at least thats what the linux
// man-page says about setuid(2)) but we cargo culted it anyway
-
-
if (seteuid(pw->pw_uid) != 0)
return _error->Errno("seteuid", "Failed to seteuid");
- /* Try changing GID/EGID */
+ // Check that uid and gid changes do not work anymore
if (pw->pw_gid != old_gid && (setgid(old_gid) != -1 || setegid(old_gid) != -1))
return _error->Error("Could restore a gid to root, privilege dropping did not work");
if (pw->pw_uid != old_uid && (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
return _error->Error("Could restore a uid to root, privilege dropping did not work");
- /* Verify the list of supplementary groups is what we expected */
+ // Verify that the user has only a single group, and the correct one
gid_t groups[1];
if (getgroups(1, groups) != 1)
return _error->Errno("getgroups", "Could not get new groups");
if (groups[0] != pw->pw_gid)
return _error->Error("Could not switch group");
- /* Verify gid, egid, uid, and euid */
+
+ // Verify that gid, egid, uid, and euid changed
if (getgid() != pw->pw_gid)
return _error->Error("Could not switch group");
if (getegid() != pw->pw_gid)
return _error->Error("Could not switch saved set-group-ID");
#endif
- /* TODO: Check saved uid/saved gid as well */
return true;
}