Path: blob/main/tools/regression/netinet/udpconnectjail/udpconnectjail.c
39491 views
/*-1* Copyright (c) 2005 Robert N. M. Watson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/jail.h>28#include <sys/socket.h>2930#include <netinet/in.h>3132#include <arpa/inet.h>3334#include <err.h>35#include <errno.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041/*42* A bug in the jail(8) code prevented processes in jail from properly43* connecting UDP sockets. This test program attempts to exercise that bug.44*/4546static void47usage(void)48{4950fprintf(stderr, "udpconnectjail: no arguments\n");51exit(-1);52}5354static void55test(const char *context, struct sockaddr_in *sin)56{57int sock;5859sock = socket(PF_INET, SOCK_DGRAM, 0);60if (sock == -1)61errx(-1, "%s: socket(PF_INET, SOCK_DGRAM, 0): %s", context,62strerror(errno));6364if (connect(sock, (struct sockaddr *)sin, sizeof(*sin)) < 0)65errx(-1, "%s: connect(%s): %s", context,66inet_ntoa(sin->sin_addr), strerror(errno));6768if (close(sock) < 0)69errx(-1, "%s: close(): %s", context, strerror(errno));70}7172int73main(int argc, __unused char *argv[])74{75struct sockaddr_in sin;76struct jail thejail;77struct in_addr ia4;7879if (argc != 1)80usage();8182bzero(&sin, sizeof(sin));83sin.sin_len = sizeof(sin);84sin.sin_family = AF_INET;85sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);86sin.sin_port = htons(8080); /* Arbitrary */8788/*89* First run the system call test outside of a jail.90*/91test("not in jail", &sin);9293/*94* Now re-run in a jail.95* XXX-BZ should switch to jail_set(2).96*/97ia4.s_addr = htonl(INADDR_LOOPBACK);9899bzero(&thejail, sizeof(thejail));100thejail.version = JAIL_API_VERSION;101thejail.path = "/";102thejail.hostname = "jail";103thejail.jailname = "udpconnectjail";104thejail.ip4s = 1;105thejail.ip4 = &ia4;106107if (jail(&thejail) < 0)108errx(-1, "jail: %s", strerror(errno));109test("in jail", &sin);110111fprintf(stdout, "PASS\n");112113return (0);114}115116117