t_kauth_pr_47598.c revision 1.2 1 1.1 martin /*-
2 1.1 martin * Copyright (c) 2013 The NetBSD Foundation, Inc.
3 1.1 martin * All rights reserved.
4 1.1 martin *
5 1.1 martin * Redistribution and use in source and binary forms, with or without
6 1.1 martin * modification, are permitted provided that the following conditions
7 1.1 martin * are met:
8 1.1 martin * 1. Redistributions of source code must retain the above copyright
9 1.1 martin * notice, this list of conditions and the following disclaimer.
10 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 martin * notice, this list of conditions and the following disclaimer in the
12 1.1 martin * documentation and/or other materials provided with the distribution.
13 1.1 martin *
14 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
25 1.1 martin */
26 1.1 martin
27 1.1 martin #include <sys/cdefs.h>
28 1.1 martin __COPYRIGHT("@(#) Copyright (c) 2013\
29 1.1 martin The NetBSD Foundation, inc. All rights reserved.");
30 1.2 martin __RCSID("$NetBSD: t_kauth_pr_47598.c,v 1.2 2013/02/28 20:41:21 martin Exp $");
31 1.1 martin
32 1.1 martin #include <errno.h>
33 1.1 martin #include <unistd.h>
34 1.1 martin #include <stdio.h>
35 1.1 martin #include <stdlib.h>
36 1.1 martin #include <sys/sysctl.h>
37 1.1 martin #include <sys/socket.h>
38 1.1 martin #include <netinet/in.h>
39 1.1 martin #include <arpa/inet.h>
40 1.1 martin #include <atf-c.h>
41 1.1 martin
42 1.1 martin /*
43 1.1 martin * PR kern/47598: if security.models.extensions.curtain = 1 we crash when
44 1.1 martin * doing a netstat while an embryonic (not yet fully accepted) connection
45 1.1 martin * exists.
46 1.1 martin * This has been fixed with rev. 1.5 of
47 1.1 martin * src/sys/secmodel/extensions/secmodel_extensions.c.
48 1.1 martin */
49 1.1 martin
50 1.1 martin
51 1.1 martin ATF_TC(kauth_curtain);
52 1.1 martin ATF_TC_HEAD(kauth_curtain, tc)
53 1.1 martin {
54 1.1 martin atf_tc_set_md_var(tc, "require.user", "root");
55 1.1 martin atf_tc_set_md_var(tc, "require.progs", "netstat");
56 1.1 martin atf_tc_set_md_var(tc, "descr",
57 1.1 martin "Checks for kernel crash with curtain active (PR kern/47598)");
58 1.1 martin }
59 1.1 martin
60 1.1 martin ATF_TC_BODY(kauth_curtain, tc)
61 1.1 martin {
62 1.1 martin static const char curtain_name[] = "security.models.bsd44.curtain";
63 1.1 martin
64 1.1 martin int old_curtain, new_curtain = 1, s, s2, err;
65 1.2 martin size_t old_curtain_len = sizeof(old_curtain);
66 1.2 martin socklen_t slen;
67 1.1 martin struct sockaddr_in sa;
68 1.1 martin
69 1.1 martin /*
70 1.1 martin * save old value of "curtain" and enable it
71 1.1 martin */
72 1.1 martin if (sysctlbyname(curtain_name, &old_curtain, &old_curtain_len,
73 1.1 martin &new_curtain, sizeof(new_curtain)) != 0)
74 1.1 martin atf_tc_fail("failed to enable %s", curtain_name);
75 1.1 martin
76 1.1 martin /*
77 1.1 martin * create a socket and bind it to some arbitray free port
78 1.1 martin */
79 1.1 martin s = socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
80 1.1 martin ATF_REQUIRE(s != -1);
81 1.1 martin memset(&sa, 0, sizeof(sa));
82 1.1 martin sa.sin_family = AF_INET;
83 1.1 martin sa.sin_len = sizeof(sa);
84 1.1 martin sa.sin_addr.s_addr = inet_addr("127.0.0.1");
85 1.1 martin ATF_REQUIRE(bind(s, (struct sockaddr *)&sa, sizeof(sa))==0);
86 1.1 martin ATF_REQUIRE(listen(s, 16)==0);
87 1.1 martin
88 1.1 martin /*
89 1.1 martin * extract address and open a connection to the port
90 1.1 martin */
91 1.1 martin slen = sizeof(sa);
92 1.1 martin ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sa, &slen)==0);
93 1.1 martin s2 = socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
94 1.1 martin ATF_REQUIRE(s2 != -1);
95 1.1 martin printf("port is %d\n", ntohs(sa.sin_port));
96 1.1 martin err = connect(s2, (struct sockaddr *)&sa, sizeof(sa));
97 1.1 martin ATF_REQUIRE_MSG(err == -1 && errno == EINPROGRESS,
98 1.1 martin "conect returned %d with errno %d", err, errno);
99 1.1 martin fflush(stdout);
100 1.1 martin fflush(stderr);
101 1.1 martin
102 1.1 martin /*
103 1.1 martin * we now have a pending, not yet accepted connection - run netstat
104 1.1 martin */
105 1.1 martin system("netstat -aA");
106 1.1 martin
107 1.1 martin /*
108 1.1 martin * cleanup
109 1.1 martin */
110 1.1 martin close(s2);
111 1.1 martin close(s);
112 1.1 martin
113 1.1 martin /*
114 1.1 martin * restore old value of curtain
115 1.1 martin */
116 1.1 martin if (sysctlbyname(curtain_name, NULL, 0,
117 1.1 martin &old_curtain, sizeof(old_curtain)) != 0)
118 1.1 martin atf_tc_fail("failed to restore %s", curtain_name);
119 1.1 martin }
120 1.1 martin
121 1.1 martin ATF_TP_ADD_TCS(tp)
122 1.1 martin {
123 1.1 martin ATF_TP_ADD_TC(tp, kauth_curtain);
124 1.1 martin
125 1.1 martin return atf_no_error();
126 1.1 martin }
127 1.1 martin
128 1.1 martin
129