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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* Copyright 2016 Jeremy Allison
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* This is a regression test for a (former) problem in illumos
* where a program that tries to send on a "connected" AF_UNIX
* _datagram_ socket fails if: (a) the file system path used for
* the socket requires privileges to access, and (b) a process
* with privileges to access that path does a connect() to that
* address and then drops privileges. In that case, a sendmsg()
* call where the "to" address is left blank should succeed.
* Before the fix for illumos 7590 that would fail.
*
* This program must be run as root. The expected output is:
*
* non_priv_send - sendmsg fail (expected) Permission denied
* CLIENT:TEST0
* SERVER:TEST0
* CLIENT:TEST1
* SERVER:TEST1
* CLIENT:TEST2
* SERVER:TEST2
* CLIENT:TEST3
* SERVER:TEST3
* CLIENT:TEST4
* SERVER:TEST4
*
* Without the fix for 7590, one would see:
* non_priv_send - sendmsg fail (expected) Permission denied
* CLIENT:TEST0
* ./sendtest - sendmsg fail Permission denied
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
static int
server(struct sockaddr_un *addr)
{
int ret;
pid_t pid;
int sock;
unsigned int i;
pid = fork();
if (pid == (pid_t)-1) {
fprintf(stderr, "server - fork fail %s\n", strerror(errno));
return (-1);
}
if (pid != 0) {
/* Parent. */
return (0);
}
/* Child. */
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock == -1) {
fprintf(stderr, "server - socket fail %s\n", strerror(errno));
exit(1);
}
ret = bind(sock, (struct sockaddr *)addr, sizeof (*addr));
if (ret == -1) {
fprintf(stderr, "server - bind fail %s\n", strerror(errno));
exit(1);
}
for (i = 0; i < 5; i++) {
struct iovec iov;
struct msghdr msg;
uint8_t buf[4096];
iov = (struct iovec) {
.iov_base = buf,
.iov_len = sizeof (buf)
};
msg = (struct msghdr) {
.msg_iov = &iov,
.msg_iovlen = 1,
};
ret = recvmsg(sock, &msg, 0);
if (ret == -1) {
fprintf(stderr, "server - recvmsg fail %s\n",
strerror(errno));
exit(1);
}
printf("SERVER:%s\n", (char *)msg.msg_iov->iov_base);
fflush(stdout);
}
exit(0);
}
static void
non_priv_send(struct sockaddr_un *addr, int uid)
{
pid_t pid;
int sock;
int ret;
struct iovec iov;
struct msghdr msg;
uint8_t buf[4096];
pid = fork();
if (pid == (pid_t)-1) {
fprintf(stderr, "non_priv_send - fork fail %s\n",
strerror(errno));
return;
}
if (pid != 0) {
/* Parent. */
return;
}
/* Child. */
memcpy(buf, "TEST1\n", sizeof ("TEST1\n"));
iov = (struct iovec) {
.iov_base = buf,
.iov_len = sizeof (buf),
};
msg = (struct msghdr) {
.msg_name = addr,
.msg_namelen = sizeof (*addr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock == -1) {
fprintf(stderr, "non_priv_send - socket fail %s\n",
strerror(errno));
exit(1);
}
ret = setreuid(uid, uid);
if (ret == -1) {
fprintf(stderr, "non_priv_send - setresuid fail %s\n",
strerror(errno));
exit(1);
}
ret = sendmsg(sock, &msg, 0);
if (ret == -1) {
printf("non_priv_send - sendmsg fail (expected) %s\n",
strerror(errno));
exit(0);
}
fprintf(stderr, "non_priv_send - UNEXPECTED sendmsg OK\n");
exit(1);
}
/*
* This should be a place only root is allowed to write.
* The test will create and destroy this directory.
*/
char testdir[100] = "/var/run/os-tests-sockfs";
struct sockaddr_un addr;
int test_uid = UID_NOBODY;
int
main(int argc, char **argv)
{
int ret;
int sock;
unsigned int i;
uid_t us = geteuid();
/* Ensure we're root. */
if (us != 0) {
fprintf(stderr, "%s: need to be root\n", argv[0]);
exit(1);
}
if (argc > 1) {
ret = strlcpy(testdir, argv[1], sizeof (testdir));
if (ret >= sizeof (testdir)) {
fprintf(stderr, "%s: too long\n", argv[1]);
exit(1);
}
}
addr.sun_family = AF_UNIX;
(void) sprintf(addr.sun_path, "%s/s", testdir);
if (mkdir(testdir, 0700) != 0) {
switch (errno) {
case EEXIST:
case EISDIR:
break;
default:
perror(testdir);
exit(1);
}
}
(void) unlink(addr.sun_path);
/* Set up the server. */
ret = server(&addr);
if (ret == -1) {
fprintf(stderr, "%s - server fork fail %s\n",
argv[0], strerror(errno));
exit(1);
}
sleep(1);
/* Chec non-priv client - should fail. */
non_priv_send(&addr, test_uid);
sleep(1);
/* Create and connect the socket endpoint. */
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock == -1) {
fprintf(stderr, "%s - socket fail %s\n",
argv[0], strerror(errno));
exit(1);
}
ret = connect(sock, (struct sockaddr *)&addr, sizeof (addr));
if (ret == -1) {
fprintf(stderr, "%s - connect fail %s\n",
argv[0], strerror(errno));
exit(1);
}
/*
* Now lose all privilages.
* The sendmsg() should still succeed as
* 'sock' has been connected to the endpoint,
* even though we don't have permissions as
* the non privileged user to access the
* UNIX domain socket.
*/
ret = setreuid(test_uid, test_uid);
if (ret == -1) {
printf("%s - setresuid fail %s\n",
argv[0], strerror(errno));
exit(1);
}
for (i = 0; i < 5; i++) {
struct iovec iov;
struct msghdr msg;
uint8_t buf[4096];
memcpy(buf, "TEST0", sizeof ("TEST0"));
buf[4] = '0' + i;
printf("CLIENT:%s\n", buf);
iov = (struct iovec) {
.iov_base = buf,
.iov_len = sizeof (buf),
};
msg = (struct msghdr) {
/*
* If not for the dropped privileges that are
* the essential feature of this test, a more
* common practice would be to fill in the
* .msg_name, .msg_namelen fields here.
* However, when we've dropped privileges,
* and when we do specify the "to" address,
* the kernel does permission checks and the
* sendmsg fails with permission denied.
* So long as we do _not_ fill in the "to"
* address, send on a connected dgram socket
* is supposed to work. Before the fix for
* illumos 7590, that would fail.
*/
.msg_iov = &iov,
.msg_iovlen = 1,
};
ret = sendmsg(sock, &msg, 0);
if (ret == -1) {
fprintf(stderr, "%s - sendmsg fail %s\n",
argv[0], strerror(errno));
exit(1);
}
fflush(stdout);
sleep(1);
}
close(sock);
return (0);
}
|