udpconnectjail.c revision 140348
1/*-
2 * Copyright (c) 2005 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/tools/regression/netinet/udpconnectjail/udpconnectjail.c 140348 2005-01-16 13:05:32Z rwatson $
27 */
28
29#include <sys/param.h>
30#include <sys/jail.h>
31#include <sys/socket.h>
32
33#include <netinet/in.h>
34
35#include <arpa/inet.h>
36
37#include <err.h>
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44/*
45 * A bug in the jail(8) code prevented processes in jail from properly
46 * connecting UDP sockets.  This test program attempts to exercise that bug.
47 */
48
49static void
50usage(void)
51{
52
53	fprintf(stderr, "udpconnectjail: no arguments\n");
54	exit(-1);
55}
56
57static void
58test(const char *context, struct sockaddr_in *sin)
59{
60	int sock;
61
62	sock = socket(PF_INET, SOCK_DGRAM, 0);
63	if (sock == -1)
64		errx(-1, "%s: socket(PF_INET, SOCK_DGRAM, 0): %s", context,
65		    strerror(errno));
66
67	if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
68		errx(-1, "%s: connect(%s): %s", context,
69		    inet_ntoa(sin->sin_addr), strerror(errno));
70
71	if (close(sock) < 0)
72		errx(-1, "%s: close(): %s", context, strerror(errno));
73}
74
75int
76main(int argc, __unused char *argv[])
77{
78	struct sockaddr_in sin;
79	struct jail thejail;
80
81	if (argc != 1)
82		usage();
83
84	bzero(&sin, sizeof(sin));
85	sin.sin_len = sizeof(sin);
86	sin.sin_family = AF_INET;
87	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
88	sin.sin_port = 0;
89
90	/*
91	 * First run the system call test outside of a jail.
92	 */
93	test("not in jail", &sin);
94
95	/*
96	 * Now re-run in a jail.
97	 */
98	bzero(&thejail, sizeof(thejail));
99	thejail.version = 0;
100	thejail.path = "/";
101	thejail.hostname = "jail";
102	thejail.ip_number = htonl(INADDR_LOOPBACK);
103	if (jail(&thejail) < 0)
104		errx(-1, "jail: %s", strerror(errno));
105	test("in jail", &sin);
106
107	fprintf(stdout, "PASS\n");
108
109	return (0);
110}
111