third_party/virtualbox/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp
/**
 * Drop any root privileges we might be holding.
 */
Drop any root privileges we might be holding.
Drop any root privileges we might be holding .
static void supR3HardenedMainDropPrivileges(void)
{
    uid_t euid, ruid, suid;
    gid_t egid, rgid, sgid;
# if defined(RT_OS_DARWIN)
    setgid(g_gid);
    setuid(g_uid);
    euid = geteuid();
    ruid = suid = getuid();
    egid = getegid();
    rgid = sgid = getgid();
# elif defined(RT_OS_SOLARIS)
    setregid(g_gid, g_gid);
    setreuid(g_uid, g_uid);
    euid = geteuid();
    ruid = suid = getuid();
    egid = getegid();
    rgid = sgid = getgid();
# else
    int res = setresgid(g_gid, g_gid, g_gid);
    NOREF(res);
    res = setresuid(g_uid, g_uid, g_uid);
    NOREF(res);
    if (getresuid(&ruid, &euid, &suid) != 0)
    {
        euid = geteuid();
        ruid = suid = getuid();
    }
    if (getresgid(&rgid, &egid, &sgid) != 0)
    {
        egid = getegid();
        rgid = sgid = getgid();
    }
# endif
    if (    euid != g_uid
        ||  ruid != g_uid
        ||  suid != g_uid
        ||  egid != g_gid
        ||  rgid != g_gid
        ||  sgid != g_gid)
        supR3HardenedFatal("SUPR3HardenedMain: failed to drop root privileges!"
                           " (euid=%d ruid=%d suid=%d  egid=%d rgid=%d sgid=%d; wanted uid=%d and gid=%d)\n",
                           euid, ruid, suid, egid, rgid, sgid, g_uid, g_gid);
# if RT_OS_LINUX
    if (g_uCaps != 0)
    {
#  ifdef USE_LIB_PCAP
        cap_set_proc(cap_from_text("cap_net_raw+ep"));
#  else
        cap_user_header_t hdr = (cap_user_header_t)alloca(sizeof(*hdr));
        cap_user_data_t   cap = (cap_user_data_t)alloca(2  * sizeof(*cap));
        memset(hdr, 0, sizeof(*hdr));
        hdr->version = g_uCapsVersion;
        memset(cap, 0, 2  * sizeof(*cap));
        cap->effective = g_uCaps;
        cap->permitted = g_uCaps;
        capset(hdr, cap);
#  endif 
    }
# endif
}
static void supR3HardenedMainDropPrivileges ( void ) { uid_t euid , ruid , suid ; gid_t egid , rgid , sgid ; # if defined ( RT_OS_DARWIN ) setgid ( g_gid ) ; setuid ( g_uid ) ; euid = geteuid ( ) ; ruid = suid = getuid ( ) ; egid = getegid ( ) ; rgid = sgid = getgid ( ) ; # elif defined ( RT_OS_SOLARIS ) setregid ( g_gid , g_gid ) ; setreuid ( g_uid , g_uid ) ; euid = geteuid ( ) ; ruid = suid = getuid ( ) ; egid = getegid ( ) ; rgid = sgid = getgid ( ) ; # else int res = setresgid ( g_gid , g_gid , g_gid ) ; NOREF ( res ) ; res = setresuid ( g_uid , g_uid , g_uid ) ; NOREF ( res ) ; if ( getresuid ( & ruid , & euid , & suid ) != 0 ) { euid = geteuid ( ) ; ruid = suid = getuid ( ) ; } if ( getresgid ( & rgid , & egid , & sgid ) != 0 ) { egid = getegid ( ) ; rgid = sgid = getgid ( ) ; } # endif if ( euid != g_uid || ruid != g_uid || suid != g_uid || egid != g_gid || rgid != g_gid || sgid != g_gid ) supR3HardenedFatal ( " " " \n " , euid , ruid , suid , egid , rgid , sgid , g_uid , g_gid ) ; # if RT_OS_LINUX if ( g_uCaps != 0 ) { #  ifdef USE_LIB_PCAP cap_set_proc ( cap_from_text ( " " ) ) ; #  else cap_user_header_t hdr = ( cap_user_header_t ) alloca ( sizeof ( * hdr ) ) ; cap_user_data_t cap = ( cap_user_data_t ) alloca ( 2 * sizeof ( * cap ) ) ; memset ( hdr , 0 , sizeof ( * hdr ) ) ; hdr -> version = g_uCapsVersion ; memset ( cap , 0 , 2 * sizeof ( * cap ) ) ; cap -> effective = g_uCaps ; cap -> permitted = g_uCaps ; capset ( hdr , cap ) ; #  endif } # endif }
Drop any root privileges we might be holding.
Drop any root privileges we might be holding .
/*
     * Try use setre[ug]id since this will clear the save uid/gid and thus
     * leave fewer traces behind that libs like GTK+ may pick up.
     */ /* The really great thing here is that setreuid isn't available on
       OS X 10.4, libc emulates it. While 10.4 have a slightly different and
       non-standard setuid implementation compared to 10.5, the following
       works the same way with both version since we're super user (10.5 req).
       The following will set all three variants of the group and user IDs. */ /* Solaris doesn't have setresuid, but the setreuid interface is BSD
       compatible and will set the saved uid to euid when we pass it a ruid
       that isn't -1 (which we do). */ /* This is the preferred one, full control no questions about semantics.
       PORTME: If this isn't work, try join one of two other gangs above. */ /* Check that it worked out all right. */ /*
     * Re-enable the cap_net_raw capability which was disabled during setresuid.
     */ /** @todo Warn if that does not work? */ /* XXX cap_net_bind_service */ /* _LINUX_CAPABILITY_U32S_3 */ /* _LINUX_CAPABILITY_U32S_3 */ /** @todo Warn if that does not work? */ /* !USE_LIB_PCAP */