Path: blob/main/tools/regression/sockets/unix_cmsg/t_peercred.c
96317 views
/*-1* Copyright (c) 2005 Andrey Simonenko2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/socket.h>28#include <sys/ucred.h>29#include <sys/un.h>30#include <stdarg.h>31#include <stdbool.h>3233#include "uc_common.h"34#include "t_generic.h"35#include "t_peercred.h"3637static int38check_xucred(const struct xucred *xucred, socklen_t len)39{40int rc;4142if (len != sizeof(*xucred)) {43uc_logmsgx("option value size %zu != %zu",44(size_t)len, sizeof(*xucred));45return (-1);46}4748uc_dbgmsg("xucred.cr_version %u", xucred->cr_version);49uc_dbgmsg("xucred.cr_uid %lu", (u_long)xucred->cr_uid);50uc_dbgmsg("xucred.cr_ngroups %d", xucred->cr_ngroups);5152rc = 0;5354if (xucred->cr_version != XUCRED_VERSION) {55uc_logmsgx("xucred.cr_version %u != %d",56xucred->cr_version, XUCRED_VERSION);57rc = -1;58}59if (xucred->cr_uid != uc_cfg.proc_cred.euid) {60uc_logmsgx("xucred.cr_uid %lu != %lu (EUID)",61(u_long)xucred->cr_uid, (u_long)uc_cfg.proc_cred.euid);62rc = -1;63}64if (xucred->cr_ngroups == 0) {65uc_logmsgx("xucred.cr_ngroups == 0");66rc = -1;67}68if (xucred->cr_ngroups < 0) {69uc_logmsgx("xucred.cr_ngroups < 0");70rc = -1;71}72if (xucred->cr_ngroups > XU_NGROUPS) {73uc_logmsgx("xucred.cr_ngroups %hu > %u (max)",74xucred->cr_ngroups, XU_NGROUPS);75rc = -1;76}77if (xucred->cr_groups[0] != uc_cfg.proc_cred.egid) {78uc_logmsgx("xucred.cr_groups[0] %lu != %lu (EGID)",79(u_long)xucred->cr_groups[0], (u_long)uc_cfg.proc_cred.egid);80rc = -1;81}82if (uc_check_groups("xucred.cr_groups", xucred->cr_groups,83"xucred.cr_ngroups", xucred->cr_ngroups, false) < 0)84rc = -1;85return (rc);86}8788static int89t_peercred_client(int fd)90{91struct xucred xucred;92socklen_t len;9394if (uc_sync_recv() < 0)95return (-1);9697if (uc_socket_connect(fd) < 0)98return (-1);99100len = sizeof(xucred);101if (getsockopt(fd, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {102uc_logmsg("getsockopt(LOCAL_PEERCRED)");103return (-1);104}105106if (check_xucred(&xucred, len) < 0)107return (-1);108109return (0);110}111112static int113t_peercred_server(int fd1)114{115struct xucred xucred;116socklen_t len;117int fd2, rv;118119if (uc_sync_send() < 0)120return (-2);121122fd2 = uc_socket_accept(fd1);123if (fd2 < 0)124return (-2);125126len = sizeof(xucred);127if (getsockopt(fd2, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {128uc_logmsg("getsockopt(LOCAL_PEERCRED)");129rv = -2;130goto done;131}132133if (check_xucred(&xucred, len) < 0) {134rv = -1;135goto done;136}137138rv = 0;139done:140if (uc_socket_close(fd2) < 0)141rv = -2;142return (rv);143}144145int146t_peercred(void)147{148return (t_generic(t_peercred_client, t_peercred_server));149}150151152