1*1fab62b1SMaxim Sobolev /*-
2*1fab62b1SMaxim Sobolev * Copyright (c) 2005 Andrey Simonenko
3*1fab62b1SMaxim Sobolev * All rights reserved.
4*1fab62b1SMaxim Sobolev *
5*1fab62b1SMaxim Sobolev * Redistribution and use in source and binary forms, with or without
6*1fab62b1SMaxim Sobolev * modification, are permitted provided that the following conditions
7*1fab62b1SMaxim Sobolev * are met:
8*1fab62b1SMaxim Sobolev * 1. Redistributions of source code must retain the above copyright
9*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer.
10*1fab62b1SMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright
11*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer in the
12*1fab62b1SMaxim Sobolev * documentation and/or other materials provided with the distribution.
13*1fab62b1SMaxim Sobolev *
14*1fab62b1SMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fab62b1SMaxim Sobolev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fab62b1SMaxim Sobolev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fab62b1SMaxim Sobolev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fab62b1SMaxim Sobolev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fab62b1SMaxim Sobolev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fab62b1SMaxim Sobolev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fab62b1SMaxim Sobolev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fab62b1SMaxim Sobolev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fab62b1SMaxim Sobolev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fab62b1SMaxim Sobolev * SUCH DAMAGE.
25*1fab62b1SMaxim Sobolev */
26*1fab62b1SMaxim Sobolev
27*1fab62b1SMaxim Sobolev #include <sys/types.h>
28*1fab62b1SMaxim Sobolev #include <sys/socket.h>
29*1fab62b1SMaxim Sobolev #include <sys/ucred.h>
30*1fab62b1SMaxim Sobolev #include <sys/un.h>
31*1fab62b1SMaxim Sobolev #include <stdarg.h>
32*1fab62b1SMaxim Sobolev #include <stdbool.h>
33*1fab62b1SMaxim Sobolev
34*1fab62b1SMaxim Sobolev #include "uc_common.h"
35*1fab62b1SMaxim Sobolev #include "t_generic.h"
36*1fab62b1SMaxim Sobolev #include "t_peercred.h"
37*1fab62b1SMaxim Sobolev
38*1fab62b1SMaxim Sobolev static int
check_xucred(const struct xucred * xucred,socklen_t len)39*1fab62b1SMaxim Sobolev check_xucred(const struct xucred *xucred, socklen_t len)
40*1fab62b1SMaxim Sobolev {
41*1fab62b1SMaxim Sobolev int rc;
42*1fab62b1SMaxim Sobolev
43*1fab62b1SMaxim Sobolev if (len != sizeof(*xucred)) {
44*1fab62b1SMaxim Sobolev uc_logmsgx("option value size %zu != %zu",
45*1fab62b1SMaxim Sobolev (size_t)len, sizeof(*xucred));
46*1fab62b1SMaxim Sobolev return (-1);
47*1fab62b1SMaxim Sobolev }
48*1fab62b1SMaxim Sobolev
49*1fab62b1SMaxim Sobolev uc_dbgmsg("xucred.cr_version %u", xucred->cr_version);
50*1fab62b1SMaxim Sobolev uc_dbgmsg("xucred.cr_uid %lu", (u_long)xucred->cr_uid);
51*1fab62b1SMaxim Sobolev uc_dbgmsg("xucred.cr_ngroups %d", xucred->cr_ngroups);
52*1fab62b1SMaxim Sobolev
53*1fab62b1SMaxim Sobolev rc = 0;
54*1fab62b1SMaxim Sobolev
55*1fab62b1SMaxim Sobolev if (xucred->cr_version != XUCRED_VERSION) {
56*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_version %u != %d",
57*1fab62b1SMaxim Sobolev xucred->cr_version, XUCRED_VERSION);
58*1fab62b1SMaxim Sobolev rc = -1;
59*1fab62b1SMaxim Sobolev }
60*1fab62b1SMaxim Sobolev if (xucred->cr_uid != uc_cfg.proc_cred.euid) {
61*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_uid %lu != %lu (EUID)",
62*1fab62b1SMaxim Sobolev (u_long)xucred->cr_uid, (u_long)uc_cfg.proc_cred.euid);
63*1fab62b1SMaxim Sobolev rc = -1;
64*1fab62b1SMaxim Sobolev }
65*1fab62b1SMaxim Sobolev if (xucred->cr_ngroups == 0) {
66*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_ngroups == 0");
67*1fab62b1SMaxim Sobolev rc = -1;
68*1fab62b1SMaxim Sobolev }
69*1fab62b1SMaxim Sobolev if (xucred->cr_ngroups < 0) {
70*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_ngroups < 0");
71*1fab62b1SMaxim Sobolev rc = -1;
72*1fab62b1SMaxim Sobolev }
73*1fab62b1SMaxim Sobolev if (xucred->cr_ngroups > XU_NGROUPS) {
74*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_ngroups %hu > %u (max)",
75*1fab62b1SMaxim Sobolev xucred->cr_ngroups, XU_NGROUPS);
76*1fab62b1SMaxim Sobolev rc = -1;
77*1fab62b1SMaxim Sobolev }
78*1fab62b1SMaxim Sobolev if (xucred->cr_groups[0] != uc_cfg.proc_cred.egid) {
79*1fab62b1SMaxim Sobolev uc_logmsgx("xucred.cr_groups[0] %lu != %lu (EGID)",
80*1fab62b1SMaxim Sobolev (u_long)xucred->cr_groups[0], (u_long)uc_cfg.proc_cred.egid);
81*1fab62b1SMaxim Sobolev rc = -1;
82*1fab62b1SMaxim Sobolev }
83*1fab62b1SMaxim Sobolev if (uc_check_groups("xucred.cr_groups", xucred->cr_groups,
84*1fab62b1SMaxim Sobolev "xucred.cr_ngroups", xucred->cr_ngroups, false) < 0)
85*1fab62b1SMaxim Sobolev rc = -1;
86*1fab62b1SMaxim Sobolev return (rc);
87*1fab62b1SMaxim Sobolev }
88*1fab62b1SMaxim Sobolev
89*1fab62b1SMaxim Sobolev static int
t_peercred_client(int fd)90*1fab62b1SMaxim Sobolev t_peercred_client(int fd)
91*1fab62b1SMaxim Sobolev {
92*1fab62b1SMaxim Sobolev struct xucred xucred;
93*1fab62b1SMaxim Sobolev socklen_t len;
94*1fab62b1SMaxim Sobolev
95*1fab62b1SMaxim Sobolev if (uc_sync_recv() < 0)
96*1fab62b1SMaxim Sobolev return (-1);
97*1fab62b1SMaxim Sobolev
98*1fab62b1SMaxim Sobolev if (uc_socket_connect(fd) < 0)
99*1fab62b1SMaxim Sobolev return (-1);
100*1fab62b1SMaxim Sobolev
101*1fab62b1SMaxim Sobolev len = sizeof(xucred);
102*1fab62b1SMaxim Sobolev if (getsockopt(fd, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {
103*1fab62b1SMaxim Sobolev uc_logmsg("getsockopt(LOCAL_PEERCRED)");
104*1fab62b1SMaxim Sobolev return (-1);
105*1fab62b1SMaxim Sobolev }
106*1fab62b1SMaxim Sobolev
107*1fab62b1SMaxim Sobolev if (check_xucred(&xucred, len) < 0)
108*1fab62b1SMaxim Sobolev return (-1);
109*1fab62b1SMaxim Sobolev
110*1fab62b1SMaxim Sobolev return (0);
111*1fab62b1SMaxim Sobolev }
112*1fab62b1SMaxim Sobolev
113*1fab62b1SMaxim Sobolev static int
t_peercred_server(int fd1)114*1fab62b1SMaxim Sobolev t_peercred_server(int fd1)
115*1fab62b1SMaxim Sobolev {
116*1fab62b1SMaxim Sobolev struct xucred xucred;
117*1fab62b1SMaxim Sobolev socklen_t len;
118*1fab62b1SMaxim Sobolev int fd2, rv;
119*1fab62b1SMaxim Sobolev
120*1fab62b1SMaxim Sobolev if (uc_sync_send() < 0)
121*1fab62b1SMaxim Sobolev return (-2);
122*1fab62b1SMaxim Sobolev
123*1fab62b1SMaxim Sobolev fd2 = uc_socket_accept(fd1);
124*1fab62b1SMaxim Sobolev if (fd2 < 0)
125*1fab62b1SMaxim Sobolev return (-2);
126*1fab62b1SMaxim Sobolev
127*1fab62b1SMaxim Sobolev len = sizeof(xucred);
128*1fab62b1SMaxim Sobolev if (getsockopt(fd2, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {
129*1fab62b1SMaxim Sobolev uc_logmsg("getsockopt(LOCAL_PEERCRED)");
130*1fab62b1SMaxim Sobolev rv = -2;
131*1fab62b1SMaxim Sobolev goto done;
132*1fab62b1SMaxim Sobolev }
133*1fab62b1SMaxim Sobolev
134*1fab62b1SMaxim Sobolev if (check_xucred(&xucred, len) < 0) {
135*1fab62b1SMaxim Sobolev rv = -1;
136*1fab62b1SMaxim Sobolev goto done;
137*1fab62b1SMaxim Sobolev }
138*1fab62b1SMaxim Sobolev
139*1fab62b1SMaxim Sobolev rv = 0;
140*1fab62b1SMaxim Sobolev done:
141*1fab62b1SMaxim Sobolev if (uc_socket_close(fd2) < 0)
142*1fab62b1SMaxim Sobolev rv = -2;
143*1fab62b1SMaxim Sobolev return (rv);
144*1fab62b1SMaxim Sobolev }
145*1fab62b1SMaxim Sobolev
146*1fab62b1SMaxim Sobolev int
t_peercred(void)147*1fab62b1SMaxim Sobolev t_peercred(void)
148*1fab62b1SMaxim Sobolev {
149*1fab62b1SMaxim Sobolev return (t_generic(t_peercred_client, t_peercred_server));
150*1fab62b1SMaxim Sobolev }
151