2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
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.
53 fprintf(stderr, "udpconnectjail: no arguments\n");
58 test(const char *context, struct sockaddr_in *sin)
62 sock = socket(PF_INET, SOCK_DGRAM, 0);
64 errx(-1, "%s: socket(PF_INET, SOCK_DGRAM, 0): %s", context,
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));
72 errx(-1, "%s: close(): %s", context, strerror(errno));
76 main(int argc, __unused char *argv[])
78 struct sockaddr_in sin;
85 bzero(&sin, sizeof(sin));
86 sin.sin_len = sizeof(sin);
87 sin.sin_family = AF_INET;
88 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
89 sin.sin_port = htons(8080); /* Arbitrary */
92 * First run the system call test outside of a jail.
94 test("not in jail", &sin);
97 * Now re-run in a jail.
98 * XXX-BZ should switch to jail_set(2).
100 ia4.s_addr = htonl(INADDR_LOOPBACK);
102 bzero(&thejail, sizeof(thejail));
103 thejail.version = JAIL_API_VERSION;
105 thejail.hostname = "jail";
106 thejail.jailname = "udpconnectjail";
110 if (jail(&thejail) < 0)
111 errx(-1, "jail: %s", strerror(errno));
112 test("in jail", &sin);
114 fprintf(stdout, "PASS\n");