third_party/virtualbox/src/VBox/HostDrivers/Support/SUPR3HardenedMain.cpp
/**
 * Grabs extra non-root capabilities / privileges that we might require.
 *
 * This is currently only used for being able to do ICMP from the NAT engine.
 *
 * @note We still have root privileges at the time of this call.
 */
Grabs extra non-root capabilities / privileges that we might require.
This is currently only used for being able to do ICMP from the NAT engine.

@note We still have root privileges at the time of this call.
Grabs extra non - root capabilities / privileges that we might require . This is currently only used for being able to do ICMP from the NAT engine . @note We still have root privileges at the time of this call .
static void supR3HardenedMainGrabCapabilites(void)
{
# if defined(RT_OS_LINUX)
    if (g_uCaps != 0)
    {
#  ifdef USE_LIB_PCAP
        if (!cap_set_proc(cap_from_text("all-eip cap_net_raw+ep")))
            prctl(PR_SET_KEEPCAPS, 1 , 0, 0, 0);
        prctl(PR_SET_DUMPABLE, 1 , 0, 0, 0);
#  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));
        capget(hdr, NULL);
        if (   hdr->version != 0x19980330 
            && hdr->version != 0x20071026 
            && hdr->version != 0x20080522 )
            hdr->version = _LINUX_CAPABILITY_VERSION;
        g_uCapsVersion = hdr->version;
        memset(cap, 0, 2  * sizeof(*cap));
        cap->effective = g_uCaps;
        cap->permitted = g_uCaps;
        if (!capset(hdr, cap))
            prctl(PR_SET_KEEPCAPS, 1 , 0, 0, 0);
        prctl(PR_SET_DUMPABLE, 1 , 0, 0, 0);
#  endif 
    }
# elif defined(RT_OS_SOLARIS)
    seteuid(g_uid);
    priv_set_t *pPrivEffective = priv_allocset();
    priv_set_t *pPrivNew = priv_allocset();
    if (pPrivEffective && pPrivNew)
    {
        int rc = getppriv(PRIV_EFFECTIVE, pPrivEffective);
        seteuid(0);
        if (!rc)
        {
            priv_copyset(pPrivEffective, pPrivNew);
            rc = priv_addset(pPrivNew, PRIV_NET_ICMPACCESS);
            if (!rc)
            {
                rc = setppriv(PRIV_SET, PRIV_EFFECTIVE, pPrivNew);
                if (rc)
                    supR3HardenedError(rc, false, "SUPR3HardenedMain: failed to set effective privilege set.\n");
                rc = setppriv(PRIV_SET, PRIV_PERMITTED, pPrivNew);
                if (rc)
                    supR3HardenedError(rc, false, "SUPR3HardenedMain: failed to set permitted privilege set.\n");
            }
            else
                supR3HardenedError(rc, false, "SUPR3HardenedMain: failed to add NET_ICMPACCESS privilege.\n");
        }
    }
    else
    {
        seteuid(0);
    }
    if (pPrivEffective)
        priv_freeset(pPrivEffective);
    if (pPrivNew)
        priv_freeset(pPrivNew);
# endif
}
static void supR3HardenedMainGrabCapabilites ( void ) { # if defined ( RT_OS_LINUX ) if ( g_uCaps != 0 ) { #  ifdef USE_LIB_PCAP if ( ! cap_set_proc ( cap_from_text ( " " ) ) ) prctl ( PR_SET_KEEPCAPS , 1 , 0 , 0 , 0 ) ; prctl ( PR_SET_DUMPABLE , 1 , 0 , 0 , 0 ) ; #  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 ) ) ; capget ( hdr , NULL ) ; if ( hdr -> version != 0x19980330 && hdr -> version != 0x20071026 && hdr -> version != 0x20080522 ) hdr -> version = _LINUX_CAPABILITY_VERSION ; g_uCapsVersion = hdr -> version ; memset ( cap , 0 , 2 * sizeof ( * cap ) ) ; cap -> effective = g_uCaps ; cap -> permitted = g_uCaps ; if ( ! capset ( hdr , cap ) ) prctl ( PR_SET_KEEPCAPS , 1 , 0 , 0 , 0 ) ; prctl ( PR_SET_DUMPABLE , 1 , 0 , 0 , 0 ) ; #  endif } # elif defined ( RT_OS_SOLARIS ) seteuid ( g_uid ) ; priv_set_t * pPrivEffective = priv_allocset ( ) ; priv_set_t * pPrivNew = priv_allocset ( ) ; if ( pPrivEffective && pPrivNew ) { int rc = getppriv ( PRIV_EFFECTIVE , pPrivEffective ) ; seteuid ( 0 ) ; if ( ! rc ) { priv_copyset ( pPrivEffective , pPrivNew ) ; rc = priv_addset ( pPrivNew , PRIV_NET_ICMPACCESS ) ; if ( ! rc ) { rc = setppriv ( PRIV_SET , PRIV_EFFECTIVE , pPrivNew ) ; if ( rc ) supR3HardenedError ( rc , false , " \n " ) ; rc = setppriv ( PRIV_SET , PRIV_PERMITTED , pPrivNew ) ; if ( rc ) supR3HardenedError ( rc , false , " \n " ) ; } else supR3HardenedError ( rc , false , " \n " ) ; } } else { seteuid ( 0 ) ; } if ( pPrivEffective ) priv_freeset ( pPrivEffective ) ; if ( pPrivNew ) priv_freeset ( pPrivNew ) ; # endif }
Grabs extra non-root capabilities / privileges that we might require.
Grabs extra non - root capabilities / privileges that we might require .
/*
     * We are about to drop all our privileges. Remove all capabilities but
     * keep the cap_net_raw capability for ICMP sockets for the NAT stack.
     */ /* XXX cap_net_bind_service */ /*keep=*/ /*dump*/ /*_LINUX_CAPABILITY_U32S_3*/ /* _LINUX_CAPABILITY_VERSION_1, _LINUX_CAPABILITY_U32S_1 = 1 */ /* _LINUX_CAPABILITY_VERSION_2, _LINUX_CAPABILITY_U32S_2 = 2 */ /* _LINUX_CAPABILITY_VERSION_3, _LINUX_CAPABILITY_U32S_3 = 2 */ /* _LINUX_CAPABILITY_U32S_3 */ /*keep*/ /*dump*/ /* !USE_LIB_PCAP */ /*
     * Add net_icmpaccess privilege to effective privileges and limit
     * permitted privileges before completely dropping root privileges.
     * This requires dropping root privileges temporarily to get the normal
     * user's privileges.
     */ /* Order is important, as one can't set a privilege which is
                 * not in the permitted privilege set. */ /* for memory allocation failures just continue */