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$ 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 49 static void 50 usage(void) 51 { 52 53 fprintf(stderr, "udpconnectjail: no arguments\n"); 54 exit(-1); 55 } 56 57 static void 58 test(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 75 int 76 main(int argc, __unused char *argv[]) 77 { 78 struct sockaddr_in sin; 79 struct jail thejail; 80 struct in_addr ia4; 81 82 if (argc != 1) 83 usage(); 84 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 */ 90 91 /* 92 * First run the system call test outside of a jail. 93 */ 94 test("not in jail", &sin); 95 96 /* 97 * Now re-run in a jail. 98 * XXX-BZ should switch to jail_set(2). 99 */ 100 ia4.s_addr = htonl(INADDR_LOOPBACK); 101 102 bzero(&thejail, sizeof(thejail)); 103 thejail.version = JAIL_API_VERSION; 104 thejail.path = "/"; 105 thejail.hostname = "jail"; 106 thejail.jailname = "udpconnectjail"; 107 thejail.ip4s = 1; 108 thejail.ip4 = &ia4; 109 110 if (jail(&thejail) < 0) 111 errx(-1, "jail: %s", strerror(errno)); 112 test("in jail", &sin); 113 114 fprintf(stdout, "PASS\n"); 115 116 return (0); 117 } 118