-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsandbox_rpc.c
205 lines (172 loc) · 4.51 KB
/
sandbox_rpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <sys/socket.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "sandbox_rpc.h"
void
_sandbox_dispose_rights(int *fdp, int fdcount)
{
int i;
for (i = 0; i < fdcount; i++)
close(fdp[i]);
}
/*
* Given a 'struct msghdr' returned by a successful call to recvmsg(),
* extract up to the desired number of file descriptors (or clean up the
* mess if something goes wrong).
*/
int
_sandbox_rpc_receive_rights(struct msghdr *msg, int *fdp, int *fdcountp)
{
int *cmsg_fdp, fdcount, i, scmrightscount;
struct cmsghdr *cmsg;
/*
* Walk the complete control message chain to count received control
* messages and rights. If there is more than one rights message or
* there are too many file descriptors, re-walk and close them all
* and return an error.
*/
fdcount = 0;
scmrightscount = 0;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS)
continue;
/* XXX IM: recheck this! */
/*fdcount += (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);*/
fdcount++;
scmrightscount++;
}
if (scmrightscount > 1 || fdcount > *fdcountp) {
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS)
continue;
cmsg_fdp = (int *)(void *)CMSG_DATA(cmsg);
fdcount = (cmsg->cmsg_len - CMSG_LEN(0)) /
sizeof(int);
_sandbox_dispose_rights(cmsg_fdp, fdcount);
}
errno = EBADMSG;
return (-1);
}
/*
* Re-walk the control messages and copy out the file descriptor
* numbers, return success. No need to recalculate fdcount.
*/
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS)
continue;
cmsg_fdp = (int *)(void *)CMSG_DATA(cmsg);
for (i = 0; i < fdcount; i++)
fdp[i] = cmsg_fdp[i];
}
*fdcountp = fdcount;
return (0);
}
ssize_t
_sandbox_rpc_send(int fd, const void *msg, size_t len, int flags)
{
ssize_t retlen;
if (fd == -1 || fd == 0) {
errno = ECHILD;
return (-1);
}
do {
retlen = send(fd, msg, len, flags);
} while (retlen < 0 && errno == EINTR);
return (retlen);
}
#define SANDBOX_RPC_API_MAXRIGHTS 16
ssize_t
_sandbox_rpc_send_rights(int fd, const void *msg, size_t len, int flags, int
*fdp, int fdcount)
{
char cmsgbuf[CMSG_SPACE(SANDBOX_RPC_API_MAXRIGHTS * sizeof(int))];
struct cmsghdr *cmsg;
struct msghdr msghdr;
struct iovec iov;
ssize_t retlen;
int i;
if (fdcount == 0)
return (_sandbox_rpc_send(fd, msg, len, flags));
if (fd == -1 || fd == 0) {
errno = ECHILD;
return (-1);
}
bzero(&iov, sizeof(iov));
iov.iov_base = __DECONST(void *, msg);
iov.iov_len = len;
bzero(&cmsgbuf, sizeof(cmsgbuf));
cmsg = (struct cmsghdr *)(void *)cmsgbuf;
cmsg->cmsg_len = CMSG_SPACE(fdcount * sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
for (i = 0; i < fdcount; i++)
((int *)(void *)CMSG_DATA(cmsg))[i] = fdp[i];
bzero(&msghdr, sizeof(msghdr));
msghdr.msg_iov = &iov;
msghdr.msg_iovlen = 1;
msghdr.msg_control = cmsg;
msghdr.msg_controllen = cmsg->cmsg_len;
/* Ignore EINTR */
do {
retlen = sendmsg(fd, &msghdr, flags);
} while (retlen < 0 && errno == EINTR);
return (retlen);
}
ssize_t
_sandbox_rpc_recv(int fd, void *buf, size_t len, int flags)
{
ssize_t retlen;
if (fd == -1 || fd == 0) {
errno = ESRCH;
return (-1);
}
do {
retlen = recv(fd, buf, len, flags);
} while (retlen < 0 && errno == EINTR);
return (retlen);
}
#define SANDBOX_RPC_API_MAXRIGHTS 16
ssize_t
_sandbox_rpc_recv_rights(int fd, void *buf, size_t len, int flags, int *fdp,
int *fdcountp)
{
char cmsgbuf[CMSG_SPACE(SANDBOX_RPC_API_MAXRIGHTS * sizeof(int))];
struct msghdr msghdr;
struct iovec iov;
ssize_t retlen;
if (*fdcountp == 0)
return (_sandbox_rpc_recv(fd, buf, len, flags));
if (fd == -1 || fd == 0) {
errno = ECHILD;
return (-1);
}
if (*fdcountp > SANDBOX_RPC_API_MAXRIGHTS) {
errno = EMSGSIZE;
return (-1);
}
bzero(&iov, sizeof(iov));
iov.iov_base = buf;
iov.iov_len = len;
bzero(cmsgbuf, sizeof(cmsgbuf));
bzero(&msghdr, sizeof(msghdr));
msghdr.msg_iov = &iov;
msghdr.msg_iovlen = 1;
msghdr.msg_control = cmsgbuf;
msghdr.msg_controllen = sizeof(cmsgbuf);
do {
retlen = recvmsg(fd, &msghdr, flags);
} while (retlen < 0 && errno == EINTR);
if (retlen < 0)
return (-1);
if (_sandbox_rpc_receive_rights(&msghdr, fdp, fdcountp) < 0)
return (-1);
return (retlen);
}