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