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