summaryrefslogtreecommitdiff
path: root/usr/src/test/os-tests/tests/sockfs/rights.c
blob: 5e8382416f82f80dfd69b4c123b517f3f80a91fb (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
 * Copyright 2020 Joyent, Inc.
 */

/*
 * Test file descriptor passing via SCM_RIGHTS, and in particular what happens
 * on message truncation in terms of the represented size of the data in the
 * control message. Ensure that no file descriptors are leaked - the kernel
 * must close any that would not fit in the available buffer space and the
 * userland application must close the rest.
 */

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <libproc.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <assert.h>
#include <alloca.h>
#include <err.h>

static boolean_t debug;

typedef struct cmsg_test {
	char *name;		/* Name of the test */
	uint_t send;		/* Number of FDs to send */
	uint_t recv;		/* Size receive buffer for this number of FDs */
	size_t predata;		/* Prepend dummy cmsg of this size */
	int bufsize;		/* Explicitly set receive buffer size. */
				/* Overrides 'recv' if non-zero */
	uint_t x_controllen;	/* Expected received msg_controllen */
	uint_t x_cmsg_datalen;	/* Expected received cmsg data length */
	uint32_t x_flags;	/* Expected received msf_flags */
} cmsg_test_t;

static cmsg_test_t tests[] = {
	{
		.name = "send 1, recv 1",
		.send = 1,
		.recv = 1,
		.predata = 0,
		.bufsize = 0,
		.x_controllen = 16,
		.x_cmsg_datalen = 4,
		.x_flags = 0,
	},
	{
		.name = "send 10, recv 10",
		.send = 10,
		.recv = 10,
		.predata = 0,
		.bufsize = 0,
		.x_controllen = 52,
		.x_cmsg_datalen = 40,
		.x_flags = 0,
	},
	{
		.name = "send 2, recv 1",
		.send = 2,
		.recv = 1,
		.predata = 0,
		.bufsize = 0,
		.x_controllen = 16,
		.x_cmsg_datalen = 4,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, buffer 5",
		.send = 2,
		.recv = 1,
		.predata = 0,
		.bufsize = sizeof (int) * 2 - 3,
		.x_controllen = 17,
		.x_cmsg_datalen = 5,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, buffer 6",
		.send = 2,
		.recv = 1,
		.predata = 0,
		.bufsize = sizeof (int) * 2 - 2,
		.x_controllen = 18,
		.x_cmsg_datalen = 6,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, buffer 7",
		.send = 2,
		.recv = 1,
		.predata = 0,
		.bufsize = sizeof (int) * 2 - 1,
		.x_controllen = 19,
		.x_cmsg_datalen = 7,
		.x_flags = MSG_CTRUNC,
	},

	/* Tests where there is no room allowed for data */

	{
		.name = "send 2, recv 0, hdronly",
		.send = 2,
		.recv = 0,
		.predata = 0,
		.bufsize = 0,
		.x_controllen = 12,
		.x_cmsg_datalen = 0,
		.x_flags = MSG_CTRUNC,
	},

	{
		.name = "send 2, recv 0, hdr - 1",
		.send = 2,
		.recv = 0,
		.predata = 0,
		.bufsize = -1,
		.x_controllen = 11,
		.x_cmsg_datalen = 0,
		.x_flags = MSG_CTRUNC,
	},

	{
		.name = "send 2, recv 0, hdr - 5",
		.send = 2,
		.recv = 0,
		.predata = 0,
		.bufsize = -5,
		.x_controllen = 7,
		.x_cmsg_datalen = 0,
		.x_flags = MSG_CTRUNC,
	},

	/* Tests where SCM_RIGHTS is not the first message */

	{
		.name = "send 1, recv 1, pre 8",
		.send = 1,
		.recv = 1,
		.predata = 8,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 4,
		.x_flags = 0,
	},
	{
		.name = "send 1, recv 1, pre 7",
		.send = 1,
		.recv = 1,
		.predata = 7,
		.bufsize = 0,
		.x_controllen = 35,
		.x_cmsg_datalen = 4,
		.x_flags = 0,
	},
	{
		.name = "send 1, recv 1, pre 6",
		.send = 1,
		.recv = 1,
		.predata = 6,
		.bufsize = 0,
		.x_controllen = 34,
		.x_cmsg_datalen = 4,
		.x_flags = 0,
	},
	{
		.name = "send 1, recv 1, pre 5",
		.send = 1,
		.recv = 1,
		.predata = 5,
		.bufsize = 0,
		.x_controllen = 33,
		.x_cmsg_datalen = 4,
		.x_flags = 0,
	},

	{
		.name = "send 2, recv 1, pre 8",
		.send = 2,
		.recv = 1,
		.predata = 8,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 7",
		.send = 2,
		.recv = 1,
		.predata = 7,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 6",
		.send = 2,
		.recv = 1,
		.predata = 6,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 5",
		.send = 2,
		.recv = 1,
		.predata = 5,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 4",
		.send = 2,
		.recv = 1,
		.predata = 4,
		.bufsize = 0,
		.x_controllen = 32,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 3",
		.send = 2,
		.recv = 1,
		.predata = 3,
		.bufsize = 0,
		.x_controllen = 32,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 2",
		.send = 2,
		.recv = 1,
		.predata = 2,
		.bufsize = 0,
		.x_controllen = 32,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 1",
		.send = 2,
		.recv = 1,
		.predata = 1,
		.bufsize = 0,
		.x_controllen = 32,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},

	{
		.name = "send 2, recv 1, pre 8, buffer 5",
		.send = 2,
		.recv = 1,
		.predata = 8,
		.bufsize = sizeof (int) * 2 - 3,
		.x_controllen = 37,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 8, buffer 6",
		.send = 2,
		.recv = 1,
		.predata = 8,
		.bufsize = sizeof (int) * 2 - 2,
		.x_controllen = 38,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 2, recv 1, pre 8, buffer 7",
		.send = 2,
		.recv = 1,
		.predata = 8,
		.bufsize = sizeof (int) * 2 - 1,
		.x_controllen = 39,
		.x_cmsg_datalen = 8,
		.x_flags = MSG_CTRUNC,
	},
	{
		.name = "send 10, recv 1, pre 8",
		.send = 10,
		.recv = 1,
		.predata = 8,
		.bufsize = 0,
		.x_controllen = 36,
		.x_cmsg_datalen = 24,
		.x_flags = MSG_CTRUNC,
	},

	/* End of tests */

	{
		.name = NULL
	}
};

static int sock = -1, testfd = -1, cfd = -1;
static int fdcount;

static int
fdwalkcb(const prfdinfo_t *info, void *arg)
{
	if (!S_ISDIR(info->pr_mode) && info->pr_fd > 2 &&
	    info->pr_fd != sock && info->pr_fd != testfd &&
	    info->pr_fd != cfd) {
		if (debug) {
			fprintf(stderr, "%s: unexpected fd: %d\n",
			    (char *)arg, info->pr_fd);
		}
		fdcount++;
	}

	return (0);

}

static void
check_fds(char *tag)
{
	fdcount = 0;
	proc_fdwalk(getpid(), fdwalkcb, tag);
}

static void
send_and_wait(pid_t pid, sigset_t *set, int osig, int isig)
{
	int sig;

	if (osig > 0)
		kill(pid, osig);

	if (isig > 0) {
		if (sigwait(set, &sig) != 0) {
			err(EXIT_FAILURE,
			    "sigwait failed waiting for %d", isig);
		}
		if (sig == SIGINT) {
			exit(1);
		}
		if (sig != isig) {
			err(EXIT_FAILURE,
			    "sigwait returned unexpected signal %d", sig);
		}
	}
}

static void
sendtest(cmsg_test_t *t)
{
	struct msghdr msg;
	struct cmsghdr *cm;
	struct iovec iov;
	ssize_t nbytes;
	char c = '*';
	int i, *p;

	bzero(&msg, sizeof (msg));

	msg.msg_name = NULL;
	msg.msg_namelen = 0;

	iov.iov_base = &c;
	iov.iov_len = sizeof (c);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	msg.msg_flags = 0;

	msg.msg_controllen = CMSG_SPACE(sizeof (int) * t->send);

	if (t->predata > 0) {
		/* A dummy cmsg will be inserted at the head of the data */
		msg.msg_controllen += CMSG_SPACE(t->predata);
	}

	msg.msg_control = alloca(msg.msg_controllen);
	bzero(msg.msg_control, msg.msg_controllen);

	cm = CMSG_FIRSTHDR(&msg);

	if (t->predata > 0) {
		/* Insert the dummy cmsg */
		cm->cmsg_len = CMSG_LEN(t->predata);
		cm->cmsg_level = SOL_SOCKET;
		cm->cmsg_type = 0;
		cm = CMSG_NXTHDR(&msg, cm);
	}

	cm->cmsg_len = CMSG_LEN(sizeof (int) * t->send);
	cm->cmsg_level = SOL_SOCKET;
	cm->cmsg_type = SCM_RIGHTS;

	p = (int *)CMSG_DATA(cm);
	for (i = 0; i < t->send; i++) {
		int s = dup(testfd);
		if (s == -1)
			err(EXIT_FAILURE, "dup()");
		*p++ = s;
	}

	if (debug)
		printf("Sending: controllen=%u\n", msg.msg_controllen);

	nbytes = sendmsg(cfd, &msg, 0);
	if (nbytes == -1)
		err(EXIT_FAILURE, "sendmsg()");

	p = (int *)CMSG_DATA(cm);
	for (i = 0; i < t->send; i++)
		(void) close(*p++);
}

static int
server(const char *sockpath, pid_t pid)
{
	struct sockaddr_un addr;
	sigset_t set;
	cmsg_test_t *t;

	sigemptyset(&set);
	sigaddset(&set, SIGUSR2);
	sigaddset(&set, SIGINT);

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock == -1)
		err(EXIT_FAILURE, "failed to create socket");
	addr.sun_family = AF_UNIX;
	strlcpy(addr.sun_path, sockpath, sizeof (addr.sun_path));
	if (bind(sock, (struct sockaddr *)&addr, sizeof (addr)) == -1)
		err(EXIT_FAILURE, "bind failed");
	if (listen(sock, 0) == -1)
		err(EXIT_FAILURE, "listen failed");

	if ((testfd = open("/dev/null", O_RDONLY)) == -1)
		err(EXIT_FAILURE, "/dev/null");

	check_fds("server");

	/* Signal the child to connect to the socket */
	send_and_wait(pid, &set, SIGUSR1, SIGUSR2);

	if ((cfd = accept(sock, NULL, 0)) == -1)
		err(EXIT_FAILURE, "accept failed");

	for (t = tests; t->name != NULL; t++) {
		if (debug)
			printf("\n>>> Starting test %s\n", t->name);

		sendtest(t);
		check_fds("server");

		send_and_wait(pid, &set, SIGUSR1, SIGUSR2);
	}

	close(cfd);
	close(testfd);
	close(sock);

	return (0);
}

static boolean_t pass;

static void
check(uint_t actual, uint_t expected, char *tag)
{
	if (actual != expected) {
		fprintf(stderr, "    !!!: "
		    "%1$s = %2$u(%2$#x) (expected %3$u(%3$#x))\n",
		    tag, actual, expected);
		pass = _B_FALSE;
	} else if (debug) {
		fprintf(stderr, "       : "
		    "%1$s = %2$u(%2$#x)\n",
		    tag, actual);
	}
}

static boolean_t
recvtest(cmsg_test_t *t)
{
	struct msghdr msg;
	struct cmsghdr *cm;
	struct iovec iov;
	size_t bufsize;
	ssize_t nbytes;
	char c = '*';

	bzero(&msg, sizeof (msg));

	msg.msg_name = NULL;
	msg.msg_namelen = 0;

	iov.iov_base = &c;
	iov.iov_len = sizeof (c);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	msg.msg_flags = 0;

	/*
	 * If the test does not specify a receive buffer size, calculate one
	 * from the number of file descriptors to receive.
	 */
	if (t->bufsize == 0) {
		bufsize = sizeof (int) * t->recv;
		bufsize = CMSG_SPACE(bufsize);
	} else {
		/*
		 * Use the specific buffer size provided but add in
		 * space for the header
		 */
		bufsize = t->bufsize + CMSG_LEN(0);
	}

	if (t->predata > 0) {
		/* A dummy cmsg will be found at the head of the data */
		bufsize += CMSG_SPACE(t->predata);
	}

	msg.msg_controllen = bufsize;
	msg.msg_control = alloca(bufsize);
	bzero(msg.msg_control, msg.msg_controllen);

	pass = _B_TRUE;

	if (debug)
		printf("Receiving: controllen=%u, \n", msg.msg_controllen);

	nbytes = recvmsg(sock, &msg, 0);

	if (nbytes == -1) {
		pass = _B_FALSE;
		fprintf(stderr, "recvmsg() failed: %s\n", strerror(errno));
		goto out;
	}

	if (debug) {
		printf("Received: controllen=%u, flags=%#x\n",
		    msg.msg_controllen, msg.msg_flags);
	}

	check(msg.msg_flags, t->x_flags, "msg_flags");
	check(msg.msg_controllen, t->x_controllen, "msg_controllen");

	for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
		void *data, *end;

		if (debug) {
			printf("    >> : Got cmsg %x/%x - %u\n", cm->cmsg_level,
			    cm->cmsg_type, cm->cmsg_len);
		}

		if (cm->cmsg_type != SCM_RIGHTS) {
			if (debug)
				printf("       : skipping cmsg\n");
			continue;
		}

		check(cm->cmsg_len - CMSG_LEN(0),
		    t->x_cmsg_datalen, "cmsg_len");

		/* Close any received file descriptors */
		data = CMSG_DATA(cm);

		if ((msg.msg_flags & MSG_CTRUNC) &&
		    CMSG_NXTHDR(&msg, cm) == NULL) {
			/*
			 * illumos did not previously adjust cmsg_len on
			 * truncation. This is the last cmsg, derive the
			 * length from msg_controllen
			 */
			end = msg.msg_control + msg.msg_controllen;
		} else {
			end = data + cm->cmsg_len - CMSG_LEN(0);
		}

		while (data <= end - sizeof (int)) {
			int *a = (int *)data;
			if (debug)
				printf("       : close(%d)\n", *a);
			if (close(*a) == -1) {
				pass = _B_FALSE;
				fprintf(stderr, "    !!!: "
				    "failed to close fd %d - %s\n", *a,
				    strerror(errno));
			}
			data += sizeof (int);
		}
	}

out:

	check_fds("client");
	check(fdcount, 0, "client descriptors");
	printf("     + : %s %s\n", pass ? "PASS" : "FAIL", t->name);

	return (pass);
}

static int
client(const char *sockpath, pid_t pid)
{
	struct sockaddr_un addr;
	sigset_t set;
	cmsg_test_t *t;
	int ret = 0;

	sigemptyset(&set);
	sigaddset(&set, SIGUSR1);
	sigaddset(&set, SIGINT);

	send_and_wait(pid, &set, 0, SIGUSR1);

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock == -1)
		err(EXIT_FAILURE, "failed to create socket");
	addr.sun_family = AF_UNIX;
	strlcpy(addr.sun_path, sockpath, sizeof (addr.sun_path));
	if (connect(sock, (struct sockaddr *)&addr, sizeof (addr)) == -1)
		err(EXIT_FAILURE, "could not connect to server socket");

	for (t = tests; t->name != NULL; t++) {
		send_and_wait(pid, &set, SIGUSR2, SIGUSR1);
		if (!recvtest(t))
			ret = 1;
	}

	close(sock);

	return (ret);
}

int
main(int argc, const char **argv)
{
	char sockpath[] = "/tmp/cmsg.testsock.XXXXXX";
	pid_t pid, ppid;
	sigset_t set;
	int ret = 0;

	/*
	 * The tests make assumptions about the number of open file descriptors
	 * present. In case we are invoked with more than just STDIN_FILENO,
	 * STDOUT_FILENO, and STDERR_FILENO open, close any other open
	 * descriptors that might exist. Otherwise their presence will violate
	 * the assumptions of the test and cause an erroneous failure.
	 */
	closefrom(STDERR_FILENO + 1);

	if (argc > 1 && strcmp(argv[1], "-d") == 0)
		debug = _B_TRUE;

	sigfillset(&set);
	sigdelset(&set, SIGINT);
	sigdelset(&set, SIGTSTP);
	sigprocmask(SIG_BLOCK, &set, NULL);

	if (mktemp(sockpath) == NULL)
		err(EXIT_FAILURE, "Failed to make temporary socket path");

	ppid = getpid();
	pid = fork();
	switch (pid) {
	case -1:
		err(EXIT_FAILURE, "fork failed");
	case 0:
		return (server(sockpath, ppid));
	default:
		break;
	}

	ret = client(sockpath, pid);
	kill(pid, SIGINT);

	unlink(sockpath);

	return (ret);
}