Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/regression/netinet/udpconnectjail/udpconnectjail.c
39491 views
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
27
#include <sys/param.h>
28
#include <sys/jail.h>
29
#include <sys/socket.h>
30
31
#include <netinet/in.h>
32
33
#include <arpa/inet.h>
34
35
#include <err.h>
36
#include <errno.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
/*
43
* A bug in the jail(8) code prevented processes in jail from properly
44
* connecting UDP sockets. This test program attempts to exercise that bug.
45
*/
46
47
static void
48
usage(void)
49
{
50
51
fprintf(stderr, "udpconnectjail: no arguments\n");
52
exit(-1);
53
}
54
55
static void
56
test(const char *context, struct sockaddr_in *sin)
57
{
58
int sock;
59
60
sock = socket(PF_INET, SOCK_DGRAM, 0);
61
if (sock == -1)
62
errx(-1, "%s: socket(PF_INET, SOCK_DGRAM, 0): %s", context,
63
strerror(errno));
64
65
if (connect(sock, (struct sockaddr *)sin, sizeof(*sin)) < 0)
66
errx(-1, "%s: connect(%s): %s", context,
67
inet_ntoa(sin->sin_addr), strerror(errno));
68
69
if (close(sock) < 0)
70
errx(-1, "%s: close(): %s", context, strerror(errno));
71
}
72
73
int
74
main(int argc, __unused char *argv[])
75
{
76
struct sockaddr_in sin;
77
struct jail thejail;
78
struct in_addr ia4;
79
80
if (argc != 1)
81
usage();
82
83
bzero(&sin, sizeof(sin));
84
sin.sin_len = sizeof(sin);
85
sin.sin_family = AF_INET;
86
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
87
sin.sin_port = htons(8080); /* Arbitrary */
88
89
/*
90
* First run the system call test outside of a jail.
91
*/
92
test("not in jail", &sin);
93
94
/*
95
* Now re-run in a jail.
96
* XXX-BZ should switch to jail_set(2).
97
*/
98
ia4.s_addr = htonl(INADDR_LOOPBACK);
99
100
bzero(&thejail, sizeof(thejail));
101
thejail.version = JAIL_API_VERSION;
102
thejail.path = "/";
103
thejail.hostname = "jail";
104
thejail.jailname = "udpconnectjail";
105
thejail.ip4s = 1;
106
thejail.ip4 = &ia4;
107
108
if (jail(&thejail) < 0)
109
errx(-1, "jail: %s", strerror(errno));
110
test("in jail", &sin);
111
112
fprintf(stdout, "PASS\n");
113
114
return (0);
115
}
116
117