From 55dcce66c64adb82093108f105dac091c8376fed Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Thu, 18 Apr 2013 11:07:44 +0200 Subject: [PATCH] struct uucred Change-Id: Ia97cb6c38bb566be30d568a252ae7b76142a21dd --- include/sys/ioc_net.h | 2 +- lib/libc/net/minix/getpeereid.c | 8 +++---- lib/libc/sys-minix/getsockopt.c | 4 ++-- lib/libminlib/getnucred.c | 40 ++++++++++++++++++--------------- man/man2/getnucred.2 | 4 ++-- servers/pfs/dev_uds.c | 6 ++--- servers/pfs/inc.h | 1 + servers/pfs/uds.c | 10 ++++----- servers/pfs/uds.h | 2 +- sys/sys/ucred.h | 12 +++------- test/test56.c | 38 +++++++++++++++---------------- 11 files changed, 63 insertions(+), 64 deletions(-) diff --git a/include/sys/ioc_net.h b/include/sys/ioc_net.h index 12f4188a2..4fe62c38e 100644 --- a/include/sys/ioc_net.h +++ b/include/sys/ioc_net.h @@ -86,7 +86,7 @@ struct msg_control /* setsockopt/setsockopt for unix domain sockets */ #define NWIOGUDSSOTYPE _IOR('n', 90, int) /* SO_TYPE */ -#define NWIOGUDSPEERCRED _IOR('n', 91, struct ucred) /* SO_PEERCRED */ +#define NWIOGUDSPEERCRED _IOR('n', 91, struct uucred) /* SO_PEERCRED */ #define NWIOGUDSSNDBUF _IOR('n', 92, size_t) /* SO_SNDBUF */ #define NWIOSUDSSNDBUF _IOW('n', 93, size_t) /* SO_SNDBUF */ #define NWIOGUDSRCVBUF _IOR('n', 94, size_t) /* SO_RCVBUF */ diff --git a/lib/libc/net/minix/getpeereid.c b/lib/libc/net/minix/getpeereid.c index 6da016b26..775256f8f 100644 --- a/lib/libc/net/minix/getpeereid.c +++ b/lib/libc/net/minix/getpeereid.c @@ -9,11 +9,11 @@ */ int getpeereid(int sd, uid_t *euid, gid_t *egid) { int rc; - struct ucred cred; + struct uucred cred; socklen_t ucred_length; /* Initialize Data Structures */ - ucred_length = sizeof(struct ucred); + ucred_length = sizeof(struct uucred); memset(&cred, '\0', ucred_length); /* Validate Input Parameters */ @@ -26,8 +26,8 @@ int getpeereid(int sd, uid_t *euid, gid_t *egid) { rc = getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_length); if (rc == 0) { /* Success - return the results */ - *euid = cred.uid; - *egid = cred.gid; + *euid = cred.cr_uid; + *egid = cred.cr_gid; return 0; } else { /* Failure - getsockopt takes care of setting errno */ diff --git a/lib/libc/sys-minix/getsockopt.c b/lib/libc/sys-minix/getsockopt.c index c37b5dc36..16408bf49 100644 --- a/lib/libc/sys-minix/getsockopt.c +++ b/lib/libc/sys-minix/getsockopt.c @@ -217,14 +217,14 @@ static int _uds_getsockopt(int sock, int level, int option_name, if (level == SOL_SOCKET && option_name == SO_PEERCRED) { - struct ucred cred; + struct uucred cred; r= ioctl(sock, NWIOGUDSPEERCRED, &cred); if (r == -1) { return -1; } - getsockopt_copy(&cred, sizeof(struct ucred), option_value, + getsockopt_copy(&cred, sizeof(struct uucred), option_value, option_len); return 0; } diff --git a/lib/libminlib/getnucred.c b/lib/libminlib/getnucred.c index cbddc7bb9..f95a1d564 100644 --- a/lib/libminlib/getnucred.c +++ b/lib/libminlib/getnucred.c @@ -1,28 +1,32 @@ -#include #include -#include +#include +#include #include -int getnucred(endpoint_t proc_ep, struct ucred *ucred) +#include + +int +getnucred(endpoint_t proc_ep, struct uucred *ucred) { - message m; - pid_t pid; + message m; + pid_t pid; - if (ucred == NULL) { - errno = EFAULT; - return -1; - } + if (ucred == NULL) { + errno = EFAULT; + return -1; + } - m.m1_i1 = proc_ep; /* search for this process */ + m.m1_i1 = proc_ep; /* search for this process */ - pid = _syscall(PM_PROC_NR, GETEPINFO, &m); - if (pid < 0) { - return -1; - } + pid = _syscall(PM_PROC_NR, GETEPINFO, &m); + if (pid < 0) { + return -1; + } - ucred->pid = pid; - ucred->uid = m.PM_NUID; - ucred->gid = m.PM_NGID; + /* Only two fields are used for now, so ensure the rest is zeroed out. */ + memset(ucred, 0, sizeof(struct uucred)); + ucred->cr_uid = m.PM_NUID; + ucred->cr_gid = m.PM_NGID; - return 0; + return 0; } diff --git a/man/man2/getnucred.2 b/man/man2/getnucred.2 index 0f989cec2..31e317d46 100644 --- a/man/man2/getnucred.2 +++ b/man/man2/getnucred.2 @@ -6,10 +6,10 @@ getnucred \- obtain the credentials that correspond to the given endpoint. #include .in +5 .ti -5 -#include +#include .ti -5 -int getnucred(endpoint_t \fIproc_ep\fP, struct ucred * \fIucred\fP); +int getnucred(endpoint_t \fIproc_ep\fP, struct uucred * \fIucred\fP); .br .ft P .SH DESCRIPTION diff --git a/servers/pfs/dev_uds.c b/servers/pfs/dev_uds.c index cada761a5..885e19440 100644 --- a/servers/pfs/dev_uds.c +++ b/servers/pfs/dev_uds.c @@ -40,7 +40,7 @@ static int uds_perform_write(int minor, endpoint_t m_source, size_t int uds_open(message *dev_m_in, message *dev_m_out) { message fs_m_in, fs_m_out; - struct ucred ucred; + struct uucred ucred; int rc, i; int minor; @@ -178,8 +178,8 @@ int uds_open(message *dev_m_in, message *dev_m_out) fs_m_in.m_type = REQ_NEWNODE; fs_m_in.REQ_MODE = I_NAMED_PIPE; fs_m_in.REQ_DEV = NO_DEV; - fs_m_in.REQ_UID = ucred.uid; - fs_m_in.REQ_GID = ucred.gid; + fs_m_in.REQ_UID = ucred.cr_uid; + fs_m_in.REQ_GID = ucred.cr_gid; /* Request a new inode on the pipe file system */ diff --git a/servers/pfs/inc.h b/servers/pfs/inc.h index 89a1801e4..0aa5f05e4 100644 --- a/servers/pfs/inc.h +++ b/servers/pfs/inc.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/servers/pfs/uds.c b/servers/pfs/uds.c index 34b9d1006..9142a87bb 100644 --- a/servers/pfs/uds.c +++ b/servers/pfs/uds.c @@ -960,7 +960,7 @@ int do_getsockopt_peercred(message *dev_m_in, message *dev_m_out) int minor; int peer_minor; int rc; - struct ucred cred; + struct uucred cred; #if DEBUG == 1 static int call_count = 0; @@ -991,7 +991,7 @@ int do_getsockopt_peercred(message *dev_m_in, message *dev_m_out) } rc = sys_safecopyto(VFS_PROC_NR, (cp_grant_id_t) dev_m_in->IO_GRANT, - (vir_bytes) 0, (vir_bytes) &cred, sizeof(struct ucred)); + (vir_bytes) 0, (vir_bytes) &cred, sizeof(struct uucred)); return rc ? EIO : OK; } @@ -1352,10 +1352,10 @@ static int recv_cred(int minor, struct ancillary *data, cmsg = CMSG_NXTHDR(&msghdr, cmsg); } - cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); + cmsg->cmsg_len = CMSG_LEN(sizeof(struct uucred)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_CREDENTIALS; - memcpy(CMSG_DATA(cmsg), &(data->cred), sizeof(struct ucred)); + memcpy(CMSG_DATA(cmsg), &(data->cred), sizeof(struct uucred)); return OK; } @@ -1481,7 +1481,7 @@ int do_recvmsg(message *dev_m_in, message *dev_m_out) /* if there is room we also include credentials */ controllen_desired = controllen_needed + - CMSG_LEN(sizeof(struct ucred)); + CMSG_LEN(sizeof(struct uucred)); if (controllen_needed > controllen_avail) { return EOVERFLOW; diff --git a/servers/pfs/uds.h b/servers/pfs/uds.h index 066a64626..93b2ee347 100644 --- a/servers/pfs/uds.h +++ b/servers/pfs/uds.h @@ -26,7 +26,7 @@ struct ancillary { filp_id_t filps[OPEN_MAX]; int fds[OPEN_MAX]; int nfiledes; - struct ucred cred; + struct uucred cred; }; /* diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h index 3ab646258..17ca57ce6 100644 --- a/sys/sys/ucred.h +++ b/sys/sys/ucred.h @@ -43,12 +43,6 @@ /* * Credentials. */ -struct ucred -{ - pid_t pid; - uid_t uid; - gid_t gid; -}; /* Userland's view of credentials. This should not change */ struct uucred { @@ -59,9 +53,9 @@ struct uucred { gid_t cr_groups[NGROUPS_MAX]; /* groups */ }; -#ifdef __minix +#if defined(__minix) #include -int getnucred(endpoint_t proc_ep, struct ucred *ucred); -#endif +int getnucred(endpoint_t proc_ep, struct uucred *ucred); +#endif /* defined(__minix) */ #endif /* !_SYS_UCRED_H_ */ diff --git a/test/test56.c b/test/test56.c index 0b1b3197a..af55d09fa 100644 --- a/test/test56.c +++ b/test/test56.c @@ -385,7 +385,7 @@ void test_socketpair(void) void test_ucred(void) { - struct ucred credentials; + struct uucred credentials; socklen_t ucred_length; uid_t euid = geteuid(); gid_t egid = getegid(); @@ -394,7 +394,7 @@ void test_ucred(void) debug("Test credentials passing"); - ucred_length = sizeof(struct ucred); + ucred_length = sizeof(struct uucred); rc = socketpair(PF_UNIX, SOCK_STREAM, 0, sv); if (rc == -1) { @@ -406,18 +406,18 @@ void test_ucred(void) &ucred_length); if (rc == -1) { test_fail("getsockopt(SO_PEERCRED) failed"); - } else if (credentials.pid != getpid() || - credentials.uid != geteuid() || - credentials.gid != getegid()) { - /* printf("%d=%d %d=%d %d=%d",credentials.pid, getpid(), - credentials.uid, geteuid(), credentials.gid, getegid()); */ + } else if (credentials.cr_ngroups != 0 || + credentials.cr_uid != geteuid() || + credentials.cr_gid != getegid()) { + /* printf("%d=%d %d=%d %d=%d",credentials.cr_ngroups, 0, + credentials.cr_uid, geteuid(), credentials.cr_gid, getegid()); */ test_fail("Credential passing gave us the wrong cred"); } rc = getpeereid(sv[0], &euid, &egid); if (rc == -1) { test_fail("getpeereid(sv[0], &euid, &egid) failed"); - } else if (credentials.uid != euid || credentials.gid != egid) { + } else if (credentials.cr_uid != euid || credentials.cr_gid != egid) { test_fail("getpeereid() didn't give the correct euid/egid"); } @@ -1195,7 +1195,7 @@ void test_xfer_sighdlr(int sig) */ void test_xfer_client(void) { - struct ucred credentials; + struct uucred credentials; socklen_t ucred_length; struct timeval tv; fd_set readfds; @@ -1208,7 +1208,7 @@ void test_xfer_client(void) debug("[client] entering test_xfer_client()"); errct = 0; /* reset error count */ - ucred_length = sizeof(struct ucred); + ucred_length = sizeof(struct uucred); memset(&buf, '\0', sizeof(buf)); while (server_ready == 0) { @@ -1296,10 +1296,10 @@ void test_xfer_client(void) if (rc == -1) { test_fail("[client] getsockopt() failed"); - } else if (credentials.uid != geteuid() || - credentials.gid != getegid()) { - printf("%d=%d=%d %d=%d=%d\n", credentials.uid, getuid(), - geteuid(), credentials.gid, getgid(), getegid()); + } else if (credentials.cr_uid != geteuid() || + credentials.cr_gid != getegid()) { + printf("%d=%d=%d %d=%d=%d\n", credentials.cr_uid, getuid(), + geteuid(), credentials.cr_gid, getgid(), getegid()); test_fail("[client] Credential passing gave us a bad UID/GID"); } @@ -2106,7 +2106,7 @@ void test_scm_credentials(void) int rc; int src; int dst; - struct ucred cred; + struct uucred cred; struct cmsghdr *cmsg = NULL; struct sockaddr_un addr; struct iovec iov[3]; @@ -2233,20 +2233,20 @@ void test_scm_credentials(void) debug("looking for credentials"); - memset(&cred, '\0', sizeof(struct ucred)); + memset(&cred, '\0', sizeof(struct uucred)); for (cmsg = CMSG_FIRSTHDR(&msg2); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg2, cmsg)) { if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) { - memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct ucred)); + memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct uucred)); break; } } - if (cred.pid != getpid() || cred.uid != geteuid() || - cred.gid != getegid()) { + if (cred.cr_ngroups != 0 || cred.cr_uid != geteuid() || + cred.cr_gid != getegid()) { test_fail("did no receive the proper credentials"); }