VirtualBox

Changeset 92676 in vbox


Ignore:
Timestamp:
Dec 1, 2021 2:59:59 PM (3 years ago)
Author:
vboxsync
Message:

iprt: Local Ipc Server: add Linux implementation for getting remote peer credentials, bugref:10134.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/localipc-posix.cpp

    r82968 r92676  
    10481048}
    10491049
     1050/**
     1051 * Get IPC session socket peer credentials.
     1052 *
     1053 * @return IPRT status code.
     1054 * @param hSession      IPC session handle.
     1055 * @param pProcess      Output buffer to store remote peer PID (can be NULL).
     1056 * @param pUid          Output buffer to store remote peer UID (can be NULL).
     1057 * @param pGid          Output buffer to store remote peer GID (can be NULL).
     1058 */
     1059static int rtLocalIpcSessionQueryUcred(RTLOCALIPCSESSION hSession, PRTPROCESS pProcess, PRTUID pUid, PRTGID pGid)
     1060{
     1061#if defined(RT_OS_LINUX)
     1062    PRTLOCALIPCSESSIONINT pThis = hSession;
     1063
     1064    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     1065    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
     1066
     1067    struct ucred PeerCred;
     1068    socklen_t cbPeerCred = sizeof(PeerCred);
     1069    int iSock = RTSocketToNative(pThis->hSocket);
     1070
     1071    RT_ZERO(PeerCred);
     1072
     1073    if (getsockopt(iSock, SOL_SOCKET, SO_PEERCRED, &PeerCred, &cbPeerCred) == -1)
     1074        return RTErrConvertFromErrno(errno);
     1075
     1076    if (pProcess)
     1077        *pProcess = PeerCred.pid;
     1078    if (pUid)
     1079        *pUid = PeerCred.uid;
     1080    if (pGid)
     1081        *pGid = PeerCred.gid;
     1082
     1083    return VINF_SUCCESS;
     1084#else
     1085    return VERR_NOT_SUPPORTED;
     1086#endif
     1087}
    10501088
    10511089RTDECL(int) RTLocalIpcSessionQueryProcess(RTLOCALIPCSESSION hSession, PRTPROCESS pProcess)
    10521090{
    1053     RT_NOREF_PV(hSession); RT_NOREF_PV(pProcess);
    1054     return VERR_NOT_SUPPORTED;
     1091    return rtLocalIpcSessionQueryUcred(hSession, pProcess, NULL, NULL);
    10551092}
    10561093
     
    10581095RTDECL(int) RTLocalIpcSessionQueryUserId(RTLOCALIPCSESSION hSession, PRTUID pUid)
    10591096{
    1060     RT_NOREF_PV(hSession); RT_NOREF_PV(pUid);
    1061     return VERR_NOT_SUPPORTED;
    1062 }
    1063 
     1097    return rtLocalIpcSessionQueryUcred(hSession, NULL, pUid, NULL);
     1098}
    10641099
    10651100RTDECL(int) RTLocalIpcSessionQueryGroupId(RTLOCALIPCSESSION hSession, PRTGID pGid)
    10661101{
    1067     RT_NOREF_PV(hSession); RT_NOREF_PV(pGid);
    1068     return VERR_NOT_SUPPORTED;
    1069 }
    1070 
     1102    return rtLocalIpcSessionQueryUcred(hSession, NULL, NULL, pGid);
     1103}
     1104
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette